2026-08-23 08:35:14 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| 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
|
| | | {
|
2026-08-23 19:36:33 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-08-29 21:53:22 -05:00 | | | class CRsaKey
|
2026-08-24 09:57:30 -05:00 | | | {
|
2026-08-29 21:53:22 -05:00 | | | protected:
|
2026-08-24 09:57:30 -05:00 | | | //---
|
2026-08-29 21:53:22 -05:00 | | | // general
|
2026-08-30 10:39:49 -05:00 | | | 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
|
2026-08-24 09:57:30 -05:00 | | |
|
2026-08-26 22:23:21 -05:00 | | |
|
2026-08-29 21:53:22 -05:00 | | | //--- Datos
|
| | | //- Desencryptado | Encrypt
|
2026-08-24 09:57:30 -05:00 | | | TSN::CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH m_cip_fhash; // tipo de funcoin para el oaep fncion HASH
|
2026-08-29 21:53:22 -05:00 | | | uchar m_cip_random_bytes[CRYPTOBYLEO_HASHFUNC_FIXED_MAX_OL];
|
2026-08-24 09:57:30 -05:00 | | |
|
2026-08-29 21:53:22 -05:00 | | | //- Firmado | Veify
|
2026-08-24 09:57:30 -05:00 | | | 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
|
2026-08-29 21:53:22 -05:00 | | | //- key size
|
2026-08-30 10:39:49 -05:00 | | | __forceinline int TotalBits() const { return m_total_sbits; }
|
| | | __forceinline int TotalBytes() const { return m_total_sbytes; }
|
2026-08-29 21:53:22 -05:00 | | |
|
| | | //- Sing (Firmado\Verficacion)
|
2026-08-24 09:57:30 -05:00 | | | // 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; }
|
| | |
|
2026-08-29 21:53:22 -05:00 | | | //- Cifrado\Descifrado
|
2026-08-24 09:57:30 -05:00 | | | 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),
|
2026-08-29 21:53:22 -05:00 | | | m_sing_padder(RSA_SING_PADDER_PSS), m_sing_data(NULL),
|
| | |
|
| | | //---
|
2026-08-30 10:39:49 -05:00 | | | m_n(BigUInteger::EMPTY_NUMBER_EXACT, 0), m_e(0), m_total_sbits(0), m_total_sbytes(0)
|
2026-08-24 09:57:30 -05:00 | | | {
|
| | | }
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | class CRsaPrivateKey : public CRsaKey
|
2026-08-23 08:35:14 -05:00 | | | {
|
2026-08-23 19:36:33 -05:00 | | | private:
|
2026-08-29 21:53:22 -05:00 | | | //---
|
2026-08-26 22:23:21 -05:00 | | | // privado
|
| | | BigUInteger m_d; // exponent privado
|
2026-08-23 19:36:33 -05:00 | | |
|
2026-08-24 09:57:30 -05:00 | | | // CRT
|
2026-08-26 22:23:21 -05:00 | | | BigUInteger m_p; // p primo
|
| | | BigUInteger m_q; // q primo
|
2026-08-24 09:57:30 -05:00 | | | BigUInteger m_dp;
|
| | | BigUInteger m_dq;
|
| | | BigUInteger m_qinv;
|
| | |
|
| | |
|
| | |
|
2026-08-23 08:35:14 -05:00 | | | public:
|
2026-08-29 20:40:52 -05:00 | | | CRsaPrivateKey(void);
|
2026-08-23 08:35:14 -05:00 | | | ~CRsaPrivateKey(void) {}
|
| | |
|
2026-08-29 20:40:52 -05:00 | | | //--- carga desde
|
| | | void Load(BigUInteger& n, BigUInteger& d, BigUInteger& p, BigUInteger& q,
|
2026-08-30 10:39:49 -05:00 | | | BigUInteger& dp, BigUInteger& dq, BigUInteger& qinv, const ulong e, int totaln_bits);
|
2026-08-29 20:40:52 -05:00 | | |
|
2026-08-24 09:57:30 -05:00 | | | //--- Desencvryptar
|
| | | bool Decrypt(const uchar& chiper_text[], uchar& out[]);
|
2026-08-23 08:35:14 -05:00 | | |
|
2026-08-24 09:57:30 -05:00 | | | //--- Firmar
|
| | | bool Firmar(const uchar& raw[], uchar& out[]);
|
2026-08-26 22:23:21 -05:00 | | |
|
| | |
|
2026-08-30 10:39:49 -05:00 | | | //--- Serializacion
|
| | |
|
2026-08-29 20:40:52 -05:00 | | | };
|
2026-08-26 22:23:21 -05:00 | | |
|
2026-08-29 20:40:52 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | CRsaPrivateKey::CRsaPrivateKey(void)
|
2026-08-29 21:53:22 -05:00 | | | :
|
| | | //---
|
| | | m_d(BigUInteger::EMPTY_NUMBER_EXACT, 0),
|
| | |
|
2026-08-29 20:40:52 -05:00 | | | //-- crt
|
2026-08-29 21:53:22 -05:00 | | | 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)
|
2026-08-29 20:40:52 -05:00 | | | {
|
| | | }
|
2026-08-26 22:23:21 -05:00 | | |
|
2026-08-23 08:35:14 -05:00 | | |
|
| | |
|
2026-08-23 19:36:33 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-08-24 09:57:30 -05:00 | | | class CRsaPublicKey : public CRsaKey
|
2026-08-23 19:36:33 -05:00 | | | {
|
| | | private:
|
2026-08-29 21:53:22 -05:00 | | | // temp
|
| | | BigUInteger m_base;
|
2026-08-26 22:23:21 -05:00 | | |
|
2026-08-23 19:36:33 -05:00 | | |
|
| | | public:
|
2026-08-29 21:53:22 -05:00 | | | CRsaPublicKey() {}
|
| | | ~CRsaPublicKey() {}
|
2026-08-23 19:36:33 -05:00 | | |
|
2026-08-29 20:40:52 -05:00 | | | //--- carga desde
|
2026-08-30 10:39:49 -05:00 | | | void Load(BigUInteger& n, const ulong e, int totaln_bits);
|
2026-08-29 20:40:52 -05:00 | | |
|
2026-08-23 19:36:33 -05:00 | | | //--- cifra un mensaje con la clave publica
|
| | | // raw = texto tal cual
|
| | | // out = salida
|
2026-08-30 10:39:49 -05:00 | | | // TRandomFunc = Funcion que genera el random
|
| | | template <typename TRandomFunc>
|
2026-08-23 19:36:33 -05:00 | | | bool Cifrar(const uchar& raw[], uchar& out[]);
|
| | |
|
| | | //---
|
| | | // verifica una firma procedente de un mensaje cifrado
|
2026-08-26 13:35:15 -05:00 | | | bool Verify(const uchar& firma[], const uchar& data[]);
|
2026-08-26 22:23:21 -05:00 | | |
|
2026-08-31 08:11:57 -05:00 | | | //--- Serilizacion X.509 / PKCS#8
|
| | | // (DER)
|
2026-08-29 17:17:26 -05:00 | | | int ToSubjectPublicKeyInfo(uchar &buf[], int w = 0) const;
|
2026-08-29 20:40:52 -05:00 | | |
|
2026-08-31 08:11:57 -05:00 | | | // (PEM)
|
2026-08-29 20:40:52 -05:00 | | | int ToSubjectPublicKeyInfoPem(uchar& buf[], int w = 0) const;
|
2026-08-31 08:11:57 -05:00 | | |
|
| | | //--- Cargado
|
| | | // DER
|
| | | bool LoadSubjectPublicKeyInfo(const uchar& buf[], int r = 0);
|
| | | // PEM
|
| | | bool LoadSubjectPublicKeyInfoPem(const uchar& buf[], int r = 0);
|
2026-08-23 19:36:33 -05:00 | | | };
|
| | |
|
2026-08-29 20:40:52 -05:00 | | | //---
|
2026-08-23 08:35:14 -05:00 | | | }
|
| | | #endif // CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH
|
| | | //+------------------------------------------------------------------+
|