2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2018-10-23 12:57:57 +00:00
|
|
|
#include <sodium/crypto_generichash.h>
|
|
|
|
#include <sodium/crypto_sign.h>
|
|
|
|
#include <sodium/crypto_scalarmult.h>
|
2020-01-28 21:55:36 +00:00
|
|
|
#include <sodium/crypto_scalarmult_ed25519.h>
|
2018-07-27 02:34:09 +00:00
|
|
|
#include <sodium/crypto_stream_xchacha20.h>
|
2020-01-27 21:30:41 +00:00
|
|
|
#include <sodium/crypto_core_ed25519.h>
|
2020-01-31 04:27:13 +00:00
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
#include <sodium/utils.h>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.hpp>
|
2020-01-27 21:30:41 +00:00
|
|
|
#include <util/endian.hpp>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cassert>
|
2020-01-31 04:27:13 +00:00
|
|
|
#include <cstring>
|
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
|
2019-01-02 01:04:03 +00:00
|
|
|
dh(llarp::SharedSecret &out, const PubKey &client_pk,
|
2019-01-02 01:04:04 +00:00
|
|
|
const PubKey &server_pk, const uint8_t *themPub, const SecretKey &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;
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2019-01-02 01:04:08 +00:00
|
|
|
if(crypto_scalarmult_curve25519(shared.data(), usSec.data(), themPub))
|
2019-01-26 15:40:58 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2019-01-26 15:40:58 +00:00
|
|
|
}
|
2019-01-13 16:30:07 +00:00
|
|
|
crypto_generichash_blake2b_init(&h, nullptr, 0U, shared.size());
|
2019-01-02 01:04:04 +00:00
|
|
|
crypto_generichash_blake2b_update(&h, client_pk.data(), 32);
|
|
|
|
crypto_generichash_blake2b_update(&h, server_pk.data(), 32);
|
|
|
|
crypto_generichash_blake2b_update(&h, shared.data(), 32);
|
2019-01-13 16:30:07 +00:00
|
|
|
crypto_generichash_blake2b_final(&h, out.data(), shared.size());
|
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
|
2019-01-26 15:40:58 +00:00
|
|
|
dh_client_priv(llarp::SharedSecret &shared, const PubKey &pk,
|
|
|
|
const SecretKey &sk, const TunnelNonce &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
|
|
|
|
2019-01-02 01:04:08 +00:00
|
|
|
if(dh(dh_result, sk.toPublic(), pk, pk.data(), sk))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-02 01:04:08 +00:00
|
|
|
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
|
2019-01-02 01:04:04 +00:00
|
|
|
dh_result.data(), 32)
|
2018-10-23 11:29:37 +00:00
|
|
|
!= -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
|
2019-01-26 15:40:58 +00:00
|
|
|
dh_server_priv(llarp::SharedSecret &shared, const PubKey &pk,
|
|
|
|
const SecretKey &sk, const TunnelNonce &n)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret dh_result;
|
2019-01-02 01:04:08 +00:00
|
|
|
if(dh(dh_result, pk, sk.toPublic(), pk.data(), sk))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-02 01:04:08 +00:00
|
|
|
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
|
2019-01-02 01:04:04 +00:00
|
|
|
dh_result.data(), 32)
|
2018-10-23 11:29:37 +00:00
|
|
|
!= -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
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
CryptoLibSodium::CryptoLibSodium()
|
|
|
|
{
|
|
|
|
if(sodium_init() == -1)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("sodium_init() returned -1");
|
|
|
|
}
|
|
|
|
char *avx2 = std::getenv("AVX2_FORCE_DISABLE");
|
|
|
|
if(avx2 && std::string(avx2) == "1")
|
|
|
|
{
|
|
|
|
ntru_init(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ntru_init(0);
|
|
|
|
}
|
|
|
|
int seed = 0;
|
2019-05-28 19:45:09 +00:00
|
|
|
randombytes(reinterpret_cast< unsigned char * >(&seed), sizeof(seed));
|
2019-01-26 15:40:58 +00:00
|
|
|
srand(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
CryptoLibSodium::xchacha20(const llarp_buffer_t &buff,
|
|
|
|
const SharedSecret &k, const TunnelNonce &n)
|
2019-01-26 15:40:58 +00:00
|
|
|
{
|
|
|
|
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz,
|
|
|
|
n.data(), k.data())
|
|
|
|
== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
CryptoLibSodium::xchacha20_alt(const llarp_buffer_t &out,
|
|
|
|
const llarp_buffer_t &in,
|
2019-01-26 15:40:58 +00:00
|
|
|
const SharedSecret &k, const byte_t *n)
|
|
|
|
{
|
|
|
|
if(in.sz > out.sz)
|
|
|
|
return false;
|
|
|
|
return crypto_stream_xchacha20_xor(out.base, in.base, in.sz, n, k.data())
|
|
|
|
== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CryptoLibSodium::dh_client(llarp::SharedSecret &shared, const PubKey &pk,
|
|
|
|
const SecretKey &sk, const TunnelNonce &n)
|
|
|
|
{
|
|
|
|
return dh_client_priv(shared, pk, sk, n);
|
|
|
|
}
|
|
|
|
/// path dh relay side
|
|
|
|
bool
|
|
|
|
CryptoLibSodium::dh_server(llarp::SharedSecret &shared, const PubKey &pk,
|
|
|
|
const SecretKey &sk, const TunnelNonce &n)
|
|
|
|
{
|
|
|
|
return dh_server_priv(shared, pk, sk, n);
|
|
|
|
}
|
|
|
|
/// transport dh client side
|
|
|
|
bool
|
|
|
|
CryptoLibSodium::transport_dh_client(llarp::SharedSecret &shared,
|
|
|
|
const PubKey &pk, const SecretKey &sk,
|
|
|
|
const TunnelNonce &n)
|
|
|
|
{
|
|
|
|
return dh_client_priv(shared, pk, sk, n);
|
|
|
|
}
|
|
|
|
/// transport dh server side
|
|
|
|
bool
|
|
|
|
CryptoLibSodium::transport_dh_server(llarp::SharedSecret &shared,
|
|
|
|
const PubKey &pk, const SecretKey &sk,
|
|
|
|
const TunnelNonce &n)
|
|
|
|
{
|
|
|
|
return dh_server_priv(shared, pk, sk, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
CryptoLibSodium::shorthash(ShortHash &result, const llarp_buffer_t &buff)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-02 01:04:08 +00:00
|
|
|
return crypto_generichash_blake2b(result.data(), ShortHash::SIZE,
|
|
|
|
buff.base, buff.sz, nullptr, 0)
|
2018-05-22 15:54:19 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
CryptoLibSodium::hmac(byte_t *result, const llarp_buffer_t &buff,
|
2019-01-26 15:40:58 +00:00
|
|
|
const SharedSecret &secret)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(result, HMACSIZE, buff.base, buff.sz,
|
2019-01-02 01:04:04 +00:00
|
|
|
secret.data(), HMACSECSIZE)
|
2018-05-22 15:54:19 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
static bool
|
|
|
|
hash(uint8_t *result, const llarp_buffer_t &buff)
|
|
|
|
{
|
|
|
|
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
|
|
|
|
nullptr, 0)
|
|
|
|
!= -1;
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
bool
|
2019-03-20 14:51:10 +00:00
|
|
|
CryptoLibSodium::sign(Signature &sig, const SecretKey &secret,
|
|
|
|
const llarp_buffer_t &buf)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-10-14 13:47:06 +00:00
|
|
|
return crypto_sign_detached(sig.data(), nullptr, buf.base, buf.sz,
|
|
|
|
secret.data())
|
|
|
|
!= -1;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2020-01-31 04:27:13 +00:00
|
|
|
bool
|
2020-01-31 21:05:50 +00:00
|
|
|
CryptoLibSodium::sign(Signature &sig, const PrivateKey &privkey,
|
2020-01-31 04:27:13 +00:00
|
|
|
const llarp_buffer_t &buf)
|
|
|
|
{
|
|
|
|
PubKey pubkey;
|
|
|
|
|
2020-01-31 20:38:08 +00:00
|
|
|
privkey.toPublic(pubkey);
|
2020-01-31 04:27:13 +00:00
|
|
|
|
|
|
|
crypto_hash_sha512_state hs;
|
2020-01-31 21:05:50 +00:00
|
|
|
unsigned char nonce[64];
|
|
|
|
unsigned char hram[64];
|
|
|
|
unsigned char mulres[32];
|
|
|
|
|
|
|
|
// r = H(s || M) where here s is pseudorandom bytes typically generated as
|
|
|
|
// part of hashing the seed (i.e. [a,s] = H(k)), but for derived
|
|
|
|
// PrivateKeys will come from a hash of the root key's s concatenated with
|
|
|
|
// the derivation hash.
|
2020-01-31 04:27:13 +00:00
|
|
|
crypto_hash_sha512_init(&hs);
|
2020-02-02 20:06:32 +00:00
|
|
|
crypto_hash_sha512_update(&hs, privkey.signingHash(), 32);
|
2020-01-31 04:27:13 +00:00
|
|
|
crypto_hash_sha512_update(&hs, buf.base, buf.sz);
|
|
|
|
crypto_hash_sha512_final(&hs, nonce);
|
|
|
|
crypto_core_ed25519_scalar_reduce(nonce, nonce);
|
|
|
|
|
|
|
|
// copy pubkey into sig to make (for now) sig = (R || A)
|
2020-01-31 05:54:50 +00:00
|
|
|
memmove(sig.data() + 32, pubkey.data(), 32);
|
2020-01-31 04:27:13 +00:00
|
|
|
|
|
|
|
// R = r * B
|
2020-01-31 05:54:50 +00:00
|
|
|
crypto_scalarmult_ed25519_base_noclamp(sig.data(), nonce);
|
2020-01-31 04:27:13 +00:00
|
|
|
|
|
|
|
// hram = H(R || A || M)
|
|
|
|
crypto_hash_sha512_init(&hs);
|
|
|
|
crypto_hash_sha512_update(&hs, sig.data(), 64);
|
|
|
|
crypto_hash_sha512_update(&hs, buf.base, buf.sz);
|
|
|
|
crypto_hash_sha512_final(&hs, hram);
|
|
|
|
|
|
|
|
// S = r + H(R || A || M) * s, so sig = (R || S)
|
|
|
|
crypto_core_ed25519_scalar_reduce(hram, hram);
|
2020-01-31 20:38:08 +00:00
|
|
|
crypto_core_ed25519_scalar_mul(mulres, hram, privkey.data());
|
2020-01-31 05:54:50 +00:00
|
|
|
crypto_core_ed25519_scalar_add(sig.data() + 32, mulres, nonce);
|
2020-01-31 04:27:13 +00:00
|
|
|
|
|
|
|
sodium_memzero(nonce, sizeof nonce);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
bool
|
2019-03-20 14:51:10 +00:00
|
|
|
CryptoLibSodium::verify(const PubKey &pub, const llarp_buffer_t &buf,
|
2019-01-26 15:40:58 +00:00
|
|
|
const Signature &sig)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-10-14 13:47:06 +00:00
|
|
|
return crypto_sign_verify_detached(sig.data(), buf.base, buf.sz,
|
|
|
|
pub.data())
|
|
|
|
!= -1;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2020-01-28 21:55:36 +00:00
|
|
|
/// clamp a 32 byte ec point
|
|
|
|
static void
|
|
|
|
clamp_ed25519(byte_t *out)
|
|
|
|
{
|
|
|
|
out[0] &= 248;
|
|
|
|
out[31] &= 127;
|
|
|
|
out[31] |= 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename K >
|
|
|
|
static K
|
|
|
|
clamp(const K &p)
|
|
|
|
{
|
|
|
|
K out = p;
|
|
|
|
clamp_ed25519(out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename K >
|
|
|
|
static bool
|
|
|
|
is_clamped(const K &key)
|
|
|
|
{
|
|
|
|
K other(key);
|
|
|
|
clamp_ed25519(other.data());
|
|
|
|
return other == key;
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:05:50 +00:00
|
|
|
constexpr static char derived_key_hash_str[161] =
|
|
|
|
"just imagine what would happen if we all decided to understand. you "
|
|
|
|
"can't in the and by be or then before so just face it this text hurts "
|
|
|
|
"to read? lokinet yolo!";
|
2020-01-31 20:38:08 +00:00
|
|
|
|
2020-01-27 21:30:41 +00:00
|
|
|
template < typename K >
|
2020-01-31 21:05:50 +00:00
|
|
|
static bool make_scalar(AlignedBuffer< 32 > &out, const K &k, uint64_t i)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2020-01-31 20:38:08 +00:00
|
|
|
// b = BLIND-STRING || k || i
|
|
|
|
std::array< byte_t, 160 + K::SIZE + sizeof(uint64_t) > buf;
|
|
|
|
std::copy(derived_key_hash_str, derived_key_hash_str + 160, buf.begin());
|
|
|
|
std::copy(k.begin(), k.end(), buf.begin() + 160);
|
|
|
|
htole64buf(buf.data() + 160 + K::SIZE, i);
|
2020-01-27 21:30:41 +00:00
|
|
|
// n = H(b)
|
2020-01-31 20:38:08 +00:00
|
|
|
// h = make_point(n)
|
|
|
|
ShortHash n;
|
2020-01-31 21:05:50 +00:00
|
|
|
return -1
|
|
|
|
!= crypto_generichash_blake2b(n.data(), ShortHash::SIZE, buf.data(),
|
|
|
|
buf.size(), nullptr, 0)
|
|
|
|
&& -1 != crypto_core_ed25519_from_uniform(out.data(), n.data());
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 21:55:36 +00:00
|
|
|
static AlignedBuffer< 32 > zero;
|
|
|
|
|
2020-01-27 21:30:41 +00:00
|
|
|
bool
|
2020-01-31 21:05:50 +00:00
|
|
|
CryptoLibSodium::derive_subkey(PubKey &out_pubkey,
|
|
|
|
const PubKey &root_pubkey, uint64_t key_n,
|
|
|
|
const AlignedBuffer< 32 > *hash)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2020-01-31 20:38:08 +00:00
|
|
|
// scalar h = H( BLIND-STRING || root_pubkey || key_n )
|
2020-01-30 16:34:05 +00:00
|
|
|
AlignedBuffer< 32 > h;
|
2020-01-31 21:05:50 +00:00
|
|
|
if(hash)
|
2020-01-30 16:34:05 +00:00
|
|
|
h = *hash;
|
2020-01-31 20:38:08 +00:00
|
|
|
else if(not make_scalar(h, root_pubkey, key_n))
|
2020-01-30 16:34:05 +00:00
|
|
|
{
|
|
|
|
LogError("cannot make scalar");
|
2020-01-27 21:30:41 +00:00
|
|
|
return false;
|
2020-01-30 16:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 21:05:50 +00:00
|
|
|
return 0
|
|
|
|
== crypto_scalarmult_ed25519(out_pubkey.data(), h.data(),
|
|
|
|
root_pubkey.data());
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-01-30 16:34:05 +00:00
|
|
|
CryptoLibSodium::derive_subkey_private(PrivateKey &out_key,
|
2020-01-31 21:05:50 +00:00
|
|
|
const SecretKey &root_key,
|
|
|
|
uint64_t key_n,
|
|
|
|
const AlignedBuffer< 32 > *hash)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2020-01-30 16:34:05 +00:00
|
|
|
// Derives a private subkey from a root key.
|
|
|
|
//
|
|
|
|
// The basic idea is:
|
|
|
|
//
|
2020-01-31 20:38:08 +00:00
|
|
|
// h = H( BLIND-STRING || A || key_n )
|
2020-01-30 16:34:05 +00:00
|
|
|
// a - private key
|
|
|
|
// A = aB - public key
|
2020-01-31 20:38:08 +00:00
|
|
|
// s - signing hash
|
2020-01-30 16:34:05 +00:00
|
|
|
// a' = ah - derived private key
|
|
|
|
// A' = a'B = (ah)B - derived public key
|
2020-01-31 20:38:08 +00:00
|
|
|
// s' = H(h || s) - derived signing hash
|
2020-01-30 16:34:05 +00:00
|
|
|
//
|
2020-01-31 21:05:50 +00:00
|
|
|
// libsodium throws some wrenches in the mechanics which are a nuisance,
|
|
|
|
// the biggest of which is that sodium's secret key is *not* `a`; rather
|
|
|
|
// it is the seed. If you want to get the private key (i.e. "a"), you
|
|
|
|
// need to SHA-512 hash it and then clamp that.
|
2020-01-30 16:34:05 +00:00
|
|
|
//
|
2020-01-31 21:05:50 +00:00
|
|
|
// This also makes signature verification harder: we can't just use
|
|
|
|
// sodium's sign function because it wants to be given the seed rather
|
|
|
|
// than the private key, and moreover we can't actually *get* the seed to
|
|
|
|
// make libsodium happy because we only have `ah` above; thus we
|
|
|
|
// reimplemented most of sodium's detached signing function but without
|
|
|
|
// the hash step.
|
2020-01-31 20:38:08 +00:00
|
|
|
//
|
2020-01-31 21:05:50 +00:00
|
|
|
// Lastly, for the signing hash s', we need some value that is both
|
|
|
|
// different from the root s but also unknowable from the public key
|
|
|
|
// (since otherwise `r` in the signing function would be known), so we
|
|
|
|
// generate it from a hash of `h` and the root key's (psuedorandom)
|
|
|
|
// signing hash, `s`.
|
2020-01-30 16:34:05 +00:00
|
|
|
//
|
|
|
|
const auto root_pubkey = root_key.toPublic();
|
|
|
|
|
|
|
|
AlignedBuffer< 32 > h;
|
2020-01-31 21:05:50 +00:00
|
|
|
if(hash)
|
2020-01-30 16:34:05 +00:00
|
|
|
h = *hash;
|
2020-01-31 20:38:08 +00:00
|
|
|
else if(not make_scalar(h, root_pubkey, key_n))
|
2020-01-28 21:55:36 +00:00
|
|
|
{
|
|
|
|
LogError("cannot make scalar");
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-30 16:34:05 +00:00
|
|
|
|
|
|
|
h[0] &= 248;
|
|
|
|
h[31] &= 63;
|
|
|
|
h[31] |= 64;
|
|
|
|
|
|
|
|
PrivateKey a;
|
2020-01-31 21:05:50 +00:00
|
|
|
if(!root_key.toPrivate(a))
|
2020-01-27 21:30:41 +00:00
|
|
|
return false;
|
2020-01-30 16:34:05 +00:00
|
|
|
|
|
|
|
// a' = ha
|
|
|
|
crypto_core_ed25519_scalar_mul(out_key.data(), h.data(), a.data());
|
|
|
|
|
2020-01-31 20:38:08 +00:00
|
|
|
// s' = H(h || s)
|
2020-01-31 21:05:50 +00:00
|
|
|
std::array< byte_t, 64 > buf;
|
2020-01-31 20:38:08 +00:00
|
|
|
std::copy(h.begin(), h.end(), buf.begin());
|
2020-02-02 20:06:32 +00:00
|
|
|
std::copy(a.signingHash(), a.signingHash() + 32, buf.begin() + 32);
|
2020-01-31 21:05:50 +00:00
|
|
|
return -1
|
2020-02-02 20:06:32 +00:00
|
|
|
!= crypto_generichash_blake2b(out_key.signingHash(), 32, buf.data(),
|
2020-01-31 21:05:50 +00:00
|
|
|
buf.size(), nullptr, 0);
|
2020-01-31 20:38:08 +00:00
|
|
|
|
2020-01-28 21:55:36 +00:00
|
|
|
return true;
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
bool
|
|
|
|
CryptoLibSodium::seed_to_secretkey(llarp::SecretKey &secret,
|
|
|
|
const llarp::IdentitySecret &seed)
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
2020-01-28 21:55:36 +00:00
|
|
|
return crypto_sign_ed25519_seed_keypair(secret.data() + 32, secret.data(),
|
2019-10-23 12:43:37 +00:00
|
|
|
seed.data())
|
|
|
|
!= -1;
|
2019-01-21 15:45:18 +00:00
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
void
|
2019-02-01 01:58:06 +00:00
|
|
|
CryptoLibSodium::randomize(const llarp_buffer_t &buff)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
randombytes((unsigned char *)buff.base, buff.sz);
|
|
|
|
}
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
void
|
2019-05-28 19:45:09 +00:00
|
|
|
CryptoLibSodium::randbytes(byte_t *ptr, size_t sz)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
randombytes((unsigned char *)ptr, sz);
|
|
|
|
}
|
2018-02-01 22:34:04 +00:00
|
|
|
|
2019-10-28 14:16:00 +00:00
|
|
|
void
|
2019-01-26 15:40:58 +00:00
|
|
|
CryptoLibSodium::identity_keygen(llarp::SecretKey &keys)
|
2018-05-23 20:37:43 +00:00
|
|
|
{
|
2019-10-22 15:58:34 +00:00
|
|
|
PubKey pk;
|
2019-10-28 14:16:00 +00:00
|
|
|
int result = crypto_sign_keypair(pk.data(), keys.data());
|
|
|
|
assert(result != -1);
|
2019-10-23 12:43:37 +00:00
|
|
|
const PubKey sk_pk = keys.toPublic();
|
2019-10-28 14:16:00 +00:00
|
|
|
assert(pk == sk_pk);
|
2019-11-04 19:21:32 +00:00
|
|
|
(void)result;
|
|
|
|
(void)sk_pk;
|
2020-01-28 21:55:36 +00:00
|
|
|
|
|
|
|
// encryption_keygen(keys);
|
2018-05-23 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:40:55 +00:00
|
|
|
bool
|
|
|
|
CryptoLibSodium::check_identity_privkey(const llarp::SecretKey &keys)
|
|
|
|
{
|
|
|
|
AlignedBuffer< crypto_sign_SEEDBYTES > seed;
|
|
|
|
llarp::PubKey pk;
|
|
|
|
llarp::SecretKey sk;
|
|
|
|
if(crypto_sign_ed25519_sk_to_seed(seed.data(), keys.data()) == -1)
|
|
|
|
return false;
|
|
|
|
if(crypto_sign_seed_keypair(pk.data(), sk.data(), seed.data()) == -1)
|
|
|
|
return false;
|
|
|
|
return keys.toPublic() == pk && sk == keys;
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
void
|
|
|
|
CryptoLibSodium::encryption_keygen(llarp::SecretKey &keys)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
auto d = keys.data();
|
2019-10-14 13:47:06 +00:00
|
|
|
randbytes(d, 32);
|
2019-01-02 01:04:03 +00:00
|
|
|
crypto_scalarmult_curve25519_base(d + 32, d);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
bool
|
2019-01-26 15:40:58 +00:00
|
|
|
CryptoLibSodium::pqe_encrypt(PQCipherBlock &ciphertext,
|
|
|
|
SharedSecret &sharedkey,
|
|
|
|
const PQPubKey &pubkey)
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-02 01:04:08 +00:00
|
|
|
return crypto_kem_enc(ciphertext.data(), sharedkey.data(), pubkey.data())
|
2019-01-02 01:04:03 +00:00
|
|
|
!= -1;
|
2018-08-13 23:22:31 +00:00
|
|
|
}
|
|
|
|
bool
|
2019-01-26 15:40:58 +00:00
|
|
|
CryptoLibSodium::pqe_decrypt(const PQCipherBlock &ciphertext,
|
|
|
|
SharedSecret &sharedkey,
|
|
|
|
const byte_t *secretkey)
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-02 01:04:08 +00:00
|
|
|
return crypto_kem_dec(sharedkey.data(), ciphertext.data(), secretkey)
|
2019-01-02 01:04:03 +00:00
|
|
|
!= -1;
|
2018-08-13 23:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-26 15:40:58 +00:00
|
|
|
CryptoLibSodium::pqe_keygen(PQKeyPair &keypair)
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
auto d = keypair.data();
|
2019-01-02 01:04:03 +00:00
|
|
|
crypto_kem_keypair(d + PQ_SECRETKEYSIZE, d);
|
2018-08-13 23:22:31 +00:00
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
} // namespace sodium
|
|
|
|
|
|
|
|
const byte_t *
|
|
|
|
seckey_topublic(const SecretKey &sec)
|
|
|
|
{
|
|
|
|
return sec.data() + 32;
|
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
|
|
|
|
const byte_t *
|
2019-01-02 01:04:04 +00:00
|
|
|
pq_keypair_to_public(const PQKeyPair &k)
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
return k.data() + PQ_SECRETKEYSIZE;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
const byte_t *
|
2019-01-02 01:04:04 +00:00
|
|
|
pq_keypair_to_secret(const PQKeyPair &k)
|
2018-08-13 23:22:31 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
return k.data();
|
2018-08-13 23:22:31 +00:00
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
uint64_t
|
|
|
|
randint()
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
randombytes((byte_t *)&i, sizeof(i));
|
|
|
|
return i;
|
|
|
|
}
|
2018-12-11 00:53:11 +00:00
|
|
|
|
|
|
|
} // namespace llarp
|