nrf_crypto_aes.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. * Copyright (c) 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_CRYPTO_AES_H__
  41. #define NRF_CRYPTO_AES_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_aes AES related functions
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Provides AES related functionality through nrf_crypto.
  49. */
  50. #include "sdk_common.h"
  51. #if NRF_MODULE_ENABLED(NRF_CRYPTO) || defined(__SDK_DOXYGEN__)
  52. #include <stdint.h>
  53. #include "nrf_crypto_types.h"
  54. #include "nrf_crypto_aes_shared.h"
  55. #include "nrf_crypto_aes_backend.h"
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**@brief External variable declaration to the info structure for AES CBC mode with a 128-bit key.
  60. * No padding.
  61. *
  62. * @note The variable is defined in the nrf_crypto backend that is
  63. * enabled in the @c sdk_config file.
  64. *
  65. */
  66. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_128_info;
  67. /**@brief External variable declaration to the info structure for AES CBC mode with a 192-bit key.
  68. * No padding.
  69. *
  70. * @note The variable is defined in the nrf_crypto backend that is
  71. * enabled in the @c sdk_config file.
  72. *
  73. */
  74. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_192_info;
  75. /**@brief External variable declaration to the info structure for AES CBC mode with a 256-bit key.
  76. * No padding.
  77. *
  78. * @note The variable is defined in the nrf_crypto backend that is
  79. * enabled in the @c sdk_config file.
  80. *
  81. */
  82. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_256_info;
  83. /**@brief External variable declaration to the info structure for AES CBC mode with a 128-bit key.
  84. * Padding pkcs7 enabled.
  85. *
  86. * @note The variable is defined in the nrf_crypto backend that is
  87. * enabled in the @c sdk_config file.
  88. *
  89. */
  90. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_128_pad_pkcs7_info;
  91. /**@brief External variable declaration to the info structure for AES CBC mode with a 192-bit key.
  92. * Padding pkcs7 enabled.
  93. *
  94. * @note The variable is defined in the nrf_crypto backend that is
  95. * enabled in the @c sdk_config file.
  96. *
  97. */
  98. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_192_pad_pkcs7_info;
  99. /**@brief External variable declaration to the info structure for AES CBC mode with a 256-bit key.
  100. * Padding pkcs7 enabled.
  101. *
  102. * @note The variable is defined in the nrf_crypto backend that is
  103. * enabled in the @c sdk_config file.
  104. *
  105. */
  106. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_256_pad_pkcs7_info;
  107. /**@brief External variable declaration to the info structure for AES CTR mode with a 128-bit key.
  108. *
  109. * @note The variable is defined in the nrf_crypto backend that is
  110. * enabled in the @c sdk_config file.
  111. *
  112. */
  113. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ctr_128_info;
  114. /**@brief External variable declaration to the info structure for AES CTR mode with a 192-bit key.
  115. *
  116. * @note The variable is defined in the nrf_crypto backend that is
  117. * enabled in the @c sdk_config file.
  118. *
  119. */
  120. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ctr_192_info;
  121. /**@brief External variable declaration to the info structure for AES CTR mode with a 256-bit key.
  122. *
  123. * @note The variable is defined in the nrf_crypto backend that is
  124. * enabled in the @c sdk_config file.
  125. *
  126. */
  127. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ctr_256_info;
  128. /**@brief External variable declaration to the info structure for AES CFB mode with a 128-bit key.
  129. *
  130. * @note The variable is defined in the nrf_crypto backend that is
  131. * enabled in the @c sdk_config file.
  132. *
  133. */
  134. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cfb_128_info;
  135. /**@brief External variable declaration to the info structure for AES CFB mode with a 192-bit key.
  136. *
  137. * @note The variable is defined in the nrf_crypto backend that is
  138. * enabled in the @c sdk_config file.
  139. *
  140. */
  141. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cfb_192_info;
  142. /**@brief External variable declaration to the info structure for AES CFB mode with a 256-bit key.
  143. *
  144. * @note The variable is defined in the nrf_crypto backend that is
  145. * enabled in the @c sdk_config file.
  146. *
  147. */
  148. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cfb_256_info;
  149. /**@brief External variable declaration to the info structure for AES ECB mode with a 128-bit key.
  150. * No padding.
  151. *
  152. * @note The variable is defined in the nrf_crypto backend that is
  153. * enabled in the @c sdk_config file.
  154. *
  155. */
  156. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_128_info;
  157. /**@brief External variable declaration to the info structure for AES ECB mode with a 192-bit key.
  158. * No padding.
  159. *
  160. * @note The variable is defined in the nrf_crypto backend that is
  161. * enabled in the @c sdk_config file.
  162. *
  163. */
  164. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_192_info;
  165. /**@brief External variable declaration to the info structure for AES ECB mode with a 256-bit key.
  166. * No padding.
  167. *
  168. * @note The variable is defined in the nrf_crypto backend that is
  169. * enabled in the @c sdk_config file.
  170. *
  171. */
  172. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_256_info;
  173. /**@brief External variable declaration to the info structure for AES ECB mode with a 128-bit key.
  174. * Padding pkcs7 enabled.
  175. *
  176. * @note The variable is defined in the nrf_crypto backend that is
  177. * enabled in the @c sdk_config file.
  178. *
  179. */
  180. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_128_pad_pkcs7_info;
  181. /**@brief External variable declaration to the info structure for AES ECB mode with a 192-bit key.
  182. * Padding pkcs7 enabled.
  183. *
  184. * @note The variable is defined in the nrf_crypto backend that is
  185. * enabled in the @c sdk_config file.
  186. *
  187. */
  188. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_192_pad_pkcs7_info;
  189. /**@brief External variable declaration to the info structure for AES ECB mode with a 256-bit key.
  190. * Padding pkcs7 enabled.
  191. *
  192. * @note The variable is defined in the nrf_crypto backend that is
  193. * enabled in the @c sdk_config file.
  194. *
  195. */
  196. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_ecb_256_pad_pkcs7_info;
  197. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 128-bit
  198. * key.
  199. *
  200. * @note The variable is defined in the nrf_crypto backend that is
  201. * enabled in the @c sdk_config file.
  202. *
  203. */
  204. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_128_info;
  205. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 192-bit
  206. * key.
  207. *
  208. * @note The variable is defined in the nrf_crypto backend that is
  209. * enabled in the @c sdk_config file.
  210. *
  211. */
  212. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_192_info;
  213. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 256-bit
  214. * key.
  215. *
  216. * @note The variable is defined in the nrf_crypto backend that is
  217. * enabled in the @c sdk_config file.
  218. *
  219. */
  220. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_256_info;
  221. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 128-bit
  222. * key.
  223. * Padding pkcs7 enabled.
  224. *
  225. * @note The variable is defined in the nrf_crypto backend that is
  226. * enabled in the @c sdk_config file.
  227. *
  228. */
  229. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_128_pad_pkcs7_info;
  230. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 192-bit
  231. * key.
  232. * Padding pkcs7 enabled.
  233. *
  234. * @note The variable is defined in the nrf_crypto backend that is
  235. * enabled in the @c sdk_config file.
  236. *
  237. */
  238. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_192_pad_pkcs7_info;
  239. /**@brief External variable declaration to the info structure for AES CBC MAC mode with a 256-bit
  240. * key.
  241. * Padding pkcs7 enabled.
  242. *
  243. * @note The variable is defined in the nrf_crypto backend that is
  244. * enabled in the @c sdk_config file.
  245. *
  246. */
  247. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cbc_mac_256_pad_pkcs7_info;
  248. /**@brief External variable declaration to the info structure for AES CMAC mode with a 128-bit key.
  249. *
  250. * @note The variable is defined in the nrf_crypto backend that is
  251. * enabled in the @c sdk_config file.
  252. *
  253. */
  254. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cmac_128_info;
  255. /**@brief External variable declaration to the info structure for AES CMAC mode with a 192-bit key.
  256. *
  257. * @note The variable is defined in the nrf_crypto backend that is
  258. * enabled in the @c sdk_config file.
  259. *
  260. */
  261. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cmac_192_info;
  262. /**@brief External variable declaration to the info structure for AES CMAC mode with a 256-bit key.
  263. *
  264. * @note The variable is defined in the nrf_crypto backend that is
  265. * enabled in the @c sdk_config file.
  266. *
  267. */
  268. extern const nrf_crypto_aes_info_t g_nrf_crypto_aes_cmac_256_info;
  269. /**
  270. * @brief Context type for AES.
  271. *
  272. * @note The size of this type is scaled for the largest AES backend context that is
  273. * enabled in @ref sdk_config.
  274. */
  275. typedef nrf_crypto_backend_aes_context_t nrf_crypto_aes_context_t;
  276. /**@brief Function for initializing the AES context.
  277. *
  278. * @param[in] p_context Pointer to the context object. It must be a context type associated
  279. * with the object provided in the p_info parameter or other memory
  280. * that can hold that context type.
  281. * @param[in] p_info Pointer to structure holding information about: selected AES mode,
  282. * key size, and padding.
  283. * @param[in] operation Parameter indicating whether an encrypt (NRF_CRYPTO_ENCRYPT),
  284. * a decrypt (NRF_CRYPTO_DECRYPT) or MAC calculation
  285. * (NRF_CRYPTO_MAC_CALCULATE) operation shall be performed.
  286. *
  287. * @return NRF_SUCCESS on success.
  288. */
  289. ret_code_t nrf_crypto_aes_init(nrf_crypto_aes_context_t * const p_context,
  290. nrf_crypto_aes_info_t const * const p_info,
  291. nrf_crypto_operation_t operation);
  292. /**@brief Internal function for uninitializing the AES context.
  293. *
  294. * @param[in] p_context Context object. Must be initialized before the call.
  295. *
  296. * @return NRF_SUCCESS on success.
  297. */
  298. ret_code_t nrf_crypto_aes_uninit(nrf_crypto_aes_context_t * const p_context);
  299. /**@brief Function for setting the AES key.
  300. *
  301. * @param[in] p_context Context object. Must be initialized before the call.
  302. * @param[in] p_key Pointer to the AES key. This buffer will be copied and there is no need
  303. * to keep it by the user.
  304. *
  305. * @return NRF_SUCCESS on success.
  306. */
  307. ret_code_t nrf_crypto_aes_key_set(nrf_crypto_aes_context_t * const p_context, uint8_t * p_key);
  308. /**@brief Function for setting an AES IV or a counter for AES modes which are using it.
  309. *
  310. * @param[in] p_context Context object. Must be initialized before the call.
  311. * @param[in] p_iv Pointer to a buffer of the IV or a counter. This buffer will be copied
  312. * and there is no need to keep it by the user.
  313. *
  314. * @return NRF_SUCCESS on success.
  315. */
  316. ret_code_t nrf_crypto_aes_iv_set(nrf_crypto_aes_context_t * const p_context, uint8_t * p_iv);
  317. /**@brief Function for getting an AES IV or a counter for mode which is supporting it.
  318. *
  319. * @param[in] p_context Context object. Must be initialized before the call.
  320. * @param[out] p_iv Pointer to a buffer of the IV or a counter.
  321. *
  322. * @return NRF_SUCCESS on success.
  323. */
  324. ret_code_t nrf_crypto_aes_iv_get(nrf_crypto_aes_context_t * const p_context, uint8_t * p_iv);
  325. /**@brief AES update function for encryption, decryption and MAC calculation. It can be called once
  326. * on the whole data block, or as many times as needed, until all the input data is processed.
  327. * Functions: @ref nrf_crypto_aes_init, @ref nrf_crypto_aes_key_set, and, for some ciphers,
  328. * @ref nrf_crypto_aes_iv_set, must be called before call to this API with the same context.
  329. *
  330. * @param[in] p_context Context object. Must be initialized before the call.
  331. * @param[in] p_data_in Pointer to the input buffer to the AES.
  332. * @param[in] data_size Size of the data to be processed in bytes.
  333. * For all modes except CFB it must be multiple of 16 bytes.
  334. * @param[out] p_data_out Pointer to the output buffer.
  335. *
  336. * @return NRF_SUCCESS on success.
  337. */
  338. ret_code_t nrf_crypto_aes_update(nrf_crypto_aes_context_t * const p_context,
  339. uint8_t * p_data_in,
  340. size_t data_size,
  341. uint8_t * p_data_out);
  342. /**@brief Function processes the last data block if needed and finalizes the AES operation (ie. adds
  343. * padding) and produces operation results (for MAC operations).
  344. * Functions: @ref nrf_crypto_aes_init, @ref nrf_crypto_aes_key_set, and, for some ciphers,
  345. * @ref nrf_crypto_aes_iv_set, must be called before call to this API with the same context.
  346. *
  347. * Upon successful operation function will deinitialize the context but for some ciphers it will be
  348. * possible to read IV. In order to fully deinitialize context you must call
  349. * @ref nrf_crypto_aes_uninit.
  350. *
  351. * @param[in] p_context Context object. Must be initialized before the call.
  352. * @param[in] p_data_in Pointer to the input buffer to the AES.
  353. * @param[in] data_size Size of the data to be processed in bytes.
  354. * @param[out] p_data_out Pointer to the output buffer.
  355. * When padding is set:
  356. * - The size of p_data_out buffer must have extra space for
  357. * padding. Otherwise, the function will return an error:
  358. * NRF_ERROR_CRYPTO_OUTPUT_LENGTH.
  359. * - When text_size is multiple of 16 bytes, p_text_out must be
  360. * allocated with size equal to text_size + an additional block
  361. * (i.e 16 bytes for padding).
  362. * - When text_size is not a multiple of 16 bytes, p_text_out
  363. * must be allocated with size aligned to the next full 16
  364. * bytes block (i.e. 1 - 15 bytes for padding).
  365. * @param[in,out] p_data_out_size IN:
  366. * Size of the p_data_out buffer.
  367. * OUT:
  368. * Upon successfull function execution value will be updated
  369. * with number of signifacnt bytes in p_data_out buffer.
  370. * On decryption with padding function will result in a value
  371. * without padded bytes.
  372. *
  373. * @return NRF_SUCCESS on success.
  374. */
  375. ret_code_t nrf_crypto_aes_finalize(nrf_crypto_aes_context_t * const p_context,
  376. uint8_t * p_data_in,
  377. size_t data_size,
  378. uint8_t * p_data_out,
  379. size_t * p_data_out_size);
  380. /**@brief AES integrated function for encryption, decryption and MAC calculation.
  381. * It should be called once on the whole data block.
  382. *
  383. * @param[in] p_context Context object. If NULL, memory will be dynamically allocated.
  384. * @param[in] p_info Pointer to structure holding information about: selected AES
  385. * mode, key size, and padding.
  386. * @param[in] operation Parameter indicating whether an encrypt (NRF_CRYPTO_ENCRYPT),
  387. * a decrypt (NRF_CRYPTO_DECRYPT) or MAC calculation
  388. * (NRF_CRYPTO_MAC_CALCULATE) operation shall be performed.
  389. * @param[in] p_key Pointer to the AES key. This buffer will be copied and there is
  390. * no need to keep it by the user.
  391. * @param[in] p_iv Pointer to a buffer of the IV or a counter. This buffer will be
  392. * copied and there is no need to keep it by the user.
  393. * Can be NULL for ECB and CMAC.
  394. * @param[in] p_data_in Pointer to the input buffer to the AES.
  395. * @param[in] data_size Size of the data to be processed in bytes.
  396. * @param[out] p_data_out Pointer to the output buffer.
  397. * When padding is set:
  398. * - The size of p_data_out buffer must have extra space for
  399. * padding. Otherwise, the function will return an error:
  400. * NRF_ERROR_CRYPTO_OUTPUT_LENGTH.
  401. * - When text_size is multiple of 16 bytes, p_text_out must be
  402. * allocated with size equal to text_size + an additional block
  403. * (i.e 16 bytes for padding).
  404. * - When text_size is not a multiple of 16 bytes, p_text_out
  405. * must be allocated with size aligned to the next full 16
  406. * bytes block (i.e. 1 - 15 bytes for padding).
  407. * @param[in,out] p_data_out_size IN:
  408. * Size of the p_data_out buffer.
  409. * OUT:
  410. * Upon successfull function execution value will be updated
  411. * with number of signifacnt bytes in p_data_out buffer.
  412. * On decryption function will result in a value without padded
  413. * bytes.
  414. *
  415. * @return NRF_SUCCESS on success.
  416. */
  417. ret_code_t nrf_crypto_aes_crypt(nrf_crypto_aes_context_t * const p_context,
  418. nrf_crypto_aes_info_t const * const p_info,
  419. nrf_crypto_operation_t operation,
  420. uint8_t * p_key,
  421. uint8_t * p_iv,
  422. uint8_t * p_data_in,
  423. size_t data_size,
  424. uint8_t * p_data_out,
  425. size_t * p_data_out_size);
  426. #ifdef __cplusplus
  427. }
  428. #endif
  429. #endif // #if NRF_MODULE_ENABLED(NRF_CRYPTO) || defined(__SDK_DOXYGEN__)
  430. /** @} */
  431. #endif // #ifndef NRF_CRYPTO_AES_H__