From 81837edb22197b48c3da4c47954681096a867766 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 28 Jan 2014 09:50:48 +0100 Subject: Fixed a slight bug in RSA encryption code. --- src/Crypto.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/Crypto.cpp') diff --git a/src/Crypto.cpp b/src/Crypto.cpp index 2045d0385..540b5cfde 100644 --- a/src/Crypto.cpp +++ b/src/Crypto.cpp @@ -214,7 +214,6 @@ int cRSAPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte ASSERT(!"Invalid a_PlainLength!"); return -1; } - size_t DecryptedLength; int res = rsa_pkcs1_encrypt( &m_Rsa, ctr_drbg_random, &m_Ctr_drbg, RSA_PUBLIC, a_PlainLength, a_PlainData, a_EncryptedData @@ -223,7 +222,7 @@ int cRSAPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte { return -1; } - return (int)DecryptedLength; + return (int)m_Rsa.len; } -- cgit v1.2.3 From f7cbb07b54e3cafdef7578693ea5340fcaf9ac3d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 28 Jan 2014 23:15:04 +0100 Subject: Fixed an error in Crypto. --- src/Crypto.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Crypto.cpp') diff --git a/src/Crypto.cpp b/src/Crypto.cpp index 540b5cfde..6f7047ab0 100644 --- a/src/Crypto.cpp +++ b/src/Crypto.cpp @@ -206,7 +206,7 @@ int cRSAPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte ASSERT(!"Invalid a_DecryptedMaxLength!"); return -1; } - if (a_PlainLength < m_Rsa.len) + if (a_EncryptedMaxLength < m_Rsa.len) { LOGD("%s: Invalid a_PlainLength: got %u, exp at least %u", __FUNCTION__, (unsigned)a_PlainLength, (unsigned)(m_Rsa.len) @@ -215,7 +215,7 @@ int cRSAPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte return -1; } int res = rsa_pkcs1_encrypt( - &m_Rsa, ctr_drbg_random, &m_Ctr_drbg, RSA_PUBLIC, + &m_Rsa, ctr_drbg_random, &m_Ctr_drbg, RSA_PRIVATE, a_PlainLength, a_PlainData, a_EncryptedData ); if (res != 0) -- cgit v1.2.3 From 9de52252acb22104584ff7a0746ee81734c3f19e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 28 Jan 2014 23:38:56 +0100 Subject: Crypto: Added public key encryption / decryption. --- src/Crypto.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'src/Crypto.cpp') diff --git a/src/Crypto.cpp b/src/Crypto.cpp index 6f7047ab0..7a06d7fa3 100644 --- a/src/Crypto.cpp +++ b/src/Crypto.cpp @@ -229,6 +229,81 @@ int cRSAPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cPublicKey: + +cPublicKey::cPublicKey(const AString & a_PublicKeyDER) +{ + pk_init(&m_Pk); + if (pk_parse_public_key(&m_Pk, (const Byte *)a_PublicKeyDER.data(), a_PublicKeyDER.size()) != 0) + { + ASSERT(!"Cannot parse PubKey"); + return; + } + InitRnd(); +} + + + + + +cPublicKey::~cPublicKey() +{ + pk_free(&m_Pk); +} + + + + + +int cPublicKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) +{ + size_t DecryptedLen = a_DecryptedMaxLength; + int res = pk_decrypt(&m_Pk, + a_EncryptedData, a_EncryptedLength, + a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength, + ctr_drbg_random, &m_Ctr_drbg + ); + if (res != 0) + { + return res; + } + return (int)DecryptedLen; +} + + + + + +int cPublicKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength) +{ + size_t EncryptedLength = a_EncryptedMaxLength; + int res = pk_encrypt(&m_Pk, + a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength, + ctr_drbg_random, &m_Ctr_drbg + ); + if (res != 0) + { + return res; + } + return (int)EncryptedLength; +} + + + + + +void cPublicKey::InitRnd(void) +{ + entropy_init(&m_Entropy); + const unsigned char pers[] = "rsa_genkey"; + ctr_drbg_init(&m_Ctr_drbg, entropy_func, &m_Entropy, pers, sizeof(pers) - 1); +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cAESCFBDecryptor: -- cgit v1.2.3