2018-02-01 13:21:00 +00:00
|
|
|
#include <assert.h>
|
2018-01-25 16:24:33 +00:00
|
|
|
#include <llarp/crypto.h>
|
2018-10-23 12:57:57 +00:00
|
|
|
#include <sodium/crypto_generichash.h>
|
|
|
|
#include <sodium/crypto_sign.h>
|
|
|
|
#include <sodium/crypto_scalarmult.h>
|
2018-07-27 02:34:09 +00:00
|
|
|
#include <sodium/crypto_stream_xchacha20.h>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-05-23 20:37:43 +00:00
|
|
|
#include "mem.hpp"
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-10-23 12:57:57 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
extern int
|
|
|
|
sodium_init(void);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace sodium
|
|
|
|
{
|
|
|
|
static bool
|
2018-06-12 11:57:14 +00:00
|
|
|
xchacha20(llarp_buffer_t buff, const byte_t *k, const byte_t *n)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-07-24 07:13:09 +00:00
|
|
|
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz, n, k)
|
2018-05-22 15:54:19 +00:00
|
|
|
== 0;
|
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
2018-08-13 23:22:31 +00:00
|
|
|
dh(uint8_t *out, const uint8_t *client_pk, const uint8_t *server_pk,
|
|
|
|
const uint8_t *themPub, const uint8_t *usSec)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret shared;
|
2018-05-22 15:54:19 +00:00
|
|
|
crypto_generichash_state h;
|
|
|
|
const size_t outsz = SHAREDKEYSIZE;
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2018-06-20 12:34:48 +00:00
|
|
|
if(crypto_scalarmult_curve25519(shared, usSec, themPub))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-10-23 11:29:37 +00:00
|
|
|
crypto_generichash_blake2b_init(&h, nullptr, 0U, outsz);
|
|
|
|
crypto_generichash_blake2b_update(&h, client_pk, 32);
|
|
|
|
crypto_generichash_blake2b_update(&h, server_pk, 32);
|
|
|
|
crypto_generichash_blake2b_update(&h, shared, 32);
|
|
|
|
crypto_generichash_blake2b_final(&h, out, outsz);
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
2018-08-13 23:22:31 +00:00
|
|
|
dh_client(uint8_t *shared, const uint8_t *pk, const uint8_t *sk,
|
|
|
|
const uint8_t *n)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret dh_result;
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
if(dh(dh_result, llarp::seckey_topublic(sk), pk, pk, sk))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(shared, 32, n, 32, dh_result, 32)
|
|
|
|
!= -1;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-08-13 08:47:24 +00:00
|
|
|
llarp::LogWarn("crypto::dh_client - dh failed");
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-18 16:08:47 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
2018-08-13 23:22:31 +00:00
|
|
|
dh_server(uint8_t *shared, const uint8_t *pk, const uint8_t *sk,
|
|
|
|
const uint8_t *n)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret dh_result;
|
2018-06-10 14:05:48 +00:00
|
|
|
if(dh(dh_result, pk, llarp::seckey_topublic(sk), pk, sk))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(shared, 32, n, 32, dh_result, 32)
|
|
|
|
!= -1;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-08-13 08:47:24 +00:00
|
|
|
llarp::LogWarn("crypto::dh_server - dh failed");
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-18 16:08:47 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
hash(uint8_t *result, llarp_buffer_t buff)
|
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
|
|
|
|
nullptr, 0)
|
2018-05-22 15:54:19 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-05-18 16:08:47 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
shorthash(uint8_t *result, llarp_buffer_t buff)
|
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(result, SHORTHASHSIZE, buff.base,
|
|
|
|
buff.sz, nullptr, 0)
|
2018-05-22 15:54:19 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
hmac(uint8_t *result, llarp_buffer_t buff, const uint8_t *secret)
|
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(result, HMACSIZE, buff.base, buff.sz,
|
|
|
|
secret, HMACSECSIZE)
|
2018-05-22 15:54:19 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
sign(uint8_t *result, const uint8_t *secret, llarp_buffer_t buff)
|
|
|
|
{
|
|
|
|
return crypto_sign_detached(result, nullptr, buff.base, buff.sz, secret)
|
|
|
|
!= -1;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
verify(const uint8_t *pub, llarp_buffer_t buff, const uint8_t *sig)
|
|
|
|
{
|
|
|
|
return crypto_sign_verify_detached(sig, buff.base, buff.sz, pub) != -1;
|
|
|
|
}
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
randomize(llarp_buffer_t buff)
|
|
|
|
{
|
|
|
|
randombytes((unsigned char *)buff.base, buff.sz);
|
|
|
|
}
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static inline void
|
|
|
|
randbytes(void *ptr, size_t sz)
|
|
|
|
{
|
|
|
|
randombytes((unsigned char *)ptr, sz);
|
|
|
|
}
|
2018-02-01 22:34:04 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
2018-05-23 20:37:43 +00:00
|
|
|
sigkeygen(uint8_t *keys)
|
|
|
|
{
|
|
|
|
crypto_sign_keypair(keys + 32, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enckeygen(uint8_t *keys)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
randombytes(keys, 32);
|
|
|
|
crypto_scalarmult_curve25519_base(keys + 32, keys);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
} // namespace sodium
|
2018-06-10 14:05:48 +00:00
|
|
|
|
|
|
|
const byte_t *
|
|
|
|
seckey_topublic(const byte_t *sec)
|
|
|
|
{
|
|
|
|
return sec + 32;
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
namespace pq
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-08-13 23:22:31 +00:00
|
|
|
bool
|
|
|
|
encrypt(byte_t *ciphertext, byte_t *sharedkey, const byte_t *pubkey)
|
|
|
|
{
|
|
|
|
return crypto_kem_enc(ciphertext, sharedkey, pubkey) != -1;
|
|
|
|
}
|
|
|
|
bool
|
|
|
|
decrypt(const byte_t *ciphertext, byte_t *sharedkey,
|
|
|
|
const byte_t *secretkey)
|
|
|
|
{
|
|
|
|
return crypto_kem_dec(sharedkey, ciphertext, secretkey) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
keygen(byte_t *keypair)
|
|
|
|
{
|
|
|
|
crypto_kem_keypair(keypair + PQ_SECRETKEYSIZE, keypair);
|
|
|
|
}
|
|
|
|
} // namespace pq
|
|
|
|
|
|
|
|
const byte_t *
|
|
|
|
pq_keypair_to_public(const byte_t *k)
|
|
|
|
{
|
|
|
|
return k + PQ_SECRETKEYSIZE;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
const byte_t *
|
|
|
|
pq_keypair_to_secret(const byte_t *k)
|
|
|
|
{
|
|
|
|
return k;
|
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
} // namespace llarp
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-06-20 17:45:44 +00:00
|
|
|
void
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(struct llarp_crypto *c)
|
2018-06-20 17:45:44 +00:00
|
|
|
{
|
2018-11-13 15:16:14 +00:00
|
|
|
assert(sodium_init() == 0);
|
2018-08-14 21:28:14 +00:00
|
|
|
char *avx2 = getenv("AVX2_FORCE_DISABLE");
|
|
|
|
if(avx2 && std::string(avx2) == "1")
|
|
|
|
ntru_init(1);
|
|
|
|
else
|
|
|
|
ntru_init(0);
|
2018-06-20 17:45:44 +00:00
|
|
|
c->xchacha20 = llarp::sodium::xchacha20;
|
|
|
|
c->dh_client = llarp::sodium::dh_client;
|
|
|
|
c->dh_server = llarp::sodium::dh_server;
|
|
|
|
c->transport_dh_client = llarp::sodium::dh_client;
|
|
|
|
c->transport_dh_server = llarp::sodium::dh_server;
|
|
|
|
c->hash = llarp::sodium::hash;
|
|
|
|
c->shorthash = llarp::sodium::shorthash;
|
|
|
|
c->hmac = llarp::sodium::hmac;
|
|
|
|
c->sign = llarp::sodium::sign;
|
|
|
|
c->verify = llarp::sodium::verify;
|
|
|
|
c->randomize = llarp::sodium::randomize;
|
|
|
|
c->randbytes = llarp::sodium::randbytes;
|
|
|
|
c->identity_keygen = llarp::sodium::sigkeygen;
|
|
|
|
c->encryption_keygen = llarp::sodium::enckeygen;
|
2018-08-13 23:22:31 +00:00
|
|
|
c->pqe_encrypt = llarp::pq::encrypt;
|
|
|
|
c->pqe_decrypt = llarp::pq::decrypt;
|
|
|
|
c->pqe_keygen = llarp::pq::keygen;
|
2018-06-20 17:45:44 +00:00
|
|
|
int seed;
|
|
|
|
c->randbytes(&seed, sizeof(seed));
|
|
|
|
srand(seed);
|
|
|
|
}
|
2018-07-20 04:50:28 +00:00
|
|
|
|
|
|
|
uint64_t
|
|
|
|
llarp_randint()
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
randombytes((byte_t *)&i, sizeof(i));
|
|
|
|
return i;
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|