mirror of https://github.com/PurpleI2P/i2pd
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
364 lines
8.5 KiB
C
364 lines
8.5 KiB
C
9 years ago
|
#ifndef CRYPTO_H__
|
||
|
#define CRYPTO_H__
|
||
10 years ago
|
|
||
|
#include <inttypes.h>
|
||
9 years ago
|
#include <string>
|
||
8 years ago
|
#include <openssl/bn.h>
|
||
|
#include <openssl/dh.h>
|
||
|
#include <openssl/aes.h>
|
||
|
#include <openssl/dsa.h>
|
||
8 years ago
|
#include <openssl/ecdsa.h>
|
||
8 years ago
|
#include <openssl/rsa.h>
|
||
8 years ago
|
#include <openssl/sha.h>
|
||
8 years ago
|
#include <openssl/evp.h>
|
||
8 years ago
|
#include <openssl/rand.h>
|
||
8 years ago
|
#include <openssl/engine.h>
|
||
8 years ago
|
|
||
9 years ago
|
#include "Base.h"
|
||
8 years ago
|
#include "Tag.h"
|
||
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace crypto
|
||
9 years ago
|
{
|
||
9 years ago
|
bool bn2buf (const BIGNUM * bn, uint8_t * buf, size_t len);
|
||
9 years ago
|
|
||
|
// DSA
|
||
9 years ago
|
DSA * CreateDSA ();
|
||
9 years ago
|
|
||
|
// RSA
|
||
9 years ago
|
const BIGNUM * GetRSAE ();
|
||
9 years ago
|
|
||
|
// DH
|
||
|
class DHKeys
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
DHKeys ();
|
||
|
~DHKeys ();
|
||
|
|
||
8 years ago
|
void GenerateKeys ();
|
||
|
const uint8_t * GetPublicKey () const { return m_PublicKey; };
|
||
9 years ago
|
void Agree (const uint8_t * pub, uint8_t * shared);
|
||
|
|
||
|
private:
|
||
|
|
||
|
DH * m_DH;
|
||
|
uint8_t m_PublicKey[256];
|
||
|
};
|
||
|
|
||
|
// ElGamal
|
||
8 years ago
|
void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding = false);
|
||
|
bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding = false);
|
||
9 years ago
|
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
|
||
10 years ago
|
{
|
||
|
uint8_t buf[16];
|
||
10 years ago
|
|
||
10 years ago
|
void operator^=(const ChipherBlock& other) // XOR
|
||
10 years ago
|
{
|
||
8 years ago
|
#if defined(__AVX__) // AVX
|
||
|
__asm__
|
||
|
(
|
||
|
"vmovups (%[buf]), %%xmm0 \n"
|
||
|
"vmovups (%[other]), %%xmm1 \n"
|
||
|
"vxorps %%xmm0, %%xmm1, %%xmm0 \n"
|
||
|
"vmovups %%xmm0, (%[buf]) \n"
|
||
|
:
|
||
|
: [buf]"r"(buf), [other]"r"(other.buf)
|
||
|
: "%xmm0", "%xmm1", "memory"
|
||
|
);
|
||
|
#elif defined(__SSE__) // SSE
|
||
10 years ago
|
__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
|
||
10 years ago
|
}
|
||
10 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; };
|
||
8 years ago
|
ChipherBlock * GetChipherBlock () { return (ChipherBlock *)m_Buf; };
|
||
|
const ChipherBlock * GetChipherBlock () const { return (const ChipherBlock *)m_Buf; };
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
|
uint8_t m_UnalignedBuffer[sz + 15]; // up to 15 bytes alignment
|
||
|
uint8_t * m_Buf;
|
||
|
};
|
||
|
|
||
|
|
||
10 years ago
|
#ifdef AESNI
|
||
10 years ago
|
class ECBCryptoAESNI
|
||
10 years ago
|
{
|
||
10 years ago
|
public:
|
||
|
|
||
10 years ago
|
uint8_t * GetKeySchedule () { return m_KeySchedule; };
|
||
10 years ago
|
|
||
10 years ago
|
protected:
|
||
|
|
||
10 years ago
|
void ExpandKey (const AESKey& key);
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
10 years ago
|
|
||
10 years ago
|
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
|
||
10 years ago
|
};
|
||
|
|
||
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { ExpandKey (key); };
|
||
10 years ago
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out);
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
10 years ago
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key);
|
||
10 years ago
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out);
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
typedef ECBEncryptionAESNI ECBEncryption;
|
||
|
typedef ECBDecryptionAESNI ECBDecryption;
|
||
|
|
||
9 years ago
|
#else // use openssl
|
||
10 years ago
|
|
||
|
class ECBEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key)
|
||
10 years ago
|
{
|
||
9 years ago
|
AES_set_encrypt_key (key, 256, &m_Key);
|
||
10 years ago
|
}
|
||
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
||
|
{
|
||
9 years ago
|
AES_encrypt (in->buf, out->buf, &m_Key);
|
||
10 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
AES_KEY m_Key;
|
||
10 years ago
|
};
|
||
|
|
||
|
class ECBDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key)
|
||
10 years ago
|
{
|
||
9 years ago
|
AES_set_decrypt_key (key, 256, &m_Key);
|
||
10 years ago
|
}
|
||
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
||
|
{
|
||
9 years ago
|
AES_decrypt (in->buf, out->buf, &m_Key);
|
||
10 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
AES_KEY m_Key;
|
||
10 years ago
|
};
|
||
|
|
||
|
|
||
10 years ago
|
#endif
|
||
|
|
||
10 years ago
|
class CBCEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
CBCEncryption () { memset ((uint8_t *)m_LastBlock, 0, 16); };
|
||
10 years ago
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
||
8 years ago
|
void SetIV (const uint8_t * iv) { memcpy ((uint8_t *)m_LastBlock, iv, 16); }; // 16 bytes
|
||
10 years ago
|
|
||
|
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||
10 years ago
|
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
10 years ago
|
void Encrypt (const uint8_t * in, uint8_t * out); // one block
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
8 years ago
|
AESAlignedBuffer<16> m_LastBlock;
|
||
10 years ago
|
|
||
10 years ago
|
ECBEncryption m_ECBEncryption;
|
||
10 years ago
|
};
|
||
|
|
||
|
class CBCDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
CBCDecryption () { memset ((uint8_t *)m_IV, 0, 16); };
|
||
10 years ago
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
|
||
8 years ago
|
void SetIV (const uint8_t * iv) { memcpy ((uint8_t *)m_IV, iv, 16); }; // 16 bytes
|
||
10 years ago
|
|
||
|
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||
10 years ago
|
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
10 years ago
|
void Decrypt (const uint8_t * in, uint8_t * out); // one block
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
8 years ago
|
AESAlignedBuffer<16> m_IV;
|
||
10 years ago
|
ECBDecryption m_ECBDecryption;
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
|
class TunnelEncryption // with double IV encryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
||
10 years ago
|
{
|
||
|
m_LayerEncryption.SetKey (layerKey);
|
||
|
m_IVEncryption.SetKey (ivKey);
|
||
|
}
|
||
|
|
||
9 years ago
|
void Encrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
ECBEncryption m_IVEncryption;
|
||
10 years ago
|
#ifdef AESNI
|
||
10 years ago
|
ECBEncryption m_LayerEncryption;
|
||
|
#else
|
||
10 years ago
|
CBCEncryption m_LayerEncryption;
|
||
10 years ago
|
#endif
|
||
10 years ago
|
};
|
||
|
|
||
|
class TunnelDecryption // with double IV encryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
|
||
10 years ago
|
{
|
||
|
m_LayerDecryption.SetKey (layerKey);
|
||
|
m_IVDecryption.SetKey (ivKey);
|
||
|
}
|
||
|
|
||
9 years ago
|
void Decrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
ECBDecryption m_IVDecryption;
|
||
10 years ago
|
#ifdef AESNI
|
||
10 years ago
|
ECBDecryption m_LayerDecryption;
|
||
|
#else
|
||
10 years ago
|
CBCDecryption m_LayerDecryption;
|
||
10 years ago
|
#endif
|
||
9 years ago
|
};
|
||
8 years ago
|
|
||
8 years ago
|
void InitCrypto (bool precomputation);
|
||
9 years ago
|
void TerminateCrypto ();
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
// take care about openssl version
|
||
|
#include <openssl/opensslv.h>
|
||
8 years ago
|
#if (OPENSSL_VERSION_NUMBER < 0x010100000) || defined(LIBRESSL_VERSION_NUMBER) // 1.1.0 or LibreSSL
|
||
8 years ago
|
// define getters and setters introduced in 1.1.0
|
||
8 years ago
|
inline int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
|
||
8 years ago
|
{
|
||
|
if (d->p) BN_free (d->p);
|
||
|
if (d->q) BN_free (d->q);
|
||
|
if (d->g) BN_free (d->g);
|
||
|
d->p = p; d->q = q; d->g = g; return 1;
|
||
|
}
|
||
8 years ago
|
inline int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
|
||
8 years ago
|
{
|
||
|
if (d->pub_key) BN_free (d->pub_key);
|
||
|
if (d->priv_key) BN_free (d->priv_key);
|
||
|
d->pub_key = pub_key; d->priv_key = priv_key; return 1;
|
||
|
}
|
||
8 years ago
|
inline void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
|
||
|
{ *pub_key = d->pub_key; *priv_key = d->priv_key; }
|
||
|
inline int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
|
||
8 years ago
|
{
|
||
|
if (sig->r) BN_free (sig->r);
|
||
|
if (sig->s) BN_free (sig->s);
|
||
|
sig->r = r; sig->s = s; return 1;
|
||
|
}
|
||
8 years ago
|
inline void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
|
||
|
{ *pr = sig->r; *ps = sig->s; }
|
||
|
|
||
8 years ago
|
inline int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
|
||
8 years ago
|
{
|
||
|
if (sig->r) BN_free (sig->r);
|
||
|
if (sig->s) BN_free (sig->s);
|
||
|
sig->r = r; sig->s = s; return 1;
|
||
|
}
|
||
8 years ago
|
inline void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
|
||
|
{ *pr = sig->r; *ps = sig->s; }
|
||
|
|
||
8 years ago
|
inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
|
||
8 years ago
|
{
|
||
|
if (r->n) BN_free (r->n);
|
||
|
if (r->e) BN_free (r->e);
|
||
|
if (r->d) BN_free (r->d);
|
||
|
r->n = n; r->e = e; r->d = d; return 1;
|
||
|
}
|
||
8 years ago
|
inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
|
||
|
{ *n = r->n; *e = r->e; *d = r->d; }
|
||
8 years ago
|
|
||
8 years ago
|
inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
|
||
8 years ago
|
{
|
||
|
if (dh->p) BN_free (dh->p);
|
||
|
if (dh->q) BN_free (dh->q);
|
||
|
if (dh->g) BN_free (dh->g);
|
||
|
dh->p = p; dh->q = q; dh->g = g; return 1;
|
||
|
}
|
||
8 years ago
|
inline int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
|
||
|
{
|
||
|
if (dh->pub_key) BN_free (dh->pub_key);
|
||
|
if (dh->priv_key) BN_free (dh->priv_key);
|
||
|
dh->pub_key = pub_key; dh->priv_key = priv_key; return 1;
|
||
|
}
|
||
|
inline void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
|
||
|
{ *pub_key = dh->pub_key; *priv_key = dh->priv_key; }
|
||
|
|
||
8 years ago
|
inline RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
|
||
|
{ return pkey->pkey.rsa; }
|
||
8 years ago
|
|
||
8 years ago
|
inline EVP_MD_CTX *EVP_MD_CTX_new ()
|
||
|
{ return EVP_MD_CTX_create(); }
|
||
|
inline void EVP_MD_CTX_free (EVP_MD_CTX *ctx)
|
||
|
{ EVP_MD_CTX_destroy (ctx); }
|
||
|
|
||
8 years ago
|
// ssl
|
||
|
#define TLS_method TLSv1_method
|
||
|
|
||
8 years ago
|
#endif
|
||
|
|
||
10 years ago
|
#endif
|