2015-11-03 14:15:49 +00:00
|
|
|
#ifndef CRYPTO_H__
|
|
|
|
#define CRYPTO_H__
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
#include <inttypes.h>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include <string>
|
2016-05-11 20:02:26 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rand.h>
|
2016-06-28 00:00:00 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Base.h"
|
2016-06-28 00:00:00 +00:00
|
|
|
#include "Tag.h"
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
2015-11-03 14:15:49 +00:00
|
|
|
{
|
2015-12-18 15:09:25 +00:00
|
|
|
bool bn2buf (const BIGNUM * bn, uint8_t * buf, size_t len);
|
2015-11-03 14:15:49 +00:00
|
|
|
|
|
|
|
// DSA
|
2015-12-18 15:09:25 +00:00
|
|
|
DSA * CreateDSA ();
|
2015-11-03 14:15:49 +00:00
|
|
|
|
|
|
|
// RSA
|
2015-12-18 15:09:25 +00:00
|
|
|
const BIGNUM * GetRSAE ();
|
2015-11-03 14:15:49 +00:00
|
|
|
|
|
|
|
// 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
|
2014-11-19 16:01:04 +00:00
|
|
|
struct ChipherBlock
|
2014-05-06 16:22:22 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[16];
|
2014-05-09 15:44:39 +00:00
|
|
|
|
2014-05-09 16:05:04 +00:00
|
|
|
void operator^=(const ChipherBlock& other) // XOR
|
2014-05-09 15:44:39 +00:00
|
|
|
{
|
2016-02-05 20:58:14 +00:00
|
|
|
#if defined(__x86_64__) || defined(__SSE__) // for Intel x84 or with SSE
|
2014-11-19 16:01:04 +00:00
|
|
|
__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
|
2014-05-09 15:44:39 +00:00
|
|
|
}
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
2014-11-01 18:56:13 +00:00
|
|
|
typedef i2p::data::Tag<32> AESKey;
|
|
|
|
|
2014-11-18 17:11:45 +00:00
|
|
|
template<size_t sz>
|
|
|
|
class AESAlignedBuffer // 16 bytes alignment
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
AESAlignedBuffer ()
|
|
|
|
{
|
|
|
|
m_Buf = m_UnalignedBuffer;
|
2014-11-26 16:04:49 +00:00
|
|
|
uint8_t rem = ((size_t)m_Buf) & 0x0f;
|
2014-11-18 17:11:45 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-02 14:05:04 +00:00
|
|
|
#ifdef AESNI
|
2014-05-07 19:40:24 +00:00
|
|
|
class ECBCryptoAESNI
|
2014-05-08 19:11:38 +00:00
|
|
|
{
|
2014-05-09 01:43:08 +00:00
|
|
|
public:
|
|
|
|
|
2014-05-13 02:51:59 +00:00
|
|
|
uint8_t * GetKeySchedule () { return m_KeySchedule; };
|
2014-11-18 17:11:45 +00:00
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
protected:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void ExpandKey (const AESKey& key);
|
2014-05-08 19:11:38 +00:00
|
|
|
|
2014-11-18 17:11:45 +00:00
|
|
|
private:
|
2014-05-08 19:11:38 +00:00
|
|
|
|
2014-11-18 17:11:45 +00:00
|
|
|
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
|
2014-05-08 19:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
2014-05-07 18:48:37 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key) { ExpandKey (key); };
|
2014-05-07 19:39:30 +00:00
|
|
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out);
|
2014-05-08 19:11:38 +00:00
|
|
|
};
|
2014-05-07 18:48:37 +00:00
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key);
|
2014-05-08 19:11:38 +00:00
|
|
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out);
|
2014-05-07 18:48:37 +00:00
|
|
|
};
|
|
|
|
|
2014-05-08 20:49:00 +00:00
|
|
|
typedef ECBEncryptionAESNI ECBEncryption;
|
|
|
|
typedef ECBDecryptionAESNI ECBDecryption;
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
#else // use openssl
|
2014-05-08 20:49:00 +00:00
|
|
|
|
|
|
|
class ECBEncryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key)
|
2014-05-08 20:49:00 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_set_encrypt_key (key, 256, &m_Key);
|
2014-05-08 20:49:00 +00:00
|
|
|
}
|
|
|
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_encrypt (in->buf, out->buf, &m_Key);
|
2014-05-08 20:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_KEY m_Key;
|
2014-05-08 20:49:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ECBDecryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key)
|
2014-05-08 20:49:00 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_set_decrypt_key (key, 256, &m_Key);
|
2014-05-08 20:49:00 +00:00
|
|
|
}
|
|
|
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_decrypt (in->buf, out->buf, &m_Key);
|
2014-05-08 20:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
AES_KEY m_Key;
|
2014-05-08 20:49:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-07 18:48:37 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-06 16:22:22 +00:00
|
|
|
class CBCEncryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
2014-05-07 02:30:09 +00:00
|
|
|
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
2014-06-08 11:56:04 +00:00
|
|
|
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
2014-05-14 18:54:01 +00:00
|
|
|
void Encrypt (const uint8_t * in, uint8_t * out); // one block
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ChipherBlock m_LastBlock;
|
2014-05-13 02:51:59 +00:00
|
|
|
|
2014-05-09 01:43:08 +00:00
|
|
|
ECBEncryption m_ECBEncryption;
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CBCDecryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CBCDecryption () { memset (m_IV.buf, 0, 16); };
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKey (const AESKey& key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
|
2014-05-07 02:30:09 +00:00
|
|
|
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
2014-06-08 11:56:04 +00:00
|
|
|
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
2014-05-14 18:54:01 +00:00
|
|
|
void Decrypt (const uint8_t * in, uint8_t * out); // one block
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ChipherBlock m_IV;
|
2014-05-09 01:43:08 +00:00
|
|
|
ECBDecryption m_ECBDecryption;
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
2014-05-15 15:21:41 +00:00
|
|
|
|
|
|
|
class TunnelEncryption // with double IV encryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
2014-05-15 15:21:41 +00:00
|
|
|
{
|
|
|
|
m_LayerEncryption.SetKey (layerKey);
|
|
|
|
m_IVEncryption.SetKey (ivKey);
|
|
|
|
}
|
|
|
|
|
2015-06-21 21:05:01 +00:00
|
|
|
void Encrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
2014-05-15 15:21:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ECBEncryption m_IVEncryption;
|
2014-06-02 14:05:04 +00:00
|
|
|
#ifdef AESNI
|
2014-05-15 16:59:07 +00:00
|
|
|
ECBEncryption m_LayerEncryption;
|
|
|
|
#else
|
2014-05-15 15:21:41 +00:00
|
|
|
CBCEncryption m_LayerEncryption;
|
2014-05-15 16:59:07 +00:00
|
|
|
#endif
|
2014-05-15 15:21:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TunnelDecryption // with double IV encryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
2014-05-15 15:21:41 +00:00
|
|
|
{
|
|
|
|
m_LayerDecryption.SetKey (layerKey);
|
|
|
|
m_IVDecryption.SetKey (ivKey);
|
|
|
|
}
|
|
|
|
|
2015-06-21 21:05:01 +00:00
|
|
|
void Decrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
2014-05-15 15:21:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ECBDecryption m_IVDecryption;
|
2014-06-02 14:05:04 +00:00
|
|
|
#ifdef AESNI
|
2014-05-15 17:10:07 +00:00
|
|
|
ECBDecryption m_LayerDecryption;
|
|
|
|
#else
|
2014-05-15 15:21:41 +00:00
|
|
|
CBCDecryption m_LayerDecryption;
|
2014-05-15 17:10:07 +00:00
|
|
|
#endif
|
2015-11-03 14:15:49 +00:00
|
|
|
};
|
2015-12-31 21:02:10 +00:00
|
|
|
|
2016-04-17 20:57:58 +00:00
|
|
|
void InitCrypto (bool precomputation);
|
2015-12-31 21:02:10 +00:00
|
|
|
void TerminateCrypto ();
|
2016-10-12 16:31:27 +00:00
|
|
|
|
|
|
|
// take care about openssl version
|
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x010100000) // 1.1.0
|
|
|
|
// define getters and setters introduced in 1.1.0
|
|
|
|
inline int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) { d->p = p; d->q = q; d->g = g; return 1; }
|
|
|
|
inline int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) { d->pub_key = pub_key; d->priv_key = priv_key; return 1; }
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
#endif
|