mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
323 lines
6.5 KiB
323 lines
6.5 KiB
9 years ago
|
#ifndef CRYPTO_H__
|
||
|
#define CRYPTO_H__
|
||
11 years ago
|
|
||
|
#include <inttypes.h>
|
||
9 years ago
|
#include <string>
|
||
|
#include <openssl/bn.h>
|
||
|
#include <openssl/dh.h>
|
||
|
#include <openssl/aes.h>
|
||
|
#include "Base.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace crypto
|
||
9 years ago
|
{
|
||
|
struct CryptoConstants
|
||
|
{
|
||
|
// DH/ElGamal
|
||
|
BIGNUM * elgp;
|
||
|
BIGNUM * elgg;
|
||
|
|
||
|
// DSA
|
||
|
BIGNUM * dsap;
|
||
|
BIGNUM * dsaq;
|
||
|
BIGNUM * dsag;
|
||
|
|
||
|
// RSA
|
||
|
BIGNUM * rsae;
|
||
|
|
||
|
CryptoConstants (const uint8_t * elgp_, int elgg_, const uint8_t * dsap_,
|
||
|
const uint8_t * dsaq_, const uint8_t * dsag_, int rsae_)
|
||
|
{
|
||
|
elgp = BN_new ();
|
||
|
BN_bin2bn (elgp_, 256, elgp);
|
||
|
elgg = BN_new ();
|
||
|
BN_set_word (elgg, elgg_);
|
||
|
dsap = BN_new ();
|
||
|
BN_bin2bn (dsap_, 128, dsap);
|
||
|
dsaq = BN_new ();
|
||
|
BN_bin2bn (dsaq_, 20, dsaq);
|
||
|
dsag = BN_new ();
|
||
|
BN_bin2bn (dsag_, 128, dsag);
|
||
|
rsae = BN_new ();
|
||
|
BN_set_word (rsae, rsae_);
|
||
|
}
|
||
|
|
||
|
~CryptoConstants ()
|
||
|
{
|
||
|
BN_free (elgp); BN_free (elgg); BN_free (dsap); BN_free (dsaq); BN_free (dsag); BN_free (rsae);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const CryptoConstants& GetCryptoConstants ();
|
||
|
|
||
|
// DH/ElGamal
|
||
|
#define elgp GetCryptoConstants ().elgp
|
||
|
#define elgg GetCryptoConstants ().elgg
|
||
|
|
||
|
// DSA
|
||
|
#define dsap GetCryptoConstants ().dsap
|
||
|
#define dsaq GetCryptoConstants ().dsaq
|
||
|
#define dsag GetCryptoConstants ().dsag
|
||
|
|
||
|
// RSA
|
||
|
#define rsae GetCryptoConstants ().rsae
|
||
|
|
||
|
bool bn2buf (const BIGNUM * bn, uint8_t * buf, size_t len);
|
||
|
|
||
|
// DH
|
||
|
class DHKeys
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
DHKeys ();
|
||
|
~DHKeys ();
|
||
|
|
||
|
void GenerateKeys (uint8_t * priv = nullptr, uint8_t * pub = nullptr);
|
||
|
const uint8_t * GetPublicKey ();
|
||
|
void Agree (const uint8_t * pub, uint8_t * shared);
|
||
|
|
||
|
private:
|
||
|
|
||
|
DH * m_DH;
|
||
|
uint8_t m_PublicKey[256];
|
||
|
bool m_IsUpdated;
|
||
|
};
|
||
|
|
||
|
// ElGamal
|
||
|
class ElGamalEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
ElGamalEncryption (const uint8_t * key);
|
||
|
~ElGamalEncryption ();
|
||
|
|
||
|
void Encrypt (const uint8_t * data, int len, uint8_t * encrypted, bool zeroPadding = false) const;
|
||
|
|
||
|
private:
|
||
|
|
||
|
BN_CTX * ctx;
|
||
|
BIGNUM * a, * b1;
|
||
|
};
|
||
|
|
||
|
bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, bool zeroPadding = false);
|
||
|
void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub);
|
||
|
|
||
|
// HMAC
|
||
|
typedef i2p::data::Tag<32> MACKey;
|
||
|
void HMACMD5Digest (uint8_t * msg, size_t len, const MACKey& key, uint8_t * digest);
|
||
|
|
||
|
// AES
|
||
10 years ago
|
struct ChipherBlock
|
||
11 years ago
|
{
|
||
|
uint8_t buf[16];
|
||
11 years ago
|
|
||
11 years ago
|
void operator^=(const ChipherBlock& other) // XOR
|
||
11 years ago
|
{
|
||
10 years ago
|
#if defined(__x86_64__) // for Intel x64
|
||
|
__asm__
|
||
|
(
|
||
|
"movups (%[buf]), %%xmm0 \n"
|
||
|
"movups (%[other]), %%xmm1 \n"
|
||
|
"pxor %%xmm1, %%xmm0 \n"
|
||
|
"movups %%xmm0, (%[buf]) \n"
|
||
|
:
|
||
|
: [buf]"r"(buf), [other]"r"(other.buf)
|
||
|
: "%xmm0", "%xmm1", "memory"
|
||
|
);
|
||
|
#else
|
||
|
// TODO: implement it better
|
||
|
for (int i = 0; i < 16; i++)
|
||
|
buf[i] ^= other.buf[i];
|
||
|
#endif
|
||
11 years ago
|
}
|
||
11 years ago
|
};
|
||
|
|
||
10 years ago
|
typedef i2p::data::Tag<32> AESKey;
|
||
|
|
||
10 years ago
|
template<size_t sz>
|
||
|
class AESAlignedBuffer // 16 bytes alignment
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
AESAlignedBuffer ()
|
||
|
{
|
||
|
m_Buf = m_UnalignedBuffer;
|
||
10 years ago
|
uint8_t rem = ((size_t)m_Buf) & 0x0f;
|
||
10 years ago
|
if (rem)
|
||
|
m_Buf += (16 - rem);
|
||
|
}
|
||
|
|
||
|
operator uint8_t * () { return m_Buf; };
|
||
|
operator const uint8_t * () const { return m_Buf; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
uint8_t m_UnalignedBuffer[sz + 15]; // up to 15 bytes alignment
|
||
|
uint8_t * m_Buf;
|
||
|
};
|
||
|
|
||
|
|
||
11 years ago
|
#ifdef AESNI
|
||
11 years ago
|
class ECBCryptoAESNI
|
||
11 years ago
|
{
|
||
11 years ago
|
public:
|
||
|
|
||
11 years ago
|
uint8_t * GetKeySchedule () { return m_KeySchedule; };
|
||
10 years ago
|
|
||
11 years ago
|
protected:
|
||
|
|
||
10 years ago
|
void ExpandKey (const AESKey& key);
|
||
11 years ago
|
|
||
10 years ago
|
private:
|
||
11 years ago
|
|
||
10 years ago
|
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
|
||
11 years ago
|
};
|
||
|
|
||
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { ExpandKey (key); };
|
||
11 years ago
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out);
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
11 years ago
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key);
|
||
11 years ago
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out);
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
typedef ECBEncryptionAESNI ECBEncryption;
|
||
|
typedef ECBDecryptionAESNI ECBDecryption;
|
||
|
|
||
9 years ago
|
#else // use openssl
|
||
11 years ago
|
|
||
|
class ECBEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key)
|
||
11 years ago
|
{
|
||
9 years ago
|
AES_set_encrypt_key (key, 256, &m_Key);
|
||
11 years ago
|
}
|
||
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
||
|
{
|
||
9 years ago
|
AES_encrypt (in->buf, out->buf, &m_Key);
|
||
11 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
AES_KEY m_Key;
|
||
11 years ago
|
};
|
||
|
|
||
|
class ECBDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key)
|
||
11 years ago
|
{
|
||
9 years ago
|
AES_set_decrypt_key (key, 256, &m_Key);
|
||
11 years ago
|
}
|
||
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
||
|
{
|
||
9 years ago
|
AES_decrypt (in->buf, out->buf, &m_Key);
|
||
11 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
AES_KEY m_Key;
|
||
11 years ago
|
};
|
||
|
|
||
|
|
||
11 years ago
|
#endif
|
||
|
|
||
11 years ago
|
class CBCEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
||
11 years ago
|
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
||
11 years ago
|
|
||
|
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||
11 years ago
|
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
11 years ago
|
void Encrypt (const uint8_t * in, uint8_t * out); // one block
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
|
ChipherBlock m_LastBlock;
|
||
11 years ago
|
|
||
11 years ago
|
ECBEncryption m_ECBEncryption;
|
||
11 years ago
|
};
|
||
|
|
||
|
class CBCDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
CBCDecryption () { memset (m_IV.buf, 0, 16); };
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
|
||
11 years ago
|
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
||
11 years ago
|
|
||
|
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||
11 years ago
|
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
11 years ago
|
void Decrypt (const uint8_t * in, uint8_t * out); // one block
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
|
ChipherBlock m_IV;
|
||
11 years ago
|
ECBDecryption m_ECBDecryption;
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
|
class TunnelEncryption // with double IV encryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
||
11 years ago
|
{
|
||
|
m_LayerEncryption.SetKey (layerKey);
|
||
|
m_IVEncryption.SetKey (ivKey);
|
||
|
}
|
||
|
|
||
10 years ago
|
void Encrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
|
ECBEncryption m_IVEncryption;
|
||
11 years ago
|
#ifdef AESNI
|
||
11 years ago
|
ECBEncryption m_LayerEncryption;
|
||
|
#else
|
||
11 years ago
|
CBCEncryption m_LayerEncryption;
|
||
11 years ago
|
#endif
|
||
11 years ago
|
};
|
||
|
|
||
|
class TunnelDecryption // with double IV encryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
||
11 years ago
|
{
|
||
|
m_LayerDecryption.SetKey (layerKey);
|
||
|
m_IVDecryption.SetKey (ivKey);
|
||
|
}
|
||
|
|
||
10 years ago
|
void Decrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
|
ECBDecryption m_IVDecryption;
|
||
11 years ago
|
#ifdef AESNI
|
||
11 years ago
|
ECBDecryption m_LayerDecryption;
|
||
|
#else
|
||
11 years ago
|
CBCDecryption m_LayerDecryption;
|
||
11 years ago
|
#endif
|
||
9 years ago
|
};
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
#endif
|