camellia.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * \file camellia.h
  3. *
  4. * \brief Camellia block cipher
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_CAMELLIA_H
  25. #define MBEDTLS_CAMELLIA_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #define MBEDTLS_CAMELLIA_ENCRYPT 1
  34. #define MBEDTLS_CAMELLIA_DECRYPT 0
  35. #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
  36. #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
  37. #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #if !defined(MBEDTLS_CAMELLIA_ALT)
  42. // Regular implementation
  43. //
  44. /**
  45. * \brief CAMELLIA context structure
  46. */
  47. typedef struct
  48. {
  49. int nr; /*!< number of rounds */
  50. uint32_t rk[68]; /*!< CAMELLIA round keys */
  51. }
  52. mbedtls_camellia_context;
  53. #else /* MBEDTLS_CAMELLIA_ALT */
  54. #include "camellia_alt.h"
  55. #endif /* MBEDTLS_CAMELLIA_ALT */
  56. /**
  57. * \brief Initialize CAMELLIA context
  58. *
  59. * \param ctx CAMELLIA context to be initialized
  60. */
  61. void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
  62. /**
  63. * \brief Clear CAMELLIA context
  64. *
  65. * \param ctx CAMELLIA context to be cleared
  66. */
  67. void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
  68. /**
  69. * \brief CAMELLIA key schedule (encryption)
  70. *
  71. * \param ctx CAMELLIA context to be initialized
  72. * \param key encryption key
  73. * \param keybits must be 128, 192 or 256
  74. *
  75. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  76. */
  77. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
  78. unsigned int keybits );
  79. /**
  80. * \brief CAMELLIA key schedule (decryption)
  81. *
  82. * \param ctx CAMELLIA context to be initialized
  83. * \param key decryption key
  84. * \param keybits must be 128, 192 or 256
  85. *
  86. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  87. */
  88. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
  89. unsigned int keybits );
  90. /**
  91. * \brief CAMELLIA-ECB block encryption/decryption
  92. *
  93. * \param ctx CAMELLIA context
  94. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  95. * \param input 16-byte input block
  96. * \param output 16-byte output block
  97. *
  98. * \return 0 if successful
  99. */
  100. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  101. int mode,
  102. const unsigned char input[16],
  103. unsigned char output[16] );
  104. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  105. /**
  106. * \brief CAMELLIA-CBC buffer encryption/decryption
  107. * Length should be a multiple of the block
  108. * size (16 bytes)
  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 CAMELLIA context
  119. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  120. * \param length length of the input data
  121. * \param iv initialization vector (updated after use)
  122. * \param input buffer holding the input data
  123. * \param output buffer holding the output data
  124. *
  125. * \return 0 if successful, or
  126. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  127. */
  128. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  129. int mode,
  130. size_t length,
  131. unsigned char iv[16],
  132. const unsigned char *input,
  133. unsigned char *output );
  134. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  135. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  136. /**
  137. * \brief CAMELLIA-CFB128 buffer encryption/decryption
  138. *
  139. * Note: Due to the nature of CFB you should use the same key schedule for
  140. * both encryption and decryption. So a context initialized with
  141. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
  142. *
  143. * \note Upon exit, the content of the IV is updated so that you can
  144. * call the function same function again on the following
  145. * block(s) of data and get the same result as if it was
  146. * encrypted in one call. This allows a "streaming" usage.
  147. * If on the other hand you need to retain the contents of the
  148. * IV, you should either save it manually or use the cipher
  149. * module instead.
  150. *
  151. * \param ctx CAMELLIA context
  152. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  153. * \param length length of the input data
  154. * \param iv_off offset in IV (updated after use)
  155. * \param iv initialization vector (updated after use)
  156. * \param input buffer holding the input data
  157. * \param output buffer holding the output data
  158. *
  159. * \return 0 if successful, or
  160. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  161. */
  162. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  163. int mode,
  164. size_t length,
  165. size_t *iv_off,
  166. unsigned char iv[16],
  167. const unsigned char *input,
  168. unsigned char *output );
  169. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  170. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  171. /**
  172. * \brief CAMELLIA-CTR buffer encryption/decryption
  173. *
  174. * Note: Due to the nature of CTR you should use the same key schedule for
  175. * both encryption and decryption. So a context initialized with
  176. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
  177. *
  178. * \warning You must never reuse a nonce value with the same key. Doing so
  179. * would void the encryption for the two messages encrypted with
  180. * the same nonce and key.
  181. *
  182. * There are two common strategies for managing nonces with CTR:
  183. *
  184. * 1. You can handle everything as a single message processed over
  185. * successive calls to this function. In that case, you want to
  186. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  187. * then preserve the values of \p nonce_counter, \p nc_off and \p
  188. * stream_block across calls to this function as they will be
  189. * updated by this function.
  190. *
  191. * With this strategy, you must not encrypt more than 2**128
  192. * blocks of data with the same key.
  193. *
  194. * 2. You can encrypt separate messages by dividing the \p
  195. * nonce_counter buffer in two areas: the first one used for a
  196. * per-message nonce, handled by yourself, and the second one
  197. * updated by this function internally.
  198. *
  199. * For example, you might reserve the first 12 bytes for the
  200. * per-message nonce, and the last 4 bytes for internal use. In that
  201. * case, before calling this function on a new message you need to
  202. * set the first 12 bytes of \p nonce_counter to your chosen nonce
  203. * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  204. * stream_block to be ignored). That way, you can encrypt at most
  205. * 2**96 messages of up to 2**32 blocks each with the same key.
  206. *
  207. * The per-message nonce (or information sufficient to reconstruct
  208. * it) needs to be communicated with the ciphertext and must be unique.
  209. * The recommended way to ensure uniqueness is to use a message
  210. * counter. An alternative is to generate random nonces, but this
  211. * limits the number of messages that can be securely encrypted:
  212. * for example, with 96-bit random nonces, you should not encrypt
  213. * more than 2**32 messages with the same key.
  214. *
  215. * Note that for both stategies, sizes are measured in blocks and
  216. * that a CAMELLIA block is 16 bytes.
  217. *
  218. * \warning Upon return, \p stream_block contains sensitive data. Its
  219. * content must not be written to insecure storage and should be
  220. * securely discarded as soon as it's no longer needed.
  221. *
  222. * \param ctx CAMELLIA context
  223. * \param length The length of the data
  224. * \param nc_off The offset in the current stream_block (for resuming
  225. * within current cipher stream). The offset pointer to
  226. * should be 0 at the start of a stream.
  227. * \param nonce_counter The 128-bit nonce and counter.
  228. * \param stream_block The saved stream-block for resuming. Is overwritten
  229. * by the function.
  230. * \param input The input data stream
  231. * \param output The output data stream
  232. *
  233. * \return 0 if successful
  234. */
  235. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  236. size_t length,
  237. size_t *nc_off,
  238. unsigned char nonce_counter[16],
  239. unsigned char stream_block[16],
  240. const unsigned char *input,
  241. unsigned char *output );
  242. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  243. /**
  244. * \brief Checkup routine
  245. *
  246. * \return 0 if successful, or 1 if the test failed
  247. */
  248. int mbedtls_camellia_self_test( int verbose );
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif /* camellia.h */