fsl_dcp.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * The Clear BSD License
  3. * Copyright 2017 NXP
  4. * All rights reserved.
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder 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. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_DCP_H_
  35. #define _FSL_DCP_H_
  36. #include "fsl_common.h"
  37. /*! @brief DCP status return codes. */
  38. enum _dcp_status
  39. {
  40. kStatus_DCP_Again = MAKE_STATUS(kStatusGroup_DCP, 0), /*!< Non-blocking function shall be called again. */
  41. };
  42. /*******************************************************************************
  43. * Definitions
  44. *******************************************************************************/
  45. /*!
  46. * @addtogroup dcp_driver
  47. * @{
  48. */
  49. /*! @name Driver version */
  50. /*@{*/
  51. /*! @brief DCP driver version. Version 2.0.0.
  52. *
  53. * Current version: 2.0.0
  54. *
  55. * Change log:
  56. * - Version 2.0.0
  57. * - Initial version
  58. */
  59. #define FSL_DCP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  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's work packet. */
  108. typedef struct _dcp_work_packet
  109. {
  110. uint32_t nextCmdAddress;
  111. uint32_t control0;
  112. uint32_t control1;
  113. uint32_t sourceBufferAddress;
  114. uint32_t destinationBufferAddress;
  115. uint32_t bufferSize;
  116. uint32_t payloadPointer;
  117. uint32_t status;
  118. } dcp_work_packet_t;
  119. /*! @brief Specify DCP's key resource and DCP channel. */
  120. typedef struct _dcp_handle
  121. {
  122. dcp_channel_t channel; /*!< Specify DCP channel. */
  123. dcp_key_slot_t keySlot; /*!< For operations with key (such as AES encryption/decryption), specify DCP key slot. */
  124. uint32_t keyWord[4];
  125. uint32_t iv[4];
  126. } dcp_handle_t;
  127. /*! @brief DCP's context buffer, used by DCP for context switching between channels. */
  128. typedef struct _dcp_context
  129. {
  130. uint32_t x[208 / sizeof(uint32_t)];
  131. } dcp_context_t;
  132. /*! @brief DCP's configuration structure. */
  133. typedef struct _dcp_config
  134. {
  135. bool gatherResidualWrites; /*!< Enable the ragged writes to the unaligned buffers. */
  136. bool enableContextCaching; /*!< Enable the caching of contexts between the operations. */
  137. bool enableContextSwitching; /*!< Enable automatic context switching for the channels. */
  138. uint8_t enableChannel; /*!< DCP channel enable. */
  139. uint8_t enableChannelInterrupt; /*!< Per-channel interrupt enable. */
  140. } dcp_config_t;
  141. /*! @} */
  142. /*******************************************************************************
  143. * AES Definitions
  144. *******************************************************************************/
  145. /*!
  146. * @addtogroup dcp_driver_aes
  147. * @{
  148. */
  149. /*! AES block size in bytes */
  150. #define DCP_AES_BLOCK_SIZE 16
  151. /*!
  152. *@}
  153. */ /* end of dcp_driver_aes */
  154. /*******************************************************************************
  155. * HASH Definitions
  156. ******************************************************************************/
  157. /*!
  158. * @addtogroup dcp_driver_hash
  159. * @{
  160. */
  161. /* DCP cannot correctly compute hash for message with zero size. When enabled, driver bypases DCP and returns correct
  162. * hash value. If you are sure, that the driver will never be called with zero sized message, you can disable this
  163. * feature to reduce code size */
  164. #define DCP_HASH_CAVP_COMPATIBLE
  165. /*! @brief Supported cryptographic block cipher functions for HASH creation */
  166. typedef enum _dcp_hash_algo_t
  167. {
  168. kDCP_Sha1, /*!< SHA_1 */
  169. kDCP_Sha256, /*!< SHA_256 */
  170. kDCP_Crc32, /*!< CRC_32 */
  171. } dcp_hash_algo_t;
  172. /*! @brief DCP HASH Context size. */
  173. #define DCP_SHA_BLOCK_SIZE 128 /*!< internal buffer block size */
  174. #define DCP_HASH_BLOCK_SIZE DCP_SHA_BLOCK_SIZE /*!< DCP hash block size */
  175. /*! @brief DCP HASH Context size. */
  176. #define DCP_HASH_CTX_SIZE 58
  177. /*! @brief Storage type used to save hash context. */
  178. typedef struct _dcp_hash_ctx_t
  179. {
  180. uint32_t x[DCP_HASH_CTX_SIZE];
  181. } dcp_hash_ctx_t;
  182. /*!
  183. *@}
  184. */ /* end of dcp_driver_hash */
  185. /*******************************************************************************
  186. * API
  187. ******************************************************************************/
  188. #if defined(__cplusplus)
  189. extern "C" {
  190. #endif
  191. /*!
  192. * @addtogroup dcp_driver
  193. * @{
  194. */
  195. /*!
  196. * @brief Enables clock to and enables DCP
  197. *
  198. * Enable DCP clock and configure DCP.
  199. *
  200. * @param base DCP base address
  201. * @param config Pointer to configuration structure.
  202. */
  203. void DCP_Init(DCP_Type *base, const dcp_config_t *config);
  204. /*!
  205. * @brief Disable DCP clock
  206. *
  207. * Reset DCP and Disable DCP clock.
  208. *
  209. * @param base DCP base address
  210. */
  211. void DCP_Deinit(DCP_Type *base);
  212. /*!
  213. * @brief Gets the default configuration structure.
  214. *
  215. * This function initializes the DCP configuration structure to a default value. The default
  216. * values are as follows.
  217. * dcpConfig->gatherResidualWrites = true;
  218. * dcpConfig->enableContextCaching = true;
  219. * dcpConfig->enableContextSwitching = true;
  220. * dcpConfig->enableChannnel = kDCP_chEnableAll;
  221. * dcpConfig->enableChannelInterrupt = kDCP_chIntDisable;
  222. *
  223. * @param[out] config Pointer to configuration structure.
  224. */
  225. void DCP_GetDefaultConfig(dcp_config_t *config);
  226. /*!
  227. * @brief Poll and wait on DCP channel.
  228. *
  229. * Polls the specified DCP channel until current it completes activity.
  230. *
  231. * @param base DCP peripheral base address.
  232. * @param handle Specifies DCP channel.
  233. * @return kStatus_Success When data processing completes without error.
  234. * @return kStatus_Fail When error occurs.
  235. */
  236. status_t DCP_WaitForChannelComplete(DCP_Type *base, dcp_handle_t *handle);
  237. /*!
  238. *@}
  239. */ /* end of dcp_driver */
  240. /*******************************************************************************
  241. * AES API
  242. ******************************************************************************/
  243. /*!
  244. * @addtogroup dcp_driver_aes
  245. * @{
  246. */
  247. /*!
  248. * @brief Set AES key to dcp_handle_t struct and optionally to DCP.
  249. *
  250. * Sets the AES key for encryption/decryption with the dcp_handle_t structure.
  251. * The dcp_handle_t input argument specifies keySlot.
  252. * If the keySlot is kDCP_OtpKey, the function will check the OTP_KEY_READY bit and will return it's ready to use
  253. * status.
  254. * For other keySlot selections, the function will copy and hold the key in dcp_handle_t struct.
  255. * If the keySlot is one of the four DCP SRAM-based keys (one of kDCP_KeySlot0, kDCP_KeySlot1, kDCP_KeySlot2,
  256. * kDCP_KeySlot3),
  257. * this function will also load the supplied key to the specified keySlot in DCP.
  258. *
  259. * @param base DCP peripheral base address.
  260. * @param handle Handle used for the request.
  261. * @param key 0-mod-4 aligned pointer to AES key.
  262. * @param keySize AES key size in bytes. Shall equal 16.
  263. * @return status from set key operation
  264. */
  265. status_t DCP_AES_SetKey(DCP_Type *base, dcp_handle_t *handle, const uint8_t *key, size_t keySize);
  266. /*!
  267. * @brief Encrypts AES on one or multiple 128-bit block(s).
  268. *
  269. * Encrypts AES.
  270. * The source plaintext and destination ciphertext can overlap in system memory.
  271. *
  272. * @param base DCP peripheral base address
  273. * @param handle Handle used for this request.
  274. * @param plaintext Input plain text to encrypt
  275. * @param[out] ciphertext Output cipher text
  276. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  277. * @return Status from encrypt operation
  278. */
  279. status_t DCP_AES_EncryptEcb(
  280. DCP_Type *base, dcp_handle_t *handle, const uint8_t *plaintext, uint8_t *ciphertext, size_t size);
  281. /*!
  282. * @brief Decrypts AES on one or multiple 128-bit block(s).
  283. *
  284. * Decrypts AES.
  285. * The source ciphertext and destination plaintext can overlap in system memory.
  286. *
  287. * @param base DCP peripheral base address
  288. * @param handle Handle used for this request.
  289. * @param ciphertext Input plain text to encrypt
  290. * @param[out] plaintext Output cipher text
  291. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  292. * @return Status from decrypt operation
  293. */
  294. status_t DCP_AES_DecryptEcb(
  295. DCP_Type *base, dcp_handle_t *handle, const uint8_t *ciphertext, uint8_t *plaintext, size_t size);
  296. /*!
  297. * @brief Encrypts AES using CBC block mode.
  298. *
  299. * Encrypts AES using CBC block mode.
  300. * The source plaintext and destination ciphertext can overlap in system memory.
  301. *
  302. * @param base DCP peripheral base address
  303. * @param handle Handle used for this request.
  304. * @param plaintext Input plain text to encrypt
  305. * @param[out] ciphertext Output cipher text
  306. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  307. * @param iv Input initial vector to combine with the first input block.
  308. * @return Status from encrypt operation
  309. */
  310. status_t DCP_AES_EncryptCbc(DCP_Type *base,
  311. dcp_handle_t *handle,
  312. const uint8_t *plaintext,
  313. uint8_t *ciphertext,
  314. size_t size,
  315. const uint8_t iv[16]);
  316. /*!
  317. * @brief Decrypts AES using CBC block mode.
  318. *
  319. * Decrypts AES using CBC block mode.
  320. * The source ciphertext and destination plaintext can overlap in system memory.
  321. *
  322. * @param base DCP peripheral base address
  323. * @param handle Handle used for this request.
  324. * @param ciphertext Input cipher text to decrypt
  325. * @param[out] plaintext Output plain 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 decrypt operation
  329. */
  330. status_t DCP_AES_DecryptCbc(DCP_Type *base,
  331. dcp_handle_t *handle,
  332. const uint8_t *ciphertext,
  333. uint8_t *plaintext,
  334. size_t size,
  335. const uint8_t iv[16]);
  336. /*!
  337. *@}
  338. */ /* end of dcp_driver_aes */
  339. /*!
  340. * @addtogroup dcp_nonblocking_driver_aes
  341. * @{
  342. */
  343. /*!
  344. * @brief Encrypts AES using the ECB block mode.
  345. *
  346. * Puts AES ECB encrypt work packet to DCP channel.
  347. *
  348. * @param base DCP peripheral base address
  349. * @param handle Handle used for this request.
  350. * @param[out] dcpPacket Memory for the DCP work packet.
  351. * @param plaintext Input plain text to encrypt.
  352. * @param[out] ciphertext Output cipher text
  353. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  354. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  355. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  356. */
  357. status_t DCP_AES_EncryptEcbNonBlocking(DCP_Type *base,
  358. dcp_handle_t *handle,
  359. dcp_work_packet_t *dcpPacket,
  360. const uint8_t *plaintext,
  361. uint8_t *ciphertext,
  362. size_t size);
  363. /*!
  364. * @brief Decrypts AES using ECB block mode.
  365. *
  366. * Puts AES ECB decrypt dcpPacket to DCP input job ring.
  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 ciphertext Input cipher text to decrypt
  372. * @param[out] plaintext Output plain 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_DecryptEcbNonBlocking(DCP_Type *base,
  378. dcp_handle_t *handle,
  379. dcp_work_packet_t *dcpPacket,
  380. const uint8_t *ciphertext,
  381. uint8_t *plaintext,
  382. size_t size);
  383. /*!
  384. * @brief Encrypts AES using CBC block mode.
  385. *
  386. * Puts AES CBC encrypt dcpPacket to DCP input job ring.
  387. *
  388. * @param base DCP peripheral base address
  389. * @param handle Handle used for this request. Specifies jobRing.
  390. * @param[out] dcpPacket Memory for the DCP work packet.
  391. * @param plaintext Input plain text to encrypt
  392. * @param[out] ciphertext Output cipher text
  393. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  394. * @param iv Input initial vector to combine with the first input block.
  395. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  396. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  397. */
  398. status_t DCP_AES_EncryptCbcNonBlocking(DCP_Type *base,
  399. dcp_handle_t *handle,
  400. dcp_work_packet_t *dcpPacket,
  401. const uint8_t *plaintext,
  402. uint8_t *ciphertext,
  403. size_t size,
  404. const uint8_t *iv);
  405. /*!
  406. * @brief Decrypts AES using CBC block mode.
  407. *
  408. * Puts AES CBC decrypt dcpPacket to DCP input job ring.
  409. *
  410. * @param base DCP peripheral base address
  411. * @param handle Handle used for this request. Specifies jobRing.
  412. * @param[out] dcpPacket Memory for the DCP work packet.
  413. * @param ciphertext Input cipher text to decrypt
  414. * @param[out] plaintext Output plain text
  415. * @param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  416. * @param iv Input initial vector to combine with the first input block.
  417. * @return kStatus_Success The work packet has been scheduled at DCP channel.
  418. * @return kStatus_DCP_Again The DCP channel is busy processing previous request.
  419. */
  420. status_t DCP_AES_DecryptCbcNonBlocking(DCP_Type *base,
  421. dcp_handle_t *handle,
  422. dcp_work_packet_t *dcpPacket,
  423. const uint8_t *ciphertext,
  424. uint8_t *plaintext,
  425. size_t size,
  426. const uint8_t *iv);
  427. /*!
  428. *@}
  429. */ /* end of dcp_nonblocking_driver_aes */
  430. /*******************************************************************************
  431. * HASH API
  432. ******************************************************************************/
  433. /*!
  434. * @addtogroup dcp_driver_hash
  435. * @{
  436. */
  437. /*!
  438. * @brief Initialize HASH context
  439. *
  440. * This function initializes the HASH.
  441. *
  442. * @param base DCP peripheral base address
  443. * @param handle Specifies the DCP channel used for hashing.
  444. * @param[out] ctx Output hash context
  445. * @param algo Underlaying algorithm to use for hash computation.
  446. * @return Status of initialization
  447. */
  448. status_t DCP_HASH_Init(DCP_Type *base, dcp_handle_t *handle, dcp_hash_ctx_t *ctx, dcp_hash_algo_t algo);
  449. /*!
  450. * @brief Add data to current HASH
  451. *
  452. * Add data to current HASH. This can be called repeatedly with an arbitrary amount of data to be
  453. * hashed. The functions blocks. If it returns kStatus_Success, the running hash
  454. * has been updated (DCP has processed the input data), so the memory at @ref input pointer
  455. * can be released back to system. The DCP context buffer is updated with the running hash
  456. * and with all necessary information to support possible context switch.
  457. *
  458. * @param base DCP peripheral base address
  459. * @param[in,out] ctx HASH context
  460. * @param input Input data
  461. * @param inputSize Size of input data in bytes
  462. * @return Status of the hash update operation
  463. */
  464. status_t DCP_HASH_Update(DCP_Type *base, dcp_hash_ctx_t *ctx, const uint8_t *input, size_t inputSize);
  465. /*!
  466. * @brief Finalize hashing
  467. *
  468. * Outputs the final hash (computed by DCP_HASH_Update()) and erases the context.
  469. *
  470. * @param[in,out] ctx Input hash context
  471. * @param[out] output Output hash data
  472. * @param[in,out] outputSize Optional parameter (can be passed as NULL). On function entry, it specifies the size of
  473. * output[] buffer. On function return, it stores the number of updated output bytes.
  474. * @return Status of the hash finish operation
  475. */
  476. status_t DCP_HASH_Finish(DCP_Type *base, dcp_hash_ctx_t *ctx, uint8_t *output, size_t *outputSize);
  477. /*!
  478. * @brief Create HASH on given data
  479. *
  480. * Perform the full SHA or CRC32 in one function call. The function is blocking.
  481. *
  482. * @param base DCP peripheral base address
  483. * @param handle Handle used for the request.
  484. * @param algo Underlaying algorithm to use for hash computation.
  485. * @param input Input data
  486. * @param inputSize Size of input data in bytes
  487. * @param[out] output Output hash data
  488. * @param[out] outputSize Output parameter storing the size of the output hash in bytes
  489. * @return Status of the one call hash operation.
  490. */
  491. status_t DCP_HASH(DCP_Type *base,
  492. dcp_handle_t *handle,
  493. dcp_hash_algo_t algo,
  494. const uint8_t *input,
  495. size_t inputSize,
  496. uint8_t *output,
  497. size_t *outputSize);
  498. /*!
  499. *@}
  500. */ /* end of dcp_driver_hash */
  501. #if defined(__cplusplus)
  502. }
  503. #endif
  504. #endif /* _FSL_DCP_H_ */