| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #ifndef _SM2_H_
- #define _SM2_H_
- #define ECCref_MAX_BITS 256
- #define ECCref_MAX_LEN ((ECCref_MAX_BITS+7)/8) //32
- #define PUBCHARLEN ECCref_MAX_LEN*2 //64
- #define ECCref_MAX_CIPHER_LEN 136
- typedef struct ECCrefPublicKey_st
- {
- unsigned int bits;
- unsigned char x[ECCref_MAX_LEN];
- unsigned char y[ECCref_MAX_LEN];
- } ECCrefPublicKey;
- typedef struct ECCrefPrivateKey_st
- {
- unsigned int bits;
- unsigned char D[ECCref_MAX_LEN];
- } ECCrefPrivateKey;
- typedef struct ECCCipher_st
- {
- // unsigned int clength;
- unsigned char x[ECCref_MAX_LEN];
- unsigned char y[ECCref_MAX_LEN];
- unsigned char C[ECCref_MAX_LEN];
- unsigned char M[ECCref_MAX_LEN];
- } ECCCipher;
- typedef struct ECCSignature_st
- {
- unsigned char r[ECCref_MAX_LEN];
- unsigned char s[ECCref_MAX_LEN];
- } ECCSignature;
- //描述: 获取指定长度的随机数
- //参数:
- // uiLength[in] 欲获取的随机数长度
- // pucRandom[out] 缓冲区指针,用于存放获取的随机数
- //返回值:
- //0 成功
- //非0 失败,失败原因参见SM2算法描述
- int SM2_GenerateRandom_me(unsigned int uiLength, unsigned char *pucRandom);
- //描述: 该函数完成SM2验证运算,用ECC公钥对明文和签名值在指定曲线上进行验证运算。函数内部完成SM3杂凑运算。
- //参数:
- // pucDataInput[in] 缓冲区指针,用于存放外部输入的数据
- // uiInputLength[in] 输入的数据长度
- // pucID[in] 签名者的ID值,产生用于ECC签名的杂凑值时有效(需要与签名时使用的一致)
- // uiIDLength[in] 签名者的ID长度
- // pucPublicKey[in] 外部ECC公钥结构,参见6.1数据结构定义
- // pucSignature[in] 缓冲区指针,用于存放输入的签名数据,参见6.1数据结构定义
- //返回值:
- //0 成功
- //非0 失败,失败原因参见SM2算法描述
- //备注: 对原文的杂凑运算,在函数内部完成。
- #ifdef CPU_AM335X
- static inline int SM2_Verify(unsigned char *pucDataInput,unsigned int uiInputLength,
- unsigned char *pucID, unsigned int uiIDLength,
- ECCrefPublicKey *pucPublicKey,ECCSignature *pucSignature)
- {
- return 0;
- }
- #else
- int SM2_Verify(unsigned char *pucDataInput,unsigned int uiInputLength,
- unsigned char *pucID, unsigned int uiIDLength,
- ECCrefPublicKey *pucPublicKey,ECCSignature *pucSignature);
- #endif
- //描述: 该函数完成SM2加密运算,用ECC公钥对明文在指定曲线上进行加密运算。
- //参数:
- // pucDataInput[in] 缓冲区指针,用于存放外部输入的数据,定长32字节,如果不足32字节,由调用者负责填充
- // uiInputLength[in] 输入的数据长度,定长32字节
- // pucPublicKey[in] 外部ECC公钥结构,参见6.1数据结构定义
- // pucRandom[in] 指定公钥加密时使用的随机数
- // uiRandomLength[in] 随机数长度,定长32字节
- // pucEncData[out] 缓冲区指针,用于存放输出的数据密文,参见6.1数据结构定义
- //返回值:
- // 0 成功
- // 非0 失败,失败原因参见SM2算法描述
- //备注: 需要外部指定随机数,以统一加密结果。
- #ifdef CPU_AM335X
- static inline int SM2_Encrypt(unsigned char *pucDataInput,unsigned int uiInputLength,
- ECCrefPublicKey *pucPublicKey, unsigned char *pucRandom,
- unsigned int uiRandomLength, ECCCipher *pucEncData)
- {
- return 0;
- }
- #else
- int SM2_Encrypt(unsigned char *pucDataInput,unsigned int uiInputLength,
- ECCrefPublicKey *pucPublicKey, unsigned char *pucRandom,
- unsigned int uiRandomLength, ECCCipher *pucEncData);
- #endif
- #ifdef CPU_AM335X
- static inline void encrypt_epri(unsigned char* in, unsigned int in_len, unsigned char* out, unsigned int* out_len, unsigned int key, int type)
- {
- }
- #else
- void encrypt_epri(unsigned char* in, unsigned int in_len, unsigned char* out, unsigned int* out_len, unsigned int key, int type);
- #endif
- void IEC101_SM2(IEC101_DEF* pt);
- void IEC104_SM2(IEC104_DEF* pt);
- extern void InitSM2Key(void);
- int encrypt_test(void);
- #endif
|