//+------------------------------------------------------------------+ //| H.mqh | //| Copyright 2026, Niquel Mendoza | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, Niquel Mendoza" #property link "https://www.mql5.com" #property strict #ifndef CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH #define CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #include "..\\Def.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaKey { protected: //--- // general BigUInteger m_n; // producto de p y q ulong m_e; // exponente int m_total_sbits; // tamaño de (n final mod) en bits int m_total_sbytes; // tamaño de (n final mod) en bytes //--- Datos //- Desencryptado | Encrypt TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH m_cip_fhash; // tipo de funcoin para el oaep fncion HASH uchar m_cip_random_bytes[CRYPTOBYLEO_HASHFUNC_FIXED_MAX_OL]; //- Firmado | Veify ENUM_RSA_SING_PADDER m_sing_padder; // padder del firmado TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH m_sing_fhash; // tipo de hasher para el mensaje PssParams* m_sing_data; // parametros para el firmado public: CRsaKey(void); ~CRsaKey(void) {} //--- Getter \ Setter //- key size __forceinline int TotalBits() const { return m_total_sbits; } __forceinline int TotalBytes() const { return m_total_sbytes; } //- Sing (Firmado\Verficacion) // padder void SingPadder(ENUM_RSA_SING_PADDER p) { m_sing_padder = p; } __forceinline ENUM_RSA_SING_PADDER SingPadder() const { return m_sing_padder; } // fhash void SingFHash(TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH fhash) { m_sing_fhash = fhash; } __forceinline TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH SingFHash() const { return m_sing_fhash; } // parametros especifioos del firmado void SingParams(void* params) { m_sing_data = params; } //- Cifrado\Descifrado void CipFHash(TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH fhash) { m_cip_fhash = fhash; } __forceinline TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH CipFHash() const { return m_cip_fhash; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CRsaKey::CRsaKey() : m_cip_fhash(CCryptoHashMeta::TSN_CRYPTO_HASH_TYPE_SHA256), m_sing_fhash(CCryptoHashMeta::TSN_CRYPTO_HASH_TYPE_SHA256), m_sing_padder(RSA_SING_PADDER_PSS), m_sing_data(NULL), //--- m_n(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_e(0), m_total_sbits(0), m_total_sbytes(0) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaPrivateKey : public CRsaKey { private: //--- // privado BigUInteger m_d; // exponent privado // CRT BigUInteger m_p; // p primo BigUInteger m_q; // q primo BigUInteger m_dp; BigUInteger m_dq; BigUInteger m_qinv; public: CRsaPrivateKey(void); ~CRsaPrivateKey(void) {} //--- carga desde void Load(BigUInteger& n, BigUInteger& d, BigUInteger& p, BigUInteger& q, BigUInteger& dp, BigUInteger& dq, BigUInteger& qinv, const ulong e, int totaln_bits); //--- Desencvryptar bool Decrypt(const uchar& chiper_text[], uchar& out[]); //--- Firmar bool Firmar(const uchar& raw[], uchar& out[]); //--- Serializacion }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CRsaPrivateKey::CRsaPrivateKey(void) : //--- m_d(BigUInteger::EMPTY_NUMBER_EXACT, 0), //-- crt m_p(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_q(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_dp(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_dq(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_qinv(BigUInteger::EMPTY_NUMBER_EXACT, 0) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaPublicKey : public CRsaKey { private: // temp BigUInteger m_base; public: CRsaPublicKey() {} ~CRsaPublicKey() {} //--- carga desde void Load(BigUInteger& n, const ulong e, int totaln_bits); //--- cifra un mensaje con la clave publica // raw = texto tal cual // out = salida // TRandomFunc = Funcion que genera el random template bool Cifrar(const uchar& raw[], uchar& out[]); //--- // verifica una firma procedente de un mensaje cifrado bool Verify(const uchar& firma[], const uchar& data[]); //--- Serilizacion X.509 / PKCS#8 // (DER) int ToSubjectPublicKeyInfo(uchar &buf[], int w = 0) const; // (PEM) int ToSubjectPublicKeyInfoPem(uchar& buf[], int w = 0) const; //--- Cargado // DER bool LoadSubjectPublicKeyInfo(const uchar& buf[], int r = 0); // PEM bool LoadSubjectPublicKeyInfoPem(const uchar& buf[], int r = 0); }; //--- } #endif // CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH //+------------------------------------------------------------------+