CryptoByLeo/Src/RSA/Keys/H.mqh

196 lines
6.6 KiB
MQL5
Raw Permalink Normal View History

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"
2026-08-23 19:36:33 -05:00
2026-08-23 08:35:14 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
2026-08-23 19:36:33 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-08-24 09:57:30 -05:00
//-- tipos de padders para firmar
2026-08-23 19:36:33 -05:00
enum ENUM_RSA_SING_PADDER
{
RSA_SING_PADDER_PSS,
RSA_SING_PADDER_PKCS1V15
};
2026-08-26 22:23:21 -05:00
//---
const uchar g_rsa_oid[9] =
{
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01
};
2026-08-23 08:35:14 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-08-24 09:57:30 -05:00
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[];
};
2026-08-26 22:23:21 -05:00
2026-08-24 09:57:30 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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
2026-08-23 08:35:14 -05:00
{
2026-08-23 19:36:33 -05:00
private:
2026-08-26 22:23:21 -05:00
// 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
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:
CRsaPrivateKey(void) {}
~CRsaPrivateKey(void) {}
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
//--- foramtos de serilizacion
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-26 22:23:21 -05:00
// general
BigUInteger m_n; // producto de p y q
ulong m_e; // exponente
int m_key_sbits; // tamaño de la clave en bits
2026-08-23 19:36:33 -05:00
public:
2026-08-26 22:23:21 -05:00
CRsaPublicKey(BigUInteger& n, ulong e, const int ksb);
2026-08-23 19:36:33 -05:00
~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
2026-08-26 13:35:15 -05:00
bool Verify(const uchar& firma[], const uchar& data[]);
2026-08-26 22:23:21 -05:00
//--- Serilizacion
// serilizacion
int ToSubjectPublicKeyInfo(uchar& out[], int o_s);
// formateo (Final PEM \ DER)
2026-08-23 19:36:33 -05:00
};
2026-08-26 13:35:15 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-08-26 22:23:21 -05:00
CRsaPublicKey::CRsaPublicKey(BigUInteger &n, ulong e, const int ksb)
2026-08-26 13:35:15 -05:00
// nota aqui le "robamos" a n sus daots (nada de copies)
2026-08-26 22:23:21 -05:00
: m_n(n.m_v, n.m_v_s), m_e(e), m_key_sbits(ksb)
2026-08-26 13:35:15 -05:00
{
}
2026-08-26 22:23:21 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CRsaPublicKey::ToSubjectPublicKeyInfo(uchar &out[], int o_s)
{
m_n.ToBytesBE()
}
2026-08-23 08:35:14 -05:00
}
#endif // CRYPTOBYLEO_SRC_RSA_KEYS_H_MQH
//+------------------------------------------------------------------+