248 lines
		
	
	
		
			9.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			248 lines
		
	
	
		
			9.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #ifndef IOT_CRYPTO_DSA_API_H
 | ||
|  | #define IOT_CRYPTO_DSA_API_H
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #include "iot_crypto_error_api.h"
 | ||
|  | 
 | ||
|  | /* define ECDSA elliptic curve type  */ | ||
|  | typedef enum { | ||
|  |     ecdsa_ecp_bp256r1 = 0x0, | ||
|  | } ecdsa_ecp_t; | ||
|  | 
 | ||
|  | #define G_SIZE_INBYTES          (32)
 | ||
|  | #define SM3_RESULT_SIZE         (32)
 | ||
|  | 
 | ||
|  | typedef struct _sm2_exchange_info_t{ | ||
|  |     uint32_t keylen; | ||
|  |     uint8_t za[SM3_RESULT_SIZE]; | ||
|  |     uint8_t zb[SM3_RESULT_SIZE]; | ||
|  |     uint8_t s1[SM3_RESULT_SIZE]; | ||
|  |     uint8_t s2[SM3_RESULT_SIZE]; | ||
|  |     void    *sm2_exchange_data; | ||
|  |     uint8_t *keygen; | ||
|  |     uint8_t *tmpbuf; | ||
|  | }sm2_exchange_info_t; | ||
|  | 
 | ||
|  | typedef struct _ecp_point_t{ | ||
|  |     uint8_t x[G_SIZE_INBYTES]; | ||
|  |     uint8_t y[G_SIZE_INBYTES]; | ||
|  | }ecp_point_t; | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_ecdsa_gen_keypair() - generate key pair. | ||
|  |  * @param ecp: curve type, see ecdsa_ecp_t. | ||
|  |  * @param public_key: returns the generated public key | ||
|  |  * @param public_key_len: returns the generated public key len | ||
|  |  * @param pri_key: returns the generated private  key | ||
|  |  * @param pri_key_len: returns the generated private key len | ||
|  |  * | ||
|  |  * @retval: error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_ecdsa_gen_keypair( | ||
|  |     ecdsa_ecp_t ecp, uint8_t *public_key, uint32_t *public_key_len, | ||
|  |     uint8_t *pri_key, uint32_t *pri_key_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_ecdsa_with_sha256_sign() - ESDA signature, message hash | ||
|  |  *        algorithm is SHA256. | ||
|  |  * @param ecp: curve type, see ecdsa_ecp_t. | ||
|  |  * @param msg: message to be signed | ||
|  |  * @param len: message len | ||
|  |  * @param pri_key: private key | ||
|  |  * @param pri_key_len: private key len | ||
|  |  * @param r_buf: return the generated signature information r part | ||
|  |  * @param r_buf_len: r part len. | ||
|  |  * @param s_buf: return the generated signature information s part | ||
|  |  * @param s_buf_len: s part len. | ||
|  |  * | ||
|  |  * @retval: error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_ecdsa_with_sha256_sign(ecdsa_ecp_t ecp, | ||
|  |     const uint8_t *msg, uint32_t len, | ||
|  |     const uint8_t *pri_key, uint32_t pri_key_len, | ||
|  |     uint8_t *r_buf, uint32_t *r_buf_len, | ||
|  |     uint8_t *s_buf, uint32_t *s_buf_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_ecdsa_with_sha256_sign_verify() - ESDA signature verify, | ||
|  |  *        message hash algorithm is SHA256. | ||
|  |  * @param ecp: curve type, see ecdsa_ecp_t. | ||
|  |  * @param msg: original message to be signed verify | ||
|  |  * @param len: original message len | ||
|  |  * @param public_key: pointer public key | ||
|  |  * @param public_key_len: public key len | ||
|  |  * @param r_buf: signature info r part for verification | ||
|  |  * @param r_buf_len: r part len | ||
|  |  * @param s_buf: signature info s part for verification | ||
|  |  * @param s_buf_len: s part len | ||
|  |  * | ||
|  |  * @retval: error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_ecdsa_with_sha256_sign_verify(ecdsa_ecp_t ecp, | ||
|  |     const uint8_t *msg, uint32_t len, | ||
|  |     const uint8_t *public_key, uint32_t public_key_len, | ||
|  |     const uint8_t *r_buf, uint32_t r_buf_len, | ||
|  |     const uint8_t *s_buf, uint32_t s_buf_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_sign() - signature with pub key and private key | ||
|  |  * @param r         signature r | ||
|  |  * @param rlen      signature r len | ||
|  |  * @param s         signature s | ||
|  |  * @param slen      signature s len | ||
|  |  * | ||
|  |  * @retval:         error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_sign(uint8_t *message, uint32_t message_size, | ||
|  |     uint8_t *ID, uint32_t ID_len, uint8_t *public_key, uint32_t public_key_len, | ||
|  |     uint8_t *r, uint32_t *rlen, uint8_t *s, uint32_t *slen, uint8_t *private_key, | ||
|  |     uint32_t private_key_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_verify() - verify signature | ||
|  |  * @param r         signature r | ||
|  |  * @param rlen      signature r len | ||
|  |  * @param s         signature s | ||
|  |  * @param slen      signature s len | ||
|  |  * | ||
|  |  * @retval:         error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_verify(uint8_t *message, uint32_t message_size, | ||
|  |     uint8_t *ID, uint32_t ID_len, uint8_t *public_key, uint32_t public_key_len, | ||
|  |     uint8_t *r, uint32_t rlen, uint8_t *s, uint32_t slen); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_gen_keypair() - gen keypair include pub key and pri key | ||
|  |  * @param public_key_len       input public_key buf len && output pub_key len | ||
|  |  * @param private_key_len      input private_key buf len && output pri_key len | ||
|  |  * | ||
|  |  * @retval:                    error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_gen_keypair(uint8_t *public_key, | ||
|  |     uint32_t *public_key_len, uint8_t *private_key, uint32_t *private_key_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_gen_share_key() -  calc share key by pub key and pri key | ||
|  |  * @pub_key:           public key, Fixed 64 bytes | ||
|  |  * @pri_key:           private key, Fixed 32 bytes | ||
|  |  * @gen_key:           buffer to store generated key | ||
|  |  * @gen_key_len        The size of the gen_key | ||
|  |  * | ||
|  |  * @retval:            error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_gen_share_key(uint8_t *pub_key, uint8_t *pri_key, | ||
|  |     uint8_t *gen_key, uint32_t gen_key_len); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_encrypt() - sm2 encrypt | ||
|  |  * @param message_plain:           plain text | ||
|  |  * @param message_size:            plain size | ||
|  |  * @param cipher_text:             buffer to store cipher text | ||
|  |  * @param ciphertext_len:          cipher buffer len | ||
|  |  * @param pubkey:                  public key | ||
|  |  * | ||
|  |  * @retval:                        error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_encrypt(uint8_t* message_plain, uint32_t message_size, | ||
|  |     uint8_t* cipher_text, uint32_t *ciphertext_len, uint8_t *pubkey); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_decrypt() - sm2_decrypt | ||
|  |  * @param cipher_text:             cipher text | ||
|  |  * @param ciphertext_len:          cipher text len | ||
|  |  * @param message_plain:           buffer to store Plain text | ||
|  |  * @param message_size:            plain size | ||
|  |  * @param prikey:                  private key | ||
|  |  * | ||
|  |  * @retval:                        error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_decrypt(uint8_t* cipher_text, uint32_t ciphertext_len, | ||
|  |     uint8_t* message_plain, uint32_t *message_size, uint8_t *prikey); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_exchange_info_init() - sm2_info init | ||
|  |  * @param pub_key_a:            public key A | ||
|  |  * @param pub_key_a_len:        public key A len | ||
|  |  * @param pub_key_b:            public key B | ||
|  |  * @param pub_key_b_len:        public key B len | ||
|  |  * @param pri_key:              private key | ||
|  |  * @param pri_key_len:          private key len | ||
|  |  * @param key_len_in_bits:      generate key len in bits | ||
|  |  * @param sm2_info:             parameter set | ||
|  |  * | ||
|  |  * @retval:                     error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_exchange_info_init(uint8_t *pub_key_a, | ||
|  |     uint32_t pub_key_a_len,uint8_t *pub_key_b, uint32_t pub_key_b_len, | ||
|  |     uint8_t *pri_key, uint32_t pri_key_len, uint32_t key_len_in_bits, | ||
|  |     sm2_exchange_info_t *sm2_info); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_exchange_info_init() -  sm2_info deinit | ||
|  |  * @param sm2_info:            parameter set | ||
|  |  */ | ||
|  | void iot_crypto_sm2_exchange_info_deinit(sm2_exchange_info_t *sm2_info); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_exchange_req_step1() - request exchange step1 | ||
|  |  * @param ida:                 a's id | ||
|  |  * @param ida_len:             a's id len | ||
|  |  * @param idb:                 b's id | ||
|  |  * @param idb_len:             b's id len | ||
|  |  * @param ra:                  generate ecp point | ||
|  |  * @param sm2_info:            parameter set | ||
|  |  * | ||
|  |  * @retval:                    error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_exchange_req_step1(uint8_t *ida, uint32_t ida_len, | ||
|  |     uint8_t *idb, uint32_t idb_len, ecp_point_t *ra, | ||
|  |     sm2_exchange_info_t *sm2_info); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_exchange_req_step2() - request exchange step2 | ||
|  |  * @param rb:                  response generate info | ||
|  |  * @param sm2_info:            parameter set | ||
|  |  * | ||
|  |  * @retval:                    error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_exchange_req_step2(ecp_point_t *rb, | ||
|  |     sm2_exchange_info_t *sm2_info); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_crypto_sm2_exchange_response_step1() - response exchange step1 | ||
|  |  * @param ida:                 a's id | ||
|  |  * @param ida_len:             a's id len | ||
|  |  * @param idb:                 b's id | ||
|  |  * @param idb_len:             b's id len | ||
|  |  * @param ra:                  request generate info | ||
|  |  * @param rb:                  response generate info | ||
|  |  * @param sm2_info:            parameter set | ||
|  |  * | ||
|  |  * @retval:                    error code. see CRYPTO_RET_XXX | ||
|  |  */ | ||
|  | uint32_t iot_crypto_sm2_exchange_response_step1(uint8_t *ida, uint32_t ida_len, | ||
|  |     uint8_t *idb, uint32_t idb_len, ecp_point_t *ra, ecp_point_t *rb, | ||
|  |     sm2_exchange_info_t *sm2_info); | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* IOT_CRYPTO_DSA_API_H */
 |