camellia.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #if !defined(MBEDTLS_CAMELLIA_ALT)
  39. // Regular implementation
  40. //
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  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. /**
  54. * \brief Initialize CAMELLIA context
  55. *
  56. * \param ctx CAMELLIA context to be initialized
  57. */
  58. void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
  59. /**
  60. * \brief Clear CAMELLIA context
  61. *
  62. * \param ctx CAMELLIA context to be cleared
  63. */
  64. void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
  65. /**
  66. * \brief CAMELLIA key schedule (encryption)
  67. *
  68. * \param ctx CAMELLIA context to be initialized
  69. * \param key encryption key
  70. * \param keybits must be 128, 192 or 256
  71. *
  72. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  73. */
  74. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
  75. unsigned int keybits );
  76. /**
  77. * \brief CAMELLIA key schedule (decryption)
  78. *
  79. * \param ctx CAMELLIA context to be initialized
  80. * \param key decryption key
  81. * \param keybits must be 128, 192 or 256
  82. *
  83. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  84. */
  85. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
  86. unsigned int keybits );
  87. /**
  88. * \brief CAMELLIA-ECB block encryption/decryption
  89. *
  90. * \param ctx CAMELLIA context
  91. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  92. * \param input 16-byte input block
  93. * \param output 16-byte output block
  94. *
  95. * \return 0 if successful
  96. */
  97. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  98. int mode,
  99. const unsigned char input[16],
  100. unsigned char output[16] );
  101. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  102. /**
  103. * \brief CAMELLIA-CBC buffer encryption/decryption
  104. * Length should be a multiple of the block
  105. * size (16 bytes)
  106. *
  107. * \note Upon exit, the content of the IV is updated so that you can
  108. * call the function same function again on the following
  109. * block(s) of data and get the same result as if it was
  110. * encrypted in one call. This allows a "streaming" usage.
  111. * If on the other hand you need to retain the contents of the
  112. * IV, you should either save it manually or use the cipher
  113. * module instead.
  114. *
  115. * \param ctx CAMELLIA context
  116. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  117. * \param length length of the input data
  118. * \param iv initialization vector (updated after use)
  119. * \param input buffer holding the input data
  120. * \param output buffer holding the output data
  121. *
  122. * \return 0 if successful, or
  123. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  124. */
  125. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  126. int mode,
  127. size_t length,
  128. unsigned char iv[16],
  129. const unsigned char *input,
  130. unsigned char *output );
  131. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  132. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  133. /**
  134. * \brief CAMELLIA-CFB128 buffer encryption/decryption
  135. *
  136. * Note: Due to the nature of CFB you should use the same key schedule for
  137. * both encryption and decryption. So a context initialized with
  138. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
  139. *
  140. * \note Upon exit, the content of the IV is updated so that you can
  141. * call the function same function again on the following
  142. * block(s) of data and get the same result as if it was
  143. * encrypted in one call. This allows a "streaming" usage.
  144. * If on the other hand you need to retain the contents of the
  145. * IV, you should either save it manually or use the cipher
  146. * module instead.
  147. *
  148. * \param ctx CAMELLIA context
  149. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  150. * \param length length of the input data
  151. * \param iv_off offset in IV (updated after use)
  152. * \param iv initialization vector (updated after use)
  153. * \param input buffer holding the input data
  154. * \param output buffer holding the output data
  155. *
  156. * \return 0 if successful, or
  157. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  158. */
  159. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  160. int mode,
  161. size_t length,
  162. size_t *iv_off,
  163. unsigned char iv[16],
  164. const unsigned char *input,
  165. unsigned char *output );
  166. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  167. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  168. /**
  169. * \brief CAMELLIA-CTR buffer encryption/decryption
  170. *
  171. * Warning: You have to keep the maximum use of your counter in mind!
  172. *
  173. * Note: Due to the nature of CTR you should use the same key schedule for
  174. * both encryption and decryption. So a context initialized with
  175. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
  176. *
  177. * \param ctx CAMELLIA context
  178. * \param length The length of the data
  179. * \param nc_off The offset in the current stream_block (for resuming
  180. * within current cipher stream). The offset pointer to
  181. * should be 0 at the start of a stream.
  182. * \param nonce_counter The 128-bit nonce and counter.
  183. * \param stream_block The saved stream-block for resuming. Is overwritten
  184. * by the function.
  185. * \param input The input data stream
  186. * \param output The output data stream
  187. *
  188. * \return 0 if successful
  189. */
  190. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  191. size_t length,
  192. size_t *nc_off,
  193. unsigned char nonce_counter[16],
  194. unsigned char stream_block[16],
  195. const unsigned char *input,
  196. unsigned char *output );
  197. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  198. #ifdef __cplusplus
  199. }
  200. #endif
  201. #else /* MBEDTLS_CAMELLIA_ALT */
  202. #include "camellia_alt.h"
  203. #endif /* MBEDTLS_CAMELLIA_ALT */
  204. #ifdef __cplusplus
  205. extern "C" {
  206. #endif
  207. /**
  208. * \brief Checkup routine
  209. *
  210. * \return 0 if successful, or 1 if the test failed
  211. */
  212. int mbedtls_camellia_self_test( int verbose );
  213. #ifdef __cplusplus
  214. }
  215. #endif
  216. #endif /* camellia.h */