aria.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /*
  13. * Copyright The Mbed TLS Contributors
  14. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  15. */
  16. #ifndef MBEDTLS_ARIA_H
  17. #define MBEDTLS_ARIA_H
  18. #include "mbedtls/private_access.h"
  19. #include "mbedtls/build_info.h"
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "mbedtls/platform_util.h"
  23. #define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */
  24. #define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */
  25. #define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */
  26. #define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maximum number of rounds in ARIA. */
  27. #define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */
  28. /** Bad input data. */
  29. #define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C
  30. /** Invalid data input length. */
  31. #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #if !defined(MBEDTLS_ARIA_ALT)
  36. // Regular implementation
  37. //
  38. /**
  39. * \brief The ARIA context-type definition.
  40. */
  41. typedef struct mbedtls_aria_context {
  42. unsigned char MBEDTLS_PRIVATE(nr); /*!< The number of rounds (12, 14 or 16) */
  43. /*! The ARIA round keys. */
  44. uint32_t MBEDTLS_PRIVATE(rk)[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];
  45. }
  46. mbedtls_aria_context;
  47. #else /* MBEDTLS_ARIA_ALT */
  48. #include "aria_alt.h"
  49. #endif /* MBEDTLS_ARIA_ALT */
  50. /**
  51. * \brief This function initializes the specified ARIA context.
  52. *
  53. * It must be the first API called before using
  54. * the context.
  55. *
  56. * \param ctx The ARIA context to initialize. This must not be \c NULL.
  57. */
  58. void mbedtls_aria_init(mbedtls_aria_context *ctx);
  59. /**
  60. * \brief This function releases and clears the specified ARIA context.
  61. *
  62. * \param ctx The ARIA context to clear. This may be \c NULL, in which
  63. * case this function returns immediately. If it is not \c NULL,
  64. * it must point to an initialized ARIA context.
  65. */
  66. void mbedtls_aria_free(mbedtls_aria_context *ctx);
  67. /**
  68. * \brief This function sets the encryption key.
  69. *
  70. * \param ctx The ARIA context to which the key should be bound.
  71. * This must be initialized.
  72. * \param key The encryption key. This must be a readable buffer
  73. * of size \p keybits Bits.
  74. * \param keybits The size of \p key in Bits. Valid options are:
  75. * <ul><li>128 bits</li>
  76. * <li>192 bits</li>
  77. * <li>256 bits</li></ul>
  78. *
  79. * \return \c 0 on success.
  80. * \return A negative error code on failure.
  81. */
  82. int mbedtls_aria_setkey_enc(mbedtls_aria_context *ctx,
  83. const unsigned char *key,
  84. unsigned int keybits);
  85. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  86. /**
  87. * \brief This function sets the decryption key.
  88. *
  89. * \param ctx The ARIA context to which the key should be bound.
  90. * This must be initialized.
  91. * \param key The decryption key. This must be a readable buffer
  92. * of size \p keybits Bits.
  93. * \param keybits The size of data passed. Valid options are:
  94. * <ul><li>128 bits</li>
  95. * <li>192 bits</li>
  96. * <li>256 bits</li></ul>
  97. *
  98. * \return \c 0 on success.
  99. * \return A negative error code on failure.
  100. */
  101. int mbedtls_aria_setkey_dec(mbedtls_aria_context *ctx,
  102. const unsigned char *key,
  103. unsigned int keybits);
  104. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  105. /**
  106. * \brief This function performs an ARIA single-block encryption or
  107. * decryption operation.
  108. *
  109. * It performs encryption or decryption (depending on whether
  110. * the key was set for encryption on decryption) on the input
  111. * data buffer defined in the \p input parameter.
  112. *
  113. * mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or
  114. * mbedtls_aria_setkey_dec() must be called before the first
  115. * call to this API with the same context.
  116. *
  117. * \param ctx The ARIA context to use for encryption or decryption.
  118. * This must be initialized and bound to a key.
  119. * \param input The 16-Byte buffer holding the input data.
  120. * \param output The 16-Byte buffer holding the output data.
  121. * \return \c 0 on success.
  122. * \return A negative error code on failure.
  123. */
  124. int mbedtls_aria_crypt_ecb(mbedtls_aria_context *ctx,
  125. const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],
  126. unsigned char output[MBEDTLS_ARIA_BLOCKSIZE]);
  127. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  128. /**
  129. * \brief This function performs an ARIA-CBC encryption or decryption operation
  130. * on full blocks.
  131. *
  132. * It performs the operation defined in the \p mode
  133. * parameter (encrypt/decrypt), on the input data buffer defined in
  134. * the \p input parameter.
  135. *
  136. * It can be called as many times as needed, until all the input
  137. * data is processed. mbedtls_aria_init(), and either
  138. * mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called
  139. * before the first call to this API with the same context.
  140. *
  141. * \note This function operates on aligned blocks, that is, the input size
  142. * must be a multiple of the ARIA block size of 16 Bytes.
  143. *
  144. * \note Upon exit, the content of the IV is updated so that you can
  145. * call the same function again on the next
  146. * block(s) of data and get the same result as if it was
  147. * encrypted in one call. This allows a "streaming" usage.
  148. * If you need to retain the contents of the IV, you should
  149. * either save it manually or use the cipher module instead.
  150. *
  151. *
  152. * \param ctx The ARIA context to use for encryption or decryption.
  153. * This must be initialized and bound to a key.
  154. * \param mode The mode of operation. This must be either
  155. * #MBEDTLS_ARIA_ENCRYPT for encryption, or
  156. * #MBEDTLS_ARIA_DECRYPT for decryption.
  157. * \param length The length of the input data in Bytes. This must be a
  158. * multiple of the block size (16 Bytes).
  159. * \param iv Initialization vector (updated after use).
  160. * This must be a readable buffer of size 16 Bytes.
  161. * \param input The buffer holding the input data. This must
  162. * be a readable buffer of length \p length Bytes.
  163. * \param output The buffer holding the output data. This must
  164. * be a writable buffer of length \p length Bytes.
  165. *
  166. * \return \c 0 on success.
  167. * \return A negative error code 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. * This must be initialized and bound to a key.
  202. * \param mode The mode of operation. This must be either
  203. * #MBEDTLS_ARIA_ENCRYPT for encryption, or
  204. * #MBEDTLS_ARIA_DECRYPT for decryption.
  205. * \param length The length of the input data \p input in Bytes.
  206. * \param iv_off The offset in IV (updated after use).
  207. * This must not be larger than 15.
  208. * \param iv The initialization vector (updated after use).
  209. * This must be a readable buffer of size 16 Bytes.
  210. * \param input The buffer holding the input data. This must
  211. * be a readable buffer of length \p length Bytes.
  212. * \param output The buffer holding the output data. This must
  213. * be a writable buffer of length \p length Bytes.
  214. *
  215. * \return \c 0 on success.
  216. * \return A negative error code on failure.
  217. */
  218. int mbedtls_aria_crypt_cfb128(mbedtls_aria_context *ctx,
  219. int mode,
  220. size_t length,
  221. size_t *iv_off,
  222. unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],
  223. const unsigned char *input,
  224. unsigned char *output);
  225. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  226. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  227. /**
  228. * \brief This function performs an ARIA-CTR encryption or decryption
  229. * operation.
  230. *
  231. * Due to the nature of CTR, you must use the same key schedule
  232. * for both encryption and decryption operations. Therefore, you
  233. * must use the context initialized with mbedtls_aria_setkey_enc()
  234. * for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.
  235. *
  236. * \warning You must never reuse a nonce value with the same key. Doing so
  237. * would void the encryption for the two messages encrypted with
  238. * the same nonce and key.
  239. *
  240. * There are two common strategies for managing nonces with CTR:
  241. *
  242. * 1. You can handle everything as a single message processed over
  243. * successive calls to this function. In that case, you want to
  244. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  245. * then preserve the values of \p nonce_counter, \p nc_off and \p
  246. * stream_block across calls to this function as they will be
  247. * updated by this function.
  248. *
  249. * With this strategy, you must not encrypt more than 2**128
  250. * blocks of data with the same key.
  251. *
  252. * 2. You can encrypt separate messages by dividing the \p
  253. * nonce_counter buffer in two areas: the first one used for a
  254. * per-message nonce, handled by yourself, and the second one
  255. * updated by this function internally.
  256. *
  257. * For example, you might reserve the first 12 bytes for the
  258. * per-message nonce, and the last 4 bytes for internal use. In that
  259. * case, before calling this function on a new message you need to
  260. * set the first 12 bytes of \p nonce_counter to your chosen nonce
  261. * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  262. * stream_block to be ignored). That way, you can encrypt at most
  263. * 2**96 messages of up to 2**32 blocks each with the same key.
  264. *
  265. * The per-message nonce (or information sufficient to reconstruct
  266. * it) needs to be communicated with the ciphertext and must be unique.
  267. * The recommended way to ensure uniqueness is to use a message
  268. * counter. An alternative is to generate random nonces, but this
  269. * limits the number of messages that can be securely encrypted:
  270. * for example, with 96-bit random nonces, you should not encrypt
  271. * more than 2**32 messages with the same key.
  272. *
  273. * Note that for both strategies, sizes are measured in blocks and
  274. * that an ARIA block is 16 bytes.
  275. *
  276. * \warning Upon return, \p stream_block contains sensitive data. Its
  277. * content must not be written to insecure storage and should be
  278. * securely discarded as soon as it's no longer needed.
  279. *
  280. * \param ctx The ARIA context to use for encryption or decryption.
  281. * This must be initialized and bound to a key.
  282. * \param length The length of the input data \p input in Bytes.
  283. * \param nc_off The offset in Bytes in the current \p stream_block,
  284. * for resuming within the current cipher stream. The
  285. * offset pointer should be \c 0 at the start of a
  286. * stream. This must not be larger than \c 15 Bytes.
  287. * \param nonce_counter The 128-bit nonce and counter. This must point to
  288. * a read/write buffer of length \c 16 bytes.
  289. * \param stream_block The saved stream block for resuming. This must
  290. * point to a read/write buffer of length \c 16 bytes.
  291. * This is overwritten by the function.
  292. * \param input The buffer holding the input data. This must
  293. * be a readable buffer of length \p length Bytes.
  294. * \param output The buffer holding the output data. This must
  295. * be a writable buffer of length \p length Bytes.
  296. *
  297. * \return \c 0 on success.
  298. * \return A negative error code on failure.
  299. */
  300. int mbedtls_aria_crypt_ctr(mbedtls_aria_context *ctx,
  301. size_t length,
  302. size_t *nc_off,
  303. unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],
  304. unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],
  305. const unsigned char *input,
  306. unsigned char *output);
  307. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  308. #if defined(MBEDTLS_SELF_TEST)
  309. /**
  310. * \brief Checkup routine.
  311. *
  312. * \return \c 0 on success, or \c 1 on failure.
  313. */
  314. int mbedtls_aria_self_test(int verbose);
  315. #endif /* MBEDTLS_SELF_TEST */
  316. #ifdef __cplusplus
  317. }
  318. #endif
  319. #endif /* aria.h */