fsl_dcp.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright 2017-2020 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_DCP_H_
  9. #define _FSL_DCP_H_
  10. #include "fsl_common.h"
  11. /*******************************************************************************
  12. * Definitions
  13. *******************************************************************************/
  14. /*!
  15. * @addtogroup dcp_driver
  16. * @{
  17. */
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief DCP driver version. Version 2.1.6.
  21. *
  22. * Current version: 2.1.6
  23. *
  24. * Change log:
  25. *
  26. * - Version 2.1.6
  27. * - Bug Fix
  28. * - MISRA C-2012 issue fix.
  29. *
  30. * - Version 2.1.5
  31. * - Improvements
  32. * - Add support for DCACHE.
  33. *
  34. * - Version 2.1.4
  35. * - Bug Fix
  36. * - Fix CRC-32 computation issue on the code's block boundary size.
  37. *
  38. * - Version 2.1.3
  39. * - Bug Fix
  40. * - MISRA C-2012 issue fixed: rule 10.1, 10.3, 10.4, 11.9, 14.4, 16.4 and 17.7.
  41. *
  42. * - Version 2.1.2
  43. * - Fix sign-compare warning in dcp_reverse_and_copy.
  44. *
  45. * - Version 2.1.1
  46. * - Add DCP status clearing when channel operation is complete
  47. *
  48. * - 2.1.0
  49. * - Add byte/word swap feature for key, input and output data
  50. *
  51. * - Version 2.0.0
  52. * - Initial version
  53. */
  54. #define FSL_DCP_DRIVER_VERSION (MAKE_VERSION(2, 1, 6))
  55. /*@}*/
  56. /*! @brief DCP status return codes. */
  57. enum _dcp_status
  58. {
  59. kStatus_DCP_Again = MAKE_STATUS(kStatusGroup_DCP, 0), /*!< Non-blocking function shall be called again. */
  60. };
  61. /*! @brief DCP channel enable.
  62. *
  63. */
  64. typedef enum _dcp_ch_enable
  65. {
  66. kDCP_chDisable = 0U, /*!< DCP channel disable */
  67. kDCP_ch0Enable = 1U, /*!< DCP channel 0 enable */
  68. kDCP_ch1Enable = 2U, /*!< DCP channel 1 enable */
  69. kDCP_ch2Enable = 4U, /*!< DCP channel 2 enable */
  70. kDCP_ch3Enable = 8U, /*!< DCP channel 3 enable */
  71. kDCP_chEnableAll = 15U, /*!< DCP channel enable all */
  72. } _dcp_ch_enable_t;
  73. /*! @brief DCP interrupt enable.
  74. *
  75. */
  76. typedef enum _dcp_ch_int_enable
  77. {
  78. kDCP_chIntDisable = 0U, /*!< DCP interrupts disable */
  79. kDCP_ch0IntEnable = 1U, /*!< DCP channel 0 interrupt enable */
  80. kDCP_ch1IntEnable = 2U, /*!< DCP channel 1 interrupt enable */
  81. kDCP_ch2IntEnable = 4U, /*!< DCP channel 2 interrupt enable */
  82. kDCP_ch3IntEnable = 8U, /*!< DCP channel 3 interrupt enable */
  83. } _dcp_ch_int_enable_t;
  84. /*! @brief DCP channel selection.
  85. *
  86. */
  87. typedef enum _dcp_channel
  88. {
  89. kDCP_Channel0 = (1u << 16), /*!< DCP channel 0. */
  90. kDCP_Channel1 = (1u << 17), /*!< DCP channel 1. */
  91. kDCP_Channel2 = (1u << 18), /*!< DCP channel 2. */
  92. kDCP_Channel3 = (1u << 19), /*!< DCP channel 3. */
  93. } dcp_channel_t;
  94. /*! @brief DCP key slot selection.
  95. *
  96. */
  97. typedef enum _dcp_key_slot
  98. {
  99. kDCP_KeySlot0 = 0U, /*!< DCP key slot 0. */
  100. kDCP_KeySlot1 = 1U, /*!< DCP key slot 1. */
  101. kDCP_KeySlot2 = 2U, /*!< DCP key slot 2.*/
  102. kDCP_KeySlot3 = 3U, /*!< DCP key slot 3. */
  103. kDCP_OtpKey = 4U, /*!< DCP OTP key. */
  104. kDCP_OtpUniqueKey = 5U, /*!< DCP unique OTP key. */
  105. kDCP_PayloadKey = 6U, /*!< DCP payload key. */
  106. } dcp_key_slot_t;
  107. /*! @brief DCP key, input & output swap options
  108. *
  109. */
  110. typedef enum _dcp_swap
  111. {
  112. kDCP_NoSwap = 0x0U,
  113. kDCP_KeyByteSwap = 0x40000U,
  114. kDCP_KeyWordSwap = 0x80000U,
  115. kDCP_InputByteSwap = 0x100000U,
  116. kDCP_InputWordSwap = 0x200000U,
  117. kDCP_OutputByteSwap = 0x400000U,
  118. kDCP_OutputWordSwap = 0x800000U,
  119. } dcp_swap_t;
  120. /*! @brief DCP's work packet. */
  121. typedef struct _dcp_work_packet
  122. {
  123. uint32_t nextCmdAddress;
  124. uint32_t control0;
  125. uint32_t control1;
  126. uint32_t sourceBufferAddress;
  127. uint32_t destinationBufferAddress;
  128. uint32_t bufferSize;
  129. uint32_t payloadPointer;
  130. uint32_t status;
  131. } dcp_work_packet_t;
  132. /*! @brief Specify DCP's key resource and DCP channel. */
  133. typedef struct _dcp_handle
  134. {
  135. dcp_channel_t channel; /*!< Specify DCP channel. */
  136. dcp_key_slot_t keySlot; /*!< For operations with key (such as AES encryption/decryption), specify DCP key slot. */
  137. uint32_t swapConfig; /*!< For configuration of key, input, output byte/word swap options */
  138. uint32_t keyWord[4];
  139. uint32_t iv[4];
  140. } dcp_handle_t;
  141. /*! @brief DCP's context buffer, used by DCP for context switching between channels. */
  142. typedef struct _dcp_context
  143. {
  144. uint32_t x[208 / sizeof(uint32_t)];
  145. } dcp_context_t;
  146. /*! @brief DCP's configuration structure. */
  147. typedef struct _dcp_config
  148. {
  149. bool gatherResidualWrites; /*!< Enable the ragged writes to the unaligned buffers. */
  150. bool enableContextCaching; /*!< Enable the caching of contexts between the operations. */
  151. bool enableContextSwitching; /*!< Enable automatic context switching for the channels. */
  152. uint8_t enableChannel; /*!< DCP channel enable. */
  153. uint8_t enableChannelInterrupt; /*!< Per-channel interrupt enable. */
  154. } dcp_config_t;
  155. /*! @} */
  156. #ifndef DCP_USE_DCACHE
  157. #define DCP_USE_DCACHE 1
  158. #endif
  159. /* 1 - driver supports DCACHE, 0 - drivers does not support DCACHE */
  160. /* When enable (DCP_USE_DCACHE = 1) Input/output buffers and hash ctx should be in */
  161. /* non-cached memory or handled properly (Clean & Invalidate DCACHE) */
  162. /*******************************************************************************
  163. * AES Definitions
  164. *******************************************************************************/
  165. /*!
  166. * @addtogroup dcp_driver_aes
  167. * @{
  168. */
  169. /*! AES block size in bytes */
  170. #define DCP_AES_BLOCK_SIZE 16
  171. /*!
  172. *@}
  173. */ /* end of dcp_driver_aes */
  174. /*******************************************************************************
  175. * HASH Definitions
  176. ******************************************************************************/
  177. /*!
  178. * @addtogroup dcp_driver_hash
  179. * @{
  180. */
  181. /* DCP cannot correctly compute hash for message with zero size. When enabled, driver bypases DCP and returns correct
  182. * hash value. If you are sure, that the driver will never be called with zero sized message, you can disable this
  183. * feature to reduce code size */
  184. #define DCP_HASH_CAVP_COMPATIBLE
  185. /*! @brief Supported cryptographic block cipher functions for HASH creation */
  186. typedef enum _dcp_hash_algo_t
  187. {
  188. kDCP_Sha1, /*!< SHA_1 */
  189. kDCP_Sha256, /*!< SHA_256 */
  190. kDCP_Crc32, /*!< CRC_32 */
  191. } dcp_hash_algo_t;
  192. /*! @brief DCP HASH Context size. */
  193. #define DCP_SHA_BLOCK_SIZE 128U /*!< internal buffer block size */
  194. #define DCP_HASH_BLOCK_SIZE DCP_SHA_BLOCK_SIZE /*!< DCP hash block size */
  195. /*! @brief DCP HASH Context size. */
  196. #define DCP_HASH_CTX_SIZE 64
  197. /*! @brief Storage type used to save hash context. */
  198. typedef struct _dcp_hash_ctx_t
  199. {
  200. uint32_t x[DCP_HASH_CTX_SIZE];
  201. } dcp_hash_ctx_t;
  202. /*!
  203. *@}
  204. */ /* end of dcp_driver_hash */
  205. /*******************************************************************************
  206. * API
  207. ******************************************************************************/
  208. #if defined(__cplusplus)
  209. extern "C" {
  210. #endif
  211. /*!
  212. * @addtogroup dcp_driver
  213. * @{
  214. */
  215. /*!
  216. * @brief Enables clock to and enables DCP
  217. *
  218. * Enable DCP clock and configure DCP.
  219. *
  220. * @param base DCP base address
  221. * @param config Pointer to configuration structure.
  222. */
  223. void DCP_Init(DCP_Type *base, const dcp_config_t *config);
  224. /*!
  225. * @brief Disable DCP clock
  226. *
  227. * Reset DCP and Disable DCP clock.
  228. *
  229. * @param base DCP base address
  230. */
  231. void DCP_Deinit(DCP_Type *base);
  232. /*!
  233. * @brief Gets the default configuration structure.
  234. *
  235. * This function initializes the DCP configuration structure to a default value. The default
  236. * values are as follows.
  237. * dcpConfig->gatherResidualWrites = true;
  238. * dcpConfig->enableContextCaching = true;
  239. * dcpConfig->enableContextSwitching = true;
  240. * dcpConfig->enableChannnel = kDCP_chEnableAll;
  241. * dcpConfig->enableChannelInterrupt = kDCP_chIntDisable;
  242. *
  243. * @param[out] config Pointer to configuration structure.
  244. */
  245. void DCP_GetDefaultConfig(dcp_config_t *config);
  246. /*!
  247. * @brief Poll and wait on DCP channel.
  248. *
  249. * Polls the specified DCP channel until current it completes activity.
  250. *
  251. * @param base DCP peripheral base address.
  252. * @param handle Specifies DCP channel.
  253. * @return kStatus_Success When data processing completes without error.
  254. * @return kStatus_Fail When error occurs.
  255. */
  256. status_t DCP_WaitForChannelComplete(DCP_Type *base, dcp_handle_t *handle);
  257. /*!
  258. *@}
  259. */ /* end of dcp_driver */
  260. /*******************************************************************************
  261. * AES API
  262. ******************************************************************************/
  263. /*!
  264. * @addtogroup dcp_driver_aes
  265. * @{
  266. */
  267. /*!
  268. * @brief Set AES key to dcp_handle_t struct and optionally to DCP.
  269. *
  270. * Sets the AES key for encryption/decryption with the dcp_handle_t structure.
  271. * The dcp_handle_t input argument specifies keySlot.
  272. * If the keySlot is kDCP_OtpKey, the function will check the OTP_KEY_READY bit and will return it's ready to use
  273. * status.
  274. * For other keySlot selections, the function will copy and hold the key in dcp_handle_t struct.
  275. * If the keySlot is one of the four DCP SRAM-based keys (one of kDCP_KeySlot0, kDCP_KeySlot1, kDCP_KeySlot2,
  276. * kDCP_KeySlot3),
  277. * this function will also load the supplied key to the specified keySlot in DCP.
  278. *
  279. * @param base DCP peripheral base address.
  280. * @param handle Handle used for the request.
  281. * @param key 0-mod-4 aligned pointer to AES key.
  282. * @param keySize AES key size in bytes. Shall equal 16.
  283. * @return status from set key operation
  284. */
  285. status_t DCP_AES_SetKey(DCP_Type *base, dcp_handle_t *handle, const uint8_t *key, size_t keySize);
  286. /*!
  287. * @brief Encrypts AES on one or multiple 128-bit block(s).
  288. *
  289. * Encrypts AES.
  290. * The source plaintext and destination ciphertext can overlap in system memory.
  291. *
  292. * @param base DCP peripheral base address
  293. * @param handle Handle used for this request.
  294. * @param plaintext Input plain text to encrypt
  295. * @param[out] ciphertext Output cipher text
  296. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  297. * @return Status from encrypt operation
  298. */
  299. status_t DCP_AES_EncryptEcb(
  300. DCP_Type *base, dcp_handle_t *handle, const uint8_t *plaintext, uint8_t *ciphertext, size_t size);
  301. /*!
  302. * @brief Decrypts AES on one or multiple 128-bit block(s).
  303. *
  304. * Decrypts AES.
  305. * The source ciphertext and destination plaintext can overlap in system memory.
  306. *
  307. * @param base DCP peripheral base address
  308. * @param handle Handle used for this request.
  309. * @param ciphertext Input plain text to encrypt
  310. * @param[out] plaintext Output cipher text
  311. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  312. * @return Status from decrypt operation
  313. */
  314. status_t DCP_AES_DecryptEcb(
  315. DCP_Type *base, dcp_handle_t *handle, const uint8_t *ciphertext, uint8_t *plaintext, size_t size);
  316. /*!
  317. * @brief Encrypts AES using CBC block mode.
  318. *
  319. * Encrypts AES using CBC block mode.
  320. * The source plaintext and destination ciphertext can overlap in system memory.
  321. *
  322. * @param base DCP peripheral base address
  323. * @param handle Handle used for this request.
  324. * @param plaintext Input plain text to encrypt
  325. * @param[out] ciphertext Output cipher text
  326. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  327. * @param iv Input initial vector to combine with the first input block.
  328. * @return Status from encrypt operation
  329. */
  330. status_t DCP_AES_EncryptCbc(DCP_Type *base,
  331. dcp_handle_t *handle,
  332. const uint8_t *plaintext,
  333. uint8_t *ciphertext,
  334. size_t size,
  335. const uint8_t iv[16]);
  336. /*!
  337. * @brief Decrypts AES using CBC block mode.
  338. *
  339. * Decrypts AES using CBC block mode.
  340. * The source ciphertext and destination plaintext can overlap in system memory.
  341. *
  342. * @param base DCP peripheral base address
  343. * @param handle Handle used for this request.
  344. * @param ciphertext Input cipher text to decrypt
  345. * @param[out] plaintext Output plain text
  346. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  347. * @param iv Input initial vector to combine with the first input block.
  348. * @return Status from decrypt operation
  349. */
  350. status_t DCP_AES_DecryptCbc(DCP_Type *base,
  351. dcp_handle_t *handle,
  352. const uint8_t *ciphertext,
  353. uint8_t *plaintext,
  354. size_t size,
  355. const uint8_t iv[16]);
  356. /*!
  357. *@}
  358. */ /* end of dcp_driver_aes */
  359. /*!
  360. * @addtogroup dcp_nonblocking_driver_aes
  361. * @{
  362. */
  363. /*!
  364. * @brief Encrypts AES using the ECB block mode.
  365. *
  366. * Puts AES ECB encrypt work packet to DCP channel.
  367. *
  368. * @param base DCP peripheral base address
  369. * @param handle Handle used for this request.
  370. * @param[out] dcpPacket Memory for the DCP work packet.
  371. * @param plaintext Input plain text to encrypt.
  372. * @param[out] ciphertext Output cipher text
  373. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  374. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  375. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  376. */
  377. status_t DCP_AES_EncryptEcbNonBlocking(DCP_Type *base,
  378. dcp_handle_t *handle,
  379. dcp_work_packet_t *dcpPacket,
  380. const uint8_t *plaintext,
  381. uint8_t *ciphertext,
  382. size_t size);
  383. /*!
  384. * @brief Decrypts AES using ECB block mode.
  385. *
  386. * Puts AES ECB decrypt dcpPacket to DCP input job ring.
  387. *
  388. * @param base DCP peripheral base address
  389. * @param handle Handle used for this request.
  390. * @param[out] dcpPacket Memory for the DCP work packet.
  391. * @param ciphertext Input cipher text to decrypt
  392. * @param[out] plaintext Output plain text
  393. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  394. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  395. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  396. */
  397. status_t DCP_AES_DecryptEcbNonBlocking(DCP_Type *base,
  398. dcp_handle_t *handle,
  399. dcp_work_packet_t *dcpPacket,
  400. const uint8_t *ciphertext,
  401. uint8_t *plaintext,
  402. size_t size);
  403. /*!
  404. * @brief Encrypts AES using CBC block mode.
  405. *
  406. * Puts AES CBC encrypt dcpPacket to DCP input job ring.
  407. *
  408. * @param base DCP peripheral base address
  409. * @param handle Handle used for this request. Specifies jobRing.
  410. * @param[out] dcpPacket Memory for the DCP work packet.
  411. * @param plaintext Input plain text to encrypt
  412. * @param[out] ciphertext Output cipher text
  413. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  414. * @param iv Input initial vector to combine with the first input block.
  415. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  416. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  417. */
  418. status_t DCP_AES_EncryptCbcNonBlocking(DCP_Type *base,
  419. dcp_handle_t *handle,
  420. dcp_work_packet_t *dcpPacket,
  421. const uint8_t *plaintext,
  422. uint8_t *ciphertext,
  423. size_t size,
  424. const uint8_t *iv);
  425. /*!
  426. * @brief Decrypts AES using CBC block mode.
  427. *
  428. * Puts AES CBC decrypt dcpPacket to DCP input job ring.
  429. *
  430. * @param base DCP peripheral base address
  431. * @param handle Handle used for this request. Specifies jobRing.
  432. * @param[out] dcpPacket Memory for the DCP work packet.
  433. * @param ciphertext Input cipher text to decrypt
  434. * @param[out] plaintext Output plain text
  435. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  436. * @param iv Input initial vector to combine with the first input block.
  437. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  438. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  439. */
  440. status_t DCP_AES_DecryptCbcNonBlocking(DCP_Type *base,
  441. dcp_handle_t *handle,
  442. dcp_work_packet_t *dcpPacket,
  443. const uint8_t *ciphertext,
  444. uint8_t *plaintext,
  445. size_t size,
  446. const uint8_t *iv);
  447. /*!
  448. *@}
  449. */ /* end of dcp_nonblocking_driver_aes */
  450. /*******************************************************************************
  451. * HASH API
  452. ******************************************************************************/
  453. /*!
  454. * @addtogroup dcp_driver_hash
  455. * @{
  456. */
  457. /*!
  458. * @brief Initialize HASH context
  459. *
  460. * This function initializes the HASH.
  461. *
  462. * @param base DCP peripheral base address
  463. * @param handle Specifies the DCP channel used for hashing.
  464. * @param[out] ctx Output hash context
  465. * @param algo Underlaying algorithm to use for hash computation.
  466. * @return Status of initialization
  467. */
  468. status_t DCP_HASH_Init(DCP_Type *base, dcp_handle_t *handle, dcp_hash_ctx_t *ctx, dcp_hash_algo_t algo);
  469. /*!
  470. * @brief Add data to current HASH
  471. *
  472. * Add data to current HASH. This can be called repeatedly with an arbitrary amount of data to be
  473. * hashed. The functions blocks. If it returns kStatus_Success, the running hash
  474. * has been updated (DCP has processed the input data), so the memory at the input pointer
  475. * can be released back to system. The DCP context buffer is updated with the running hash
  476. * and with all necessary information to support possible context switch.
  477. *
  478. * @param base DCP peripheral base address
  479. * @param[in,out] ctx HASH context
  480. * @param input Input data
  481. * @param inputSize Size of input data in bytes
  482. * @return Status of the hash update operation
  483. */
  484. status_t DCP_HASH_Update(DCP_Type *base, dcp_hash_ctx_t *ctx, const uint8_t *input, size_t inputSize);
  485. /*!
  486. * @brief Finalize hashing
  487. *
  488. * Outputs the final hash (computed by DCP_HASH_Update()) and erases the context.
  489. *
  490. * @param base DCP peripheral base address
  491. * @param[in,out] ctx Input hash context
  492. * @param[out] output Output hash data
  493. * @param[in,out] outputSize Optional parameter (can be passed as NULL). On function entry, it specifies the size of
  494. * output[] buffer. On function return, it stores the number of updated output bytes.
  495. * @return Status of the hash finish operation
  496. */
  497. status_t DCP_HASH_Finish(DCP_Type *base, dcp_hash_ctx_t *ctx, uint8_t *output, size_t *outputSize);
  498. /*!
  499. * @brief Create HASH on given data
  500. *
  501. * Perform the full SHA or CRC32 in one function call. The function is blocking.
  502. *
  503. * @param base DCP peripheral base address
  504. * @param handle Handle used for the request.
  505. * @param algo Underlaying algorithm to use for hash computation.
  506. * @param input Input data
  507. * @param inputSize Size of input data in bytes
  508. * @param[out] output Output hash data
  509. * @param[out] outputSize Output parameter storing the size of the output hash in bytes
  510. * @return Status of the one call hash operation.
  511. */
  512. status_t DCP_HASH(DCP_Type *base,
  513. dcp_handle_t *handle,
  514. dcp_hash_algo_t algo,
  515. const uint8_t *input,
  516. size_t inputSize,
  517. uint8_t *output,
  518. size_t *outputSize);
  519. /*!
  520. *@}
  521. */ /* end of dcp_driver_hash */
  522. #if defined(__cplusplus)
  523. }
  524. #endif
  525. #endif /* _FSL_DCP_H_ */