#include "chinaEncrypt.h" #include "CMBSMDLL.h" #include #include int SM3Hash(unsigned char* pData, int nLen, unsigned char hash[32]) { return CMBSM3Digest(pData, nLen, hash); } char* GetSMVersion() { return CMBSMGetVersion(); } int CreateSM2KeyPair(unsigned char* pPubKeyBuf, int* pPubKeyBufLen, unsigned char* pPriKeyBuf, int* pPriKeyBufLen) { return CMBSM2KeyGen(pPubKeyBuf, pPubKeyBufLen, pPriKeyBuf, pPriKeyBufLen); } int EncWithSM2PubKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPubKey, int nKeyLen) { return CMBSM2Encrypt(pPubKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen); } int DecWithSM2PriKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPriKey, int nKeyLen) { return CMBSM2Decrypt(pPriKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen); } int SM2SignWithSM3(unsigned char* privkey, int privkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int* sig_len) { return CMBSM2SignWithSM3(privkey, privkey_len, msg, msg_len, signature, sig_len); } int SM2VerifyWithSM3(unsigned char* pubkey, int pubkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int sig_len) { return CMBSM2VerifyWithSM3(pubkey, pubkey_len, msg, msg_len, signature, sig_len); } void GenerateSM4_ECBkey(char *keyStr, int keyStr_len, unsigned char key[16]) { unsigned char tempKey[32] = { 0 }; if (keyStr != NULL) { SM3Hash((unsigned char *)keyStr, keyStr_len > 32 ? 32 : keyStr_len, tempKey); memcpy(key, tempKey, 16); } else memset(key, 0, 16); //Aschex_2_bcdhex(tempKey, 32, key); } int EncStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len) { unsigned char key[17] = { 0 }; GenerateSM4_ECBkey(keyStr, keyStr_len, key); return CMBSM4EncryptWithECB(key, input, length, output, output_len); } int DecStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len) { unsigned char key[17] = { 0 }; GenerateSM4_ECBkey(keyStr, keyStr_len, key); return CMBSM4DecryptWithECB(key, input, length, output, output_len); } int EncWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len) { return CMBSM4EncryptWithECB(key, input, length, output, output_len); } int DecWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len) { return CMBSM4DecryptWithECB(key, input, length, output, output_len); } int EncStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len) { unsigned char key[17] = { 0 }; GenerateSM4_ECBkey(keyStr, keyStr_len, key); return CMBSM4EncryptWithCBC(key, iv, input, length, output, output_len); } int DecStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len) { unsigned char key[17] = { 0 }; GenerateSM4_ECBkey(keyStr, keyStr_len, key); return CMBSM4DecryptWithCBC(key, iv, input, length, output, output_len); } int SM3File(char* file, unsigned char hash[32]) { return CMBSM3FileDigest(file, hash); }