diff --git a/libi2pd/Crypto.cpp b/libi2pd/Crypto.cpp index caae5d82..13c86188 100644 --- a/libi2pd/Crypto.cpp +++ b/libi2pd/Crypto.cpp @@ -372,8 +372,8 @@ namespace crypto BN_CTX_free (ctx); } -// ECICS - void ECICSEncrypt (const EC_GROUP * curve, const EC_POINT * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) +// ECIES + void ECIESEncrypt (const EC_GROUP * curve, const EC_POINT * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) { BN_CTX_start (ctx); BIGNUM * q = BN_CTX_get (ctx); @@ -410,7 +410,7 @@ namespace crypto BN_CTX_end (ctx); } - bool ECICSDecrypt (const EC_GROUP * curve, const BIGNUM * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) + bool ECIESDecrypt (const EC_GROUP * curve, const BIGNUM * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) { bool ret = true; BN_CTX_start (ctx); @@ -460,7 +460,7 @@ namespace crypto return ret; } - void GenerateECICSKeyPair (const EC_GROUP * curve, BIGNUM *& priv, EC_POINT *& pub) + void GenerateECIESKeyPair (const EC_GROUP * curve, BIGNUM *& priv, EC_POINT *& pub) { BN_CTX * ctx = BN_CTX_new (); BIGNUM * q = BN_new (); diff --git a/libi2pd/Crypto.h b/libi2pd/Crypto.h index 18948490..68378b4e 100644 --- a/libi2pd/Crypto.h +++ b/libi2pd/Crypto.h @@ -52,10 +52,10 @@ namespace crypto bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding = false); void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub); - // ECICS - void ECICSEncrypt (const EC_GROUP * curve, const EC_POINT * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx); // 222 bytes data, 512 bytes encrypted - bool ECICSDecrypt (const EC_GROUP * curve, const BIGNUM * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx); - void GenerateECICSKeyPair (const EC_GROUP * curve, BIGNUM *& priv, EC_POINT *& pub); + // ECIES + void ECIESEncrypt (const EC_GROUP * curve, const EC_POINT * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx); // 222 bytes data, 512 bytes encrypted + bool ECIESDecrypt (const EC_GROUP * curve, const BIGNUM * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx); + void GenerateECIESKeyPair (const EC_GROUP * curve, BIGNUM *& priv, EC_POINT *& pub); // HMAC typedef i2p::data::Tag<32> MACKey; diff --git a/libi2pd/CryptoKey.cpp b/libi2pd/CryptoKey.cpp new file mode 100644 index 00000000..e76b67b2 --- /dev/null +++ b/libi2pd/CryptoKey.cpp @@ -0,0 +1,27 @@ +#include "CryptoKey.h" + +namespace i2p +{ +namespace crypto +{ + void CreateECIESP256RandomKeys (uint8_t * priv, uint8_t * pub) + { + EC_GROUP * curve = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1); + EC_POINT * p = nullptr; + BIGNUM * key = nullptr; + GenerateECIESKeyPair (curve, key, p); + bn2buf (key, priv, 32); + RAND_bytes (priv + 32, 224); + BN_free (key); + BIGNUM * x = BN_new (), * y = BN_new (); + EC_POINT_get_affine_coordinates_GFp (curve, p, x, y, NULL); + bn2buf (x, pub, 32); + bn2buf (y, pub + 32, 32); + RAND_bytes (priv + 64, 192); + EC_POINT_free (p); + BN_free (x); BN_free (y); + EC_GROUP_free (curve); + } +} +} + diff --git a/libi2pd/CryptoKey.h b/libi2pd/CryptoKey.h new file mode 100644 index 00000000..4a08a3b6 --- /dev/null +++ b/libi2pd/CryptoKey.h @@ -0,0 +1,16 @@ +#ifndef CRYPTO_KEY_H__ +#define CRYPTO_KEY_H__ + +#include +#include "Crypto.h" + +namespace i2p +{ +namespace crypto +{ + void CreateECIESP256RandomKeys (uint8_t * priv, uint8_t * pub); +} +} + +#endif + diff --git a/libi2pd/Identity.cpp b/libi2pd/Identity.cpp index 847c63ca..87a5d84c 100644 --- a/libi2pd/Identity.cpp +++ b/libi2pd/Identity.cpp @@ -1,6 +1,7 @@ #include #include #include "Crypto.h" +#include "CryptoKey.h" #include "I2PEndian.h" #include "Log.h" #include "Identity.h" @@ -627,24 +628,8 @@ namespace data i2p::crypto::GenerateElGamalKeyPair(priv, pub); break; case CRYPTO_KEY_TYPE_ECICS_P256_SHA256_AES256CBC: - { - EC_GROUP * curve = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1); - EC_POINT * p = nullptr; - BIGNUM * key = nullptr; - i2p::crypto::GenerateECICSKeyPair (curve, key, p); - i2p::crypto::bn2buf (key, priv, 32); - RAND_bytes (priv + 32, 224); - BN_free (key); - BIGNUM * x = BN_new (), * y = BN_new (); - EC_POINT_get_affine_coordinates_GFp (curve, p, x, y, NULL); - i2p::crypto::bn2buf (x, pub, 32); - i2p::crypto::bn2buf (y, pub + 32, 32); - RAND_bytes (priv + 64, 192); - EC_POINT_free (p); - BN_free (x); BN_free (y); - EC_GROUP_free (curve); - break; - } + i2p::crypto::CreateECIESP256RandomKeys (priv, pub); + break; default: LogPrint (eLogError, "Identity: Crypto key type ", (int)type, " is not supported"); } diff --git a/qt/i2pd_qt/i2pd_qt.pro b/qt/i2pd_qt/i2pd_qt.pro index 78078904..f575438c 100644 --- a/qt/i2pd_qt/i2pd_qt.pro +++ b/qt/i2pd_qt/i2pd_qt.pro @@ -30,6 +30,7 @@ SOURCES += DaemonQT.cpp mainwindow.cpp \ ../../libi2pd/BloomFilter.cpp \ ../../libi2pd/Config.cpp \ ../../libi2pd/Crypto.cpp \ + ../../libi2pd/CryptoKey.cpp \ ../../libi2pd/Datagram.cpp \ ../../libi2pd/Destination.cpp \ ../../libi2pd/Event.cpp \ @@ -107,6 +108,7 @@ HEADERS += DaemonQT.h mainwindow.h \ ../../libi2pd/BloomFilter.h \ ../../libi2pd/Config.h \ ../../libi2pd/Crypto.h \ + ../../libi2pd/CryptoKey.h \ ../../libi2pd/Datagram.h \ ../../libi2pd/Destination.h \ ../../libi2pd/Event.h \