camellia.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * \file camellia.h
  3. *
  4. * \brief Camellia block cipher
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_CAMELLIA_H
  11. #define MBEDTLS_CAMELLIA_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. #include "mbedtls/platform_util.h"
  17. #define MBEDTLS_CAMELLIA_ENCRYPT 1
  18. #define MBEDTLS_CAMELLIA_DECRYPT 0
  19. /** Bad input data. */
  20. #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024
  21. /** Invalid data input length. */
  22. #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #if !defined(MBEDTLS_CAMELLIA_ALT)
  27. // Regular implementation
  28. //
  29. /**
  30. * \brief CAMELLIA context structure
  31. */
  32. typedef struct mbedtls_camellia_context {
  33. int MBEDTLS_PRIVATE(nr); /*!< number of rounds */
  34. uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */
  35. }
  36. mbedtls_camellia_context;
  37. #else /* MBEDTLS_CAMELLIA_ALT */
  38. #include "camellia_alt.h"
  39. #endif /* MBEDTLS_CAMELLIA_ALT */
  40. /**
  41. * \brief Initialize a CAMELLIA context.
  42. *
  43. * \param ctx The CAMELLIA context to be initialized.
  44. * This must not be \c NULL.
  45. */
  46. void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
  47. /**
  48. * \brief Clear a CAMELLIA context.
  49. *
  50. * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
  51. * in which case this function returns immediately. If it is not
  52. * \c NULL, it must be initialized.
  53. */
  54. void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
  55. /**
  56. * \brief Perform a CAMELLIA key schedule operation for encryption.
  57. *
  58. * \param ctx The CAMELLIA context to use. This must be initialized.
  59. * \param key The encryption key to use. This must be a readable buffer
  60. * of size \p keybits Bits.
  61. * \param keybits The length of \p key in Bits. This must be either \c 128,
  62. * \c 192 or \c 256.
  63. *
  64. * \return \c 0 if successful.
  65. * \return A negative error code on failure.
  66. */
  67. int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
  68. const unsigned char *key,
  69. unsigned int keybits);
  70. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  71. /**
  72. * \brief Perform a CAMELLIA key schedule operation for decryption.
  73. *
  74. * \param ctx The CAMELLIA context to use. This must be initialized.
  75. * \param key The decryption key. This must be a readable buffer
  76. * of size \p keybits Bits.
  77. * \param keybits The length of \p key in Bits. This must be either \c 128,
  78. * \c 192 or \c 256.
  79. *
  80. * \return \c 0 if successful.
  81. * \return A negative error code on failure.
  82. */
  83. int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
  84. const unsigned char *key,
  85. unsigned int keybits);
  86. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  87. /**
  88. * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
  89. *
  90. * \param ctx The CAMELLIA context to use. This must be initialized
  91. * and bound to a key.
  92. * \param mode The mode of operation. This must be either
  93. * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  94. * \param input The input block. This must be a readable buffer
  95. * of size \c 16 Bytes.
  96. * \param output The output block. This must be a writable buffer
  97. * of size \c 16 Bytes.
  98. *
  99. * \return \c 0 if successful.
  100. * \return A negative error code on failure.
  101. */
  102. int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
  103. int mode,
  104. const unsigned char input[16],
  105. unsigned char output[16]);
  106. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  107. /**
  108. * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
  109. *
  110. * \note Upon exit, the content of the IV is updated so that you can
  111. * call the function same function again on the following
  112. * block(s) of data and get the same result as if it was
  113. * encrypted in one call. This allows a "streaming" usage.
  114. * If on the other hand you need to retain the contents of the
  115. * IV, you should either save it manually or use the cipher
  116. * module instead.
  117. *
  118. * \param ctx The CAMELLIA context to use. This must be initialized
  119. * and bound to a key.
  120. * \param mode The mode of operation. This must be either
  121. * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  122. * \param length The length in Bytes of the input data \p input.
  123. * This must be a multiple of \c 16 Bytes.
  124. * \param iv The initialization vector. This must be a read/write buffer
  125. * of length \c 16 Bytes. It is updated to allow streaming
  126. * use as explained above.
  127. * \param input The buffer holding the input data. This must point to a
  128. * readable buffer of length \p length Bytes.
  129. * \param output The buffer holding the output data. This must point to a
  130. * writable buffer of length \p length Bytes.
  131. *
  132. * \return \c 0 if successful.
  133. * \return A negative error code on failure.
  134. */
  135. int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
  136. int mode,
  137. size_t length,
  138. unsigned char iv[16],
  139. const unsigned char *input,
  140. unsigned char *output);
  141. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  142. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  143. /**
  144. * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
  145. * operation.
  146. *
  147. * \note Due to the nature of CFB mode, you should use the same
  148. * key for both encryption and decryption. In particular, calls
  149. * to this function should be preceded by a key-schedule via
  150. * mbedtls_camellia_setkey_enc() regardless of whether \p mode
  151. * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  152. *
  153. * \note Upon exit, the content of the IV is updated so that you can
  154. * call the function same function again on the following
  155. * block(s) of data and get the same result as if it was
  156. * encrypted in one call. This allows a "streaming" usage.
  157. * If on the other hand you need to retain the contents of the
  158. * IV, you should either save it manually or use the cipher
  159. * module instead.
  160. *
  161. * \param ctx The CAMELLIA context to use. This must be initialized
  162. * and bound to a key.
  163. * \param mode The mode of operation. This must be either
  164. * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  165. * \param length The length of the input data \p input. Any value is allowed.
  166. * \param iv_off The current offset in the IV. This must be smaller
  167. * than \c 16 Bytes. It is updated after this call to allow
  168. * the aforementioned streaming usage.
  169. * \param iv The initialization vector. This must be a read/write buffer
  170. * of length \c 16 Bytes. It is updated after this call to
  171. * allow the aforementioned streaming usage.
  172. * \param input The buffer holding the input data. This must be a readable
  173. * buffer of size \p length Bytes.
  174. * \param output The buffer to hold the output data. This must be a writable
  175. * buffer of length \p length Bytes.
  176. *
  177. * \return \c 0 if successful.
  178. * \return A negative error code on failure.
  179. */
  180. int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
  181. int mode,
  182. size_t length,
  183. size_t *iv_off,
  184. unsigned char iv[16],
  185. const unsigned char *input,
  186. unsigned char *output);
  187. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  188. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  189. /**
  190. * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
  191. *
  192. * *note Due to the nature of CTR mode, you should use the same
  193. * key for both encryption and decryption. In particular, calls
  194. * to this function should be preceded by a key-schedule via
  195. * mbedtls_camellia_setkey_enc() regardless of whether the mode
  196. * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  197. *
  198. * \warning You must never reuse a nonce value with the same key. Doing so
  199. * would void the encryption for the two messages encrypted with
  200. * the same nonce and key.
  201. *
  202. * There are two common strategies for managing nonces with CTR:
  203. *
  204. * 1. You can handle everything as a single message processed over
  205. * successive calls to this function. In that case, you want to
  206. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  207. * then preserve the values of \p nonce_counter, \p nc_off and \p
  208. * stream_block across calls to this function as they will be
  209. * updated by this function.
  210. *
  211. * With this strategy, you must not encrypt more than 2**128
  212. * blocks of data with the same key.
  213. *
  214. * 2. You can encrypt separate messages by dividing the \p
  215. * nonce_counter buffer in two areas: the first one used for a
  216. * per-message nonce, handled by yourself, and the second one
  217. * updated by this function internally.
  218. *
  219. * For example, you might reserve the first \c 12 Bytes for the
  220. * per-message nonce, and the last \c 4 Bytes for internal use.
  221. * In that case, before calling this function on a new message you
  222. * need to set the first \c 12 Bytes of \p nonce_counter to your
  223. * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
  224. * (which will cause \p stream_block to be ignored). That way, you
  225. * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
  226. * each with the same key.
  227. *
  228. * The per-message nonce (or information sufficient to reconstruct
  229. * it) needs to be communicated with the ciphertext and must be
  230. * unique. The recommended way to ensure uniqueness is to use a
  231. * message counter. An alternative is to generate random nonces,
  232. * but this limits the number of messages that can be securely
  233. * encrypted: for example, with 96-bit random nonces, you should
  234. * not encrypt more than 2**32 messages with the same key.
  235. *
  236. * Note that for both strategies, sizes are measured in blocks and
  237. * that a CAMELLIA block is \c 16 Bytes.
  238. *
  239. * \warning Upon return, \p stream_block contains sensitive data. Its
  240. * content must not be written to insecure storage and should be
  241. * securely discarded as soon as it's no longer needed.
  242. *
  243. * \param ctx The CAMELLIA context to use. This must be initialized
  244. * and bound to a key.
  245. * \param length The length of the input data \p input in Bytes.
  246. * Any value is allowed.
  247. * \param nc_off The offset in the current \p stream_block (for resuming
  248. * within current cipher stream). The offset pointer to
  249. * should be \c 0 at the start of a stream. It is updated
  250. * at the end of this call.
  251. * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
  252. * buffer of length \c 16 Bytes.
  253. * \param stream_block The saved stream-block for resuming. This must be a
  254. * read/write buffer of length \c 16 Bytes.
  255. * \param input The input data stream. This must be a readable buffer of
  256. * size \p length Bytes.
  257. * \param output The output data stream. This must be a writable buffer
  258. * of size \p length Bytes.
  259. *
  260. * \return \c 0 if successful.
  261. * \return A negative error code on failure.
  262. */
  263. int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
  264. size_t length,
  265. size_t *nc_off,
  266. unsigned char nonce_counter[16],
  267. unsigned char stream_block[16],
  268. const unsigned char *input,
  269. unsigned char *output);
  270. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  271. #if defined(MBEDTLS_SELF_TEST)
  272. /**
  273. * \brief Checkup routine
  274. *
  275. * \return 0 if successful, or 1 if the test failed
  276. */
  277. int mbedtls_camellia_self_test(int verbose);
  278. #endif /* MBEDTLS_SELF_TEST */
  279. #ifdef __cplusplus
  280. }
  281. #endif
  282. #endif /* camellia.h */