123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
-
- #ifndef SMCLASS_H
- #define SMCLASS_H
- #include <memory>
- #include <QLibrary>
- #include <QMutex>
- #include "defs.h"
- #ifndef _stdcall
- #define _stdcall
- #endif
- typedef bool(_stdcall *psm2CreateKey)(sm2_context* ctx);
- typedef bool(_stdcall *psm2PublicKey)(sm2_context* ctx, uint8_t key[130]);
- typedef bool(_stdcall *psm2PublicKeyASN1)(sm2_context* ctx, uint8_t* key, size_t* keylen);
- typedef bool(_stdcall *psm2PublicKeyToPem)(sm2_context* ctx, const char* path, int len);
- typedef bool(_stdcall *psm2SetPublicKey)(sm2_context* ctx, int mode, const uint8_t* data, size_t len);
- typedef bool(_stdcall *psm2EncryptRaw)(sm2_context* ctx, int mode, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
- typedef bool(_stdcall *psm2DecryptRaw)(sm2_context* ctx, int mode, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
- typedef bool(_stdcall *psm2EncryptASN1)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
- typedef bool(_stdcall *psm2DecryptASN1)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* output, size_t* outlen);
- typedef bool(_stdcall *psm2Signature)(sm2_context* ctx, const uint8_t* msg, size_t len, uint8_t* sign, size_t* siglen);
- typedef bool(_stdcall *psm2VerifySign)(sm2_context* ctx, const uint8_t* msg, size_t len, const uint8_t* sign, size_t siglen);
- typedef void(_stdcall *psm3)(uint8_t*input, int len, uint8_t output[64]);
- typedef int (_stdcall *psm4Encrypt)(const char* key, size_t len, const char* input, size_t inlen, char* output, size_t* outlen);
- typedef int (_stdcall *psm4Decrypt)(const char* key, size_t len, const char* input, size_t inlen, char* output, size_t* outlen);
- typedef void(_stdcall *psm4RandKey)(uint8_t* key, size_t len);
- typedef void(_stdcall *psm4SetKeyEnc)(sm4_context* ctx, const uint8_t key[32]);
- typedef void(_stdcall *psm4SetKeyDec)(sm4_context* ctx, const uint8_t key[32]);
- typedef int(_stdcall *psm4EncryptEcb)(sm4_context *ctx, const uint8_t* input, int length, uint8_t* output);
- typedef int(_stdcall *psm4DecryptEcb)(sm4_context *ctx, const uint8_t* input, int length, uint8_t* output);
- typedef int(_stdcall *psm4EncryptCbc)(sm4_context *ctx, const uint8_t iv[32], const uint8_t* input, size_t len, uint8_t* output);
- typedef int(_stdcall *psm4DecryptCbc)(sm4_context *ctx, const uint8_t iv[32], const uint8_t* input, size_t len, uint8_t* output);
- class SMClass
- {
- private:
- using pUchar = std::unique_ptr<uint8_t[]>;
- SMClass();
- SMClass(const SMClass&) =delete;
- SMClass& operator=(const SMClass&) =delete;
- public:
- static SMClass* Instance();
- ~SMClass();
-
- void LoadFunc();
-
- void Release();
-
- bool Sm2CreateKey(sm2_context* ctx);
-
- bool Sm2PublicKey(sm2_context* ctx, QString& key);
-
- bool Sm2PublicKeyASN1(sm2_context* ctx, QString& key);
-
- bool Sm2PublicKey2Pem(sm2_context* ctx, const QString& path);
-
- bool Sm2SetPublicKey(sm2_context* ctx, int mode, const QString& data);
-
- bool Sm2EncryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
-
- bool Sm2DecryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
-
- bool Sm2EncryptASN1(sm2_context* ctx, const QString& msg, QString& output);
-
- bool Sm2DecryptASN1(sm2_context* ctx, const QString& msg, QString& output);
-
- bool Sm2Signature(sm2_context* ctx, const QString& msg, QString& sign);
-
- bool Sm2VerifySign(sm2_context* ctx, const QString& msg, const QString& sign);
-
- void Sm3Hash(const QString& input, QString& output);
-
- int Sm4Encrypt(const QString& key, const QString& input, QString& output);
-
- int Sm4Decrypt(const QString& key, const QString& input, QString& output);
-
- void Sm4RandKey(QString& key);
-
- void Sm4KeyEncrypt(sm4_context* ctx, const QString& key);
-
- void Sm4KeyDecrypt(sm4_context* ctx, const QString& key);
-
- int Sm4EncryptEcb(sm4_context *ctx, const QString& input, QString& output);
-
- int Sm4DecryptEcb(sm4_context *ctx, const QString& input, QString& output);
-
- int Sm4EncryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);
-
- int Sm4DecryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);
-
- void ExeSm3Hash(const QString& input, QString& out);
- int ExeSm4Encrypt(const QString& key, const QString& input, QString& out);
- int ExeSm4Decrypt(const QString& key, const QString& input, QString& out);
- void sm3(const QString& input, QString& out);
- int sm4Enc(const QString& key, const QString& input, QString& out);
- int sm4Dec(const QString& key, const QString& input, QString& out);
- private:
- QString uchartoHex(uint8_t* input, size_t size);
- std::unique_ptr<uint8_t[]> hextoUchar(const QString& hexString, size_t& size);
- void readResultAndDelete(const QString& fileName, QString& out);
- private:
- static SMClass* s_inst;
- static QMutex s_mtx;
- QLibrary* m_plib;
- psm2CreateKey m_pfCreateK;
- psm2PublicKey m_pfPubKey;
- psm2PublicKeyASN1 m_pfPubKeyASN1;
- psm2PublicKeyToPem m_pfPubKey2Pem;
- psm2SetPublicKey m_pfSetPubKey;
- psm2EncryptRaw m_pfEncRaw;
- psm2DecryptRaw m_pfDecRaw;
- psm2EncryptASN1 m_pfEncrypt;
- psm2DecryptASN1 m_pfDecrypt;
- psm2Signature m_pfSignature;
- psm2VerifySign m_pfVerifySign;
- psm3 m_pfSm3;
-
- psm4Encrypt m_pfSm4Encrypt;
- psm4Decrypt m_pfSm4Decrypt;
- psm4RandKey m_pfSm4RandKey;
- psm4SetKeyEnc m_pfSm4Enc;
- psm4SetKeyDec m_pfSm4Dec;
- psm4EncryptEcb m_pfSm4EncEcb;
- psm4DecryptEcb m_pfSm4DecEcb;
- psm4EncryptCbc m_pfSm4EncCbc;
- psm4DecryptCbc m_pfSm4DecCbc;
- };
- #endif
|