utils_aes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. * Licensed under the MIT License (the "License"); you may not use this file
  6. except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://opensource.org/licenses/MIT
  9. * Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is
  11. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND,
  13. * either express or implied. See the License for the specific language
  14. governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef QCLOUD_IOT_UTILS_AES_H_
  19. #define QCLOUD_IOT_UTILS_AES_H_
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. //========Platform================================//
  26. #define UTILS_AES_C
  27. #define UTILS_CIPHER_MODE_CBC
  28. //#define UTILS_SELF_TEST
  29. #define UTILS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070 /**< Hardware accelerator failed */
  30. #define UTILS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 /**< The requested feature is not supported by the platform */
  31. /* Internal macros meant to be called only from within the library. */
  32. #define UTILS_INTERNAL_VALIDATE_RET(cond, ret) \
  33. do { \
  34. } while (0)
  35. #define UTILS_INTERNAL_VALIDATE(cond) \
  36. do { \
  37. } while (0)
  38. //==============================================//
  39. /* padlock.c and aesni.c rely on these values! */
  40. #define UTILS_AES_ENCRYPT 1 /**< AES encryption. */
  41. #define UTILS_AES_DECRYPT 0 /**< AES decryption. */
  42. #define UTILS_AES_BLOCK_LEN 16
  43. #define AES_KEY_BITS_128 128
  44. #define AES_KEY_BITS_192 192
  45. #define AES_KEY_BITS_256 256
  46. /* Error codes in range 0x0020-0x0022 */
  47. #define UTILS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  48. #define UTILS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  49. /* Error codes in range 0x0021-0x0025 */
  50. #define UTILS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
  51. /* UTILS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */
  52. #define UTILS_ERR_AES_FEATURE_UNAVAILABLE \
  53. -0x0023 /**< Feature not available. For example, an unsupported AES key \
  54. size. */
  55. /* UTILS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */
  56. #define UTILS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
  57. #if !defined(UTILS_AES_ALT)
  58. // Regular implementation
  59. //
  60. /**
  61. * \brief The AES context-type definition.
  62. */
  63. typedef struct utils_aes_context {
  64. int nr; /*!< The number of rounds. */
  65. uint32_t *rk; /*!< AES round keys. */
  66. uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
  67. hold 32 extra Bytes, which can be used for
  68. one of the following purposes:
  69. <ul><li>Alignment if VIA padlock is
  70. used.</li>
  71. <li>Simplifying key expansion in the 256-bit
  72. case by generating an extra round key.
  73. </li></ul> */
  74. } utils_aes_context;
  75. #else /* UTILS_AES_ALT */
  76. #include "aes_alt.h"
  77. #endif /* UTILS_AES_ALT */
  78. /**
  79. * \brief This function initializes the specified AES context.
  80. *
  81. * It must be the first API called before using
  82. * the context.
  83. *
  84. * \param ctx The AES context to initialize. This must not be \c NULL.
  85. */
  86. void utils_aes_init(utils_aes_context *ctx);
  87. /**
  88. * \brief This function releases and clears the specified AES context.
  89. *
  90. * \param ctx The AES context to clear.
  91. * If this is \c NULL, this function does nothing.
  92. * Otherwise, the context must have been at least initialized.
  93. */
  94. void utils_aes_free(utils_aes_context *ctx);
  95. /**
  96. * \brief This function sets the encryption key.
  97. *
  98. * \param ctx The AES context to which the key should be bound.
  99. * It must be initialized.
  100. * \param key The encryption key.
  101. * This must be a readable buffer of size \p keybits bits.
  102. * \param keybits The size of data passed in bits. 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.
  108. * \return #UTILS_ERR_AES_INVALID_KEY_LENGTH on failure.
  109. */
  110. int utils_aes_setkey_enc(utils_aes_context *ctx, const unsigned char *key, unsigned int keybits);
  111. /**
  112. * \brief This function sets the decryption key.
  113. *
  114. * \param ctx The AES context to which the key should be bound.
  115. * It must be initialized.
  116. * \param key The decryption key.
  117. * This must be a readable buffer of size \p keybits bits.
  118. * \param keybits The size of data passed. Valid options are:
  119. * <ul><li>128 bits</li>
  120. * <li>192 bits</li>
  121. * <li>256 bits</li></ul>
  122. *
  123. * \return \c 0 on success.
  124. * \return #UTILS_ERR_AES_INVALID_KEY_LENGTH on failure.
  125. */
  126. int utils_aes_setkey_dec(utils_aes_context *ctx, const unsigned char *key, unsigned int keybits);
  127. /**
  128. * \brief This function performs an AES single-block encryption or
  129. * decryption operation.
  130. *
  131. * It performs the operation defined in the \p mode parameter
  132. * (encrypt or decrypt), on the input data buffer defined in
  133. * the \p input parameter.
  134. *
  135. * utils_aes_init(), and either utils_aes_setkey_enc() or
  136. * utils_aes_setkey_dec() must be called before the first
  137. * call to this API with the same context.
  138. *
  139. * \param ctx The AES context to use for encryption or decryption.
  140. * It must be initialized and bound to a key.
  141. * \param mode The AES operation: #UTILS_AES_ENCRYPT or
  142. * #UTILS_AES_DECRYPT.
  143. * \param input The buffer holding the input data.
  144. * It must be readable and at least \c 16 Bytes long.
  145. * \param output The buffer where the output data will be written.
  146. * It must be writeable and at least \c 16 Bytes long.
  147. * \return \c 0 on success.
  148. */
  149. int utils_aes_crypt_ecb(utils_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16]);
  150. #if defined(UTILS_CIPHER_MODE_CBC)
  151. /**
  152. * \brief This function performs an AES-CBC encryption or decryption operation
  153. * on full blocks.
  154. *
  155. * It performs the operation defined in the \p mode
  156. * parameter (encrypt/decrypt), on the input data buffer defined in
  157. * the \p input parameter.
  158. *
  159. * It can be called as many times as needed, until all the input
  160. * data is processed. utils_aes_init(), and either
  161. * utils_aes_setkey_enc() or utils_aes_setkey_dec() must be called
  162. * before the first call to this API with the same context.
  163. *
  164. * \note This function operates on full blocks, that is, the input size
  165. * must be a multiple of the AES block size of \c 16 Bytes.
  166. *
  167. * \note Upon exit, the content of the IV is updated so that you can
  168. * call the same function again on the next
  169. * block(s) of data and get the same result as if it was
  170. * encrypted in one call. This allows a "streaming" usage.
  171. * If you need to retain the contents of the IV, you should
  172. * either save it manually or use the cipher module instead.
  173. *
  174. *
  175. * \param ctx The AES context to use for encryption or decryption.
  176. * It must be initialized and bound to a key.
  177. * \param mode The AES operation: #UTILS_AES_ENCRYPT or
  178. * #UTILS_AES_DECRYPT.
  179. * \param length The length of the input data in Bytes. This must be a
  180. * multiple of the block size (\c 16 Bytes).
  181. * \param iv Initialization vector (updated after use).
  182. * It must be a readable and writeable buffer of \c 16 Bytes.
  183. * \param input The buffer holding the input data.
  184. * It must be readable and of size \p length Bytes.
  185. * \param output The buffer holding the output data.
  186. * It must be writeable and of size \p length Bytes.
  187. *
  188. * \return \c 0 on success.
  189. * \return #UTILS_ERR_AES_INVALID_INPUT_LENGTH
  190. * on failure.
  191. */
  192. int utils_aes_crypt_cbc(utils_aes_context *ctx, int mode, size_t length, unsigned char iv[16],
  193. const unsigned char *input, unsigned char *output);
  194. #endif /* UTILS_CIPHER_MODE_CBC */
  195. /**
  196. * \brief Internal AES block encryption function. This is only
  197. * exposed to allow overriding it using
  198. * \c UTILS_AES_ENCRYPT_ALT.
  199. *
  200. * \param ctx The AES context to use for encryption.
  201. * \param input The plaintext block.
  202. * \param output The output (ciphertext) block.
  203. *
  204. * \return \c 0 on success.
  205. */
  206. int utils_internal_aes_encrypt(utils_aes_context *ctx, const unsigned char input[16], unsigned char output[16]);
  207. /**
  208. * \brief Internal AES block decryption function. This is only
  209. * exposed to allow overriding it using see
  210. * \c UTILS_AES_DECRYPT_ALT.
  211. *
  212. * \param ctx The AES context to use for decryption.
  213. * \param input The ciphertext block.
  214. * \param output The output (plaintext) block.
  215. *
  216. * \return \c 0 on success.
  217. */
  218. int utils_internal_aes_decrypt(utils_aes_context *ctx, const unsigned char input[16], unsigned char output[16]);
  219. #if !defined(UTILS_DEPRECATED_REMOVED)
  220. #if defined(UTILS_DEPRECATED_WARNING)
  221. #define UTILS_DEPRECATED __attribute__((deprecated))
  222. #else
  223. #define UTILS_DEPRECATED
  224. #endif
  225. /**
  226. * \brief Deprecated internal AES block encryption function
  227. * without return value.
  228. *
  229. * \deprecated Superseded by utils_internal_aes_encrypt()
  230. *
  231. * \param ctx The AES context to use for encryption.
  232. * \param input Plaintext block.
  233. * \param output Output (ciphertext) block.
  234. */
  235. UTILS_DEPRECATED void utils_aes_encrypt(utils_aes_context *ctx, const unsigned char input[16],
  236. unsigned char output[16]);
  237. /**
  238. * \brief Deprecated internal AES block decryption function
  239. * without return value.
  240. *
  241. * \deprecated Superseded by utils_internal_aes_decrypt()
  242. *
  243. * \param ctx The AES context to use for decryption.
  244. * \param input Ciphertext block.
  245. * \param output Output (plaintext) block.
  246. */
  247. UTILS_DEPRECATED void utils_aes_decrypt(utils_aes_context *ctx, const unsigned char input[16],
  248. unsigned char output[16]);
  249. #undef UTILS_DEPRECATED
  250. #endif /* !UTILS_DEPRECATED_REMOVED */
  251. #if defined(UTILS_SELF_TEST)
  252. /**
  253. * \brief Checkup routine.
  254. *
  255. * \return \c 0 on success.
  256. * \return \c 1 on failure.
  257. */
  258. int utils_aes_self_test(int verbose);
  259. #endif /* UTILS_SELF_TEST */
  260. int aes_sample(int verbose);
  261. int utils_aes_cbc(uint8_t *pInData, uint32_t datalen, uint8_t *pOutData, uint32_t outBuffLen, uint8_t mode,
  262. uint8_t *pKey, uint16_t keybits, uint8_t *iv);
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif