camellia.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * \file camellia.h
  3. *
  4. * \brief Camellia block cipher
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_CAMELLIA_H
  24. #define MBEDTLS_CAMELLIA_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #define MBEDTLS_CAMELLIA_ENCRYPT 1
  33. #define MBEDTLS_CAMELLIA_DECRYPT 0
  34. #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
  35. #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
  36. #if !defined(MBEDTLS_CAMELLIA_ALT)
  37. // Regular implementation
  38. //
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /**
  43. * \brief CAMELLIA context structure
  44. */
  45. typedef struct
  46. {
  47. int nr; /*!< number of rounds */
  48. uint32_t rk[68]; /*!< CAMELLIA round keys */
  49. }
  50. mbedtls_camellia_context;
  51. /**
  52. * \brief Initialize CAMELLIA context
  53. *
  54. * \param ctx CAMELLIA context to be initialized
  55. */
  56. void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
  57. /**
  58. * \brief Clear CAMELLIA context
  59. *
  60. * \param ctx CAMELLIA context to be cleared
  61. */
  62. void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
  63. /**
  64. * \brief CAMELLIA key schedule (encryption)
  65. *
  66. * \param ctx CAMELLIA context to be initialized
  67. * \param key encryption key
  68. * \param keybits must be 128, 192 or 256
  69. *
  70. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  71. */
  72. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
  73. unsigned int keybits );
  74. /**
  75. * \brief CAMELLIA key schedule (decryption)
  76. *
  77. * \param ctx CAMELLIA context to be initialized
  78. * \param key decryption key
  79. * \param keybits must be 128, 192 or 256
  80. *
  81. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  82. */
  83. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
  84. unsigned int keybits );
  85. /**
  86. * \brief CAMELLIA-ECB block encryption/decryption
  87. *
  88. * \param ctx CAMELLIA context
  89. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  90. * \param input 16-byte input block
  91. * \param output 16-byte output block
  92. *
  93. * \return 0 if successful
  94. */
  95. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  96. int mode,
  97. const unsigned char input[16],
  98. unsigned char output[16] );
  99. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  100. /**
  101. * \brief CAMELLIA-CBC buffer encryption/decryption
  102. * Length should be a multiple of the block
  103. * size (16 bytes)
  104. *
  105. * \note Upon exit, the content of the IV is updated so that you can
  106. * call the function same function again on the following
  107. * block(s) of data and get the same result as if it was
  108. * encrypted in one call. This allows a "streaming" usage.
  109. * If on the other hand you need to retain the contents of the
  110. * IV, you should either save it manually or use the cipher
  111. * module instead.
  112. *
  113. * \param ctx CAMELLIA context
  114. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  115. * \param length length of the input data
  116. * \param iv initialization vector (updated after use)
  117. * \param input buffer holding the input data
  118. * \param output buffer holding the output data
  119. *
  120. * \return 0 if successful, or
  121. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  122. */
  123. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  124. int mode,
  125. size_t length,
  126. unsigned char iv[16],
  127. const unsigned char *input,
  128. unsigned char *output );
  129. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  130. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  131. /**
  132. * \brief CAMELLIA-CFB128 buffer encryption/decryption
  133. *
  134. * Note: Due to the nature of CFB you should use the same key schedule for
  135. * both encryption and decryption. So a context initialized with
  136. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
  137. *
  138. * \note Upon exit, the content of the IV is updated so that you can
  139. * call the function same function again on the following
  140. * block(s) of data and get the same result as if it was
  141. * encrypted in one call. This allows a "streaming" usage.
  142. * If on the other hand you need to retain the contents of the
  143. * IV, you should either save it manually or use the cipher
  144. * module instead.
  145. *
  146. * \param ctx CAMELLIA context
  147. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  148. * \param length length of the input data
  149. * \param iv_off offset in IV (updated after use)
  150. * \param iv initialization vector (updated after use)
  151. * \param input buffer holding the input data
  152. * \param output buffer holding the output data
  153. *
  154. * \return 0 if successful, or
  155. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  156. */
  157. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  158. int mode,
  159. size_t length,
  160. size_t *iv_off,
  161. unsigned char iv[16],
  162. const unsigned char *input,
  163. unsigned char *output );
  164. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  165. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  166. /**
  167. * \brief CAMELLIA-CTR buffer encryption/decryption
  168. *
  169. * Warning: You have to keep the maximum use of your counter in mind!
  170. *
  171. * Note: Due to the nature of CTR you should use the same key schedule for
  172. * both encryption and decryption. So a context initialized with
  173. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
  174. *
  175. * \param ctx CAMELLIA context
  176. * \param length The length of the data
  177. * \param nc_off The offset in the current stream_block (for resuming
  178. * within current cipher stream). The offset pointer to
  179. * should be 0 at the start of a stream.
  180. * \param nonce_counter The 128-bit nonce and counter.
  181. * \param stream_block The saved stream-block for resuming. Is overwritten
  182. * by the function.
  183. * \param input The input data stream
  184. * \param output The output data stream
  185. *
  186. * \return 0 if successful
  187. */
  188. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  189. size_t length,
  190. size_t *nc_off,
  191. unsigned char nonce_counter[16],
  192. unsigned char stream_block[16],
  193. const unsigned char *input,
  194. unsigned char *output );
  195. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #else /* MBEDTLS_CAMELLIA_ALT */
  200. #include "camellia_alt.h"
  201. #endif /* MBEDTLS_CAMELLIA_ALT */
  202. #ifdef __cplusplus
  203. extern "C" {
  204. #endif
  205. /**
  206. * \brief Checkup routine
  207. *
  208. * \return 0 if successful, or 1 if the test failed
  209. */
  210. int mbedtls_camellia_self_test( int verbose );
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* camellia.h */