summaryrefslogtreecommitdiffstats
path: root/src/PolarSSL++/PublicKey.cpp
blob: 49794a0c889f7c8713bdc66f0fc0bd8222ca30dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

// PublicKey.cpp

// Implements the cPublicKey class representing a RSA public key in PolarSSL

#include "Globals.h"
#include "PublicKey.h"





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;
	}
	m_CtrDrbg.Initialize("rsa_pubkey", 10);
}





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_CtrDrbg.GetInternal()
	);
	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_CtrDrbg.GetInternal()
	);
	if (res != 0)
	{
		return res;
	}
	return (int)EncryptedLength;
}