chinaEncrypt.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "chinaEncrypt.h"
  2. #include "CMBSMDLL.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #ifdef RVC_OS_WIN
  6. #include <windows.h>
  7. #endif
  8. int SM3Hash(unsigned char* pData, int nLen, unsigned char hash[32])
  9. {
  10. return CMBSM3Digest(pData, nLen, hash);
  11. }
  12. char* GetSMVersion()
  13. {
  14. return CMBSMGetVersion();
  15. }
  16. int CreateSM2KeyPair(unsigned char* pPubKeyBuf, int* pPubKeyBufLen, unsigned char* pPriKeyBuf, int* pPriKeyBufLen)
  17. {
  18. return CMBSM2KeyGen(pPubKeyBuf, pPubKeyBufLen, pPriKeyBuf, pPriKeyBufLen);
  19. }
  20. int EncWithSM2PubKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPubKey, int nKeyLen)
  21. {
  22. return CMBSM2Encrypt(pPubKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  23. }
  24. int DecWithSM2PriKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPriKey, int nKeyLen)
  25. {
  26. return CMBSM2Decrypt(pPriKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  27. }
  28. int SM2SignWithSM3(unsigned char* privkey, int privkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int* sig_len)
  29. {
  30. return CMBSM2SignWithSM3(privkey, privkey_len, msg, msg_len, signature, sig_len);
  31. }
  32. int SM2VerifyWithSM3(unsigned char* pubkey, int pubkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int sig_len)
  33. {
  34. return CMBSM2VerifyWithSM3(pubkey, pubkey_len, msg, msg_len, signature, sig_len);
  35. }
  36. void GenerateSM4_ECBkey(char *keyStr, int keyStr_len, unsigned char key[16])
  37. {
  38. unsigned char tempKey[32] = { 0 };
  39. if (keyStr != NULL)
  40. {
  41. SM3Hash((unsigned char *)keyStr, keyStr_len > 32 ? 32 : keyStr_len, tempKey);
  42. memcpy(key, tempKey, 16);
  43. }
  44. else
  45. memset(key, 0, 16);
  46. //Aschex_2_bcdhex(tempKey, 32, key);
  47. }
  48. int EncStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len)
  49. {
  50. unsigned char key[17] = { 0 };
  51. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  52. return CMBSM4EncryptWithECB(key, input, length, output, output_len);
  53. }
  54. int DecStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len)
  55. {
  56. unsigned char key[17] = { 0 };
  57. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  58. return CMBSM4DecryptWithECB(key, input, length, output, output_len);
  59. }
  60. int EncWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  61. {
  62. return CMBSM4EncryptWithECB(key, input, length, output, output_len);
  63. }
  64. int DecWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  65. {
  66. return CMBSM4DecryptWithECB(key, input, length, output, output_len);
  67. }
  68. int EncStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  69. {
  70. unsigned char key[17] = { 0 };
  71. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  72. return CMBSM4EncryptWithCBC(key, iv, input, length, output, output_len);
  73. }
  74. int DecStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  75. {
  76. unsigned char key[17] = { 0 };
  77. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  78. return CMBSM4DecryptWithCBC(key, iv, input, length, output, output_len);
  79. }
  80. int SM3File(char* file, unsigned char hash[32])
  81. {
  82. return CMBSM3FileDigest(file, hash);
  83. }