summaryrefslogtreecommitdiffstats
path: root/src/Crypto.cpp
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2014-01-28 23:38:56 +0100
committermadmaxoft <github@xoft.cz>2014-01-28 23:53:54 +0100
commit9de52252acb22104584ff7a0746ee81734c3f19e (patch)
treed5e6ae6b55d0cd51c5238dbad454986e495f84ad /src/Crypto.cpp
parentFixed an error in Crypto. (diff)
downloadcuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar.gz
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar.bz2
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar.lz
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar.xz
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.tar.zst
cuberite-9de52252acb22104584ff7a0746ee81734c3f19e.zip
Diffstat (limited to 'src/Crypto.cpp')
-rw-r--r--src/Crypto.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/Crypto.cpp b/src/Crypto.cpp
index 6f7047ab0..7a06d7fa3 100644
--- a/src/Crypto.cpp
+++ b/src/Crypto.cpp
@@ -230,6 +230,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:
cAESCFBDecryptor::cAESCFBDecryptor(void) :