![]() |
Gray C++ Libraries
0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
|
#include <cKeyRSA.h>
Public Member Functions | |
cKeyRSA (PKCS1_VER_TYPE ePadding=PKCS1_VER_15, SSL_Hash_TYPE eHashType=SSL_Hash_NONE) | |
virtual | ~cKeyRSA () |
bool | IsValidPrivateKey () const |
virtual size_t | get_KeySize () const override |
Get key size in bytes. More... | |
virtual int | GetKeyValues (cValue *items) const override |
Interface with the debug module. get debug values. More... | |
virtual void | SetZeroKey () override |
Secure zero the key. More... | |
void | SetCopyKey (const cKeyRSA &src) |
HRESULT | DoRSAPublic (BYTE *pOutput, const BYTE *pInput) const |
HRESULT | DoRSAPrivate (BYTE *pOutput, const BYTE *pInput, IRandomNoise *pRandom) const |
HRESULT | VerifySignatureV15 (SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, const BYTE *pSig) const |
HRESULT | VerifySignatureV21 (SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, const BYTE *pSig, SSL_Hash_TYPE mgf1_hash_id, int expected_salt_len) const |
virtual HRESULT | CheckPublicPrivatePair (const cKeyBase *pKeyPrivate) const override |
Check public-private key pair. More... | |
virtual HRESULT | VerifySignature (SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, const BYTE *pSig, size_t nSizeSig) const override |
Verify Key signature. Make const ?? More... | |
virtual HRESULT | MakeSignature (BYTE *pSig, SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, IRandomNoise *pRandom) const override |
Make signature From Key. Make const ?? More... | |
HRESULT | DecryptWithKey (BYTE *pOutput, size_t nSizeOut, const BYTE *pInput, size_t nSizeIn, IRandomNoise *pRandom) |
HRESULT | EncryptWithKey (BYTE *pOutput, size_t nSizeOut, const BYTE *pInput, size_t nSizeIn, IRandomNoise *pRandom) |
bool | UpdateReadPrivate () |
virtual HRESULT | ReadPublicKey (cASNReader &r, cASNBuf *pAlgParams) override |
virtual HRESULT | WritePublicKey (cASNWriterRev &w) const override |
HRESULT | ReadPrivateKeyPVK (const BYTE *pKeyData, int nSize) |
virtual HRESULT | ReadPrivateKey (cASNReader &r, cASNBuf *pAlgParams) override |
Read not encrypted. (already decrypted) More... | |
virtual HRESULT | WritePrivateKey (cASNWriterRev &w) const override |
Write not encrypted. (may be encrypted/secured later) More... | |
HRESULT | GenerateKeys (BIT_ENUM_t nBits, UINT nExpoMax, IRandomNoise *pRandom) |
UNITTEST_FRIEND (cKeyRSA) | |
![]() | |
virtual | ~cKeyBase () |
![]() | |
cRefBase (int iRefCount=0) noexcept | |
virtual | ~cRefBase () |
int | get_RefCount () const noexcept |
HASHCODE_t | get_HashCode () const noexcept |
STDMETHOD_ (HASHCODE_t, get_HashCodeX)() const noexcept | |
virtual void | onFinalRelease () |
bool | isValidObj () const noexcept |
STDMETHOD_ (ULONG, AddRef)(void) override | |
STDMETHOD_ (ULONG, Release)(void) override | |
STDMETHOD() | QueryInterface (const IID &riid, void __RPC_FAR *__RPC_FAR *ppvObject) override |
void | IncRefCount () |
void | DecRefCount () |
bool | isStaticConstruct () const noexcept |
void | StaticConstruct () |
void | StaticDestruct () |
bool | isDestructing () noexcept |
void | SetDestructing () |
![]() | |
virtual | ~CObject () |
virtual bool | isValidCheck () const noexcept |
< memory allocation and structure definitions are valid. More... | |
virtual void | AssertValid () const |
< memory allocation and structure definitions are valid. More... | |
virtual void | Serialize (cArchive &a) |
![]() | |
cKeyRSA1 (void) | |
cKeyRSA1 (const VAL_t &_exp, const VAL_t &_mod) | |
virtual | ~cKeyRSA1 (void) |
bool | IsValidKey () const |
void | SetZeroKey () |
void | SetCopyKey (const cKeyRSA1 &src) |
void | GenerateKeys (OUT VAL_t &d, const VAL_t &p, const VAL_t &q) |
void | GenerateKeys (BIT_ENUM_t nBits, OUT VAL_t &d, OUT VAL_t *pP=nullptr, OUT VAL_t *pQ=nullptr) |
VAL_t | EncryptDecrypt (const VAL_t &source) const |
HRESULT | WritePublicKey (cASNWriterRev &w) const |
HRESULT | ReadPublicKey (cASNReader &r) |
HRESULT | LoadKeyFile (cStreamInput &rInp, const char *pszName=nullptr) |
HRESULT | LoadKeyFile (const FILECHAR_t *pszFileName, const char *pszName=nullptr) |
UNITTEST_FRIEND (cKeyRSA1) | |
Public Attributes | |
size_t | m_nSizeBlock |
sizeof(N) in bytes. Block size for DecryptWithKey More... | |
PKCS1_VER_TYPE | m_ePadding |
PKCS1_VER_15 for 1.5 padding and PKCS1_VER_21 for OAEP/PSS. More... | |
SSL_Hash_TYPE | m_eHashType |
Hash identifier for the EME-OAEP and EMSA-PSS encoding. MGF1. More... | |
VAL_t | m_D |
private exponent More... | |
VAL_t | m_P |
1st prime factor (private) (r) More... | |
VAL_t | m_Q |
2nd prime factor (private) More... | |
VAL_t | m_DP |
D % (P - 1) const derived value. More... | |
VAL_t | m_DQ |
D % (Q - 1) const derived value. More... | |
VAL_t | m_QP |
1 / (Q % P) const derived value. More... | |
cThreadLockMutex | m_Mutex |
Thread-safety mutex for mutable cached stuff. More... | |
VAL_t | m_RN |
cached R^2 mod N (for public) More... | |
VAL_t | m_RP |
cached R^2 mod P for DoRSAPrivate More... | |
VAL_t | m_RQ |
cached R^2 mod Q for DoRSAPrivate More... | |
VAL_t | m_BlindAdd |
cached blinding value for DoRSAPrivate More... | |
VAL_t | m_BlindRemove |
cached un-blinding value for DoRSAPrivate More... | |
![]() | |
VAL_t | m_N |
modulus of the key (usually public) = multiplication of the 2 primes (pq). More... | |
VAL_t | m_E |
exponent of the key (public or private half of the key depending on use.) More... | |
Static Public Attributes | |
static const int | k_nSizeSaltAny = -1 |
![]() | |
static const int | k_MAX_VALUES = 2 |
Maximum number of cValue sent for debugging. More... | |
Protected Member Functions | |
HRESULT | CreateBlindingValues (IRandomNoise *pRandom) const |
HRESULT | EncryptWithKeyV15 (BYTE *pOutput, const BYTE *pInput, size_t nSizeIn, IRandomNoise *pRandom) |
HRESULT | EncryptWithKeyV21 (BYTE *pOutput, const BYTE *pInput, size_t nSizeIn, IRandomNoise *pRandom, const BYTE *pLabel=nullptr, size_t nSizeLabel=0) |
HRESULT | DecryptWithKeyV15 (BYTE *pOutput, size_t output_max_len, const BYTE *pInput, IRandomNoise *pRandom) |
HRESULT | DecryptWithKeyV21 (BYTE *pOutput, size_t output_max_len, const BYTE *pInput, IRandomNoise *pRandom, const BYTE *pLabel=nullptr, size_t nSizeLabel=0) |
HRESULT | MakeSignatureV15 (BYTE *pSig, SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, IRandomNoise *pRandom) const |
HRESULT | MakeSignatureV21 (BYTE *pSig, SSL_Hash_TYPE eHashType, const BYTE *pHash, size_t nSizeHash, IRandomNoise *pRandom) const |
Additional Inherited Members | |
![]() | |
enum | VALTYPE_TYPE { VALTYPE_NONE = 0 , VALTYPE_RSA = 1 , VALTYPE_ECP = 2 } |
![]() | |
typedef cBigUnsigned | VAL_t |
RSA public/private key structure. SSL_Key_RSA
GrayLib::cKeyRSA::cKeyRSA | ( | PKCS1_VER_TYPE | ePadding = PKCS1_VER_15 , |
SSL_Hash_TYPE | eHashType = SSL_Hash_NONE |
||
) |
Initialize an RSA context
|
virtual |
|
overridevirtual |
Check public-private key pair.
Check if contexts holding a public and private key match Check a public-private RSA key pair. Check each of the keys, and make sure they match. this is RSA context holding the public key
Implements GrayLib::cKeyBase.
|
protected |
Generate or update blinding values, see section 10 of: KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer Berlin Heidelberg, 1996. p. 104-113.
HRESULT GrayLib::cKeyRSA::DecryptWithKey | ( | BYTE * | pOutput, |
size_t | nSizeOut, | ||
const BYTE * | pInput, | ||
size_t | nSizeIn, | ||
IRandomNoise * | pRandom | ||
) |
Generic wrapper to perform a PKCS#1 decryption using the mode from the context. Do an RSA operation, then remove the message padding
|
protected |
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) Use private key to decrypt.
|
protected |
Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
HRESULT GrayLib::cKeyRSA::DoRSAPrivate | ( | BYTE * | pOutput, |
const BYTE * | pInput, | ||
IRandomNoise * | pRandom | ||
) | const |
Do an RSA private key operation When using the Chinese Remainder Theorem, we use blinding values.
HRESULT GrayLib::cKeyRSA::DoRSAPublic | ( | BYTE * | pOutput, |
const BYTE * | pInput | ||
) | const |
Do an RSA public key operation
HRESULT GrayLib::cKeyRSA::EncryptWithKey | ( | BYTE * | pOutput, |
size_t | nSizeOut, | ||
const BYTE * | pInput, | ||
size_t | nSizeIn, | ||
IRandomNoise * | pRandom | ||
) |
Perform a PKCS#1 encryption
|
protected |
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) Use public key to encrypt.
|
protected |
Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) Use public key to encrypt.
HRESULT GrayLib::cKeyRSA::GenerateKeys | ( | BIT_ENUM_t | nBits, |
UINT | nExpoMax, | ||
IRandomNoise * | pRandom | ||
) |
Generate an RSA key pair. TODO Merge with cKeyRSA1 and cThreadState
|
inlineoverridevirtual |
|
inlineoverridevirtual |
bool GrayLib::cKeyRSA::IsValidPrivateKey | ( | ) | const |
Check a private RSA key
|
overridevirtual |
Make signature From Key. Make const ??
Generic wrapper to perform a PKCS#1 signature using the mode from the context. Do a private RSA operation to sign a message digest
Implements GrayLib::cKeyBase.
|
protected |
Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) PKCS1_VER_15 Do an RSA operation to sign the message digest Use private key to sign.
|
protected |
Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) Use private key to sign.
|
overridevirtual |
Read not encrypted. (already decrypted)
Parse a PKCS#1 encoded private RSA key from ASN/DER format. This function parses the RSAPrivateKey (PKCS#1)
RSAPrivateKey ::= SEQUENCE { version Version, modulus INTEGER, – n publicExponent INTEGER, – e privateExponent INTEGER, – d prime1 INTEGER, – r (p?) prime2 INTEGER, – q exponent1 INTEGER, – d mod (p-1) exponent2 INTEGER, – d mod (q-1) coefficient INTEGER, – (inverse of q) mod p otherPrimeInfos OtherPrimeInfos OPTIONAL }
Implements GrayLib::cKeyBase.
HRESULT GrayLib::cKeyRSA::ReadPrivateKeyPVK | ( | const BYTE * | pKeyData, |
int | nSize | ||
) |
Read Windows PVK format. not encrypted.
|
inlineoverridevirtual |
Implements GrayLib::cKeyBase.
void GrayLib::cKeyRSA::SetCopyKey | ( | const cKeyRSA & | src | ) |
Copy the components of an RSA key
|
overridevirtual |
GrayLib::cKeyRSA::UNITTEST_FRIEND | ( | cKeyRSA | ) |
bool GrayLib::cKeyRSA::UpdateReadPrivate | ( | ) |
|
overridevirtual |
Verify Key signature. Make const ??
Do an RSA operation and check the message digest Use public key to verify pSig. Generic wrapper to perform a PKCS#1 verification using the mode from the context. Do a public RSA operation and check the message digest
Implements GrayLib::cKeyBase.
HRESULT GrayLib::cKeyRSA::VerifySignatureV15 | ( | SSL_Hash_TYPE | eHashType, |
const BYTE * | pHash, | ||
size_t | nSizeHash, | ||
const BYTE * | pSig | ||
) | const |
Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) Use public key to verify pSig.
HRESULT GrayLib::cKeyRSA::VerifySignatureV21 | ( | SSL_Hash_TYPE | eHashType, |
const BYTE * | pHash, | ||
size_t | nSizeHash, | ||
const BYTE * | pSig, | ||
SSL_Hash_TYPE | mgf1_hash_id, | ||
int | expected_salt_len | ||
) | const |
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) Use public key to verify pSig. (This is the version with "full" options.) Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) (This is the "simple" version.)
|
overridevirtual |
Write not encrypted. (may be encrypted/secured later)
Write private key in ASN/DER format
Implements GrayLib::cKeyBase.
|
inlineoverridevirtual |
Implements GrayLib::cKeyBase.
|
static |
|
mutable |
cached blinding value for DoRSAPrivate
|
mutable |
cached un-blinding value for DoRSAPrivate
VAL_t GrayLib::cKeyRSA::m_D |
private exponent
VAL_t GrayLib::cKeyRSA::m_DP |
D % (P - 1) const derived value.
VAL_t GrayLib::cKeyRSA::m_DQ |
D % (Q - 1) const derived value.
SSL_Hash_TYPE GrayLib::cKeyRSA::m_eHashType |
Hash identifier for the EME-OAEP and EMSA-PSS encoding. MGF1.
PKCS1_VER_TYPE GrayLib::cKeyRSA::m_ePadding |
PKCS1_VER_15 for 1.5 padding and PKCS1_VER_21 for OAEP/PSS.
|
mutable |
Thread-safety mutex for mutable cached stuff.
size_t GrayLib::cKeyRSA::m_nSizeBlock |
sizeof(N) in bytes. Block size for DecryptWithKey
VAL_t GrayLib::cKeyRSA::m_P |
1st prime factor (private) (r)
VAL_t GrayLib::cKeyRSA::m_Q |
2nd prime factor (private)
VAL_t GrayLib::cKeyRSA::m_QP |
1 / (Q % P) const derived value.
|
mutable |
cached R^2 mod N (for public)
|
mutable |
cached R^2 mod P for DoRSAPrivate
|
mutable |
cached R^2 mod Q for DoRSAPrivate