fsl_hashcrypt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright 2017-2018 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef _FSL_HASHCRYPT_H_
  8. #define _FSL_HASHCRYPT_H_
  9. #include "fsl_common.h"
  10. /*! @brief HASHCRYPT status return codes. */
  11. enum _hashcrypt_status
  12. {
  13. kStatus_HASHCRYPT_Again =
  14. MAKE_STATUS(kStatusGroup_HASHCRYPT, 0), /*!< Non-blocking function shall be called again. */
  15. };
  16. /*******************************************************************************
  17. * Definitions
  18. *******************************************************************************/
  19. /*!
  20. * @addtogroup hashcrypt_driver
  21. * @{
  22. */
  23. /*! @name Driver version */
  24. /*@{*/
  25. /*! @brief HASHCRYPT driver version. Version 2.0.0.
  26. *
  27. * Current version: 2.0.0
  28. *
  29. * Change log:
  30. * - Version 2.0.0
  31. * - Initial version
  32. */
  33. #define FSL_HASHCRYPT_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  34. /*@}*/
  35. /*! @brief Algorithm used for Hashcrypt operation */
  36. typedef enum _hashcrypt_algo_t
  37. {
  38. kHASHCRYPT_Sha1 = 1, /*!< SHA_1 */
  39. kHASHCRYPT_Sha256 = 2, /*!< SHA_256 */
  40. kHASHCRYPT_Sha512 = 3, /*!< SHA_512 */
  41. kHASHCRYPT_Aes = 4, /*!< AES */
  42. kHASHCRYPT_AesIcb = 5, /*!< AES_ICB */
  43. } hashcrypt_algo_t;
  44. /*! @} */
  45. /*******************************************************************************
  46. * AES Definitions
  47. *******************************************************************************/
  48. /*!
  49. * @addtogroup hashcrypt_driver_aes
  50. * @{
  51. */
  52. /*! AES block size in bytes */
  53. #define HASHCRYPT_AES_BLOCK_SIZE 16
  54. #define AES_ENCRYPT 0
  55. #define AES_DECRYPT 1
  56. /*! @brief AES mode */
  57. typedef enum _hashcrypt_aes_mode_t
  58. {
  59. kHASHCRYPT_AesEcb = 0U, /*!< AES ECB mode */
  60. kHASHCRYPT_AesCbc = 1U, /*!< AES CBC mode */
  61. kHASHCRYPT_AesCtr = 2U, /*!< AES CTR mode */
  62. } hashcrypt_aes_mode_t;
  63. /*! @brief Size of AES key */
  64. typedef enum _hashcrypt_aes_keysize_t
  65. {
  66. kHASHCRYPT_Aes128 = 0U, /*!< AES 128 bit key */
  67. kHASHCRYPT_Aes192 = 1U, /*!< AES 192 bit key */
  68. kHASHCRYPT_Aes256 = 2U, /*!< AES 256 bit key */
  69. kHASHCRYPT_InvalidKey = 3U, /*!< AES invalid key */
  70. } hashcrypt_aes_keysize_t;
  71. /*! @brief HASHCRYPT key source selection.
  72. *
  73. */
  74. typedef enum _hashcrypt_key
  75. {
  76. kHASHCRYPT_UserKey = 0xc3c3U, /*!< HASHCRYPT user key */
  77. kHASHCRYPT_SecretKey = 0x3c3cU, /*!< HASHCRYPT secret key (dedicated hw bus from PUF) */
  78. } hashcrypt_key_t;
  79. /*! @brief Specify HASHCRYPT's key resource. */
  80. typedef struct _hashcrypt_handle
  81. {
  82. uint32_t keyWord[8]; /*!< Copy of user key (set by HASHCRYPT_AES_SetKey(). */
  83. hashcrypt_aes_keysize_t keySize;
  84. hashcrypt_key_t keyType; /*!< For operations with key (such as AES encryption/decryption), specify key type. */
  85. } hashcrypt_handle_t;
  86. /*!
  87. *@}
  88. */ /* end of hashcrypt_driver_aes */
  89. /*******************************************************************************
  90. * HASH Definitions
  91. ******************************************************************************/
  92. /*!
  93. * @addtogroup hashcrypt_driver_hash
  94. * @{
  95. */
  96. /*! @brief HASHCRYPT HASH Context size. */
  97. #define HASHCRYPT_HASH_CTX_SIZE 22
  98. /*! @brief Storage type used to save hash context. */
  99. typedef struct _hashcrypt_hash_ctx_t
  100. {
  101. uint32_t x[HASHCRYPT_HASH_CTX_SIZE]; /*!< storage */
  102. } hashcrypt_hash_ctx_t;
  103. /*! @brief HASHCRYPT background hash callback function. */
  104. typedef void (*hashcrypt_callback_t)(HASHCRYPT_Type *base, hashcrypt_hash_ctx_t *ctx, status_t status, void *userData);
  105. /*!
  106. *@}
  107. */ /* end of hashcrypt_driver_hash */
  108. /*******************************************************************************
  109. * API
  110. ******************************************************************************/
  111. #if defined(__cplusplus)
  112. extern "C" {
  113. #endif
  114. /*!
  115. * @addtogroup hashcrypt_driver
  116. * @{
  117. */
  118. /*!
  119. * @brief Enables clock and disables reset for HASHCRYPT peripheral.
  120. *
  121. * Enable clock and disable reset for HASHCRYPT.
  122. *
  123. * @param base HASHCRYPT base address
  124. */
  125. void HASHCRYPT_Init(HASHCRYPT_Type *base);
  126. /*!
  127. * @brief Disables clock for HASHCRYPT peripheral.
  128. *
  129. * Disable clock and enable reset.
  130. *
  131. * @param base HASHCRYPT base address
  132. */
  133. void HASHCRYPT_Deinit(HASHCRYPT_Type *base);
  134. /*!
  135. *@}
  136. */ /* end of hashcrypt_driver */
  137. /*******************************************************************************
  138. * AES API
  139. ******************************************************************************/
  140. /*!
  141. * @addtogroup hashcrypt_driver_aes
  142. * @{
  143. */
  144. /*!
  145. * @brief Set AES key to hashcrypt_handle_t struct and optionally to HASHCRYPT.
  146. *
  147. * Sets the AES key for encryption/decryption with the hashcrypt_handle_t structure.
  148. * The hashcrypt_handle_t input argument specifies key source.
  149. *
  150. * @param base HASHCRYPT peripheral base address.
  151. * @param handle Handle used for the request.
  152. * @param key 0-mod-4 aligned pointer to AES key.
  153. * @param keySize AES key size in bytes. Shall equal 16, 24 or 32.
  154. * @return status from set key operation
  155. */
  156. status_t HASHCRYPT_AES_SetKey(HASHCRYPT_Type *base, hashcrypt_handle_t *handle, const uint8_t *key, size_t keySize);
  157. /*!
  158. * @brief Encrypts AES on one or multiple 128-bit block(s).
  159. *
  160. * Encrypts AES.
  161. * The source plaintext and destination ciphertext can overlap in system memory.
  162. *
  163. * @param base HASHCRYPT peripheral base address
  164. * @param handle Handle used for this request.
  165. * @param plaintext Input plain text to encrypt
  166. * @param[out] ciphertext Output cipher text
  167. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  168. * @return Status from encrypt operation
  169. */
  170. status_t HASHCRYPT_AES_EncryptEcb(
  171. HASHCRYPT_Type *base, hashcrypt_handle_t *handle, const uint8_t *plaintext, uint8_t *ciphertext, size_t size);
  172. /*!
  173. * @brief Decrypts AES on one or multiple 128-bit block(s).
  174. *
  175. * Decrypts AES.
  176. * The source ciphertext and destination plaintext can overlap in system memory.
  177. *
  178. * @param base HASHCRYPT peripheral base address
  179. * @param handle Handle used for this request.
  180. * @param ciphertext Input plain text to encrypt
  181. * @param[out] plaintext Output cipher text
  182. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  183. * @return Status from decrypt operation
  184. */
  185. status_t HASHCRYPT_AES_DecryptEcb(
  186. HASHCRYPT_Type *base, hashcrypt_handle_t *handle, const uint8_t *ciphertext, uint8_t *plaintext, size_t size);
  187. /*!
  188. * @brief Encrypts AES using CBC block mode.
  189. *
  190. * @param base HASHCRYPT peripheral base address
  191. * @param handle Handle used for this request.
  192. * @param plaintext Input plain text to encrypt
  193. * @param[out] ciphertext Output cipher text
  194. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  195. * @param iv Input initial vector to combine with the first input block.
  196. * @return Status from encrypt operation
  197. */
  198. status_t HASHCRYPT_AES_EncryptCbc(HASHCRYPT_Type *base,
  199. hashcrypt_handle_t *handle,
  200. const uint8_t *plaintext,
  201. uint8_t *ciphertext,
  202. size_t size,
  203. const uint8_t iv[16]);
  204. /*!
  205. * @brief Decrypts AES using CBC block mode.
  206. *
  207. * @param base HASHCRYPT peripheral base address
  208. * @param handle Handle used for this request.
  209. * @param ciphertext Input cipher text to decrypt
  210. * @param[out] plaintext Output plain text
  211. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  212. * @param iv Input initial vector to combine with the first input block.
  213. * @return Status from decrypt operation
  214. */
  215. status_t HASHCRYPT_AES_DecryptCbc(HASHCRYPT_Type *base,
  216. hashcrypt_handle_t *handle,
  217. const uint8_t *ciphertext,
  218. uint8_t *plaintext,
  219. size_t size,
  220. const uint8_t iv[16]);
  221. /*!
  222. * @brief Encrypts or decrypts AES using CTR block mode.
  223. *
  224. * Encrypts or decrypts AES using CTR block mode.
  225. * AES CTR mode uses only forward AES cipher and same algorithm for encryption and decryption.
  226. * The only difference between encryption and decryption is that, for encryption, the input argument
  227. * is plain text and the output argument is cipher text. For decryption, the input argument is cipher text
  228. * and the output argument is plain text.
  229. *
  230. * @param base HASHCRYPT peripheral base address
  231. * @param handle Handle used for this request.
  232. * @param input Input data for CTR block mode
  233. * @param[out] output Output data for CTR block mode
  234. * @param size Size of input and output data in bytes
  235. * @param[in,out] counter Input counter (updates on return)
  236. * @param[out] counterlast Output cipher of last counter, for chained CTR calls (statefull encryption). NULL can be
  237. * passed if chained calls are
  238. * not used.
  239. * @param[out] szLeft Output number of bytes in left unused in counterlast block. NULL can be passed if chained calls
  240. * are not used.
  241. * @return Status from encrypt operation
  242. */
  243. status_t HASHCRYPT_AES_CryptCtr(HASHCRYPT_Type *base,
  244. hashcrypt_handle_t *handle,
  245. const uint8_t *input,
  246. uint8_t *output,
  247. size_t size,
  248. uint8_t counter[HASHCRYPT_AES_BLOCK_SIZE],
  249. uint8_t counterlast[HASHCRYPT_AES_BLOCK_SIZE],
  250. size_t *szLeft);
  251. /*!
  252. *@}
  253. */ /* end of hashcrypt_driver_aes */
  254. /*******************************************************************************
  255. * HASH API
  256. ******************************************************************************/
  257. /*!
  258. * @addtogroup hashcrypt_driver_hash
  259. * @{
  260. */
  261. /*!
  262. * @brief Create HASH on given data
  263. *
  264. * Perform the full SHA in one function call. The function is blocking.
  265. *
  266. * @param base HASHCRYPT peripheral base address
  267. * @param algo Underlaying algorithm to use for hash computation.
  268. * @param input Input data
  269. * @param inputSize Size of input data in bytes
  270. * @param[out] output Output hash data
  271. * @param[out] outputSize Output parameter storing the size of the output hash in bytes
  272. * @return Status of the one call hash operation.
  273. */
  274. status_t HASHCRYPT_SHA(HASHCRYPT_Type *base,
  275. hashcrypt_algo_t algo,
  276. const uint8_t *input,
  277. size_t inputSize,
  278. uint8_t *output,
  279. size_t *outputSize);
  280. /*!
  281. * @brief Initialize HASH context
  282. *
  283. * This function initializes the HASH.
  284. *
  285. * @param base HASHCRYPT peripheral base address
  286. * @param[out] ctx Output hash context
  287. * @param algo Underlaying algorithm to use for hash computation.
  288. * @return Status of initialization
  289. */
  290. status_t HASHCRYPT_SHA_Init(HASHCRYPT_Type *base, hashcrypt_hash_ctx_t *ctx, hashcrypt_algo_t algo);
  291. /*!
  292. * @brief Add data to current HASH
  293. *
  294. * Add data to current HASH. This can be called repeatedly with an arbitrary amount of data to be
  295. * hashed. The functions blocks. If it returns kStatus_Success, the running hash
  296. * has been updated (HASHCRYPT has processed the input data), so the memory at \p input pointer
  297. * can be released back to system. The HASHCRYPT context buffer is updated with the running hash
  298. * and with all necessary information to support possible context switch.
  299. *
  300. * @param base HASHCRYPT peripheral base address
  301. * @param[in,out] ctx HASH context
  302. * @param input Input data
  303. * @param inputSize Size of input data in bytes
  304. * @return Status of the hash update operation
  305. */
  306. status_t HASHCRYPT_SHA_Update(HASHCRYPT_Type *base, hashcrypt_hash_ctx_t *ctx, const uint8_t *input, size_t inputSize);
  307. /*!
  308. * @brief Finalize hashing
  309. *
  310. * Outputs the final hash (computed by HASHCRYPT_HASH_Update()) and erases the context.
  311. *
  312. * @param base HASHCRYPT peripheral base address
  313. * @param[in,out] ctx Input hash context
  314. * @param[out] output Output hash data
  315. * @param[in,out] outputSize Optional parameter (can be passed as NULL). On function entry, it specifies the size of
  316. * output[] buffer. On function return, it stores the number of updated output bytes.
  317. * @return Status of the hash finish operation
  318. */
  319. status_t HASHCRYPT_SHA_Finish(HASHCRYPT_Type *base, hashcrypt_hash_ctx_t *ctx, uint8_t *output, size_t *outputSize);
  320. /*!
  321. *@}
  322. */ /* end of hashcrypt_driver_hash */
  323. /*!
  324. * @addtogroup hashcrypt_background_driver_hash
  325. * @{
  326. */
  327. /*!
  328. * @brief Initializes the HASHCRYPT handle for background hashing.
  329. *
  330. * This function initializes the hash context for background hashing
  331. * (Non-blocking) APIs. This is less typical interface to hash function, but can be used
  332. * for parallel processing, when main CPU has something else to do.
  333. * Example is digital signature RSASSA-PKCS1-V1_5-VERIFY((n,e),M,S) algorithm, where
  334. * background hashing of M can be started, then CPU can compute S^e mod n
  335. * (in parallel with background hashing) and once the digest becomes available,
  336. * CPU can proceed to comparison of EM with EM'.
  337. *
  338. * @param base HASHCRYPT peripheral base address.
  339. * @param[out] ctx Hash context.
  340. * @param callback Callback function.
  341. * @param userData User data (to be passed as an argument to callback function, once callback is invoked from isr).
  342. */
  343. void HASHCRYPT_SHA_SetCallback(HASHCRYPT_Type *base,
  344. hashcrypt_hash_ctx_t *ctx,
  345. hashcrypt_callback_t callback,
  346. void *userData);
  347. /*!
  348. * @brief Create running hash on given data.
  349. *
  350. * Configures the HASHCRYPT to compute new running hash as AHB master
  351. * and returns immediately. HASHCRYPT AHB Master mode supports only aligned \p input
  352. * address and can be called only once per continuous block of data. Every call to this function
  353. * must be preceded with HASHCRYPT_SHA_Init() and finished with HASHCRYPT_SHA_Finish().
  354. * Once callback function is invoked by HASHCRYPT isr, it should set a flag
  355. * for the main application to finalize the hashing (padding) and to read out the final digest
  356. * by calling HASHCRYPT_SHA_Finish().
  357. *
  358. * @param base HASHCRYPT peripheral base address
  359. * @param ctx Specifies callback. Last incomplete 512-bit block of the input is copied into clear buffer for padding.
  360. * @param input 32-bit word aligned pointer to Input data.
  361. * @param inputSize Size of input data in bytes (must be word aligned)
  362. * @return Status of the hash update operation.
  363. */
  364. status_t HASHCRYPT_SHA_UpdateNonBlocking(HASHCRYPT_Type *base,
  365. hashcrypt_hash_ctx_t *ctx,
  366. const uint8_t *input,
  367. size_t inputSize);
  368. /*!
  369. *@}
  370. */ /* end of hashcrypt_background_driver_hash */
  371. #if defined(__cplusplus)
  372. }
  373. #endif
  374. #endif /* _FSL_HASHCRYPT_H_ */