smclass.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * 描述:SM算法类,提供SM2, SM3和SM4加解密算法接口
  3. * 版本:V1.0.0.0
  4. * 日期:2023/08
  5. */
  6. #ifndef SMCLASS_H
  7. #define SMCLASS_H
  8. #include <memory>
  9. #include <QLibrary>
  10. #include <QMutex>
  11. #include "defs.h"
  12. #ifndef _stdcall
  13. #define _stdcall
  14. #endif
  15. // sm2
  16. typedef bool(_stdcall *psm2CreateKey)(sm2_context* ctx);
  17. typedef bool(_stdcall *psm2PublicKey)(sm2_context* ctx, uint8_t key[130]);
  18. typedef bool(_stdcall *psm2PublicKeyASN1)(sm2_context* ctx, uint8_t* key, size_t* keylen);
  19. typedef bool(_stdcall *psm2PublicKeyToPem)(sm2_context* ctx, const char* path, int len);
  20. typedef bool(_stdcall *psm2SetPublicKey)(sm2_context* ctx, int mode, const uint8_t* data, size_t len);
  21. typedef bool(_stdcall *psm2EncryptRaw)(sm2_context* ctx, int mode, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
  22. typedef bool(_stdcall *psm2DecryptRaw)(sm2_context* ctx, int mode, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
  23. typedef bool(_stdcall *psm2EncryptASN1)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
  24. typedef bool(_stdcall *psm2DecryptASN1)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
  25. typedef bool(_stdcall *psm2Signature)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* sign, size_t* siglen);
  26. typedef bool(_stdcall *psm2VerifySign)(sm2_context* ctx, const uint8_t* msg, size_t len, const uint8_t* sign, size_t siglen);
  27. // sm3
  28. typedef void(_stdcall *psm3)(uint8_t*input, int len, uint8_t output[64]);
  29. //typedef int(_stdcall *psm3_file)(char* path, unsigned char output[32]);
  30. // sm4
  31. typedef int (_stdcall *psm4Encrypt)(const char* key, size_t len, const char* input, size_t inlen, char* output, size_t* outlen);
  32. typedef int (_stdcall *psm4Decrypt)(const char* key, size_t len, const char* input, size_t inlen, char* output, size_t* outlen);
  33. typedef void(_stdcall *psm4RandKey)(uint8_t* key, size_t len);
  34. typedef void(_stdcall *psm4SetKeyEnc)(sm4_context* ctx, const uint8_t key[32]);
  35. typedef void(_stdcall *psm4SetKeyDec)(sm4_context* ctx, const uint8_t key[32]);
  36. typedef int(_stdcall *psm4EncryptEcb)(sm4_context *ctx, const uint8_t* input, int length, uint8_t* output);
  37. typedef int(_stdcall *psm4DecryptEcb)(sm4_context *ctx, const uint8_t* input, int length, uint8_t* output);
  38. typedef int(_stdcall *psm4EncryptCbc)(sm4_context *ctx, const uint8_t iv[32], const uint8_t* input, size_t len, uint8_t* output);
  39. typedef int(_stdcall *psm4DecryptCbc)(sm4_context *ctx, const uint8_t iv[32], const uint8_t* input, size_t len, uint8_t* output);
  40. class SMClass
  41. {
  42. private:
  43. using pUchar = std::unique_ptr<uint8_t[]>;
  44. SMClass();
  45. SMClass(const SMClass&) =delete;
  46. SMClass& operator=(const SMClass&) =delete;
  47. public:
  48. static SMClass* Instance();
  49. ~SMClass();
  50. /**
  51. * @brief 装载函数接口
  52. */
  53. void LoadFunc();
  54. /**
  55. * @brief 释放函数接口
  56. */
  57. void Release();
  58. /**
  59. * 描述: 创建sm2算法的密钥对
  60. * 参数:
  61. * ctx 上下文结构体,不能为空
  62. */
  63. bool Sm2CreateKey(sm2_context* ctx);
  64. /**
  65. * 描述: 获取上下文中未压缩的公钥信息
  66. * 参数:
  67. * ctx 上下文结构体
  68. * Key 二进制公钥信息,固定长度65 bytes
  69. */
  70. bool Sm2PublicKey(sm2_context* ctx, QString& key);
  71. /**
  72. * 描述: 获取上下文中ASN1编码的公钥信息
  73. * 参数:
  74. * ctx 上下文结构体
  75. * Key 二进制公钥信息
  76. */
  77. bool Sm2PublicKeyASN1(sm2_context* ctx, QString& key);
  78. /**
  79. * 描述: 从上下文中导出公钥信息到PEM文件
  80. * 参数:
  81. * ctx 上下文结构体
  82. * path pem文件路径
  83. */
  84. bool Sm2PublicKey2Pem(sm2_context* ctx, const QString& path);
  85. /**
  86. * 描述: 设置公钥信息到上下文中
  87. * 参数:
  88. * ctx 上下文结构体
  89. * mode 0-Hex格式,1-ASN1编码格式,2-Pem数据
  90. * data 二进制公钥数据(16进制)
  91. */
  92. bool Sm2SetPublicKey(sm2_context* ctx, int mode, const QString& data);
  93. /**
  94. * 描述: 不压缩消息加密
  95. * 参数:
  96. * ctx 上下文结构体
  97. * mode 0-C1 C2 C3排列,1-C1 C3 C2排列
  98. * msg 待加密信息(最大长度255)
  99. * output 输出加密信息
  100. */
  101. bool Sm2EncryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
  102. /**
  103. * 描述: 不压缩消息解密
  104. * 参数:
  105. * ctx 上下文结构体
  106. * mode 0-C1 C2 C3排列,1-C1 C3 C2排列
  107. * msg 待解密信息
  108. * output 输出明文
  109. */
  110. bool Sm2DecryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
  111. /**
  112. * 描述: ASN1编码消息加密
  113. * 参数:
  114. * ctx 上下文结构体
  115. * msg 待加密信息(最大长度255)
  116. * output 输出加密信息
  117. */
  118. bool Sm2EncryptASN1(sm2_context* ctx, const QString& msg, QString& output);
  119. /**
  120. * 描述: ASN1编码消息解密
  121. * 参数:
  122. * ctx 上下文结构体
  123. * msg 待解密信息
  124. * output 输出明文
  125. */
  126. bool Sm2DecryptASN1(sm2_context* ctx, const QString& msg, QString& output);
  127. /**
  128. * 描述: 私钥签名
  129. * 参数:
  130. * ctx 上下文结构体
  131. * msg 待签名数据
  132. * sign 签名数据
  133. */
  134. bool Sm2Signature(sm2_context* ctx, const QString& msg, QString& sign);
  135. /**
  136. * 描述: 公钥验证签名
  137. * 参数:
  138. * ctx 上下文结构体
  139. * msg 待验证密文数据(Hex)
  140. * sign 签名数据
  141. * 返回值 true-验证通过,false-验证失败
  142. */
  143. bool Sm2VerifySign(sm2_context* ctx, const QString& msg, const QString& sign);
  144. /**
  145. * 描述: 计算输入数据input的256位哈希值
  146. * 参数:
  147. * input 输入数据
  148. * output 输出256位消息摘要
  149. */
  150. void Sm3Hash(const QString& input, QString& output);
  151. /**
  152. * 描述: SM4 自定义加密
  153. * 参数:
  154. * key 自定义密钥
  155. * input 待加密数据
  156. * output 输出的密文数据
  157. * 返回值:
  158. * 输出数据长度,<=0为失败
  159. */
  160. int Sm4Encrypt(const QString& key, const QString& input, QString& output);
  161. /**
  162. * 描述: SM4 自定义解密
  163. * 参数:
  164. * key 自定义密钥
  165. * input 待解密数据
  166. * output 输出的明文数据
  167. * 返回值:
  168. * 输出数据长度,<=0为失败
  169. */
  170. int Sm4Decrypt(const QString& key, const QString& input, QString& output);
  171. /**
  172. * 描述: SM4产生随机密钥
  173. * 参数:
  174. * key 密钥数据,长度固定16 bytes
  175. */
  176. void Sm4RandKey(QString& key);
  177. /**
  178. * 描述: SM4加密密钥扩展
  179. * 参数:
  180. * ctx 上下文结构体
  181. * key 原始128位密钥
  182. */
  183. void Sm4KeyEncrypt(sm4_context* ctx, const QString& key);
  184. /**
  185. * 描述: SM4解密密钥扩展
  186. * 参数:
  187. * ctx 上下文结构体
  188. * key 原始128位密钥
  189. */
  190. void Sm4KeyDecrypt(sm4_context* ctx, const QString& key);
  191. /**
  192. * 描述: SM4-ECB方式加密128bit
  193. * 参数:
  194. * ctx 上下文结构体
  195. * input 输入信息(pkcs7填充)
  196. * output 输出信息
  197. * 返回值:
  198. * int 密文长度
  199. */
  200. int Sm4EncryptEcb(sm4_context *ctx, const QString& input, QString& output);
  201. /**
  202. * 描述: SM4-ECB方式解密128bit
  203. * 参数:
  204. * ctx 上下文结构体
  205. * input 输入信息(pkcs7填充)
  206. * output 输出信息
  207. * 返回值:
  208. * int 明文长度
  209. */
  210. int Sm4DecryptEcb(sm4_context *ctx, const QString& input, QString& output);
  211. /**
  212. * 描述: SM4-CBC方式加密128bit
  213. * 参数:
  214. * ctx 上下文结构体
  215. * iv 初始化向量(IV),通常采用随机数值或者预设的固定值,长度16 bytes
  216. * input 输入信息(pkcs7填充)
  217. * output 输出信息
  218. * 返回值:
  219. * int 密文长度
  220. */
  221. int Sm4EncryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);
  222. /**
  223. * 描述: SM4-CBC方式解密128bit
  224. * 参数:
  225. * ctx 上下文结构体
  226. * iv 初始化向量(IV),通常采用随机数值或者预设的固定值,长度16 bytes
  227. * input 输入信息(pkcs7填充)
  228. * output 输出信息
  229. * 返回值:
  230. * int 明文长度
  231. */
  232. int Sm4DecryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);
  233. /* 返回其他进程的结果,防止curl库的干扰 */
  234. void ExeSm3Hash(const QString& input, QString& out);
  235. int ExeSm4Encrypt(const QString& key, const QString& input, QString& out);
  236. int ExeSm4Decrypt(const QString& key, const QString& input, QString& out);
  237. void sm3(const QString& input, QString& out);
  238. int sm4Enc(const QString& key, const QString& input, QString& out);
  239. int sm4Dec(const QString& key, const QString& input, QString& out);
  240. private:
  241. QString uchartoHex(uint8_t* input, size_t size);
  242. std::unique_ptr<uint8_t[]> hextoUchar(const QString& hexString, size_t& size);
  243. void readResultAndDelete(const QString& fileName, QString& out);
  244. private:
  245. static SMClass* s_inst;
  246. static QMutex s_mtx;
  247. QLibrary* m_plib;
  248. psm2CreateKey m_pfCreateK;
  249. psm2PublicKey m_pfPubKey;
  250. psm2PublicKeyASN1 m_pfPubKeyASN1;
  251. psm2PublicKeyToPem m_pfPubKey2Pem;
  252. psm2SetPublicKey m_pfSetPubKey;
  253. psm2EncryptRaw m_pfEncRaw;
  254. psm2DecryptRaw m_pfDecRaw;
  255. psm2EncryptASN1 m_pfEncrypt;
  256. psm2DecryptASN1 m_pfDecrypt;
  257. psm2Signature m_pfSignature;
  258. psm2VerifySign m_pfVerifySign;
  259. psm3 m_pfSm3;
  260. //psm3_file m_pfSm3File;
  261. psm4Encrypt m_pfSm4Encrypt;
  262. psm4Decrypt m_pfSm4Decrypt;
  263. psm4RandKey m_pfSm4RandKey;
  264. psm4SetKeyEnc m_pfSm4Enc;
  265. psm4SetKeyDec m_pfSm4Dec;
  266. psm4EncryptEcb m_pfSm4EncEcb;
  267. psm4DecryptEcb m_pfSm4DecEcb;
  268. psm4EncryptCbc m_pfSm4EncCbc;
  269. psm4DecryptCbc m_pfSm4DecCbc;
  270. };
  271. #endif // SMCLASS_H