168 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			168 lines
		
	
	
		
			5.2 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_H
							 | 
						||
| 
								 | 
							
								#define IOT_CRYPTO_H
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "iot_crypto_aes_api.h"
							 | 
						||
| 
								 | 
							
								#include "iot_crypto_hash_api.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    SM4_ECB,
							 | 
						||
| 
								 | 
							
								    SM4_CBC,
							 | 
						||
| 
								 | 
							
								    SM4_CFB,
							 | 
						||
| 
								 | 
							
								    SM4_OFB
							 | 
						||
| 
								 | 
							
								}SEC_SYS_SM4_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    SM4_OPT_DEC,
							 | 
						||
| 
								 | 
							
								    SM4_OPT_ENC,
							 | 
						||
| 
								 | 
							
								}SEC_SYS_SM4_OPT_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    AES_ECB,
							 | 
						||
| 
								 | 
							
								    AES_CBC,
							 | 
						||
| 
								 | 
							
								    AES_CFB,
							 | 
						||
| 
								 | 
							
								    AES_OFB
							 | 
						||
| 
								 | 
							
								}SEC_SYS_AES_CALC_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    AES_OPT_DEC,
							 | 
						||
| 
								 | 
							
								    AES_OPT_ENC,
							 | 
						||
| 
								 | 
							
								}SEC_SYS_AES_OPT_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    DES_ECB,
							 | 
						||
| 
								 | 
							
								    DES_CBC,
							 | 
						||
| 
								 | 
							
								    DES_CFB,
							 | 
						||
| 
								 | 
							
								    DES_OFB
							 | 
						||
| 
								 | 
							
								}SEC_SYS_DES_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    DES_OPT_ENC,
							 | 
						||
| 
								 | 
							
								    DES_OPT_DEC,
							 | 
						||
| 
								 | 
							
								}SEC_SYS_DES_OPT_MODE;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define SM2_GEN_PUBKEY_LEN                          (64)
							 | 
						||
| 
								 | 
							
								#define SM2_GEN_PRIKEY_LEN                          (32)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define ECDSA_GEN_PUBKEY_LEN                        (64)
							 | 
						||
| 
								 | 
							
								#define ECDSA_GEN_PRIKEY_LEN                        (32)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_crypto_init() - Initialize crypto module
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void iot_crypto_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief uint32_t iot_crypto_sm4
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param opt_mode:             dec or enc
							 | 
						||
| 
								 | 
							
								 * @param mode:                 ecb or cbc
							 | 
						||
| 
								 | 
							
								 * @param indata:               input data
							 | 
						||
| 
								 | 
							
								 * @param indata_len:           indata len
							 | 
						||
| 
								 | 
							
								 * @param outdata:              output data
							 | 
						||
| 
								 | 
							
								 * @param outdata_len:          output len
							 | 
						||
| 
								 | 
							
								 * @param sm4_key:              sm4 key
							 | 
						||
| 
								 | 
							
								 * @param iv:                   sm4 iv
							 | 
						||
| 
								 | 
							
								 * @param round:                round count
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @retval:                     error code. see CRYPTO_RET_XXX
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t iot_crypto_sm4(SEC_SYS_SM4_OPT_MODE opt_mode, SEC_SYS_SM4_MODE mode,
							 | 
						||
| 
								 | 
							
								    uint8_t *indata, uint32_t indata_len, uint8_t *outdata,
							 | 
						||
| 
								 | 
							
								    uint32_t *outdata_len, uint8_t sm4_key[16], uint8_t iv[16], uint32_t round);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief uint32_t iot_crypto_sm4_ind
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param opt_mode:             dec or enc
							 | 
						||
| 
								 | 
							
								 * @param mode:                 ecb or cbc
							 | 
						||
| 
								 | 
							
								 * @param indata:               input data
							 | 
						||
| 
								 | 
							
								 * @param indata_len:           indata len
							 | 
						||
| 
								 | 
							
								 * @param outdata:              output data
							 | 
						||
| 
								 | 
							
								 * @param outdata_len:          output len
							 | 
						||
| 
								 | 
							
								 * @param sm4_key:              sm4 key
							 | 
						||
| 
								 | 
							
								 * @param iv:                   sm4 iv
							 | 
						||
| 
								 | 
							
								 * @param round:                round count
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @retval:                     error code. see CRYPTO_RET_XXX
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t iot_crypto_sm4_ind(SEC_SYS_SM4_OPT_MODE opt_mode, SEC_SYS_SM4_MODE mode,
							 | 
						||
| 
								 | 
							
								    uint8_t *indata, uint32_t indata_len, uint8_t *outdata, uint32_t *outdata_len,
							 | 
						||
| 
								 | 
							
								    uint8_t sm4_key[16], uint8_t iv[16], uint32_t round);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief uint32_t iot_crypto_des
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param opt_mode:             enc or dec
							 | 
						||
| 
								 | 
							
								 * @param mode:                 ecb or cbc
							 | 
						||
| 
								 | 
							
								 * @param tdes:                 0: des 1:3des
							 | 
						||
| 
								 | 
							
								 * @param indata:               input data
							 | 
						||
| 
								 | 
							
								 * @param indata_len:           input data len
							 | 
						||
| 
								 | 
							
								 * @param outdata:              output data
							 | 
						||
| 
								 | 
							
								 * @param outdata_len:          outdata_len data len
							 | 
						||
| 
								 | 
							
								 * @param des_key:              des key
							 | 
						||
| 
								 | 
							
								 * @param iv:                   des iv
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @retval:                     error code. see CRYPTO_RET_XXX
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t iot_crypto_des(SEC_SYS_DES_OPT_MODE opt_mode, SEC_SYS_DES_MODE mode,
							 | 
						||
| 
								 | 
							
								    uint8_t tdes, uint8_t *indata, uint32_t indata_len, uint8_t *outdata,
							 | 
						||
| 
								 | 
							
								    uint32_t *outdata_len, uint8_t des_key[24], uint8_t iv[8]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_crypto_rng_get_random() - generate random data from RNG
							 | 
						||
| 
								 | 
							
								 * @param buf:           random data buffer
							 | 
						||
| 
								 | 
							
								 * @param len:           random data len
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void iot_crypto_rng_get_random(uint8_t *buf, uint32_t len);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_crypto_aes_gcm
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param is_enc:               1 - encrypt, 0 - decrypt
							 | 
						||
| 
								 | 
							
								 * @param key:                  key buffer
							 | 
						||
| 
								 | 
							
								 * @param keybits:              key bit number
							 | 
						||
| 
								 | 
							
								 * @param input:                for encrypt, this is plaintext
							 | 
						||
| 
								 | 
							
								 *                              for decrypt, this is chipertext
							 | 
						||
| 
								 | 
							
								 * @param input_len:            input text byte length
							 | 
						||
| 
								 | 
							
								 * @param iv:                   input iv buffer
							 | 
						||
| 
								 | 
							
								 * @param iv_len:               iv byte length
							 | 
						||
| 
								 | 
							
								 * @param add:                  add buffer
							 | 
						||
| 
								 | 
							
								 * @param add_len:              add byte length
							 | 
						||
| 
								 | 
							
								 * @param output:               for encrypt, this is chipertext
							 | 
						||
| 
								 | 
							
								 *                              for decrypt, this is plaintext
							 | 
						||
| 
								 | 
							
								 * @param tag:                  output tag buffer
							 | 
						||
| 
								 | 
							
								 * @param round                 run round loop count
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @retval:                     error code. see CRYPTO_RET_XXX
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t iot_crypto_aes_gcm_cert(uint8_t is_enc, uint8_t *key,
							 | 
						||
| 
								 | 
							
								    uint32_t keybits, uint8_t *input, uint32_t input_len, uint8_t *iv,
							 | 
						||
| 
								 | 
							
								    uint32_t iv_len, uint8_t *add, uint32_t add_len, uint8_t *output,
							 | 
						||
| 
								 | 
							
								    uint8_t *tag, uint32_t round);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif //IOT_CRYPTO_H
							 |