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