mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	CryptoKey added
This commit is contained in:
		
							parent
							
								
									b3c836f298
								
							
						
					
					
						commit
						efacfced45
					
				
					 6 changed files with 56 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -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 ();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;		
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										27
									
								
								libi2pd/CryptoKey.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								libi2pd/CryptoKey.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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);	
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								libi2pd/CryptoKey.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								libi2pd/CryptoKey.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,16 @@
 | 
			
		|||
#ifndef CRYPTO_KEY_H__
 | 
			
		||||
#define CRYPTO_KEY_H__
 | 
			
		||||
 | 
			
		||||
#include <inttypes.h>
 | 
			
		||||
#include "Crypto.h"
 | 
			
		||||
 | 
			
		||||
namespace i2p
 | 
			
		||||
{
 | 
			
		||||
namespace crypto
 | 
			
		||||
{
 | 
			
		||||
	void CreateECIESP256RandomKeys (uint8_t * priv, uint8_t * pub);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
#include <time.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#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");
 | 
			
		||||
		}	
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 \
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue