lokinet/llarp/crypto_libsodium.cpp

103 lines
3.5 KiB
C++
Raw Normal View History

2018-02-01 13:21:00 +00:00
#include <assert.h>
2018-01-25 16:24:33 +00:00
#include <llarp/crypto.h>
2018-01-31 19:59:26 +00:00
#include <sodium.h>
2017-10-03 19:14:46 +00:00
2018-01-29 14:27:24 +00:00
namespace llarp {
namespace sodium {
2018-02-01 13:21:00 +00:00
static bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k,
llarp_nounce_t n) {
2018-01-29 14:27:24 +00:00
uint8_t *base = (uint8_t *)buff.base;
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
}
2017-10-03 19:14:46 +00:00
2018-02-01 13:21:00 +00:00
static bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk,
uint8_t *server_pk, uint8_t *remote_key, uint8_t *local_key) {
2018-01-29 14:27:24 +00:00
uint8_t *out = *shared;
const size_t outsz = SHAREDKEYSIZE;
crypto_generichash_state h;
2018-02-01 13:21:00 +00:00
if (crypto_scalarmult(out, local_key, remote_key) == -1) return false;
2018-01-29 14:27:24 +00:00
crypto_generichash_init(&h, NULL, 0U, outsz);
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
crypto_generichash_final(&h, out, outsz);
return true;
}
2017-10-03 19:14:46 +00:00
2018-01-31 19:59:26 +00:00
static bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
2018-02-01 13:21:00 +00:00
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
2018-01-29 14:27:24 +00:00
llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_pk, sk);
if (dh(shared, local_pk, pk, pk, sk)) {
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
TUNNOUNCESIZE) != -1;
}
return false;
}
2017-10-03 19:14:46 +00:00
2018-01-31 19:59:26 +00:00
static bool dh_server(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
2018-02-01 13:21:00 +00:00
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
2018-01-29 14:27:24 +00:00
llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_pk, sk);
if (dh(shared, pk, local_pk, pk, sk)) {
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
TUNNOUNCESIZE) != -1;
}
return false;
}
2018-01-19 16:51:27 +00:00
2018-01-31 19:59:26 +00:00
static bool hash(llarp_hash_t *result, llarp_buffer_t buff) {
2018-01-29 14:27:24 +00:00
const uint8_t *base = (const uint8_t *)buff.base;
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
}
2018-01-19 16:51:27 +00:00
2018-02-01 13:21:00 +00:00
static bool hmac(llarp_hash_t *result, llarp_buffer_t buff,
llarp_seckey_t secret) {
2018-01-29 14:27:24 +00:00
const uint8_t *base = (const uint8_t *)buff.base;
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz,
secret, HMACSECSIZE) != -1;
}
2018-01-19 16:51:27 +00:00
2018-02-01 13:21:00 +00:00
static bool sign(llarp_sig_t *result, llarp_seckey_t secret,
llarp_buffer_t buff) {
2018-01-29 14:27:24 +00:00
const uint8_t *base = (const uint8_t *)buff.base;
return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1;
}
2018-01-19 16:51:27 +00:00
2018-01-31 19:59:26 +00:00
static bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) {
2018-01-29 14:27:24 +00:00
const uint8_t *base = (const uint8_t *)buff.base;
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
2017-10-03 19:14:46 +00:00
}
2018-01-31 19:59:26 +00:00
2018-02-01 13:21:00 +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-02-01 22:34:04 +00:00
static inline void randbytes(void *ptr, size_t sz) {
randombytes((unsigned char *)ptr, sz);
2018-02-01 22:04:58 +00:00
}
2018-02-01 22:34:04 +00:00
2018-02-01 22:04:58 +00:00
static void keygen(llarp_seckey_t *keys) {
unsigned char seed[32];
2018-02-01 22:34:04 +00:00
uint8_t *pk = llarp_seckey_topublic(*keys);
2018-02-01 22:04:58 +00:00
crypto_sign_seed_keypair(pk, *keys, seed);
2018-02-01 13:21:00 +00:00
}
} // namespace sodium
} // namespace llarp
2017-10-03 19:14:46 +00:00
extern "C" {
2018-01-29 14:27:24 +00:00
void llarp_crypto_libsodium_init(struct llarp_crypto *c) {
2018-01-31 19:59:26 +00:00
assert(sodium_init() != -1);
2018-01-29 14:27:24 +00:00
c->xchacha20 = llarp::sodium::xchacha20;
c->dh_client = llarp::sodium::dh_client;
c->dh_server = llarp::sodium::dh_server;
c->hash = llarp::sodium::hash;
c->hmac = llarp::sodium::hmac;
c->sign = llarp::sodium::sign;
c->verify = llarp::sodium::verify;
2018-01-31 19:59:26 +00:00
c->randomize = llarp::sodium::randomize;
2018-02-01 22:04:58 +00:00
c->randbytes = llarp::sodium::randbytes;
2018-01-31 19:59:26 +00:00
c->keygen = llarp::sodium::keygen;
2018-01-29 14:27:24 +00:00
}
2017-10-03 19:14:46 +00:00
}