diff options
author | thead_admin <occ_thead@service.alibaba.com> | 2023-03-05 22:36:05 +0800 |
---|---|---|
committer | thead_admin <occ_thead@service.alibaba.com> | 2023-03-05 22:36:05 +0800 |
commit | 51a2c4f06046f571caa47a9c7e4e725493020f3c (patch) | |
tree | c2dc07815806ab8236b742337e9f573a8facda6e /lib | |
parent | 02deb8b059118cd2b0fb3c8bbb6d73fd1f87115a (diff) |
Linux_SDK_V1.1.2Linux_SDK_V1.1.2
Diffstat (limited to 'lib')
59 files changed, 799 insertions, 317 deletions
diff --git a/lib/sec_library/include/aes.h b/lib/sec_library/include/aes.h index 04d8fd06..93c809d3 100644..100755 --- a/lib/sec_library/include/aes.h +++ b/lib/sec_library/include/aes.h @@ -21,12 +21,44 @@ extern "C" { #endif /*----- Encrypt & Decrypt: Config key length -----*/ +#define AES_KEY_LEN_BYTES_32 (32) +#define AES_KEY_LEN_BYTES_24 (24) +#define AES_KEY_LEN_BYTES_16 (16) + +#define AES_BLOCK_IV_SIZE (16) +#define AES_BLOCK_TAG_SIZE (16) +#define AES_BLOCK_CRYPTO_SIZE (16) + +#define AES_DIR_ENCRYPT (1) +#define AES_DIR_DECRYPT (0) + +#define KEY_128_BITS (0x08) +#define KEY_192_BITS (0x10) +#define KEY_256_BITS (0x18) + +#define AES_DMA_ENABLE (1) +#define AES_DMA_DISABLE (0) + +/** +\brief DES data transfer mode config +*/ +typedef enum { + AES_SLAVE_MODE = 0U, ///< slave mode + AES_DMA_MODE, ///< dma mode +} csi_aes_trans_mode_t; + +/** +\brief AES Keylen type +*/ typedef enum { AES_KEY_LEN_BITS_128 = 0, ///< 128 Data bits AES_KEY_LEN_BITS_192, ///< 192 Data bits AES_KEY_LEN_BITS_256 ///< 256 Data bits } csi_aes_key_bits_t; +/** +\brief AES mode config +*/ typedef enum{ AES_MODE_ECB = 0, AES_MODE_CBC = 0x20000020, @@ -35,48 +67,19 @@ typedef enum{ AES_MODE_GCM = 0x20030040, AES_MODE_CCM = 0x21D40040, AES_MODE_OFB = 0x24000000, -} aes_mode_t; - -#define AES_KEY_LEN_BYTES_32 32 -#define AES_KEY_LEN_BYTES_24 24 -#define AES_KEY_LEN_BYTES_16 16 - -#define AES_CRYPTO_CTRL_CBC_256 0x20000038 -#define AES_CRYPTO_CTRL_CBC_192 0x20000030 -#define AES_CRYPTO_CTRL_CBC_128 0x20000028 -#define AES_CRYPTO_CTRL_ECB_256 0x00000018 -#define AES_CRYPTO_CTRL_ECB_192 0x00000010 -#define AES_CRYPTO_CTRL_ECB_128 0x00000008 - -#define AES_BLOCK_IV_SIZE 16 -#define AES_BLOCK_TAG_SIZE 16 -#define AES_BLOCK_CRYPTO_SIZE 16 - -#define AES_DIR_ENCRYPT 1 -#define AES_DIR_DECRYPT 0 - -#define KEY_128_BITS 0x8 -#define KEY_192_BITS 0x10 -#define KEY_256_BITS 0x18 - -#define AES_DMA_ENABLE 1 -#define AES_DMA_DISABLE 0 - - -typedef enum{ - AES_CRYPTO_ECB_256_MODE = 0, - AES_CRYPTO_ECB_192_MODE, - AES_CRYPTO_ECB_128_MODE, - AES_CRYPTO_CBC_256_MODE, - AES_CRYPTO_CBC_192_MODE, - AES_CRYPTO_CBC_128_MODE, } csi_aes_mode_t; +/** +\brief AES state +*/ typedef struct { uint32_t busy : 1; ///< Calculate busy flag uint32_t error : 1; ///< Calculate error flag } csi_aes_state_t; +/** +\brief AES Context +*/ typedef struct { uint32_t key_len_byte; uint8_t key[32]; ///< Data block being processed @@ -343,11 +346,11 @@ csi_error_t csi_aes_enable_pm(csi_aes_t *aes); void csi_aes_disable_pm(csi_aes_t *aes); /** - \brief Config AES mode dma or slave - \param[in] dam_en zero disable dma, not zero enable dma + \brief Config AES data transfer mode + \param[in] mode \ref csi_des_trans_mode_t \return None */ -void csi_aes_dma_enable(csi_aes_t *aes, uint8_t dma_en); +void csi_aes_trans_config(csi_aes_t *aes, csi_aes_trans_mode_t mode); #ifdef __cplusplus } diff --git a/lib/sec_library/include/common.h b/lib/sec_library/include/common.h index 4cc1bd09..efd5b4a6 100644..100755 --- a/lib/sec_library/include/common.h +++ b/lib/sec_library/include/common.h @@ -68,11 +68,13 @@ extern "C" { } while (0); typedef enum { - CSI_OK = 0, - CSI_ERROR = -1, - CSI_BUSY = -2, - CSI_TIMEOUT = -3, - CSI_UNSUPPORTED = -4 + CSI_OK = 0, + CSI_ERROR = -1, + CSI_BUSY = -2, + CSI_TIMEOUT = -3, + CSI_UNSUPPORTED = -4, + CSI_INVALID_PARAM = -5, + CSI_CRYPT_FAIL = -6, } csi_error_t; typedef struct { @@ -132,9 +134,7 @@ typedef struct { csi_error_t target_get(csi_dev_tag_t dev_tag, uint32_t idx, csi_dev_t *dev); csi_error_t target_get_optimal_dma_channel(void *dma_list, uint32_t ctrl_num, csi_dev_t *parent_dev, void *ch_info); -//void mdelay(uint32_t ms); -//void udelay(uint32_t us); -//void msleep(uint32_t ms); +void msleep(uint32_t ms); #ifdef __cplusplus } diff --git a/lib/sec_library/include/core/README.txt b/lib/sec_library/include/core/README.txt index bb1bf4a3..bb1bf4a3 100644..100755 --- a/lib/sec_library/include/core/README.txt +++ b/lib/sec_library/include/core/README.txt diff --git a/lib/sec_library/include/core/cmsis/ARMCM0.h b/lib/sec_library/include/core/cmsis/ARMCM0.h index 93881d5e..93881d5e 100644..100755 --- a/lib/sec_library/include/core/cmsis/ARMCM0.h +++ b/lib/sec_library/include/core/cmsis/ARMCM0.h diff --git a/lib/sec_library/include/core/cmsis/cmsis_compiler.h b/lib/sec_library/include/core/cmsis/cmsis_compiler.h index fdb1a971..fdb1a971 100644..100755 --- a/lib/sec_library/include/core/cmsis/cmsis_compiler.h +++ b/lib/sec_library/include/core/cmsis/cmsis_compiler.h diff --git a/lib/sec_library/include/core/cmsis/cmsis_gcc.h b/lib/sec_library/include/core/cmsis/cmsis_gcc.h index d86b0a2d..d86b0a2d 100644..100755 --- a/lib/sec_library/include/core/cmsis/cmsis_gcc.h +++ b/lib/sec_library/include/core/cmsis/cmsis_gcc.h diff --git a/lib/sec_library/include/core/cmsis/cmsis_version.h b/lib/sec_library/include/core/cmsis/cmsis_version.h index 660f612a..660f612a 100644..100755 --- a/lib/sec_library/include/core/cmsis/cmsis_version.h +++ b/lib/sec_library/include/core/cmsis/cmsis_version.h diff --git a/lib/sec_library/include/core/cmsis/core_cm0.h b/lib/sec_library/include/core/cmsis/core_cm0.h index fcf27578..fcf27578 100644..100755 --- a/lib/sec_library/include/core/cmsis/core_cm0.h +++ b/lib/sec_library/include/core/cmsis/core_cm0.h diff --git a/lib/sec_library/include/core/cmsis/csi_core.h b/lib/sec_library/include/core/cmsis/csi_core.h index 9082bdf0..9082bdf0 100644..100755 --- a/lib/sec_library/include/core/cmsis/csi_core.h +++ b/lib/sec_library/include/core/cmsis/csi_core.h diff --git a/lib/sec_library/include/core/cmsis/system_ARMCM0.h b/lib/sec_library/include/core/cmsis/system_ARMCM0.h index 7fe7e914..7fe7e914 100644..100755 --- a/lib/sec_library/include/core/cmsis/system_ARMCM0.h +++ b/lib/sec_library/include/core/cmsis/system_ARMCM0.h diff --git a/lib/sec_library/include/core/core_801.h b/lib/sec_library/include/core/core_801.h index 5c55f991..5c55f991 100644..100755 --- a/lib/sec_library/include/core/core_801.h +++ b/lib/sec_library/include/core/core_801.h diff --git a/lib/sec_library/include/core/core_802.h b/lib/sec_library/include/core/core_802.h index 5465ac79..5465ac79 100644..100755 --- a/lib/sec_library/include/core/core_802.h +++ b/lib/sec_library/include/core/core_802.h diff --git a/lib/sec_library/include/core/core_803.h b/lib/sec_library/include/core/core_803.h index c178e14d..c178e14d 100644..100755 --- a/lib/sec_library/include/core/core_803.h +++ b/lib/sec_library/include/core/core_803.h diff --git a/lib/sec_library/include/core/core_804.h b/lib/sec_library/include/core/core_804.h index 4562309c..4562309c 100644..100755 --- a/lib/sec_library/include/core/core_804.h +++ b/lib/sec_library/include/core/core_804.h diff --git a/lib/sec_library/include/core/core_805.h b/lib/sec_library/include/core/core_805.h index 5811da39..5811da39 100644..100755 --- a/lib/sec_library/include/core/core_805.h +++ b/lib/sec_library/include/core/core_805.h diff --git a/lib/sec_library/include/core/core_807.h b/lib/sec_library/include/core/core_807.h index 54010d6c..54010d6c 100644..100755 --- a/lib/sec_library/include/core/core_807.h +++ b/lib/sec_library/include/core/core_807.h diff --git a/lib/sec_library/include/core/core_810.h b/lib/sec_library/include/core/core_810.h index fa4c25e4..fa4c25e4 100644..100755 --- a/lib/sec_library/include/core/core_810.h +++ b/lib/sec_library/include/core/core_810.h diff --git a/lib/sec_library/include/core/core_ck610.h b/lib/sec_library/include/core/core_ck610.h index f6b68dd3..f6b68dd3 100644..100755 --- a/lib/sec_library/include/core/core_ck610.h +++ b/lib/sec_library/include/core/core_ck610.h diff --git a/lib/sec_library/include/core/core_ck801.h b/lib/sec_library/include/core/core_ck801.h index debb17a0..debb17a0 100644..100755 --- a/lib/sec_library/include/core/core_ck801.h +++ b/lib/sec_library/include/core/core_ck801.h diff --git a/lib/sec_library/include/core/core_ck802.h b/lib/sec_library/include/core/core_ck802.h index 7db01a63..7db01a63 100644..100755 --- a/lib/sec_library/include/core/core_ck802.h +++ b/lib/sec_library/include/core/core_ck802.h diff --git a/lib/sec_library/include/core/core_ck803.h b/lib/sec_library/include/core/core_ck803.h index 58352228..58352228 100644..100755 --- a/lib/sec_library/include/core/core_ck803.h +++ b/lib/sec_library/include/core/core_ck803.h diff --git a/lib/sec_library/include/core/core_ck807.h b/lib/sec_library/include/core/core_ck807.h index 98205535..98205535 100644..100755 --- a/lib/sec_library/include/core/core_ck807.h +++ b/lib/sec_library/include/core/core_ck807.h diff --git a/lib/sec_library/include/core/core_ck810.h b/lib/sec_library/include/core/core_ck810.h index 69cdd0d1..69cdd0d1 100644..100755 --- a/lib/sec_library/include/core/core_ck810.h +++ b/lib/sec_library/include/core/core_ck810.h diff --git a/lib/sec_library/include/core/core_rv32.h b/lib/sec_library/include/core/core_rv32.h index a9a0bbf7..a9a0bbf7 100644..100755 --- a/lib/sec_library/include/core/core_rv32.h +++ b/lib/sec_library/include/core/core_rv32.h diff --git a/lib/sec_library/include/core/core_rv64.h b/lib/sec_library/include/core/core_rv64.h index 2facecd3..2facecd3 100644..100755 --- a/lib/sec_library/include/core/core_rv64.h +++ b/lib/sec_library/include/core/core_rv64.h diff --git a/lib/sec_library/include/core/csi_gcc.h b/lib/sec_library/include/core/csi_gcc.h index 5cccffa6..5cccffa6 100644..100755 --- a/lib/sec_library/include/core/csi_gcc.h +++ b/lib/sec_library/include/core/csi_gcc.h diff --git a/lib/sec_library/include/core/csi_rv32_gcc.h b/lib/sec_library/include/core/csi_rv32_gcc.h index 7f38d0d1..7f38d0d1 100644..100755 --- a/lib/sec_library/include/core/csi_rv32_gcc.h +++ b/lib/sec_library/include/core/csi_rv32_gcc.h diff --git a/lib/sec_library/include/core/csi_rv64_gcc.h b/lib/sec_library/include/core/csi_rv64_gcc.h index 0341b768..0341b768 100644..100755 --- a/lib/sec_library/include/core/csi_rv64_gcc.h +++ b/lib/sec_library/include/core/csi_rv64_gcc.h diff --git a/lib/sec_library/include/csi_core.h b/lib/sec_library/include/csi_core.h index b9347a12..b9347a12 100644..100755 --- a/lib/sec_library/include/csi_core.h +++ b/lib/sec_library/include/csi_core.h diff --git a/lib/sec_library/include/csi_efuse_api.h b/lib/sec_library/include/csi_efuse_api.h index 082f9138..082f9138 100644..100755 --- a/lib/sec_library/include/csi_efuse_api.h +++ b/lib/sec_library/include/csi_efuse_api.h diff --git a/lib/sec_library/include/csi_sec_img_verify.h b/lib/sec_library/include/csi_sec_img_verify.h index 9ca97cf6..9ca97cf6 100644..100755 --- a/lib/sec_library/include/csi_sec_img_verify.h +++ b/lib/sec_library/include/csi_sec_img_verify.h diff --git a/lib/sec_library/include/des.h b/lib/sec_library/include/des.h new file mode 100755 index 00000000..feb87959 --- /dev/null +++ b/lib/sec_library/include/des.h @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2017-2020 Alibaba Group Holding Limited + */ + +/****************************************************************************** + * @file drv/des.h + * @brief Header File for DES Driver + * @version V1.0 + * @date 24. Oct 2022 + * @model des + ******************************************************************************/ + +#ifndef _DRV_DES_H_ +#define _DRV_DES_H_ + +#include <stdint.h> +#include "common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*----- Encrypt & Decrypt: Config key length -----*/ +/** +\brief DES data transfer mode config +*/ +typedef enum { + DES_SLAVE_MODE = 0U, ///< slave mode + DES_DMA_MODE, ///< dma mode +} csi_des_trans_mode_t; + +/** +\brief DES key-len-bits type +*/ +typedef enum { + DES_KEY_LEN_BITS_64 = 0, ///< 64 Data bits + DES_KEY_LEN_BITS_128, ///< 128 Data bits + DES_KEY_LEN_BITS_192, ///< 192 Data bits +} csi_des_key_bits_t; + +typedef enum{ + DES_MODE_ECB = 0x00000000, + DES_MODE_CBC = 0x20000020, + TDES_MODE_ECB = 0x00000008, + TDES_MODE_CBC = 0x20000028, +} des_mode_t; + + +#define DES_KEY_LEN_BYTES_32 (32) +#define DES_KEY_LEN_BYTES_16 (16) +#define DES_KEY_LEN_BYTES_24 (24) +#define DES_KEY_LEN_BYTES_8 (8) + +#define DES_BLOCK_IV_SIZE (16) +#define DES_BLOCK_CRYPTO_SIZE (8) +#define TDES_BLOCK_CRYPTO_SIZE (16) + +#define DES_DIR_ENCRYPT (1) +#define DES_DIR_DECRYPT (0) + +#define DES_KEY_128_BITS (0x8) +#define DES_KEY_192_BITS (0x10) + +/** +\brief DES State +*/ +typedef struct { + uint32_t busy : 1; ///< Calculate busy flag + uint32_t error : 1; ///< Calculate error flag +} csi_des_state_t; + +/** +\brief DES Context +*/ +typedef struct { + uint32_t key_len_byte; + uint8_t key[32]; ///< Data block being processed + uint32_t sca; + uint32_t is_kdf; + uint32_t is_dma; +} csi_des_context_t; + +/** +\brief DES Ctrl Block +*/ +typedef struct { + csi_des_state_t state; + csi_des_context_t context; + csi_dev_t dev; + void *priv; +} csi_des_t; + +/** + \brief Initialize DES interface. Initializes the resources needed for the DES interface + \param[in] des Handle to operate + \param[in] idx Device id + \return Error code \ref csi_error_t +*/ +csi_error_t csi_des_init(csi_des_t *des, uint32_t idx); + +/** + \brief De-initialize DES interface. Stops operation and releases the software resources used by the interface + \param[in] des Dandle to operate + \return None +*/ +void csi_des_uninit(csi_des_t *des); + +/** + \brief Set encrypt key + \param[in] des Handle to operate + \param[in] key Pointer to the key buf + \param[in] key_len Pointer to \ref csi_des_key_bits_t + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_set_encrypt_key(csi_des_t *des, void *key, csi_des_key_bits_t key_len); + +/** + \brief Set decrypt key + \param[in] des Handle to operate + \param[in] key Pointer to the key buf + \param[in] key_len Pointer to \ref csi_des_key_bits_t + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_set_decrypt_key(csi_des_t *des, void *key, csi_des_key_bits_t key_len); + +/** + \brief DES ecb encrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_ecb_encrypt(csi_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief DES ecb decrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_ecb_decrypt(csi_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief DES cbc encrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \param[in] iv Init vector + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_cbc_encrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv) ; + +/** + \brief DES cbc decrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \param[in] iv Init vector + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_des_cbc_decrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief TDES ecb encrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_tdes_ecb_encrypt(csi_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief TDES ecb decrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_tdes_ecb_decrypt(csi_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief TDES cbc encrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \param[in] iv Init vector + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_tdes_cbc_encrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv) ; + +/** + \brief TDES cbc decrypt + \param[in] des Handle to operate + \param[in] in Pointer to the source data + \param[out] out Pointer to the result data + \param[in] size The source data size + \param[in] iv Init vector + \return Error code \ref Csi_error_t +*/ +csi_error_t csi_tdes_cbc_decrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief Config DES mode dma or slave + \param[in] mode \ref csi_des_trans_mode_t + \return None +*/ +void csi_des_trans_config(csi_des_t *des, csi_des_trans_mode_t mode); + +#ifdef __cplusplus +} +#endif + +#endif /* _DRV_AES_H_ */ diff --git a/lib/sec_library/include/dev_tag.h b/lib/sec_library/include/dev_tag.h index aa8b642e..aa8b642e 100644..100755 --- a/lib/sec_library/include/dev_tag.h +++ b/lib/sec_library/include/dev_tag.h diff --git a/lib/sec_library/include/device_types.h b/lib/sec_library/include/device_types.h index 7fd33918..7fd33918 100644..100755 --- a/lib/sec_library/include/device_types.h +++ b/lib/sec_library/include/device_types.h diff --git a/lib/sec_library/include/ecc.h b/lib/sec_library/include/ecc.h index 4c0d3ebc..58540647 100644..100755 --- a/lib/sec_library/include/ecc.h +++ b/lib/sec_library/include/ecc.h @@ -20,8 +20,15 @@ extern "C" { #endif -#define ECC_PRIME_CURVE_G_BYTES 64 -#define ECC_PRIME_CURVE_P_BYTES 70 +#define CSI_ECC_PUBKEY_LEN (65-1) +#define CSI_ECC_PRIVKEY_LEN (32) +#define CSI_ECC_PUBKEYTMP_LEN (65) +#define CSI_ECC_RK_LEN (24) //random +#define CSI_ECC_SIGNATURE_LEN (64) +#define CSI_ECC_DIGEST_LEN (32) + +#define ECC_PRIME_CURVE_G_BYTES (64) +#define ECC_PRIME_CURVE_P_BYTES (70) typedef struct { uint32_t ecc_curve : 1; ///< supports 256bits curve @@ -35,11 +42,17 @@ typedef enum { ECC_C1C2C3, } ecc_cipher_order_e; +/** +\brief ECC endian mode +*/ typedef enum { ECC_ENDIAN_LITTLE = 0, ///< Little Endian ECC_ENDIAN_BIG ///< Big Endian } ecc_endian_mode_e; +/** +\brief ECC prime curve type +*/ typedef enum { ECC_PRIME256V1 = 0, } ecc_prime_curve_type; @@ -59,11 +72,17 @@ typedef enum { ECC_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed } ecc_event_e; +/** +\brief ECC prime curve param +*/ typedef struct { ecc_prime_curve_type type; uint32_t *p; } csi_ecc_prime_curve_t; +/** +\brief ECC curve type g param +*/ typedef struct { ecc_prime_curve_type type; uint8_t *G; @@ -77,6 +96,9 @@ typedef struct { uint32_t busy : 1; ///< Calculate busy flag } csi_ecc_state_t; +/** +\brief ECC handle +*/ typedef struct { csi_dev_t dev; void * cb; @@ -90,9 +112,8 @@ typedef void (*csi_ecc_callback_t)(ecc_event_e event); /** \brief Initialize ECC. - \param[in] ecc ecc handle to operate. \param[in] idx device id - \return \ref uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_ecc_init(csi_ecc_t *ecc, uint32_t idx); @@ -106,111 +127,111 @@ void csi_ecc_uninit(csi_ecc_t *ecc); /** \brief ecc get capability. \param[in] ecc Operate handle. - \return \ref uint32_t + \return Error code \ref csi_error_t */ -csi_error_t csi_ecc_config(csi_ecc_t *ecc, ecc_cipher_order_e co, - ecc_endian_mode_e endian); +csi_error_t csi_ecc_config(csi_ecc_t *ecc, ecc_cipher_order_e co, ecc_endian_mode_e endian); /** \brief Attach the callback handler to ECC \param[in] ecc Operate handle. \param[in] cb Callback function \param[in] arg User can define it by himself as callback's param - \return Error code \ref csi_error_t + \return Error code Error code \ref csi_error_t */ -csi_error_t csi_ecc_attach_callback(csi_ecc_t *ecc, csi_ecc_callback_t cb, - void *arg); +csi_error_t csi_ecc_attach_callback(csi_ecc_t *ecc, csi_ecc_callback_t cb, void *arg); /** \brief Detach the callback handler \param[in] ecc Operate handle. + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_detach_callback(csi_ecc_t *ecc); /** \brief ecc get capability. \param[in] ecc Operate handle. - \param[out] cap Pointer of ecc_capabilities_t. - \return \ref uint32_t + \param[out] cap Pointer of ecc_capabilities_t. + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_get_capabilities(csi_ecc_t *ecc, ecc_capabilities_t *cap); +/** + \brief check whether the public key and private key are a pair. + \param[in] ecc ecc handle to operate. + \param[in] private Pointer to the ecc private key, alloc by caller. + \param[in] public Pointer to the ecc public key, alloc by caller. + \return Error code \ref csi_error_t +*/ csi_error_t csi_ecc_check_keypair(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief generate ecc key. \param[in] ecc ecc handle to operate. \param[out] private Pointer to the ecc private key, alloc by caller. - \param[out] public Pointer to the ecc public key, alloc by caller. - \return \ref uint32_t + \param[out] public Pointer to the ecc public key, alloc by caller. + \return Error code \ref csi_error_t */ -csi_error_t csi_ecc_gen_key(csi_ecc_t *ecc, uint8_t pubkey[65], - uint8_t prikey[32]); +csi_error_t csi_ecc_gen_key(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]); /** - \brief generate ecc pubkey by privkey. + \brief generate ecc public key by private key. \param[in] ecc ecc handle to operate. - \param[in] private Pointer to the ecc private key, alloc by caller. - \param[out] public Pointer to the ecc public key, alloc by caller. - \return \ref uint32_t + \param[out] private Pointer to the ecc private key, alloc by caller. + \param[out] public Pointer to the ecc public key, alloc by caller. + \return Error code \ref csi_error_t */ -csi_error_t csi_ecc_gen_pubkey(csi_ecc_t *ecc, uint8_t pubkey[65], - uint8_t prikey[32]); +csi_error_t csi_ecc_gen_pubkey(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief ecc sign - \param[in] ecc ecc handle to operate. - \param[in] d Pointer to the digest. - \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \param[in] ecc ecc handle to operate. + \param[in] d Pointer to the digest. + \param[out] privkey Pointer to the private key + \param[out] s Pointer to the signature + \return Error code \ref csi_error_t */ -csi_error_t csi_ecc_sign(csi_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32], - uint8_t s[64]); +csi_error_t csi_ecc_sign(csi_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /** - \brief ecc sign - \param[in] ecc ecc handle to operate. - \param[in] d Pointer to the digest. - \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \brief ecc sign asybnc + \param[in] ecc ecc handle to operate. + \param[in] d Pointer to the digest. + \param[out] privkey Pointer to the private key + \param[out] s Pointer to the signature + \return Error code \ref csi_error_t */ -csi_error_t csi_ecc_sign_async(csi_ecc_t *ecc, uint8_t d[32], - uint8_t prikey[32], uint8_t s[64]); +csi_error_t csi_ecc_sign_async(csi_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /* TODO */ /** \brief ecc verify - \param[in] ecc ecc handle to operate. - \param[in] d Pointer to the digest. - \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return verify result + \param[in] ecc ecc handle to operate. + \param[in] d Pointer to the digest. + \param[out] privkey Pointer to the private key + \param[out] s Pointer to the signature + \return verify result */ -bool csi_ecc_verify(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool csi_ecc_verify(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief ecc verify - \param[in] ecc ecc handle to operate. - \param[in] d Pointer to the digest. - \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature + \param[in] ecc ecc handle to operate. + \param[in] d Pointer to the digest. + \param[out] privkey Pointer to the private key + \param[out] s Pointer to the signature \return verify result */ -bool csi_ecc_verify_async(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool csi_ecc_verify_async(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief ecc encrypto - \param[in] ecc ecc handle to operate. - \param[in] Plain Pointer to the plaintext. - \param[in] PlainByteLen plaintext len - \param[in] pubKey public key. - \param[out] Cipher Pointer to the chipher - \param[out] CipherByteLen Pointer to the chipher len. - \return uint32_t + \param[in] ecc ecc handle to operate. + \param[in] Plain Pointer to the plaintext. + \param[in] PlainByteLen plaintext len + \param[in] pubKey public key. + \param[out] Cipher Pointer to the chipher + \param[out] CipherByteLen Pointer to the chipher len. + \return Error code \ref csi_error_t */ csi_error_t csi_ecc_encrypt(csi_ecc_t *ecc, uint8_t *Plain, uint32_t PlainByteLen, uint8_t pubKey[65], @@ -218,13 +239,13 @@ csi_error_t csi_ecc_encrypt(csi_ecc_t *ecc, uint8_t *Plain, /** \brief ecc encrypto - \param[in] ecc ecc handle to operate. - \param[in] Cipher Pointer to the chipher - \param[in] CipherByteLen chipher len. - \param[in] prikey private key. - \param[out] Plain Pointer to the plaintext. - \param[out] PlainByteLen plaintext len - \return uint32_t + \param[in] ecc ecc handle to operate. + \param[in] Cipher Pointer to the chipher + \param[in] CipherByteLen chipher len. + \param[in] prikey private key. + \param[out] Plain Pointer to the plaintext. + \param[out] PlainByteLen plaintext len + \return Error code \ref csi_error_t */ csi_error_t csi_ecc_decrypt(csi_ecc_t *ecc, uint8_t *Cipher, uint32_t CipherByteLen, uint8_t prikey[32], @@ -233,7 +254,7 @@ csi_error_t csi_ecc_decrypt(csi_ecc_t *ecc, uint8_t *Cipher, /** \brief ecc key exchange \param[in] ecc ecc handle to operate. - \return uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_ecc_exchangekey(csi_ecc_t *ecc, ecc_exchange_role_e role, uint8_t *dA, uint8_t *PB, uint8_t *rA, @@ -244,7 +265,7 @@ csi_error_t csi_ecc_exchangekey(csi_ecc_t *ecc, ecc_exchange_role_e role, /** \brief ecc key exchange get Z. \param[in] ecc ecc handle to operate. - \return uint32_t + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_getZ(csi_ecc_t *ecc, uint8_t *ID, uint32_t byteLenofID, uint8_t pubKey[65], uint8_t Z[32]); @@ -252,7 +273,7 @@ csi_error_t csi_ecc_getZ(csi_ecc_t *ecc, uint8_t *ID, uint32_t byteLenofID, /** \brief ecc key exchange get E \param[in] ecc ecc handle to operate. - \return uint32_t + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_getE(csi_ecc_t *ecc, uint8_t *M, uint32_t byteLen, uint8_t Z[32], uint8_t E[32]); @@ -261,14 +282,14 @@ csi_error_t csi_ecc_getE(csi_ecc_t *ecc, uint8_t *M, uint32_t byteLen, \brief Get ECC state. \param[in] ecc ECC handle to operate. \param[out] state ECC state \ref csi_ecc_state_t. - \return Error code \ref csi_error_t + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_get_state(csi_ecc_t *ecc, csi_ecc_state_t *state); /** \brief Enable ecc power manage \param[in] ecc ECC handle to operate. - \return Error code \ref csi_error_t + \return Error code Error code \ref csi_error_t */ csi_error_t csi_ecc_enable_pm(csi_ecc_t *ecc); diff --git a/lib/sec_library/include/ecdh.h b/lib/sec_library/include/ecdh.h index 06258ca1..cf1db702 100644..100755 --- a/lib/sec_library/include/ecdh.h +++ b/lib/sec_library/include/ecdh.h @@ -20,19 +20,21 @@ extern "C" { #endif +#define CSI_ECDH_PUBKEY_LEN (65-1) +#define CSI_ECDH_PRIVKEY_LEN (32) +#define CSI_ECDH_SHARE_LEN (64) +#define CSI_ECDH_SHAREKEY_LEN (32) /** \brief ecdh cacl share secret - \param[in] ecc ecc handle to operate. - \param[in] pubkey Pointer to the A public key. - \param[in] privkey Pointer to the B private key. - \param[out] shareKey Pointer to the share secret. - \param[out] len length of the share secret. - \return \ref uint32_t + \param[in] ecc ecc handle to operate. + \param[in] pubkey Pointer to the A public key. + \param[in] prikey Pointer to the B private key. + \param[out] shareKey Pointer to the share secret. + \param[out] len length of the share secret. + \return Error code \ref csi_error_t */ -csi_error_t csi_ecdh_calc_secret(csi_ecc_t *ecc, uint8_t privkey[32], - uint8_t pubkey[65], uint8_t shareKey[32], - uint32_t *len); +csi_error_t csi_ecdh_calc_secret(csi_ecc_t *ecc, uint8_t privkey[32], uint8_t pubkey[65], uint8_t shareKey[32], uint32_t *len); #ifdef __cplusplus extern "C" { diff --git a/lib/sec_library/include/kdf.h b/lib/sec_library/include/kdf.h index 7f8f5964..7f8f5964 100644..100755 --- a/lib/sec_library/include/kdf.h +++ b/lib/sec_library/include/kdf.h diff --git a/lib/sec_library/include/keyram.h b/lib/sec_library/include/keyram.h index f8127417..f8127417 100644..100755 --- a/lib/sec_library/include/keyram.h +++ b/lib/sec_library/include/keyram.h diff --git a/lib/sec_library/include/list.h b/lib/sec_library/include/list.h index 80028214..80028214 100644..100755 --- a/lib/sec_library/include/list.h +++ b/lib/sec_library/include/list.h diff --git a/lib/sec_library/include/rambus.h b/lib/sec_library/include/rambus.h index 780d774b..780d774b 100644..100755 --- a/lib/sec_library/include/rambus.h +++ b/lib/sec_library/include/rambus.h diff --git a/lib/sec_library/include/rng.h b/lib/sec_library/include/rng.h index 1b9a911b..1b9a911b 100644..100755 --- a/lib/sec_library/include/rng.h +++ b/lib/sec_library/include/rng.h diff --git a/lib/sec_library/include/rsa.h b/lib/sec_library/include/rsa.h index bbb67315..7c842663 100644..100755 --- a/lib/sec_library/include/rsa.h +++ b/lib/sec_library/include/rsa.h @@ -18,17 +18,17 @@ extern "C" { #include <stdint.h> #include "common.h" -#define RSA_PRIME_256_BIT_LEN 128 -#define RSA_PRIME_512_BIT_LEN 256 -#define RSA_PRIME_1024_BIT_LEN 512 -#define RSA_PRIME_2048_BIT_LEN 1024 -#define RSA_PRIME_4096_BIT_LEN 2048 - -#define RSA_256_BYTE_LEN 32 -#define RSA_512_BYTE_LEN 64 -#define RSA_1024_BYTE_LEN 128 -#define RSA_2048_BYTE_LEN 256 -#define RSA_4096_BYTE_LEN 512 +#define RSA_PRIME_256_BIT_LEN (128) +#define RSA_PRIME_512_BIT_LEN (256) +#define RSA_PRIME_1024_BIT_LEN (512) +#define RSA_PRIME_2048_BIT_LEN (1024) +#define RSA_PRIME_4096_BIT_LEN (2048) + +#define RSA_256_BYTE_LEN (32) +#define RSA_512_BYTE_LEN (64) +#define RSA_1024_BYTE_LEN (128) +#define RSA_2048_BYTE_LEN (256) +#define RSA_4096_BYTE_LEN (512) #define RSA_EM_BYTE_LEN RSA_4096_BYTE_LEN #define SHA256_DIGEST_BYTE_LEN 32 @@ -301,7 +301,7 @@ csi_error_t csi_rsa_get_publickey(csi_rsa_t *rsa, void *p, uint32_t p_byte_len, /** \brief Generation rsa keyparis \param[in] rsa rsa handle to operate. - \param[in] context Pointer to the rsa context + \param[in] context Pointer to the rsa context \param[in] keybits_len Pointer to the publickey bits length \return \ref csi_error_t */ diff --git a/lib/sec_library/include/sec_crypto_aes.h b/lib/sec_library/include/sec_crypto_aes.h index 51ccebcd..0e5714b1 100755 --- a/lib/sec_library/include/sec_crypto_aes.h +++ b/lib/sec_library/include/sec_crypto_aes.h @@ -31,6 +31,17 @@ extern "C" { #endif +/** +\brief AES data transfer mode config +*/ +typedef enum { + SC_AES_SLAVE_MODE = 0U, ///< slave mode + SC_AES_DMA_MODE, ///< dma mode +} sc_aes_trans_mode_t; + +/** +\brief AES key-len-bits type +*/ typedef enum { SC_AES_KEY_LEN_BITS_128 = 0U, ///< 128 Data bits SC_AES_KEY_LEN_BITS_192, ///< 192 Data bits @@ -297,7 +308,10 @@ uint32_t sc_aes_ccm_encrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, ui */ uint32_t sc_aes_ccm_decrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv, uint8_t* tag_out); -void sc_aes_dma_enable(sc_aes_t *aes, uint8_t en); +/** + \brief Aes data transfer config +*/ +void sc_aes_trans_config(sc_aes_t *aes, sc_aes_trans_mode_t mode) ; #ifdef __cplusplus } diff --git a/lib/sec_library/include/sec_crypto_common.h b/lib/sec_library/include/sec_crypto_common.h index f95322fd..f95322fd 100644..100755 --- a/lib/sec_library/include/sec_crypto_common.h +++ b/lib/sec_library/include/sec_crypto_common.h diff --git a/lib/sec_library/include/sec_crypto_des.h b/lib/sec_library/include/sec_crypto_des.h new file mode 100755 index 00000000..0b077412 --- /dev/null +++ b/lib/sec_library/include/sec_crypto_des.h @@ -0,0 +1,205 @@ +/* + * Copyright (C) 2017-2022 Alibaba Group Holding Limited + */ +/****************************************************************************** + * @file sec_crypt0_des.h + * @brief Header File for DES + * @version V1.0 + * @date 24. Oct 2022 + * @model des + ******************************************************************************/ +#ifndef _SC_DES_H_ +#define _SC_DES_H_ + +#include "sec_include_config.h" +#include <stdint.h> +#include "sec_crypto_errcode.h" + +#ifdef CONFIG_SYSTEM_SECURE +#ifdef SEC_LIB_VERSION +#include <drv/des.h> +#else +#include "des.h" +#endif +#endif + +#ifdef CONFIG_SEC_CRYPTO_DES_SW +#include "crypto_des.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** +\brief DES data transfer mode config +*/ +typedef enum { + SC_DES_SLAVE_MODE = 0U, ///< slave mode + SC_DES_DMA_MODE, ///< dma mode +} sc_des_trans_mode_t; + +/** +\brief DES key-len-bits type +*/ +typedef enum { + SC_DES_KEY_LEN_BITS_64 = 0U, ///< 64 Data bits + SC_DES_KEY_LEN_BITS_128, ///< 128 Data bits + SC_TDES_KEY_LEN_BITS_192, ///< 192 Data bits +} sc_des_key_bits_t; + +/** +\brief DES Ctrl Block +*/ +typedef struct { +#ifdef CONFIG_SYSTEM_SECURE +#ifdef CONFIG_CSI_V1 + des_handle_t handle; + unsigned char key[32]; + unsigned int key_len; +#endif +#ifdef CONFIG_CSI_V2 + csi_des_t csi_des; + //unsigned char sc_ctx[SC_DES_CTX_SIZE]; +#endif +#endif +#if defined(CONFIG_TEE_CA) + unsigned char key[32]; + unsigned int key_len; +#endif +#if defined(CONFIG_SEC_CRYPTO_DES_SW) + sc_mbedtls_des_context des_ctx; +#endif + //void *ctx; +} sc_des_t; + +// Function documentation +/** + \brief Initialize DES Interface. Initializes the resources needed for the DES interface + \param[in] des operate handle + \param[in] idx device id + \return error code \ref uint32_t +*/ +uint32_t sc_des_init(sc_des_t *des, uint32_t idx); + +/** + \brief De-initialize DES Interface. stops operation and releases the software resources used by the interface + \param[in] des handle to operate + \return None +*/ +void sc_des_uninit(sc_des_t *des); + +/** + \brief Set encrypt key + \param[in] des handle to operate + \param[in] key Pointer to the key buf + \param[in] key_len Pointer to \ref sc_des_key_bits_t + \return error code \ref uint32_t +*/ +uint32_t sc_des_set_encrypt_key(sc_des_t *des, void *key, sc_des_key_bits_t key_len); + +/** + \brief Set decrypt key + \param[in] des handle to operate + \param[in] key Pointer to the key buf + \param[in] key_len Pointer to \ref sc_des_key_bits_t + \return error code \ref uint32_t +*/ +uint32_t sc_des_set_decrypt_key(sc_des_t *des, void *key, sc_des_key_bits_t key_len); + +/** + \brief Des ecb encrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \return error code \ref uint32_t +*/ +uint32_t sc_des_ecb_encrypt(sc_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief Des ecb decrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \return error code \ref uint32_t +*/ +uint32_t sc_des_ecb_decrypt(sc_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief Des cbc encrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \param[in] iv init vector + \return error code \ref uint32_t +*/ +uint32_t sc_des_cbc_encrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief Des cbc decrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \param[in] iv init vector + \return error code \ref uint32_t +*/ +uint32_t sc_des_cbc_decrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief TDes ecb encrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \return error code \ref uint32_t +*/ +uint32_t sc_tdes_ecb_encrypt(sc_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief TDes ecb decrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \return error code \ref uint32_t +*/ +uint32_t sc_tdes_ecb_decrypt(sc_des_t *des, void *in, void *out, uint32_t size); + +/** + \brief TDes cbc encrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \param[in] iv init vector + \return error code \ref uint32_t +*/ +uint32_t sc_tdes_cbc_encrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief TDes cbc decrypt + \param[in] des handle to operate + \param[in] in Pointer to the Source data + \param[out] out Pointer to the Result data + \param[in] size the Source data size + \param[in] iv init vector + \return error code \ref uint32_t +*/ +uint32_t sc_tdes_cbc_decrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv); + +/** + \brief Config DES mode dma or slave + \param[in] mode \ref sc_des_trans_mode_t + \return None +*/ +void sc_des_trans_config(sc_des_t *des, sc_des_trans_mode_t mode) ; + +#ifdef __cplusplus +} +#endif + +#endif /* _SC_DES_H_ */ diff --git a/lib/sec_library/include/sec_crypto_ecc.h b/lib/sec_library/include/sec_crypto_ecc.h index 04fae214..04fae214 100644..100755 --- a/lib/sec_library/include/sec_crypto_ecc.h +++ b/lib/sec_library/include/sec_crypto_ecc.h diff --git a/lib/sec_library/include/sec_crypto_ecdh.h b/lib/sec_library/include/sec_crypto_ecdh.h index 83f52f8c..83f52f8c 100644..100755 --- a/lib/sec_library/include/sec_crypto_ecdh.h +++ b/lib/sec_library/include/sec_crypto_ecdh.h diff --git a/lib/sec_library/include/sec_crypto_kdf.h b/lib/sec_library/include/sec_crypto_kdf.h index 1f08fd3a..1f08fd3a 100644..100755 --- a/lib/sec_library/include/sec_crypto_kdf.h +++ b/lib/sec_library/include/sec_crypto_kdf.h diff --git a/lib/sec_library/include/sec_crypto_mac.h b/lib/sec_library/include/sec_crypto_mac.h index 233ebdc4..dd82105f 100644..100755 --- a/lib/sec_library/include/sec_crypto_mac.h +++ b/lib/sec_library/include/sec_crypto_mac.h @@ -15,15 +15,15 @@ #include "sec_crypto_errcode.h" #include "sec_crypto_sha.h" -#define SC_MAC_KEY_LEN_MAX 64 -#define HMAC_SHA1_BLOCK_SIZE 64 -#define HMAC_SHA224_BLOCK_SIZE 64 -#define HMAC_SM3_BLOCK_SIZE 64 -#define HMAC_SHA256_BLOCK_SIZE 64 -#define HMAC_MD5_BLOCK_SIZE 64 -#define HMAC_SHA384_BLOCK_SIZE 128 -#define HMAC_SHA512_BLOCK_SIZE 128 -#define HMAC_MAX_BLOCK_SIZE 128 +#define SC_MAC_KEY_LEN_MAX (64) +#define HMAC_SHA1_BLOCK_SIZE (64) +#define HMAC_SHA224_BLOCK_SIZE (64) +#define HMAC_SM3_BLOCK_SIZE (64) +#define HMAC_SHA256_BLOCK_SIZE (64) +#define HMAC_MD5_BLOCK_SIZE (64) +#define HMAC_SHA384_BLOCK_SIZE (128) +#define HMAC_SHA512_BLOCK_SIZE (128) +#define HMAC_MAX_BLOCK_SIZE (128) #ifdef __cplusplus extern "C" { @@ -44,7 +44,7 @@ typedef struct { /** \brief Initialize MAC Interface. Initializes the resources needed for the MAC interface \param[in] mac operate handle. - \param[in] idx index of mac + \param[in] idx index of mac \return error code \ref uint32_t */ uint32_t sc_mac_init(sc_mac_t *mac, uint32_t idx); @@ -57,23 +57,22 @@ uint32_t sc_mac_init(sc_mac_t *mac, uint32_t idx); void sc_mac_uninit(sc_mac_t *mac); /** - \brief MAC set key function. - \param[in] mac mac handle to operate. - \param[in] key Pointer to the mac key. - \param[in] key_len Length of key. + \brief MAC set key function. + \param[in] mac mac handle to operate. + \param[in] key Pointer to the mac key. + \param[in] key_len Length of key. \return error code */ uint32_t sc_mac_set_key(sc_mac_t *mac, uint8_t *key, uint32_t key_len); /** \brief MAC operation function. - \param[in] mac mac handle to operate. - \param[in] mode sc_sha_mode_t. - \param[in] msg Pointer to the mac input message. - \param[in] msg_len Length of msg. - \param[out] out mac buffer, malloc by caller. - \param[out] out_len, out mac length, - should 32 bytes if HMAC_SHA256 mode. + \param[in] mac mac handle to operate. + \param[in] mode sc_sha_mode_t. + \param[in] msg Pointer to the mac input message. + \param[in] msg_len Length of msg. + \param[out] out mac buffer, malloc by caller. + \param[out] out_len out mac length, should 32 bytes if HMAC_SHA256 mode. \return error code */ uint32_t sc_mac_calc(sc_mac_t *mac, sc_sha_mode_t mode, uint8_t *msg, @@ -81,35 +80,32 @@ uint32_t sc_mac_calc(sc_mac_t *mac, sc_sha_mode_t mode, uint8_t *msg, /** \brief MAC start operation function. - \param[in] mac mac handle to operate. - \param[in] context mac context pointer. - \param[in] mode sc_sha_mode_t. + \param[in] mac mac handle to operate. + \param[in] context mac context pointer. + \param[in] mode sc_sha_mode_t. \return error code */ -uint32_t sc_mac_start(sc_mac_t *mac, sc_mac_context_t *context, - sc_sha_mode_t mode); +uint32_t sc_mac_start(sc_mac_t *mac, sc_mac_context_t *context, sc_sha_mode_t mode); /** \brief MAC start operation function. - \param[in] mac mac handle to operate. - \param[in] context mac context pointer. - \param[in] msg Pointer to the mac input message. - \param[in] msg_len Length of msg. + \param[in] mac mac handle to operate. + \param[in] context mac context pointer. + \param[in] msg Pointer to the mac input message. + \param[in] msg_len Length of msg. \return error code */ -uint32_t sc_mac_update(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *msg, - uint32_t msg_len); +uint32_t sc_mac_update(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *msg, uint32_t msg_len); /** \brief MAC start operation function. - \param[in] mac mac handle to operate. - \param[in] context mac context pointer. - \param[out] out mac buffer, malloc by caller. - \param[out] out_len, out mac length, + \param[in] mac mac handle to operate. + \param[in] context mac context. + \param[out] out mac buffer, malloc by caller. + \param[out] out_len out mac length, \return error code */ -uint32_t sc_mac_finish(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *out, - uint32_t *out_len); +uint32_t sc_mac_finish(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *out, uint32_t *out_len); #ifdef __cplusplus } #endif diff --git a/lib/sec_library/include/sec_crypto_sha.h b/lib/sec_library/include/sec_crypto_sha.h index 09d71a08..3d45103c 100755 --- a/lib/sec_library/include/sec_crypto_sha.h +++ b/lib/sec_library/include/sec_crypto_sha.h @@ -44,13 +44,13 @@ extern "C" { /*----- SHA Control Codes: Mode -----*/ typedef enum { SC_SHA_MODE_SHA1 = 1U, ///< SHA_1 mode - SC_SHA_MODE_256, ///< SHA_256 mode - SC_SHA_MODE_224, ///< SHA_224 mode - SC_SHA_MODE_512, ///< SHA_512 mode - SC_SHA_MODE_384, ///< SHA_384 mode - SC_SHA_MODE_512_256, ///< SHA_512_256 mode - SC_SHA_MODE_512_224, ///< SHA_512_224 mode - SC_SHA_MODE_MD5, ///< MD5 mode + SC_SHA_MODE_256, ///< SHA_256 mode + SC_SHA_MODE_224, ///< SHA_224 mode + SC_SHA_MODE_512, ///< SHA_512 mode + SC_SHA_MODE_384, ///< SHA_384 mode + SC_SHA_MODE_512_256, ///< SHA_512_256 mode + SC_SHA_MODE_512_224, ///< SHA_512_224 mode + SC_SHA_MODE_MD5, ///< MD5 mode SC_SM3_MODE, } sc_sha_mode_t; @@ -125,7 +125,7 @@ void sc_sha_uninit(sc_sha_t *sha); /** \brief attach the callback handler to SHA - \param[in] sha operate handle. + \param[in] sha operate handle. \param[in] callback callback function \param[in] arg callback's param \return error code @@ -165,8 +165,7 @@ uint32_t sc_sha_update(sc_sha_t *sha, sc_sha_context_t *context, const void *inp \param[in] size the data size \return error code \ref uint32_t */ -uint32_t sc_sha_update_async(sc_sha_t *sha, sc_sha_context_t *context, const void *input, - uint32_t size); +uint32_t sc_sha_update_async(sc_sha_t *sha, sc_sha_context_t *context, const void *input, uint32_t size); /** \brief finish the engine diff --git a/lib/sec_library/include/sec_crypto_sm2.h b/lib/sec_library/include/sec_crypto_sm2.h index 17be90b3..db04880b 100755 --- a/lib/sec_library/include/sec_crypto_sm2.h +++ b/lib/sec_library/include/sec_crypto_sm2.h @@ -77,7 +77,7 @@ typedef void (*sc_sm2_callback_t)(sc_sm2_event_e event); \brief Initialize SM2. \param[in] sm2 sm2 handle to operate. \param[in] idx device id - \return \ref uint32_t + \return Error code \ref uint32_t */ uint32_t sc_sm2_init(sc_sm2_t *sm2, uint32_t idx); @@ -91,14 +91,13 @@ void sc_sm2_uninit(sc_sm2_t *sm2); /** \brief sm2 get capability. \param[in] sm2 Operate handle. - \return \ref uint32_t + \return Error code \ref uint32_t */ -uint32_t sc_sm2_config(sc_sm2_t *sm2, sc_sm2_cipher_order_e co, - sc_sm2_endian_mode_e endian); +uint32_t sc_sm2_config(sc_sm2_t *sm2, sc_sm2_cipher_order_e co, sc_sm2_endian_mode_e endian); /** \brief Attach the callback handler to SM2 - \param[in] sm2 Operate handle. + \param[in] sm2 Operate handle. \param[in] cb Callback function \param[in] arg User can define it by himself as callback's param \return Error code \ref uint32_t @@ -114,91 +113,92 @@ void sc_sm2_detach_callback(sc_sm2_t *sm2); /** \brief sm2 get capability. \param[in] sm2 Operate handle. - \param[out] cap Pointer of sc_sm2_capabilities_t. - \return \ref uint32_t + \param[out] cap Pointer of sc_sm2_capabilities_t. + \return Error code \ref uint32_t */ uint32_t sc_sm2_get_capabilities(sc_sm2_t *sm2, sc_sm2_capabilities_t *cap); -uint32_t sc_sm2_check_keypair(sc_sm2_t *sm2, uint8_t pubkey[65], - uint8_t prikey[32]); +/** + \brief check whether the public key and private key are a pair. + \param[in] sm2 sm2 handle to operate. + \param[in] private Pointer to the sm2 private key, alloc by caller. + \param[in] public Pointer to the sm2 public key, alloc by caller. + \return Error code \ref uint32_t +*/ +uint32_t sc_sm2_check_keypair(sc_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief generate sm2 key. \param[in] sm2 sm2 handle to operate. \param[out] private Pointer to the sm2 private key, alloc by caller. - \param[out] public Pointer to the sm2 public key, alloc by caller. - \return \ref uint32_t + \param[out] public Pointer to the sm2 public key, alloc by caller. + \return Error code \ref uint32_t */ uint32_t sc_sm2_gen_key(sc_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief sm2 sign - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \param[out] s Pointer to the signature + \return Error code \ref uint32_t */ -uint32_t sc_sm2_sign(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], - uint8_t s[64]); +uint32_t sc_sm2_sign(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /** \brief sm2 sign - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \param[out] s Pointer to the signature + \return Error code \ref uint32_t */ -uint32_t sc_sm2_sign_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], - uint8_t s[64]); +uint32_t sc_sm2_sign_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /* TODO */ /** \brief sm2 verify - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature + \param[out] s Pointer to the signature \return verify result */ -bool sc_sm2_verify(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool sc_sm2_verify(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief sm2 verify - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature + \param[out] s Pointer to the signature \return verify result */ -bool sc_sm2_verify_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool sc_sm2_verify_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief sm2 encrypto - \param[in] sm2 sm2 handle to operate. - \param[in] plain Pointer to the plaintext. - \param[in] PlainByteLen plaintext len - \param[in] pubKey public key. - \param[out] cipher Pointer to the chipher - \param[out] cipher_byte_len Pointer to the chipher len. - \return uint32_t + \param[in] sm2 sm2 handle to operate. + \param[in] plain Pointer to the plaintext. + \param[in] PlainByteLen plaintext len + \param[in] pubKey public key. + \param[out] cipher Pointer to the chipher + \param[out] cipher_byte_len Pointer to the chipher len. + \return Error code \ref uint32_t */ uint32_t sc_sm2_encrypt(sc_sm2_t *sm2, uint8_t *plain, uint32_t plain_len, - uint8_t pubKey[65], uint8_t *cipher, - uint32_t *cipher_len); + uint8_t pubKey[65], uint8_t *cipher, uint32_t *cipher_len); /** \brief sm2 encrypto - \param[in] sm2 sm2 handle to operate. - \param[in] cipher Pointer to the chipher - \param[in] CipherByteLen chipher len. - \param[in] prikey private key. - \param[out] plain Pointer to the plaintext. - \param[out] PlainByteLen plaintext len - \return uint32_t + \param[in] sm2 sm2 handle to operate. + \param[in] cipher Pointer to the chipher + \param[in] CipherByteLen chipher len. + \param[in] prikey private key. + \param[out] plain Pointer to the plaintext. + \param[out] PlainByteLen plaintext len + \return Error code \ref uint32_t */ uint32_t sc_sm2_decrypt(sc_sm2_t *sm2, uint8_t *cipher, uint32_t cipher_len, uint8_t prikey[32], uint8_t *plain, @@ -207,7 +207,7 @@ uint32_t sc_sm2_decrypt(sc_sm2_t *sm2, uint8_t *cipher, uint32_t cipher_len, /** \brief sm2 key exchange \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref uint32_t */ uint32_t sc_sm2_exchangekey(sc_sm2_t *sm2, sc_sm2_exchange_role_e role, uint8_t *da, uint8_t *pb, uint8_t *ra1, uint8_t *ra, @@ -218,7 +218,7 @@ uint32_t sc_sm2_exchangekey(sc_sm2_t *sm2, sc_sm2_exchange_role_e role, /** \brief sm2 key exchange get Z. \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref uint32_t */ uint32_t sc_sm2_getZ(sc_sm2_t *sm2, uint8_t *id, uint32_t id_len, uint8_t pubkey[65], uint8_t z[32]); @@ -226,10 +226,9 @@ uint32_t sc_sm2_getZ(sc_sm2_t *sm2, uint8_t *id, uint32_t id_len, /** \brief sm2 key exchange get E \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref uint32_t */ -uint32_t sc_sm2_getE(sc_sm2_t *sm2, uint8_t *m, uint32_t len, uint8_t z[32], - uint8_t e[32]); +uint32_t sc_sm2_getE(sc_sm2_t *sm2, uint8_t *m, uint32_t len, uint8_t z[32], uint8_t e[32]); /** \brief Get SM2 state. diff --git a/lib/sec_library/include/sec_include_config.h b/lib/sec_library/include/sec_include_config.h index c5976ba4..c5976ba4 100644..100755 --- a/lib/sec_library/include/sec_include_config.h +++ b/lib/sec_library/include/sec_include_config.h diff --git a/lib/sec_library/include/sha.h b/lib/sec_library/include/sha.h index c5216a40..33e8baad 100644..100755 --- a/lib/sec_library/include/sha.h +++ b/lib/sec_library/include/sha.h @@ -19,14 +19,28 @@ extern "C" { #endif -#define HASH_DATAIN_BLOCK_SIZE 64 - -#define SHA1_DIGEST_OUT_SIZE 20 -#define SHA224_DIGEST_OUT_SIZE 28 -#define SHA256_DIGEST_OUT_SIZE 32 -#define SHA384_DIGEST_OUT_SIZE 48 -#define SHA512_DIGEST_OUT_SIZE 64 -#define MD5_DIGEST_OUT_SIZE 16 +#define HASH_DATAIN_BLOCK_SIZE (64) + +#define SHA1_DIGEST_OUT_SIZE (20) +#define SHA224_DIGEST_OUT_SIZE (28) +#define SHA256_DIGEST_OUT_SIZE (32) +#define SHA384_DIGEST_OUT_SIZE (48) +#define SHA512_DIGEST_OUT_SIZE (64) +#define MD5_DIGEST_OUT_SIZE (16) + +#define CSI_SHA256_MODE (0x00000008) +#define CSI_SHA224_MODE (0x00000010) +#define CSI_SHA384_MODE (0x00000040) +#define CSI_SHA512_MODE (0x00000020) +#define CSI_MD5_MODE (0x00000002) +#define CSI_SHA1_MODE (0x00000004) + +#define CSI_SHA256_NEW_MODE (0x00000009) +#define CSI_SHA224_MEW_MODE (0x00000011) +#define CSI_SHA384_NEW_MODE (0x00000041) +#define CSI_SHA512_NEW_MODE (0x00000021) +#define CSI_MD5_NEW_MODE (0x00000003) +#define CSI_SHA1_NEW_MODE (0x00000005) /****** SHA mode ******/ typedef enum { diff --git a/lib/sec_library/include/sm2.h b/lib/sec_library/include/sm2.h index dd5c4d50..f5a6834c 100644..100755 --- a/lib/sec_library/include/sm2.h +++ b/lib/sec_library/include/sm2.h @@ -20,6 +20,16 @@ extern "C" { #endif +#define CSI_SM2_PUBKEY_LEN (65-1) +#define CSI_SM2_PRIVKEY_LEN (32) +#define CSI_SM2_PUBKEYTMP_LEN (65) +#define CSI_SM2_RK_LEN (32) //random +#define CSI_SM2_SIGNATURE_LEN (64) +#define CSI_SM2_DIGEST_LEN (32) + +#define SM2_PRIME_CURVE_G_BYTES (64) +#define SM2_PRIME_CURVE_N_BYTES (32) + typedef struct { uint32_t sm2_curve : 1; ///< supports 256bits curve } sm2_capabilities_t; @@ -95,95 +105,96 @@ csi_error_t csi_sm2_config(csi_sm2_t *sm2, sm2_cipher_order_e co, /** \brief Attach the callback handler to SM2 - \param[in] sm2 Operate handle. + \param[in] sm2 Operate handle. \param[in] cb Callback function \param[in] arg User can define it by himself as callback's param \return Error code \ref csi_error_t */ -csi_error_t csi_sm2_attach_callback(csi_sm2_t *sm2, csi_sm2_callback_t cb, - void *arg); +csi_error_t csi_sm2_attach_callback(csi_sm2_t *sm2, csi_sm2_callback_t cb, void *arg); /** \brief Detach the callback handler \param[in] sm2 Operate handle. + \return none */ void csi_sm2_detach_callback(csi_sm2_t *sm2); /** \brief sm2 get capability. \param[in] sm2 Operate handle. - \param[out] cap Pointer of sm2_capabilities_t. - \return \ref uint32_t + \param[out] cap Pointer of sm2_capabilities_t. + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_get_capabilities(csi_sm2_t *sm2, sm2_capabilities_t *cap); -csi_error_t csi_sm2_check_keypair(csi_sm2_t *sm2, uint8_t pubkey[65], - uint8_t prikey[32]); +/** + \brief check whether the public key and private key are a pair. + \param[in] sm2 sm2 handle to operate. + \param[in] private Pointer to the sm2 private key, alloc by caller. + \param[in] public Pointer to the sm2 public key, alloc by caller. + \return Error code \ref csi_error_t +*/ +csi_error_t csi_sm2_check_keypair(csi_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief generate sm2 key. \param[in] sm2 sm2 handle to operate. \param[out] private Pointer to the sm2 private key, alloc by caller. - \param[out] public Pointer to the sm2 public key, alloc by caller. - \return \ref uint32_t + \param[out] public Pointer to the sm2 public key, alloc by caller. + \return Error code \ref csi_error_t */ -csi_error_t csi_sm2_gen_key(csi_sm2_t *sm2, uint8_t pubkey[65], - uint8_t prikey[32]); +csi_error_t csi_sm2_gen_key(csi_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]); /** \brief sm2 sign - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \param[out] s Pointer to the signature + \return Error code \ref csi_error_t */ -csi_error_t csi_sm2_sign(csi_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], - uint8_t s[64]); +csi_error_t csi_sm2_sign(csi_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /** \brief sm2 sign - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature - \return \ref uint32_t + \param[out] s Pointer to the signature + \return Error code \ref csi_error_t */ -csi_error_t csi_sm2_sign_async(csi_sm2_t *sm2, uint8_t d[32], - uint8_t prikey[32], uint8_t s[64]); +csi_error_t csi_sm2_sign_async(csi_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]); /* TODO */ /** \brief sm2 verify - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature + \param[out] s Pointer to the signature \return verify result */ -bool csi_sm2_verify(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool csi_sm2_verify(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief sm2 verify - \param[in] sm2 sm2 handle to operate. + \param[in] sm2 sm2 handle to operate. \param[in] d Pointer to the digest. \param[out] privkey Pointer to the private key - \param[out] s Pointer to the signature + \param[out] s Pointer to the signature \return verify result */ -bool csi_sm2_verify_async(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], - uint8_t s[64]); +bool csi_sm2_verify_async(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]); /** \brief sm2 encrypto - \param[in] sm2 sm2 handle to operate. - \param[in] Plain Pointer to the plaintext. - \param[in] PlainByteLen plaintext len - \param[in] pubKey public key. - \param[out] Cipher Pointer to the chipher + \param[in] sm2 sm2 handle to operate. + \param[in] Plain Pointer to the plaintext. + \param[in] PlainByteLen plaintext len + \param[in] pubKey public key. + \param[out] Cipher Pointer to the chipher \param[out] CipherByteLen Pointer to the chipher len. - \return uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_encrypt(csi_sm2_t *sm2, uint8_t *Plain, uint32_t PlainByteLen, uint8_t pubKey[65], @@ -191,13 +202,13 @@ csi_error_t csi_sm2_encrypt(csi_sm2_t *sm2, uint8_t *Plain, /** \brief sm2 encrypto - \param[in] sm2 sm2 handle to operate. - \param[in] Cipher Pointer to the chipher - \param[in] CipherByteLen chipher len. - \param[in] prikey private key. - \param[out] Plain Pointer to the plaintext. - \param[out] PlainByteLen plaintext len - \return uint32_t + \param[in] sm2 sm2 handle to operate. + \param[in] Cipher Pointer to the chipher + \param[in] CipherByteLen chipher len. + \param[in] prikey private key. + \param[out] Plain Pointer to the plaintext. + \param[out] PlainByteLen plaintext len + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_decrypt(csi_sm2_t *sm2, uint8_t *Cipher, uint32_t CipherByteLen, uint8_t prikey[32], @@ -206,7 +217,7 @@ csi_error_t csi_sm2_decrypt(csi_sm2_t *sm2, uint8_t *Cipher, /** \brief sm2 key exchange \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_exchangekey(csi_sm2_t *sm2, sm2_exchange_role_e role, uint8_t *dA, uint8_t *PB, uint8_t *rA, @@ -217,7 +228,7 @@ csi_error_t csi_sm2_exchangekey(csi_sm2_t *sm2, sm2_exchange_role_e role, /** \brief sm2 key exchange get Z. \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_getZ(csi_sm2_t *sm2, uint8_t *ID, uint32_t byteLenofID, uint8_t pubKey[65], uint8_t Z[32]); @@ -225,7 +236,7 @@ csi_error_t csi_sm2_getZ(csi_sm2_t *sm2, uint8_t *ID, uint32_t byteLenofID, /** \brief sm2 key exchange get E \param[in] sm2 sm2 handle to operate. - \return uint32_t + \return Error code \ref csi_error_t */ csi_error_t csi_sm2_getE(csi_sm2_t *sm2, uint8_t *M, uint32_t byteLen, uint8_t Z[32], uint8_t E[32]); diff --git a/lib/sec_library/include/sm3.h b/lib/sec_library/include/sm3.h index 1f63f15e..e86ee261 100644..100755 --- a/lib/sec_library/include/sm3.h +++ b/lib/sec_library/include/sm3.h @@ -20,39 +20,39 @@ extern "C" { #endif -#define SM3_DATAIN_BLOCK_SIZE 64 -#define SM3_DIGEST_OUT_SIZE 32 +#define SM3_DATAIN_BLOCK_SIZE (64) +#define SM3_DIGEST_OUT_SIZE (32) typedef struct { - uint32_t total[2]; ///< Number of bytes processed - uint32_t state[16]; ///< Intermediate digest state + uint32_t total[2]; ///< Number of bytes processed + uint32_t state[16]; ///< Intermediate digest state uint8_t buffer[SM3_DATAIN_BLOCK_SIZE]; ///< Data block beingprocessed - uint8_t result[SM3_DIGEST_OUT_SIZE]; ///< Data block has processed + uint8_t result[SM3_DIGEST_OUT_SIZE]; ///< Data block has processed } csi_sm3_context_t; /****** SM3 State ******/ typedef struct { - uint32_t busy : 1; ///< Calculate busy flag - uint32_t error : 1; ///< Calculate error flag + uint32_t busy : 1; ///< Calculate busy flag + uint32_t error : 1; ///< Calculate error flag } csi_sm3_state_t; /****** SM3 Event ******/ typedef enum { - SM3_EVENT_COMPLETE = 0U, ///< Calculate completed + SM3_EVENT_COMPLETE = 0U, ///< Calculate completed SM3_EVENT_UPDATE, SM3_EVENT_START, - SM3_EVENT_ERROR ///< Calculate error + SM3_EVENT_ERROR ///< Calculate error } csi_sm3_event_t; typedef struct csi_sm3_t csi_sm3_t; struct csi_sm3_t { - csi_dev_t dev; ///< SM3 hw-device info + csi_dev_t dev; ///< SM3 hw-device info void (*callback)(csi_sm3_t *sm3, csi_sm3_event_t event, void *arg); ///< SM3 event callback for user - void * arg; ///< SM3 custom designed param passed to evt_cb - csi_sm3_state_t state; ///< SM3 state + void * arg; ///< SM3 custom designed param passed to evt_cb + csi_sm3_state_t state; ///< SM3 state void * priv; }; @@ -62,7 +62,7 @@ struct csi_sm3_t { \brief Initialize SM3 Interface. Initializes the resources needed for the SM3 interface \param[in] sm3 operate handle. \param[in] idx index of sm3 - \return error code \ref uint32_t + \return error code \ref csi_error_t */ csi_error_t csi_sm3_init(csi_sm3_t *sm3, uint32_t idx); @@ -93,7 +93,7 @@ void csi_sm3_detach_callback(csi_sm3_t *sm3); \brief start the engine \param[in] sm3 sm3 handle to .operate \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t - \return error code \ref uint32_t + \return error code \ref csi_error_t */ csi_error_t csi_sm3_start(csi_sm3_t *sm3, csi_sm3_context_t *context); @@ -103,10 +103,9 @@ csi_error_t csi_sm3_start(csi_sm3_t *sm3, csi_sm3_context_t *context); \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t \param[in] input Pointer to the Source data \param[in] size the data size - \return error code \ref uint32_t + \return error code \ref csi_error_t */ -csi_error_t csi_sm3_update(csi_sm3_t *sm3, csi_sm3_context_t *context, - const uint8_t *input, uint32_t size); +csi_error_t csi_sm3_update(csi_sm3_t *sm3, csi_sm3_context_t *context, const uint8_t *input, uint32_t size); /** \brief Accumulate the engine (async mode) @@ -116,8 +115,7 @@ csi_error_t csi_sm3_update(csi_sm3_t *sm3, csi_sm3_context_t *context, \param[in] size The data size \return Error code \ref csi_error_t */ -csi_error_t csi_sm3_update_async(csi_sm3_t *sm3, csi_sm3_context_t *context, - const uint8_t *input, uint32_t size); +csi_error_t csi_sm3_update_async(csi_sm3_t *sm3, csi_sm3_context_t *context, const uint8_t *input, uint32_t size); /** \brief finish the engine @@ -125,10 +123,9 @@ csi_error_t csi_sm3_update_async(csi_sm3_t *sm3, csi_sm3_context_t *context, \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t \param[out] output Pointer to the result data \param[out] out_size Pointer to the result data size(bytes) - \return error code \ref uint32_t + \return error code \ref csi_error_t */ -csi_error_t csi_sm3_finish(csi_sm3_t *sm3, csi_sm3_context_t *context, - uint8_t *output, uint32_t *out_size); +csi_error_t csi_sm3_finish(csi_sm3_t *sm3, csi_sm3_context_t *context, uint8_t *output, uint32_t *out_size); /** \brief Get SM3 state diff --git a/lib/sec_library/include/sm4.h b/lib/sec_library/include/sm4.h index 0d66cb88..ae7b15a3 100644..100755 --- a/lib/sec_library/include/sm4.h +++ b/lib/sec_library/include/sm4.h @@ -20,9 +20,11 @@ extern "C" { #endif -#define SM4_KEY_LEN_BYTES_32 32 -#define SM4_KEY_LEN_BYTES_24 24 -#define SM4_KEY_LEN_BYTES_16 16 +#define SM4_KEY_LEN_BYTES_32 (32) +#define SM4_KEY_LEN_BYTES_24 (24) +#define SM4_KEY_LEN_BYTES_16 (16) + +#define SM4_IV_LEN_BYTES_16 (16) typedef enum { SM4_KEY_LEN_BITS_128 = 0, /*128 Data bits*/ @@ -91,8 +93,7 @@ csi_error_t csi_sm4_set_decrypt_key(csi_sm4_t *sm4, uint8_t *key, csi_sm4_key_bi \param[in] size the Source data size \return error code \ref uint32_t */ -csi_error_t csi_sm4_ecb_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out, - uint32_t size); +csi_error_t csi_sm4_ecb_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out, uint32_t size); /** \brief sm4 ecb decrypt @@ -102,8 +103,7 @@ csi_error_t csi_sm4_ecb_encrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out, \param[in] size the Source data size \return error code \ref uint32_t */ -csi_error_t csi_sm4_ecb_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out, - uint32_t size); +csi_error_t csi_sm4_ecb_decrypt(csi_sm4_t *sm4, uint8_t *in, uint8_t *out, uint32_t size); /** \brief sm4 cbc encrypt diff --git a/lib/sec_library/include/soc.h b/lib/sec_library/include/soc.h index 99a35f48..99a35f48 100644..100755 --- a/lib/sec_library/include/soc.h +++ b/lib/sec_library/include/soc.h diff --git a/lib/sec_library/include/sys_clk.h b/lib/sec_library/include/sys_clk.h index c04c26e8..c04c26e8 100644..100755 --- a/lib/sec_library/include/sys_clk.h +++ b/lib/sec_library/include/sys_clk.h diff --git a/lib/sec_library/libsec_library.a b/lib/sec_library/libsec_library.a Binary files differindex cbaca386..c61537c0 100644..100755 --- a/lib/sec_library/libsec_library.a +++ b/lib/sec_library/libsec_library.a |