/**
  * 描述:SM算法类,提供SM2, SM3和SM4加解密算法接口
  * 版本:V1.0.0.0
  * 日期:2023/08
  */
#ifndef SMCLASS_H
#define SMCLASS_H

#include <memory>
#include <QLibrary>
#include <QMutex>
#include "defs.h"
#ifndef _stdcall
#define _stdcall
#endif

// sm2
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);
// sm3
typedef void(_stdcall *psm3)(uint8_t*input, int len, uint8_t output[64]);
//typedef int(_stdcall *psm3_file)(char* path, unsigned char output[32]);
// sm4
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();
    /**
     * @brief 装载函数接口
     */
    void LoadFunc();
    /**
     * @brief 释放函数接口
     */
    void Release();

    /**
     * 描述:	创建sm2算法的密钥对
     * 参数:
     * ctx      上下文结构体,不能为空
     */
    bool Sm2CreateKey(sm2_context* ctx);
    /**
    * 描述:		获取上下文中未压缩的公钥信息
    * 参数:
    * ctx		上下文结构体
    * Key       二进制公钥信息,固定长度65 bytes
    */
    bool Sm2PublicKey(sm2_context* ctx, QString& key);
    /**
    * 描述:		获取上下文中ASN1编码的公钥信息
    * 参数:
    * ctx		上下文结构体
    * Key       二进制公钥信息
    */
    bool Sm2PublicKeyASN1(sm2_context* ctx, QString& key);
    /**
    * 描述:		从上下文中导出公钥信息到PEM文件
    * 参数:
    * ctx		上下文结构体
    * path      pem文件路径
    */
    bool Sm2PublicKey2Pem(sm2_context* ctx, const QString& path);
    /**
    * 描述:		设置公钥信息到上下文中
    * 参数:
    * ctx		上下文结构体
    * mode      0-Hex格式,1-ASN1编码格式,2-Pem数据
    * data      二进制公钥数据(16进制)
    */
    bool Sm2SetPublicKey(sm2_context* ctx, int mode, const QString& data);
    /**
    * 描述:		不压缩消息加密
    * 参数:
    * ctx		上下文结构体
    * mode      0-C1 C2 C3排列,1-C1 C3 C2排列
    * msg       待加密信息(最大长度255)
    * output    输出加密信息
    */
    bool Sm2EncryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
    /**
    * 描述:		不压缩消息解密
    * 参数:
    * ctx		上下文结构体
    * mode      0-C1 C2 C3排列,1-C1 C3 C2排列
    * msg       待解密信息
    * output    输出明文
    */
    bool Sm2DecryptRaw(sm2_context* ctx, int mode, const QString& msg, QString& output);
    /**
    * 描述:		ASN1编码消息加密
    * 参数:
    * ctx		上下文结构体
    * msg       待加密信息(最大长度255)
    * output    输出加密信息
    */
    bool Sm2EncryptASN1(sm2_context* ctx, const QString& msg, QString& output);
    /**
    * 描述:		ASN1编码消息解密
    * 参数:
    * ctx		上下文结构体
    * msg       待解密信息
    * output    输出明文
    */
    bool Sm2DecryptASN1(sm2_context* ctx, const QString& msg, QString& output);
    /**
    * 描述:		私钥签名
    * 参数:
    * ctx		上下文结构体
    * msg       待签名数据
    * sign      签名数据
    */
    bool Sm2Signature(sm2_context* ctx, const QString& msg, QString& sign);
    /**
    * 描述:		公钥验证签名
    * 参数:
    * ctx		上下文结构体
    * msg		待验证密文数据(Hex)
    * sign		签名数据
    * 返回值     true-验证通过,false-验证失败
    */
    bool Sm2VerifySign(sm2_context* ctx, const QString& msg, const QString& sign);

    /**
    * 描述:		计算输入数据input的256位哈希值
    * 参数:
    * input		输入数据
    * output	输出256位消息摘要
    */
    void Sm3Hash(const QString& input, QString& output);

    /**
    * 描述:		SM4 自定义加密
    * 参数:
    * key       自定义密钥
    * input		待加密数据
    * output	输出的密文数据
    * 返回值:
    * 输出数据长度,<=0为失败
    */
    int Sm4Encrypt(const QString& key, const QString& input, QString& output);
    /**
    * 描述:		SM4 自定义解密
    * 参数:
    * key       自定义密钥
    * input		待解密数据
    * output	输出的明文数据
    * 返回值:
    * 输出数据长度,<=0为失败
    */
    int Sm4Decrypt(const QString& key, const QString& input, QString& output);

    /**
    * 描述:		SM4产生随机密钥
    * 参数:
    * key		密钥数据,长度固定16 bytes
    */
    void Sm4RandKey(QString& key);
    /**
    * 描述:		SM4加密密钥扩展
    * 参数:
    * ctx		上下文结构体
    * key		原始128位密钥
    */
    void Sm4KeyEncrypt(sm4_context* ctx, const QString& key);
    /**
    * 描述:		SM4解密密钥扩展
    * 参数:
    * ctx		上下文结构体
    * key		原始128位密钥
    */
    void Sm4KeyDecrypt(sm4_context* ctx, const QString& key);
    /**
    * 描述:		SM4-ECB方式加密128bit
    * 参数:
    * ctx		上下文结构体
    * input		输入信息(pkcs7填充)
    * output	输出信息
    * 返回值:
    * int       密文长度
    */
    int Sm4EncryptEcb(sm4_context *ctx, const QString& input, QString& output);
    /**
    * 描述:		SM4-ECB方式解密128bit
    * 参数:
    * ctx		上下文结构体
    * input		输入信息(pkcs7填充)
    * output	输出信息
    * 返回值:
    * int       明文长度
    */
    int Sm4DecryptEcb(sm4_context *ctx, const QString& input, QString& output);
    /**
    * 描述:		SM4-CBC方式加密128bit
    * 参数:
    * ctx		上下文结构体
    * iv		初始化向量(IV),通常采用随机数值或者预设的固定值,长度16 bytes
    * input		输入信息(pkcs7填充)
    * output	输出信息
    * 返回值:
    * int       密文长度
    */
    int Sm4EncryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);
    /**
    * 描述:		SM4-CBC方式解密128bit
    * 参数:
    * ctx		上下文结构体
    * iv		初始化向量(IV),通常采用随机数值或者预设的固定值,长度16 bytes
    * input		输入信息(pkcs7填充)
    * output	输出信息
    * 返回值:
    * int       明文长度
    */
    int Sm4DecryptCbc(sm4_context *ctx, const QString& iv, const QString& input, QString& output);

    /* 返回其他进程的结果,防止curl库的干扰 */
    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;
    //psm3_file m_pfSm3File;

    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 // SMCLASS_H