18 #include <openssl/hmac.h> 41 static void noise_kdf (uint8_t *, uint8_t *, uint8_t *,
const uint8_t *,
42 size_t,
size_t,
size_t,
size_t,
47 const uint8_t[NOISE_PUBLIC_KEY_LEN]);
53 uint8_t[NOISE_HASH_LEN],
55 const uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
57 uint8_t[NOISE_HASH_LEN],
65 uint8_t[NOISE_HASH_LEN],
173 uint8_t es[NOISE_PUBLIC_KEY_LEN +
380 noise_kdf (key_send, key_recv, NULL, NULL,
387 noise_kdf (key_recv, key_send, NULL, NULL,
398 VNET_CRYPTO_ALG_CHACHA20_POLY1305,
401 VNET_CRYPTO_ALG_CHACHA20_POLY1305,
517 if (op_id == VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC)
525 op->
src = !src ? src_ :
src;
535 if (op_id == VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC)
540 return (op->
status == VNET_CRYPTO_OP_STATUS_COMPLETED);
545 uint64_t * nonce, uint8_t *
src,
size_t srclen,
573 VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC,
597 uint64_t nonce, uint8_t *
src,
size_t srclen,
633 VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC,
697 struct noise_upcall *u = &local->
l_upcall;
711 struct noise_upcall *u = &local->
l_upcall;
720 struct noise_upcall *u = &local->
l_upcall;
736 uint64_t
i, top, index_recv, index_ctr;
745 if (recv + COUNTER_WINDOW_SIZE < ctr->c_recv)
756 for (i = 1; i <= top; i++)
775 noise_kdf (uint8_t *
a, uint8_t * b, uint8_t *
c,
const uint8_t * x,
776 size_t a_len,
size_t b_len,
size_t c_len,
size_t x_len,
784 HMAC (EVP_blake2s256 (), ck, NOISE_HASH_LEN, x, x_len, sec, &l);
786 if (a == NULL || a_len == 0)
795 if (b == NULL || b_len == 0)
805 if (c == NULL || c_len == 0)
826 const uint8_t
public[NOISE_PUBLIC_KEY_LEN])
832 NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN,
844 if (
clib_memcmp (ss, null_point, NOISE_PUBLIC_KEY_LEN) == 0)
847 NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN,
867 const uint8_t psk[NOISE_SYMMETRIC_KEY_LEN])
872 NOISE_HASH_LEN, NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN,
873 NOISE_SYMMETRIC_KEY_LEN, ck);
885 strlen (NOISE_HANDSHAKE_NAME), NULL, 0);
890 strlen (NOISE_IDENTIFIER_NAME));
898 size_t src_len, uint32_t key_idx,
903 VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC, key_idx);
909 size_t src_len, uint32_t key_idx,
914 VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC, key_idx))
926 NOISE_PUBLIC_KEY_LEN, ck);
933 uint32_t unix_nanosec;
944 sec = htobe64 (0x400000000000000aULL + unix_sec);
945 nsec = htobe32 (unix_nanosec);
949 clib_memcpy (output +
sizeof (sec), &nsec,
sizeof (nsec));
955 static void *(*
const volatile memset_v) (
void *,
int,
size_t) = &memset;
u32 vnet_crypto_process_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], u32 n_ops)
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
static bool noise_msg_decrypt(vlib_main_t *vm, uint8_t *, uint8_t *, size_t, uint32_t key_idx, uint8_t[NOISE_HASH_LEN])
bool noise_create_initiation(vlib_main_t *vm, noise_remote_t *r, uint32_t *s_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN], uint8_t es[NOISE_PUBLIC_KEY_LEN+NOISE_AUTHTAG_LEN], uint8_t ets[NOISE_TIMESTAMP_LEN+NOISE_AUTHTAG_LEN])
#define REJECT_AFTER_TIME
enum noise_state_crypt noise_remote_encrypt(vlib_main_t *vm, noise_remote_t *r, uint32_t *r_idx, uint64_t *nonce, uint8_t *src, size_t srclen, uint8_t *dst)
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
#define NOISE_TIMESTAMP_LEN
void noise_local_init(noise_local_t *l, struct noise_upcall *upcall)
#define NOISE_AUTHTAG_LEN
#define NOISE_HANDSHAKE_NAME
void noise_remote_expire_current(noise_remote_t *r)
bool noise_remote_ready(noise_remote_t *r)
#define NOISE_IDENTIFIER_NAME
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
static void noise_param_init(uint8_t[NOISE_HASH_LEN], uint8_t[NOISE_HASH_LEN], const uint8_t[NOISE_PUBLIC_KEY_LEN])
uint8_t hs_hash[NOISE_HASH_LEN]
static void noise_tai64n_now(uint8_t[NOISE_TIMESTAMP_LEN])
uint8_t r_timestamp[NOISE_TIMESTAMP_LEN]
uint8_t hs_ck[NOISE_HASH_LEN]
static uint32_t noise_remote_handshake_index_get(noise_remote_t *)
uint8_t r_public[NOISE_PUBLIC_KEY_LEN]
void noise_remote_init(noise_remote_t *r, uint32_t peer_pool_idx, const uint8_t public[NOISE_PUBLIC_KEY_LEN], u32 noise_local_idx)
#define clib_memcpy(d, s, n)
bool noise_remote_begin_session(vlib_main_t *vm, noise_remote_t *r)
static void noise_mix_hash(uint8_t[NOISE_HASH_LEN], const uint8_t *, size_t)
vnet_crypto_key_index_t kp_send_index
static_always_inline void vnet_crypto_op_init(vnet_crypto_op_t *op, vnet_crypto_op_id_t type)
void noise_remote_clear(vlib_main_t *vm, noise_remote_t *r)
bool noise_create_response(vlib_main_t *vm, noise_remote_t *r, uint32_t *s_idx, uint32_t *r_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN], uint8_t en[0+NOISE_AUTHTAG_LEN])
static void noise_msg_ephemeral(uint8_t[NOISE_HASH_LEN], uint8_t[NOISE_HASH_LEN], const uint8_t src[NOISE_PUBLIC_KEY_LEN])
static void unix_time_now_nsec_fraction(u32 *sec, u32 *nsec)
u32 vnet_crypto_key_add(vlib_main_t *vm, vnet_crypto_alg_t alg, u8 *data, u16 length)
static void noise_kdf(uint8_t *, uint8_t *, uint8_t *, const uint8_t *, size_t, size_t, size_t, size_t, const uint8_t[NOISE_HASH_LEN])
#define NOISE_PUBLIC_KEY_LEN
static noise_keypair_t * noise_remote_keypair_allocate(noise_remote_t *)
static void clib_rwlock_init(clib_rwlock_t *p)
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
#define REKEY_AFTER_TIME_RECV
#define NOISE_SYMMETRIC_KEY_LEN
static uint64_t noise_counter_send(noise_counter_t *)
struct noise_local::noise_upcall l_upcall
void vnet_crypto_key_del(vlib_main_t *vm, vnet_crypto_key_index_t index)
unsigned long c_backtrack[COUNTER_NUM]
bool noise_local_set_private(noise_local_t *l, const uint8_t private[NOISE_PUBLIC_KEY_LEN])
uint8_t r_psk[NOISE_SYMMETRIC_KEY_LEN]
uint8_t l_public[NOISE_PUBLIC_KEY_LEN]
vnet_crypto_key_index_t kp_recv_index
enum noise_state_hs hs_state
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
static bool wg_birthdate_has_expired(f64 birthday_seconds, f64 expiration_seconds)
noise_remote_t *(* u_remote_get)(const uint8_t[NOISE_PUBLIC_KEY_LEN])
#define clib_memcmp(s1, s2, m1)
sll srl srl sll sra u16x4 i
vl_api_ikev2_sa_transform_t dh
static bool noise_mix_ss(uint8_t ck[NOISE_HASH_LEN], uint8_t key[NOISE_SYMMETRIC_KEY_LEN], const uint8_t ss[NOISE_PUBLIC_KEY_LEN])
#define REJECT_AFTER_MESSAGES
static void noise_remote_keypair_free(vlib_main_t *vm, noise_remote_t *, noise_keypair_t **)
bool noise_consume_response(vlib_main_t *vm, noise_remote_t *r, uint32_t s_idx, uint32_t r_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN], uint8_t en[0+NOISE_AUTHTAG_LEN])
static bool noise_mix_dh(uint8_t[NOISE_HASH_LEN], uint8_t[NOISE_SYMMETRIC_KEY_LEN], const uint8_t[NOISE_PUBLIC_KEY_LEN], const uint8_t[NOISE_PUBLIC_KEY_LEN])
bool noise_consume_initiation(vlib_main_t *vm, noise_local_t *l, noise_remote_t **rp, uint32_t s_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN], uint8_t es[NOISE_PUBLIC_KEY_LEN+NOISE_AUTHTAG_LEN], uint8_t ets[NOISE_TIMESTAMP_LEN+NOISE_AUTHTAG_LEN])
uint8_t r_ss[NOISE_PUBLIC_KEY_LEN]
noise_keypair_t * r_previous
static void clib_mem_free(void *p)
#define REJECT_INTERVAL_MASK
u32 vnet_crypto_key_index_t
bool curve25519_gen_secret(u8 secret_key[CURVE25519_KEY_SIZE])
static void * clib_mem_alloc(uword size)
uint8_t l_private[NOISE_PUBLIC_KEY_LEN]
static_always_inline vnet_crypto_key_t * vnet_crypto_get_key(vnet_crypto_key_index_t index)
uint32_t(* u_index_set)(noise_remote_t *)
int blake2s_final(blake2s_state_t *S, void *out, size_t outlen)
clib_rwlock_t r_keypair_lock
bool curve25519_gen_public(u8 public_key[CURVE25519_KEY_SIZE], const u8 secret_key[CURVE25519_KEY_SIZE])
noise_keypair_t * r_current
vnet_crypto_op_status_t status
noise_handshake_t r_handshake
int blake2s(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen)
static bool chacha20poly1305_calc(vlib_main_t *vm, u8 *src, u32 src_len, u8 *dst, u8 *aad, u32 aad_len, u64 nonce, vnet_crypto_op_id_t op_id, vnet_crypto_key_index_t key_index)
int blake2s_update(blake2s_state_t *S, const void *pin, size_t inlen)
uint8_t hs_e[NOISE_PUBLIC_KEY_LEN]
int blake2s_init(blake2s_state_t *S, size_t outlen)
static_always_inline noise_local_t * noise_local_get(uint32_t locali)
enum noise_state_crypt noise_remote_decrypt(vlib_main_t *vm, noise_remote_t *r, uint32_t r_idx, uint64_t nonce, uint8_t *src, size_t srclen, uint8_t *dst)
static bool noise_counter_recv(noise_counter_t *, uint64_t)
static void secure_zero_memory(void *v, size_t n)
bool curve25519_gen_shared(u8 shared_key[CURVE25519_KEY_SIZE], const u8 secret_key[CURVE25519_KEY_SIZE], const u8 basepoint[CURVE25519_KEY_SIZE])
noise_local_t * noise_local_pool
#define REKEY_AFTER_MESSAGES
void(* u_index_drop)(uint32_t)
static void noise_msg_encrypt(vlib_main_t *vm, uint8_t *, uint8_t *, size_t, uint32_t key_idx, uint8_t[NOISE_HASH_LEN])
static void noise_mix_psk(uint8_t[NOISE_HASH_LEN], uint8_t[NOISE_HASH_LEN], uint8_t[NOISE_SYMMETRIC_KEY_LEN], const uint8_t[NOISE_SYMMETRIC_KEY_LEN])
static void noise_remote_handshake_index_drop(noise_remote_t *)
void noise_remote_precompute(noise_remote_t *r)