mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-29 12:17:49 +02:00
Initial commit of garlic
This commit is contained in:
parent
474aa9068a
commit
e0e5e8ad36
3 changed files with 122 additions and 5 deletions
18
ElGamal.h
18
ElGamal.h
|
@ -12,23 +12,31 @@ namespace i2p
|
|||
{
|
||||
namespace crypto
|
||||
{
|
||||
inline void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, int len, uint8_t * encrypted)
|
||||
inline void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, int len,
|
||||
uint8_t * encrypted, bool zeroPadding = false) // 514 with padding and 512 without
|
||||
{
|
||||
CryptoPP::AutoSeededRandomPool rnd;
|
||||
CryptoPP::Integer y(key, 256), k(rnd, CryptoPP::Integer::One(), elgp-1);
|
||||
|
||||
a_exp_b_mod_c (elgg, k, elgp).Encode (encrypted, 256);
|
||||
if (zeroPadding)
|
||||
{
|
||||
encrypted[0] = 0;
|
||||
encrypted[257] = 0;
|
||||
}
|
||||
a_exp_b_mod_c (elgg, k, elgp).Encode (zeroPadding ? encrypted + 1 : encrypted, 256);
|
||||
uint8_t m[255];
|
||||
m[0] = 0xFF;
|
||||
memcpy (m+33, data, len);
|
||||
CryptoPP::SHA256().CalculateDigest(m+1, m+33, 222);
|
||||
a_times_b_mod_c (a_exp_b_mod_c (y, k, elgp),
|
||||
CryptoPP::Integer (m, 255), elgp).Encode (encrypted + 256, 256);
|
||||
CryptoPP::Integer (m, 255), elgp).Encode (zeroPadding ? encrypted + 258 : encrypted + 256, 256);
|
||||
}
|
||||
|
||||
inline bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data)
|
||||
inline bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted,
|
||||
uint8_t * data, bool zeroPadding = false)
|
||||
{
|
||||
CryptoPP::Integer x(key, 256), a(encrypted, 256), b(encrypted + 256, 256);
|
||||
CryptoPP::Integer x(key, 256), a(zeroPadding? encrypted +1 : encrypted, 256),
|
||||
b(zeroPadding? encrypted + 258 :encrypted + 256, 256);
|
||||
uint8_t m[255], hash[32];
|
||||
a_times_b_mod_c (b, a_exp_b_mod_c (a, elgp - x - 1, elgp), elgp).Encode (m, 255);
|
||||
CryptoPP::SHA256().CalculateDigest(hash, m+33, 222);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue