aria.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * \file aria.h
  3. *
  4. * \brief ARIA block cipher
  5. *
  6. * The ARIA algorithm is a symmetric block cipher that can encrypt and
  7. * decrypt information. It is defined by the Korean Agency for
  8. * Technology and Standards (KATS) in <em>KS X 1213:2004</em> (in
  9. * Korean, but see http://210.104.33.10/ARIA/index-e.html in English)
  10. * and also described by the IETF in <em>RFC 5794</em>.
  11. */
  12. /* Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
  13. * SPDX-License-Identifier: Apache-2.0
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. *
  27. * This file is part of mbed TLS (https://tls.mbed.org)
  28. */
  29. #ifndef MBEDTLS_ARIA_H
  30. #define MBEDTLS_ARIA_H
  31. #if !defined(MBEDTLS_CONFIG_FILE)
  32. #include "config.h"
  33. #else
  34. #include MBEDTLS_CONFIG_FILE
  35. #endif
  36. #include <stddef.h>
  37. #include <stdint.h>
  38. #define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
  39. #define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
  40. #define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */
  41. #define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maxiumum number of rounds in ARIA. */
  42. #define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
  43. #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */
  44. #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */
  45. #define MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE -0x005A /**< Feature not available. For example, an unsupported ARIA key size. */
  46. #define MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED -0x0058 /**< ARIA hardware accelerator failed. */
  47. #if !defined(MBEDTLS_ARIA_ALT)
  48. // Regular implementation
  49. //
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * \brief The ARIA context-type definition.
  55. */
  56. typedef struct
  57. {
  58. unsigned char nr; /*!< The number of rounds (12, 14 or 16) */
  59. /*! The ARIA round keys. */
  60. uint32_t rk[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
  61. }
  62. mbedtls_aria_context;
  63. #else /* MBEDTLS_ARIA_ALT */
  64. #include "aria_alt.h"
  65. #endif /* MBEDTLS_ARIA_ALT */
  66. /**
  67. * \brief This function initializes the specified ARIA context.
  68. *
  69. * It must be the first API called before using
  70. * the context.
  71. *
  72. * \param ctx The ARIA context to initialize.
  73. */
  74. void mbedtls_aria_init( mbedtls_aria_context *ctx );
  75. /**
  76. * \brief This function releases and clears the specified ARIA context.
  77. *
  78. * \param ctx The ARIA context to clear.
  79. */
  80. void mbedtls_aria_free( mbedtls_aria_context *ctx );
  81. /**
  82. * \brief This function sets the encryption key.
  83. *
  84. * \param ctx The ARIA context to which the key should be bound.
  85. * \param key The encryption key.
  86. * \param keybits The size of data passed in bits. Valid options are:
  87. * <ul><li>128 bits</li>
  88. * <li>192 bits</li>
  89. * <li>256 bits</li></ul>
  90. *
  91. * \return \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH
  92. * on failure.
  93. */
  94. int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
  95. const unsigned char *key,
  96. unsigned int keybits );
  97. /**
  98. * \brief This function sets the decryption key.
  99. *
  100. * \param ctx The ARIA context to which the key should be bound.
  101. * \param key The decryption key.
  102. * \param keybits The size of data passed. Valid options are:
  103. * <ul><li>128 bits</li>
  104. * <li>192 bits</li>
  105. * <li>256 bits</li></ul>
  106. *
  107. * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure.
  108. */
  109. int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
  110. const unsigned char *key,
  111. unsigned int keybits );
  112. /**
  113. * \brief This function performs an ARIA single-block encryption or
  114. * decryption operation.
  115. *
  116. * It performs encryption or decryption (depending on whether
  117. * the key was set for encryption on decryption) on the input
  118. * data buffer defined in the \p input parameter.
  119. *
  120. * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
  121. * mbedtls_aria_setkey_dec() must be called before the first
  122. * call to this API with the same context.
  123. *
  124. * \param ctx The ARIA context to use for encryption or decryption.
  125. * \param input The 16-Byte buffer holding the input data.
  126. * \param output The 16-Byte buffer holding the output data.
  127. * \return \c 0 on success.
  128. */
  129. int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
  130. const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
  131. unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] );
  132. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  133. /**
  134. * \brief This function performs an ARIA-CBC encryption or decryption operation
  135. * on full blocks.
  136. *
  137. * It performs the operation defined in the \p mode
  138. * parameter (encrypt/decrypt), on the input data buffer defined in
  139. * the \p input parameter.
  140. *
  141. * It can be called as many times as needed, until all the input
  142. * data is processed. mbedtls_aria_init(), and either
  143. * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
  144. * before the first call to this API with the same context.
  145. *
  146. * \note This function operates on aligned blocks, that is, the input size
  147. * must be a multiple of the ARIA block size of 16 Bytes.
  148. *
  149. * \note Upon exit, the content of the IV is updated so that you can
  150. * call the same function again on the next
  151. * block(s) of data and get the same result as if it was
  152. * encrypted in one call. This allows a "streaming" usage.
  153. * If you need to retain the contents of the IV, you should
  154. * either save it manually or use the cipher module instead.
  155. *
  156. *
  157. * \param ctx The ARIA context to use for encryption or decryption.
  158. * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
  159. * #MBEDTLS_ARIA_DECRYPT.
  160. * \param length The length of the input data in Bytes. This must be a
  161. * multiple of the block size (16 Bytes).
  162. * \param iv Initialization vector (updated after use).
  163. * \param input The buffer holding the input data.
  164. * \param output The buffer holding the output data.
  165. *
  166. * \return \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH
  167. * on failure.
  168. */
  169. int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
  170. int mode,
  171. size_t length,
  172. unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
  173. const unsigned char *input,
  174. unsigned char *output );
  175. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  176. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  177. /**
  178. * \brief This function performs an ARIA-CFB128 encryption or decryption
  179. * operation.
  180. *
  181. * It performs the operation defined in the \p mode
  182. * parameter (encrypt or decrypt), on the input data buffer
  183. * defined in the \p input parameter.
  184. *
  185. * For CFB, you must set up the context with mbedtls_aria_setkey_enc(),
  186. * regardless of whether you are performing an encryption or decryption
  187. * operation, that is, regardless of the \p mode parameter. This is
  188. * because CFB mode uses the same key schedule for encryption and
  189. * decryption.
  190. *
  191. * \note Upon exit, the content of the IV is updated so that you can
  192. * call the same function again on the next
  193. * block(s) of data and get the same result as if it was
  194. * encrypted in one call. This allows a "streaming" usage.
  195. * If you need to retain the contents of the
  196. * IV, you must either save it manually or use the cipher
  197. * module instead.
  198. *
  199. *
  200. * \param ctx The ARIA context to use for encryption or decryption.
  201. * \param mode The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or
  202. * #MBEDTLS_ARIA_DECRYPT.
  203. * \param length The length of the input data.
  204. * \param iv_off The offset in IV (updated after use).
  205. * \param iv The initialization vector (updated after use).
  206. * \param input The buffer holding the input data.
  207. * \param output The buffer holding the output data.
  208. *
  209. * \return \c 0 on success.
  210. */
  211. int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
  212. int mode,
  213. size_t length,
  214. size_t *iv_off,
  215. unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
  216. const unsigned char *input,
  217. unsigned char *output );
  218. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  219. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  220. /**
  221. * \brief This function performs an ARIA-CTR encryption or decryption
  222. * operation.
  223. *
  224. * This function performs the operation defined in the \p mode
  225. * parameter (encrypt/decrypt), on the input data buffer
  226. * defined in the \p input parameter.
  227. *
  228. * Due to the nature of CTR, you must use the same key schedule
  229. * for both encryption and decryption operations. Therefore, you
  230. * must use the context initialized with mbedtls_aria_setkey_enc()
  231. * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
  232. *
  233. * \warning You must never reuse a nonce value with the same key. Doing so
  234. * would void the encryption for the two messages encrypted with
  235. * the same nonce and key.
  236. *
  237. * There are two common strategies for managing nonces with CTR:
  238. *
  239. * 1. You can handle everything as a single message processed over
  240. * successive calls to this function. In that case, you want to
  241. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  242. * then preserve the values of \p nonce_counter, \p nc_off and \p
  243. * stream_block across calls to this function as they will be
  244. * updated by this function.
  245. *
  246. * With this strategy, you must not encrypt more than 2**128
  247. * blocks of data with the same key.
  248. *
  249. * 2. You can encrypt separate messages by dividing the \p
  250. * nonce_counter buffer in two areas: the first one used for a
  251. * per-message nonce, handled by yourself, and the second one
  252. * updated by this function internally.
  253. *
  254. * For example, you might reserve the first 12 bytes for the
  255. * per-message nonce, and the last 4 bytes for internal use. In that
  256. * case, before calling this function on a new message you need to
  257. * set the first 12 bytes of \p nonce_counter to your chosen nonce
  258. * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  259. * stream_block to be ignored). That way, you can encrypt at most
  260. * 2**96 messages of up to 2**32 blocks each with the same key.
  261. *
  262. * The per-message nonce (or information sufficient to reconstruct
  263. * it) needs to be communicated with the ciphertext and must be unique.
  264. * The recommended way to ensure uniqueness is to use a message
  265. * counter. An alternative is to generate random nonces, but this
  266. * limits the number of messages that can be securely encrypted:
  267. * for example, with 96-bit random nonces, you should not encrypt
  268. * more than 2**32 messages with the same key.
  269. *
  270. * Note that for both stategies, sizes are measured in blocks and
  271. * that an ARIA block is 16 bytes.
  272. *
  273. * \warning Upon return, \p stream_block contains sensitive data. Its
  274. * content must not be written to insecure storage and should be
  275. * securely discarded as soon as it's no longer needed.
  276. *
  277. * \param ctx The ARIA context to use for encryption or decryption.
  278. * \param length The length of the input data.
  279. * \param nc_off The offset in the current \p stream_block, for
  280. * resuming within the current cipher stream. The
  281. * offset pointer should be 0 at the start of a stream.
  282. * \param nonce_counter The 128-bit nonce and counter.
  283. * \param stream_block The saved stream block for resuming. This is
  284. * overwritten by the function.
  285. * \param input The buffer holding the input data.
  286. * \param output The buffer holding the output data.
  287. *
  288. * \return \c 0 on success.
  289. */
  290. int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
  291. size_t length,
  292. size_t *nc_off,
  293. unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
  294. unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
  295. const unsigned char *input,
  296. unsigned char *output );
  297. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  298. #if defined(MBEDTLS_SELF_TEST)
  299. /**
  300. * \brief Checkup routine.
  301. *
  302. * \return \c 0 on success, or \c 1 on failure.
  303. */
  304. int mbedtls_aria_self_test( int verbose );
  305. #endif /* MBEDTLS_SELF_TEST */
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif /* aria.h */