//+------------------------------------------------------------------+ //| 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 { //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //-- tipos de padders para firmar enum ENUM_RSA_SING_PADDER { RSA_SING_PADDER_PSS, RSA_SING_PADDER_PKCS1V15 }; //--- const uchar g_rsa_oid[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class PssParams { public: PssParams(uchar& _salt[], const int _salt_len) : salt_len(_salt_len) { ArraySwap(salt, _salt);} PssParams() : salt_len(0) {} ~PssParams(void) {} //--- int salt_len; uchar salt[]; }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaKey { protected: //--- Parametros //- Desencryptado TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH m_cip_fhash; // tipo de funcoin para el oaep fncion HASH //- Firmado 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 //-- 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) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaPrivateKey : public CRsaKey { private: // general BigUInteger m_n; // producto de p y q ulong m_e; // exponente int m_key_sbits; // tamaño de la clave en bits // 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) {} //--- Desencvryptar bool Decrypt(const uchar& chiper_text[], uchar& out[]); //--- Firmar bool Firmar(const uchar& raw[], uchar& out[]); //--- foramtos de serilizacion }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CRsaPublicKey : public CRsaKey { private: // general BigUInteger m_n; // producto de p y q ulong m_e; // exponente int m_key_sbits; // tamaño de la clave en bits public: CRsaPublicKey(BigUInteger& n, ulong e, const int ksb); ~CRsaPublicKey(void) {} //--- cifra un mensaje con la clave publica // raw = texto tal cual // out = salida bool Cifrar(const uchar& raw[], uchar& out[]); //--- // verifica una firma procedente de un mensaje cifrado bool Verify(const uchar& firma[], const uchar& data[]); //--- Serilizacion // serilizacion int ToSubjectPublicKeyInfo(uchar& out[], int o_s); // formateo (Final PEM \ DER) }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CRsaPublicKey::CRsaPublicKey(BigUInteger &n, ulong e, const int ksb) // nota aqui le "robamos" a n sus daots (nada de copies) : m_n(n.m_v, n.m_v_s), m_e(e), m_key_sbits(ksb) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CRsaPublicKey::ToSubjectPublicKeyInfo(uchar &out[], int o_s) { m_n.ToBytesBE() } } #endif // CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH //+------------------------------------------------------------------+