2017-09-19 04:34:08 -04:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
// RsaPrivateKey.h
|
|
|
|
|
|
|
|
// Declares the cRsaPrivateKey class representing a private key for RSA operations.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CtrDrbgContext.h"
|
2017-08-30 10:00:06 -04:00
|
|
|
#include "mbedtls/rsa.h"
|
2014-04-29 05:04:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Encapsulates an RSA private key used in PKI cryptography */
|
|
|
|
class cRsaPrivateKey
|
|
|
|
{
|
2014-04-30 18:28:27 -04:00
|
|
|
friend class cSslContext;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
public:
|
|
|
|
/** Creates a new empty object, the key is not assigned */
|
|
|
|
cRsaPrivateKey(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
/** Deep-copies the key from a_Other */
|
|
|
|
cRsaPrivateKey(const cRsaPrivateKey & a_Other);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
~cRsaPrivateKey();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
/** Generates a new key within this object, with the specified size in bits.
|
|
|
|
Returns true on success, false on failure. */
|
|
|
|
bool Generate(unsigned a_KeySizeBits = 1024);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
/** Returns the public key part encoded in ASN1 DER encoding */
|
|
|
|
AString GetPubKeyDER(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
/** Decrypts the data using RSAES-PKCS#1 algorithm.
|
|
|
|
Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
|
|
|
|
Returns the number of bytes decrypted, or negative number for error. */
|
|
|
|
int Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
/** Encrypts the data using RSAES-PKCS#1 algorithm.
|
|
|
|
Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
|
|
|
|
Returns the number of bytes decrypted, or negative number for error. */
|
|
|
|
int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
protected:
|
2017-08-30 10:00:06 -04:00
|
|
|
/** The mbedTLS key context */
|
|
|
|
mbedtls_rsa_context m_Rsa;
|
2014-04-29 05:04:54 -04:00
|
|
|
|
|
|
|
/** The random generator used for generating the key and encryption / decryption */
|
|
|
|
cCtrDrbgContext m_CtrDrbg;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
/** Returns the internal context ptr. Only use in mbedTLS API calls. */
|
|
|
|
mbedtls_rsa_context * GetInternal(void) { return &m_Rsa; }
|
2014-04-29 05:04:54 -04:00
|
|
|
} ;
|
|
|
|
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cRsaPrivateKey> cRsaPrivateKeyPtr;
|
2014-04-30 18:28:27 -04:00
|
|
|
|
2014-04-29 05:04:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|