hpm_sdp_drv.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_SDP_DRV_H
  8. #define HPM_SDP_DRV_H
  9. /**
  10. * @brief SDP driver APIs
  11. * @defgroup sdp_interface SDP driver APIs
  12. * @ingroup sdp_interfaces
  13. * @{
  14. *
  15. */
  16. #include "hpm_common.h"
  17. #include "hpm_sdp_regs.h"
  18. #include "hpm_soc_feature.h"
  19. /***********************************************************************************************************************
  20. * Definitions
  21. **********************************************************************************************************************/
  22. /**
  23. * @brief SDP AES key bit options
  24. */
  25. typedef enum {
  26. sdp_aes_keybits_128 = 0, /**< 128 bit AES key */
  27. sdp_aes_keybits_256 = 1, /**< 256 bit AES key */
  28. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  29. sdp_sm4_keybits_128 = sdp_aes_keybits_128, /* SM4 Key bits */
  30. #endif
  31. } sdp_crypto_key_bits_t;
  32. typedef sdp_crypto_key_bits_t sdp_aes_key_bits_t;
  33. typedef sdp_crypto_key_bits_t sdp_sm4_key_bits_t;
  34. /**
  35. * @brief Crypto operation option
  36. */
  37. typedef enum {
  38. sdp_aes_op_encrypt, /**< AES Encrypt operation */
  39. sdp_aes_op_decrypt, /**< AES Decrypt operation */
  40. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  41. sdp_sm4_op_encrypt = sdp_aes_op_encrypt, /**< SM4 Encrypt operation */
  42. sdp_sm4_op_decrypt = sdp_aes_op_decrypt, /**< SM4 Decrypt operation */
  43. #endif
  44. } sdp_crypto_op_t;
  45. typedef sdp_crypto_op_t sdp_aes_op_t;
  46. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  47. typedef sdp_crypto_op_t sdp_sm4_op_t;
  48. #endif
  49. /**
  50. * @brief SDP Crypto algorithms
  51. *
  52. */
  53. typedef enum {
  54. sdp_crypto_alg_aes, /**< AES */
  55. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  56. sdp_crypto_alg_sm4, /**< SM4 */
  57. #endif
  58. } sdp_crypto_alg_t;
  59. /**
  60. * @brief SDP HASH algorithm definitions
  61. */
  62. typedef enum {
  63. sdp_hash_alg_sha1 = 0, /**< SDP SHA1 */
  64. sdp_hash_alg_crc32 = 1, /**< SDP CRC32 */
  65. sdp_hash_alg_sha256 = 2, /**< SDP SHA256 */
  66. #if defined(SDP_HAS_SM3_SUPPORT) && (SDP_HAS_SM3_SUPPORT == 1)
  67. sdp_hash_alg_sm3 = 8, /**< SDP SM3 */
  68. sdp_hash_alg_max = sdp_hash_alg_sm3,
  69. #else
  70. sdp_hash_alg_max = sdp_hash_alg_sha256,
  71. #endif
  72. } sdp_hash_alg_t;
  73. #define HASH_BLOCK_SIZE (64U) /**< Hash block size in bytes */
  74. #define AES_BLOCK_SIZE (16U) /**< AES block size in bytes */
  75. #define AES_128_KEY_SIZE (0x10U) /**< AES 128-bit key size in bytes */
  76. #define AES_256_KEY_SIZE (0x20U) /**< AES 256-bit key size in bytes */
  77. #define SM4_BLOCK_SIZE (AES_BLOCK_SIZE) /**< SM4 block size in bytes */
  78. #define SM4_KEY_SIZE (AES_128_KEY_SIZE) /**< SM4 Key size in bytes */
  79. /**
  80. * @brief Bitfield definitions for the PKT_CTRL
  81. */
  82. #define SDP_PKT_CTRL_DERSEMA_MASK (1U << 2)
  83. #define SDP_PKT_CTRL_CHAIN_MASK (1U << 3)
  84. #define SDP_PKT_CTRL_HASHINIT_MASK (1U << 4)
  85. #define SDP_PKT_CTRL_HASHFINISH_MASK (1U << 5)
  86. #define SDP_PKT_CTRL_CIPHIV_MASK (1U << 6)
  87. /**
  88. * @brief SDP packet data structure
  89. */
  90. typedef struct _sdp_packet_struct {
  91. struct _sdp_packet_struct *next_cmd;
  92. union {
  93. struct {
  94. uint32_t RESERVED0: 1;
  95. uint32_t PKTINT: 1; /**< Packet interrupt flag */
  96. uint32_t DCRSEMA: 1; /**< Descrement Semaphore flag */
  97. uint32_t CHAIN: 1; /**< Chain Packet flag */
  98. uint32_t HASHINIT: 1; /**< Hash initialize flag */
  99. uint32_t HASHFINISH: 1; /**< Hash finish flag */
  100. uint32_t CIPHIV: 1; /**< Cipher IV flag */
  101. uint32_t RESERVED1: 17;
  102. uint32_t PKTTAG: 8; /**< Packet tag flag, not used */
  103. };
  104. uint32_t PKT_CTRL; /**< Packet control word */
  105. } pkt_ctrl;
  106. uint32_t src_addr; /**< Source address */
  107. uint32_t dst_addr; /**< Destination address */
  108. uint32_t buf_size; /**< Data buffer size in bytes */
  109. uint32_t reserved[3];
  110. } sdp_pkt_struct_t;
  111. /**
  112. * @brief SDP AES context structure
  113. */
  114. typedef struct {
  115. uint8_t key_idx; /**< AES key index */
  116. uint8_t key_bits; /**< AES key bits */
  117. uint16_t crypto_algo;
  118. sdp_pkt_struct_t sdp_pkt; /**< SDP packet for AES operation */
  119. uint32_t buf0[AES_BLOCK_SIZE / sizeof(uint32_t)]; /**< buf0 */
  120. uint32_t buf1[AES_BLOCK_SIZE / sizeof(uint32_t)]; /**< buf1 */
  121. uint32_t buf2[AES_BLOCK_SIZE / sizeof(uint32_t)]; /**< buf2 */
  122. uint32_t buf3[AES_BLOCK_SIZE / sizeof(uint32_t)]; /**< buf3 */
  123. } sdp_crypto_ctx_t;
  124. typedef sdp_crypto_ctx_t sdp_aes_ctx_t;
  125. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  126. typedef sdp_crypto_ctx_t sdp_sm4_ctx_t;
  127. #endif
  128. /**
  129. * @brief SDP DMA context
  130. */
  131. typedef struct {
  132. sdp_pkt_struct_t sdp_pkt; /**< SDP packet for DMA operation (memset/memcpy) */
  133. } sdp_dma_ctx_t;
  134. /**
  135. * @brief SDP HASH context
  136. */
  137. typedef struct {
  138. sdp_pkt_struct_t sdp_pkt; /**< SDP packet for HASH operation */
  139. uint32_t internal[64]; /**< internal buffer */
  140. } sdp_hash_ctx_t;
  141. /**
  142. * @brief SDP error status definitions
  143. */
  144. enum {
  145. status_sdp_no_crypto_support = MAKE_STATUS(status_group_sdp, 0), /**< The crypto algorithm is not supported */
  146. status_sdp_no_hash_support = MAKE_STATUS(status_group_sdp, 1), /**< The hash algorithm is not supported */
  147. status_sdp_invalid_key_src = MAKE_STATUS(status_group_sdp, 2), /**< Invalid AES key source */
  148. status_sdp_error_packet = MAKE_STATUS(status_group_sdp, 3), /**< Error packet */
  149. status_sdp_aes_busy = MAKE_STATUS(status_group_sdp, 4), /**< AES engine is busy */
  150. status_sdp_hash_busy = MAKE_STATUS(status_group_sdp, 5), /**< HASH engine is busy */
  151. status_sdp_error_setup = MAKE_STATUS(status_group_sdp, 6), /**< Error setup in SDP IP */
  152. status_sdp_error_src = MAKE_STATUS(status_group_sdp, 7), /**< Error source address */
  153. status_sdp_error_dst = MAKE_STATUS(status_group_sdp, 8), /**< Error destination address */
  154. status_sdp_error_hash = MAKE_STATUS(status_group_sdp, 9), /**< Error Hash digest */
  155. status_sdp_error_chain = MAKE_STATUS(status_group_sdp, 10), /**< Error packet chain */
  156. status_sdp_error_invalid_mac = MAKE_STATUS(status_group_sdp, 11),/**< Inavlid Message Athenticaion Code (MAC) */
  157. status_sdp_invalid_alg = MAKE_STATUS(status_group_sdp, 12), /**< Invalid algorithm */
  158. };
  159. #ifdef __cplusplus
  160. extern "C"
  161. {
  162. #endif
  163. /***********************************************************************************************************************
  164. * Prototypes
  165. **********************************************************************************************************************/
  166. /**
  167. * @brief Initialize the SDP controller
  168. * @param [in] base SDP base address
  169. * @retval API execution status.
  170. */
  171. hpm_stat_t sdp_init(SDP_Type *base);
  172. /**
  173. * @brief De-initialize the SDP controller
  174. * @param [in] base SDP base address
  175. * @retval API execution status.
  176. */
  177. hpm_stat_t sdp_deinit(SDP_Type *base);
  178. /**
  179. * @brief Set the AES key for the SDP AES operation
  180. * @param [in] base SDP base address
  181. * @param [in] aes_ctx AES operation context
  182. * @param [in] key AES key
  183. * @param [in] key_bits AES key-bit option
  184. * @param [in] key_idx AES key index
  185. * @retval API execution status.
  186. */
  187. hpm_stat_t sdp_aes_set_key(SDP_Type *base,
  188. sdp_aes_ctx_t *aes_ctx,
  189. const uint8_t *key,
  190. sdp_aes_key_bits_t key_bits,
  191. uint32_t key_idx);
  192. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  193. /**
  194. * @brief Set the SM4 key for the SDP SM4 operation
  195. * @param [in] base SDP base address
  196. * @param [in] sm4_ctx AES operation context
  197. * @param [in] key SM4 key
  198. * @param [in] key_bits SM4 key-bit option
  199. * @param [in] key_idx AES key index
  200. * @retval API execution status.
  201. */
  202. hpm_stat_t sdp_sm4_set_key(SDP_Type *base,
  203. sdp_sm4_ctx_t *sm4_ctx,
  204. const uint8_t *key,
  205. sdp_sm4_key_bits_t key_bits,
  206. uint32_t key_idx);
  207. #endif
  208. /**
  209. * @brief Perform the basic AES ECB operation
  210. * @param [in] base SDP base address
  211. * @param [in] aes_ctx AES operation context
  212. * @param [in] op AES operation option
  213. * @param [in] len AES data length in bytes
  214. * @param [in] in Input buffer
  215. * @param [out] out Output buffer
  216. * @retval API execution status.
  217. */
  218. hpm_stat_t sdp_aes_crypt_ecb(SDP_Type *base,
  219. sdp_aes_ctx_t *aes_ctx,
  220. sdp_aes_op_t op,
  221. uint32_t len,
  222. const uint8_t *in,
  223. uint8_t *out);
  224. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  225. /**
  226. * @brief Perform the basic SM4 ECB operation
  227. * @param [in] base SDP base address
  228. * @param [in] sm4_ctx SM4 operation context
  229. * @param [in] op SM4 operation option
  230. * @param [in] len SM4 data length in bytes
  231. * @param [in] in Input buffer
  232. * @param [out] out Output buffer
  233. * @retval API execution status.
  234. */
  235. #define sdp_sm4_crypt_ecb sdp_aes_crypt_ecb
  236. #endif
  237. /**
  238. * @brief Perform the AES CBC operation
  239. * @param [in] base SDP base address
  240. * @param [in] aes_ctx AES operation context
  241. * @param [in] op AES operation option
  242. * @param [in] length AES data length in bytes
  243. * @param [in] iv Initial vector/nonce
  244. * @param [in] input Input buffer
  245. * @param [out] output Output buffer
  246. * @retval API execution status.
  247. */
  248. hpm_stat_t sdp_aes_crypt_cbc(SDP_Type *base,
  249. sdp_aes_ctx_t *aes_ctx,
  250. sdp_aes_op_t op,
  251. uint32_t length,
  252. const uint8_t iv[16],
  253. const uint8_t *input,
  254. uint8_t *output);
  255. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  256. /**
  257. * @brief Perform the SM4 CBC operation
  258. * @param [in] base SM4 base address
  259. * @param [in] sm4_ctx SM4 operation context
  260. * @param [in] op SM4 operation option
  261. * @param [in] length SM4 data length in bytes
  262. * @param [in] iv Initial vector/nonce
  263. * @param [in] input Input buffer
  264. * @param [out] output Output buffer
  265. * @retval API execution status.
  266. */
  267. #define sdp_sm4_crypt_cbc sdp_aes_crypt_cbc
  268. #endif
  269. /**
  270. * @brief Perform the AES-CTR operation
  271. * See NIST Special Publication800-38A for more details
  272. * @param [in] base SDP base address
  273. * @param [in] aes_ctx AES operation context
  274. * @param [in] nonce_counter AES-CTR nounce/counter
  275. * @param [in] input Input buffer
  276. * @param [out] output Output buffer
  277. * @param [in] length Length of data for AES-CTR operation
  278. * @retval API execution status.
  279. */
  280. hpm_stat_t sdp_aes_crypt_ctr(SDP_Type *base,
  281. sdp_aes_ctx_t *aes_ctx,
  282. uint8_t *nonce_counter,
  283. uint8_t *input,
  284. uint8_t *output,
  285. uint32_t length);
  286. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  287. /**
  288. * @brief Perform the SM4-CTR operation
  289. * @param [in] base SDP base address
  290. * @param [in] sm4_ctx SM4 operation context
  291. * @param [in] nonce_counter SM4-CTR nounce/counter
  292. * @param [in] input Input buffer
  293. * @param [out] output Output buffer
  294. * @param [in] length Length of data for SM4-CTR operation
  295. * @retval API execution status.
  296. */
  297. #define sdp_sm4_crypt_ctr sdp_aes_crypt_ctr
  298. #endif
  299. /**
  300. * @brief Perform the AES-CCM generate and encrypt
  301. * See NIST Special Publication 800-38C for more details
  302. * @param [in] base SDP base address
  303. * @param [in] aes_ctx AES operation context
  304. * @param [in] input_len Input data length in bytes
  305. * @param [in] iv Initial vector
  306. * @param [in] iv_len Initial vector length in bytes
  307. * @param [in] aad Additional Authentication data
  308. * @param [in] aad_len Additional authentication data size
  309. * @param [in] input Input data buffer
  310. * @param [out] output Output buffer
  311. * @param [out] tag MAC buffer
  312. * @param [in] tag_len Tag/MAC size in bytes
  313. * @retval API execution status.
  314. */
  315. hpm_stat_t sdp_aes_ccm_generate_encrypt(SDP_Type *base,
  316. sdp_aes_ctx_t *aes_ctx,
  317. uint32_t input_len,
  318. const uint8_t *iv,
  319. uint32_t iv_len,
  320. const uint8_t *aad,
  321. uint32_t aad_len,
  322. const uint8_t *input,
  323. uint8_t *output,
  324. uint8_t *tag,
  325. uint32_t tag_len);
  326. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  327. /**
  328. * @brief Perform the SM4-CCM generate and encrypt
  329. * See NIST Special Publication 800-38C for more details
  330. * @param [in] base SDP base address
  331. * @param [in] sm4_ctx SM4 operation context
  332. * @param [in] input_len Input data length in bytes
  333. * @param [in] iv Initial vector
  334. * @param [in] iv_len Initial vector length in bytes
  335. * @param [in] aad Additional Authentication data
  336. * @param [in] aad_len Additional authentication data size
  337. * @param [in] input Input data buffer
  338. * @param [out] output Output buffer
  339. * @param [out] tag MAC buffer
  340. * @param [in] tag_len Tag/MAC size in bytes
  341. * @retval API execution status.
  342. */
  343. #define sdp_sm4_ccm_generate_encrypt sdp_aes_ccm_generate_encrypt
  344. #endif
  345. /**
  346. * @brief Perform the AES-CCM decrypt and verify
  347. * See NIST Special Publication 800-38C for more details
  348. * @param [in] base SDP base address
  349. * @param [in] aes_ctx AES operation context
  350. * @param [in] input_len Input data length in bytes
  351. * @param [in] iv Initial vector
  352. * @param [in] iv_len Initial vector length in bytes
  353. * @param [in] aad Additional Authentication data
  354. * @param [in] aad_len Additional authentication data size
  355. * @param [in] input Input data buffer
  356. * @param [out] output Output buffer
  357. * @param [in] tag MAC buffer
  358. * @param [in] tag_len Tag/MAC size in bytes
  359. * @retval API execution status.
  360. */
  361. hpm_stat_t sdp_aes_ccm_decrypt_verify(SDP_Type *base,
  362. sdp_aes_ctx_t *aes_ctx,
  363. uint32_t input_len,
  364. const uint8_t *iv,
  365. uint32_t iv_len,
  366. const uint8_t *aad,
  367. uint32_t aad_len,
  368. const uint8_t *input,
  369. uint8_t *output,
  370. const uint8_t *tag,
  371. uint32_t tag_len);
  372. #if defined(SDP_HAS_SM4_SUPPORT) && (SDP_HAS_SM4_SUPPORT == 1)
  373. /**
  374. * @brief Perform the SM4-CCM decrypt and verify
  375. * @param [in] base SDP base address
  376. * @param [in] sm4_ctx SM4 operation context
  377. * @param [in] input_len Input data length in bytes
  378. * @param [in] iv Initial vector
  379. * @param [in] iv_len Initial vector length in bytes
  380. * @param [in] aad Additional Authentication data
  381. * @param [in] aad_len Additional authentication data size
  382. * @param [in] input Input data buffer
  383. * @param [out] output Output buffer
  384. * @param [in] tag MAC buffer
  385. * @param [in] tag_len Tag/MAC size in bytes
  386. * @retval API execution status.
  387. */
  388. #define sdp_sm4_ccm_decrypt_verify sdp_aes_ccm_decrypt_verify
  389. #endif
  390. /**
  391. * @brief Perform the DMA accelerated memcpy
  392. * @param [in] base SDP base address
  393. * @param [in] sdp_ctx SDP DMA context
  394. * @param [out] dst Destination address for memcpy operation
  395. * @param [in] src Source address for memcpy operation
  396. * @param [in] length Length of the data to be copied
  397. * @retval API execution status.
  398. */
  399. hpm_stat_t sdp_memcpy(SDP_Type *base, sdp_dma_ctx_t *sdp_ctx, void *dst, const void *src, uint32_t length);
  400. /**
  401. * @brief Perform the DMA accelerated memset
  402. * @param [in] base SDP base address
  403. * @param [in] sdp_ctx SDP DMA context
  404. * @param [out] dst SDP destination address for memset operation
  405. * @param [in] pattern pattern for memset operation
  406. * @param [in] length length of the memory for memset operation
  407. * @retval API execution status.
  408. */
  409. hpm_stat_t sdp_memset(SDP_Type *base, sdp_dma_ctx_t *sdp_ctx, void *dst, uint8_t pattern, uint32_t length);
  410. /**
  411. * @brief Initialize the HASH engine
  412. * @param [in] base SDP base address
  413. * @param [in] hash_ctx HASH operation context
  414. * @param [in] alg Hash algorithm
  415. * @retval API execution status. status_success or status_invalid_argument
  416. */
  417. hpm_stat_t sdp_hash_init(SDP_Type *base, sdp_hash_ctx_t *hash_ctx, sdp_hash_alg_t alg);
  418. /**
  419. * @brief Compute the HASH digest
  420. * @param [in] base SDP base address
  421. * @param [in] hash_ctx HASH operation context
  422. * @param [in] data Data for HASH computing
  423. * @param [in] length Data size for HASH computing
  424. * @retval API execution status.
  425. */
  426. hpm_stat_t sdp_hash_update(SDP_Type *base, sdp_hash_ctx_t *hash_ctx, const uint8_t *data, uint32_t length);
  427. /**
  428. * @brief Finish the HASH calculation and output the digest
  429. * @param [in] base SDP base address
  430. * @param [in] hash_ctx HASH operation context
  431. * @param [out] digest Digest buffer
  432. * @retval API execution status.
  433. */
  434. hpm_stat_t sdp_hash_finish(SDP_Type *base, sdp_hash_ctx_t *hash_ctx, uint8_t *digest);
  435. /**
  436. * @brief Wait until the SDP operation gets done
  437. * @retval API execution status.
  438. */
  439. hpm_stat_t sdp_wait_done(SDP_Type *base);
  440. #ifdef __cplusplus
  441. }
  442. #endif
  443. /**
  444. * @}
  445. */
  446. #endif /* HPM_SDP_DRV_H */