123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include "chinaEncrypt.h"
- #include "CMBSMDLL.h"
- #include <stdio.h>
- #include <string.h>
- #ifdef RVC_OS_WIN
- #include <windows.h>
- #endif
- 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);
- }
|