SM2.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef _SM2_H_
  2. #define _SM2_H_
  3. #define ECCref_MAX_BITS 256
  4. #define ECCref_MAX_LEN ((ECCref_MAX_BITS+7)/8) //32
  5. #define PUBCHARLEN ECCref_MAX_LEN*2 //64
  6. #define ECCref_MAX_CIPHER_LEN 136
  7. typedef struct ECCrefPublicKey_st
  8. {
  9. unsigned int bits;
  10. unsigned char x[ECCref_MAX_LEN];
  11. unsigned char y[ECCref_MAX_LEN];
  12. } ECCrefPublicKey;
  13. typedef struct ECCrefPrivateKey_st
  14. {
  15. unsigned int bits;
  16. unsigned char D[ECCref_MAX_LEN];
  17. } ECCrefPrivateKey;
  18. typedef struct ECCCipher_st
  19. {
  20. // unsigned int clength;
  21. unsigned char x[ECCref_MAX_LEN];
  22. unsigned char y[ECCref_MAX_LEN];
  23. unsigned char C[ECCref_MAX_LEN];
  24. unsigned char M[ECCref_MAX_LEN];
  25. } ECCCipher;
  26. typedef struct ECCSignature_st
  27. {
  28. unsigned char r[ECCref_MAX_LEN];
  29. unsigned char s[ECCref_MAX_LEN];
  30. } ECCSignature;
  31. //描述: 获取指定长度的随机数
  32. //参数:
  33. // uiLength[in] 欲获取的随机数长度
  34. // pucRandom[out] 缓冲区指针,用于存放获取的随机数
  35. //返回值:
  36. //0 成功
  37. //非0 失败,失败原因参见SM2算法描述
  38. int SM2_GenerateRandom_me(unsigned int uiLength, unsigned char *pucRandom);
  39. //描述: 该函数完成SM2验证运算,用ECC公钥对明文和签名值在指定曲线上进行验证运算。函数内部完成SM3杂凑运算。
  40. //参数:
  41. // pucDataInput[in] 缓冲区指针,用于存放外部输入的数据
  42. // uiInputLength[in] 输入的数据长度
  43. // pucID[in] 签名者的ID值,产生用于ECC签名的杂凑值时有效(需要与签名时使用的一致)
  44. // uiIDLength[in] 签名者的ID长度
  45. // pucPublicKey[in] 外部ECC公钥结构,参见6.1数据结构定义
  46. // pucSignature[in] 缓冲区指针,用于存放输入的签名数据,参见6.1数据结构定义
  47. //返回值:
  48. //0 成功
  49. //非0 失败,失败原因参见SM2算法描述
  50. //备注: 对原文的杂凑运算,在函数内部完成。
  51. #ifdef CPU_AM335X
  52. static inline int SM2_Verify(unsigned char *pucDataInput,unsigned int uiInputLength,
  53. unsigned char *pucID, unsigned int uiIDLength,
  54. ECCrefPublicKey *pucPublicKey,ECCSignature *pucSignature)
  55. {
  56. return 0;
  57. }
  58. #else
  59. int SM2_Verify(unsigned char *pucDataInput,unsigned int uiInputLength,
  60. unsigned char *pucID, unsigned int uiIDLength,
  61. ECCrefPublicKey *pucPublicKey,ECCSignature *pucSignature);
  62. #endif
  63. //描述: 该函数完成SM2加密运算,用ECC公钥对明文在指定曲线上进行加密运算。
  64. //参数:
  65. // pucDataInput[in] 缓冲区指针,用于存放外部输入的数据,定长32字节,如果不足32字节,由调用者负责填充
  66. // uiInputLength[in] 输入的数据长度,定长32字节
  67. // pucPublicKey[in] 外部ECC公钥结构,参见6.1数据结构定义
  68. // pucRandom[in] 指定公钥加密时使用的随机数
  69. // uiRandomLength[in] 随机数长度,定长32字节
  70. // pucEncData[out] 缓冲区指针,用于存放输出的数据密文,参见6.1数据结构定义
  71. //返回值:
  72. // 0 成功
  73. // 非0 失败,失败原因参见SM2算法描述
  74. //备注: 需要外部指定随机数,以统一加密结果。
  75. #ifdef CPU_AM335X
  76. static inline int SM2_Encrypt(unsigned char *pucDataInput,unsigned int uiInputLength,
  77. ECCrefPublicKey *pucPublicKey, unsigned char *pucRandom,
  78. unsigned int uiRandomLength, ECCCipher *pucEncData)
  79. {
  80. return 0;
  81. }
  82. #else
  83. int SM2_Encrypt(unsigned char *pucDataInput,unsigned int uiInputLength,
  84. ECCrefPublicKey *pucPublicKey, unsigned char *pucRandom,
  85. unsigned int uiRandomLength, ECCCipher *pucEncData);
  86. #endif
  87. #ifdef CPU_AM335X
  88. static inline void encrypt_epri(unsigned char* in, unsigned int in_len, unsigned char* out, unsigned int* out_len, unsigned int key, int type)
  89. {
  90. }
  91. #else
  92. void encrypt_epri(unsigned char* in, unsigned int in_len, unsigned char* out, unsigned int* out_len, unsigned int key, int type);
  93. #endif
  94. void IEC101_SM2(IEC101_DEF* pt);
  95. void IEC104_SM2(IEC104_DEF* pt);
  96. extern void InitSM2Key(void);
  97. int encrypt_test(void);
  98. #endif