2017-11-06 18:40:58 +00:00
|
|
|
#ifndef CRYPTO_KEY_H__
|
|
|
|
#define CRYPTO_KEY_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include "Crypto.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2018-01-06 03:48:51 +00:00
|
|
|
class CryptoKeyEncryptor
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~CryptoKeyEncryptor () {};
|
2018-03-09 19:56:06 +00:00
|
|
|
virtual void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding) = 0; // 222 bytes data, 512/514 bytes encrypted
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2017-11-06 20:54:18 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
class CryptoKeyDecryptor
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~CryptoKeyDecryptor () {};
|
2018-03-09 19:56:06 +00:00
|
|
|
virtual bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding) = 0; // 512/514 bytes encrypted, 222 bytes data
|
2017-11-06 20:54:18 +00:00
|
|
|
};
|
|
|
|
|
2017-11-09 20:01:07 +00:00
|
|
|
// ElGamal
|
2017-11-07 20:05:22 +00:00
|
|
|
class ElGamalEncryptor: public CryptoKeyEncryptor // for destination
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ElGamalEncryptor (const uint8_t * pub);
|
2018-03-09 19:56:06 +00:00
|
|
|
void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding);
|
2017-11-06 20:54:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
uint8_t m_PublicKey[256];
|
|
|
|
};
|
|
|
|
|
2017-11-07 20:05:22 +00:00
|
|
|
class ElGamalDecryptor: public CryptoKeyDecryptor // for destination
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ElGamalDecryptor (const uint8_t * priv);
|
2018-03-09 19:56:06 +00:00
|
|
|
bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding);
|
2017-11-06 20:54:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
uint8_t m_PrivateKey[256];
|
|
|
|
};
|
|
|
|
|
2017-11-09 20:01:07 +00:00
|
|
|
// ECIES P256
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
class ECIESP256Encryptor: public CryptoKeyEncryptor
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ECIESP256Encryptor (const uint8_t * pub);
|
|
|
|
~ECIESP256Encryptor ();
|
2018-03-09 19:56:06 +00:00
|
|
|
void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding);
|
2017-11-06 20:54:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
EC_GROUP * m_Curve;
|
|
|
|
EC_POINT * m_PublicKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-11-07 20:05:22 +00:00
|
|
|
class ECIESP256Decryptor: public CryptoKeyDecryptor
|
2017-11-06 20:54:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ECIESP256Decryptor (const uint8_t * priv);
|
|
|
|
~ECIESP256Decryptor ();
|
2018-03-09 19:56:06 +00:00
|
|
|
bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding);
|
2017-11-06 20:54:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
EC_GROUP * m_Curve;
|
|
|
|
BIGNUM * m_PrivateKey;
|
|
|
|
};
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
void CreateECIESP256RandomKeys (uint8_t * priv, uint8_t * pub);
|
2017-11-09 20:01:07 +00:00
|
|
|
|
|
|
|
// ECIES GOST R 34.10
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
class ECIESGOSTR3410Encryptor: public CryptoKeyEncryptor
|
2017-11-09 20:01:07 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ECIESGOSTR3410Encryptor (const uint8_t * pub);
|
|
|
|
~ECIESGOSTR3410Encryptor ();
|
2018-03-09 19:56:06 +00:00
|
|
|
void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding);
|
2017-11-09 20:01:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
EC_POINT * m_PublicKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ECIESGOSTR3410Decryptor: public CryptoKeyDecryptor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ECIESGOSTR3410Decryptor (const uint8_t * priv);
|
|
|
|
~ECIESGOSTR3410Decryptor ();
|
2018-03-09 19:56:06 +00:00
|
|
|
bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding);
|
2017-11-09 20:01:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
BIGNUM * m_PrivateKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
void CreateECIESGOSTR3410RandomKeys (uint8_t * priv, uint8_t * pub);
|
2017-11-06 18:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|