2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Copyright (c) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>.
4 * Copyright (c) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <openssl/hmac.h>
19 #include <wireguard/wireguard.h>
21 /* This implements Noise_IKpsk2:
25 * -> e, es, s, ss, {t}
26 * <- e, ee, se, psk, {}
29 noise_local_t *noise_local_pool;
31 /* Private functions */
32 static noise_keypair_t *noise_remote_keypair_allocate (noise_remote_t *);
33 static void noise_remote_keypair_free (vlib_main_t * vm, noise_remote_t *,
35 static uint32_t noise_remote_handshake_index_get (noise_remote_t *);
36 static void noise_remote_handshake_index_drop (noise_remote_t *);
38 static uint64_t noise_counter_send (noise_counter_t *);
39 static bool noise_counter_recv (noise_counter_t *, uint64_t);
41 static void noise_kdf (uint8_t *, uint8_t *, uint8_t *, const uint8_t *,
42 size_t, size_t, size_t, size_t,
43 const uint8_t[NOISE_HASH_LEN]);
44 static bool noise_mix_dh (uint8_t[NOISE_HASH_LEN],
45 uint8_t[NOISE_SYMMETRIC_KEY_LEN],
46 const uint8_t[NOISE_PUBLIC_KEY_LEN],
47 const uint8_t[NOISE_PUBLIC_KEY_LEN]);
48 static bool noise_mix_ss (uint8_t ck[NOISE_HASH_LEN],
49 uint8_t key[NOISE_SYMMETRIC_KEY_LEN],
50 const uint8_t ss[NOISE_PUBLIC_KEY_LEN]);
51 static void noise_mix_hash (uint8_t[NOISE_HASH_LEN], const uint8_t *, size_t);
52 static void noise_mix_psk (uint8_t[NOISE_HASH_LEN],
53 uint8_t[NOISE_HASH_LEN],
54 uint8_t[NOISE_SYMMETRIC_KEY_LEN],
55 const uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
56 static void noise_param_init (uint8_t[NOISE_HASH_LEN],
57 uint8_t[NOISE_HASH_LEN],
58 const uint8_t[NOISE_PUBLIC_KEY_LEN]);
60 static void noise_msg_encrypt (vlib_main_t * vm, uint8_t *, uint8_t *, size_t,
61 uint32_t key_idx, uint8_t[NOISE_HASH_LEN]);
62 static bool noise_msg_decrypt (vlib_main_t * vm, uint8_t *, uint8_t *, size_t,
63 uint32_t key_idx, uint8_t[NOISE_HASH_LEN]);
64 static void noise_msg_ephemeral (uint8_t[NOISE_HASH_LEN],
65 uint8_t[NOISE_HASH_LEN],
66 const uint8_t src[NOISE_PUBLIC_KEY_LEN]);
68 static void noise_tai64n_now (uint8_t[NOISE_TIMESTAMP_LEN]);
70 static void secure_zero_memory (void *v, size_t n);
72 /* Set/Get noise parameters */
74 noise_local_init (noise_local_t * l, struct noise_upcall *upcall)
76 clib_memset (l, 0, sizeof (*l));
77 l->l_upcall = *upcall;
81 noise_local_set_private (noise_local_t * l,
82 const uint8_t private[NOISE_PUBLIC_KEY_LEN])
84 clib_memcpy (l->l_private, private, NOISE_PUBLIC_KEY_LEN);
86 return curve25519_gen_public (l->l_public, private);
90 noise_remote_init (noise_remote_t * r, uint32_t peer_pool_idx,
91 const uint8_t public[NOISE_PUBLIC_KEY_LEN],
94 clib_memset (r, 0, sizeof (*r));
95 clib_memcpy (r->r_public, public, NOISE_PUBLIC_KEY_LEN);
96 clib_rwlock_init (&r->r_keypair_lock);
97 r->r_peer_idx = peer_pool_idx;
98 r->r_local_idx = noise_local_idx;
99 r->r_handshake.hs_state = HS_ZEROED;
101 noise_remote_precompute (r);
105 noise_remote_precompute (noise_remote_t * r)
107 noise_local_t *l = noise_local_get (r->r_local_idx);
109 if (!curve25519_gen_shared (r->r_ss, l->l_private, r->r_public))
110 clib_memset (r->r_ss, 0, NOISE_PUBLIC_KEY_LEN);
112 noise_remote_handshake_index_drop (r);
113 secure_zero_memory (&r->r_handshake, sizeof (r->r_handshake));
116 /* Handshake functions */
118 noise_create_initiation (vlib_main_t * vm, noise_remote_t * r,
119 uint32_t * s_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN],
120 uint8_t es[NOISE_PUBLIC_KEY_LEN + NOISE_AUTHTAG_LEN],
121 uint8_t ets[NOISE_TIMESTAMP_LEN + NOISE_AUTHTAG_LEN])
123 noise_handshake_t *hs = &r->r_handshake;
124 noise_local_t *l = noise_local_get (r->r_local_idx);
125 uint8_t _key[NOISE_SYMMETRIC_KEY_LEN];
131 vnet_crypto_key_add (vm, VNET_CRYPTO_ALG_CHACHA20_POLY1305, _key,
132 NOISE_SYMMETRIC_KEY_LEN);
133 key = vnet_crypto_get_key (key_idx)->data;
135 noise_param_init (hs->hs_ck, hs->hs_hash, r->r_public);
138 curve25519_gen_secret (hs->hs_e);
139 if (!curve25519_gen_public (ue, hs->hs_e))
141 noise_msg_ephemeral (hs->hs_ck, hs->hs_hash, ue);
144 if (!noise_mix_dh (hs->hs_ck, key, hs->hs_e, r->r_public))
148 noise_msg_encrypt (vm, es, l->l_public, NOISE_PUBLIC_KEY_LEN, key_idx,
152 if (!noise_mix_ss (hs->hs_ck, key, r->r_ss))
156 noise_tai64n_now (ets);
157 noise_msg_encrypt (vm, ets, ets, NOISE_TIMESTAMP_LEN, key_idx, hs->hs_hash);
158 noise_remote_handshake_index_drop (r);
159 hs->hs_state = CREATED_INITIATION;
160 hs->hs_local_index = noise_remote_handshake_index_get (r);
161 *s_idx = hs->hs_local_index;
164 secure_zero_memory (key, NOISE_SYMMETRIC_KEY_LEN);
165 vnet_crypto_key_del (vm, key_idx);
170 noise_consume_initiation (vlib_main_t * vm, noise_local_t * l,
171 noise_remote_t ** rp, uint32_t s_idx,
172 uint8_t ue[NOISE_PUBLIC_KEY_LEN],
173 uint8_t es[NOISE_PUBLIC_KEY_LEN +
175 uint8_t ets[NOISE_TIMESTAMP_LEN +
179 noise_handshake_t hs;
180 uint8_t _key[NOISE_SYMMETRIC_KEY_LEN];
181 uint8_t r_public[NOISE_PUBLIC_KEY_LEN];
182 uint8_t timestamp[NOISE_TIMESTAMP_LEN];
188 vnet_crypto_key_add (vm, VNET_CRYPTO_ALG_CHACHA20_POLY1305, _key,
189 NOISE_SYMMETRIC_KEY_LEN);
190 key = vnet_crypto_get_key (key_idx)->data;
192 noise_param_init (hs.hs_ck, hs.hs_hash, l->l_public);
195 noise_msg_ephemeral (hs.hs_ck, hs.hs_hash, ue);
198 if (!noise_mix_dh (hs.hs_ck, key, l->l_private, ue))
203 if (!noise_msg_decrypt (vm, r_public, es,
204 NOISE_PUBLIC_KEY_LEN + NOISE_AUTHTAG_LEN, key_idx,
208 /* Lookup the remote we received from */
209 if ((r = l->l_upcall.u_remote_get (r_public)) == NULL)
213 if (!noise_mix_ss (hs.hs_ck, key, r->r_ss))
217 if (!noise_msg_decrypt (vm, timestamp, ets,
218 NOISE_TIMESTAMP_LEN + NOISE_AUTHTAG_LEN, key_idx,
223 hs.hs_state = CONSUMED_INITIATION;
224 hs.hs_local_index = 0;
225 hs.hs_remote_index = s_idx;
226 clib_memcpy (hs.hs_e, ue, NOISE_PUBLIC_KEY_LEN);
229 if (clib_memcmp (timestamp, r->r_timestamp, NOISE_TIMESTAMP_LEN) > 0)
230 clib_memcpy (r->r_timestamp, timestamp, NOISE_TIMESTAMP_LEN);
235 if (wg_birthdate_has_expired (r->r_last_init, REJECT_INTERVAL))
236 r->r_last_init = vlib_time_now (vm);
240 /* Ok, we're happy to accept this initiation now */
241 noise_remote_handshake_index_drop (r);
247 secure_zero_memory (key, NOISE_SYMMETRIC_KEY_LEN);
248 vnet_crypto_key_del (vm, key_idx);
249 secure_zero_memory (&hs, sizeof (hs));
254 noise_create_response (vlib_main_t * vm, noise_remote_t * r, uint32_t * s_idx,
255 uint32_t * r_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN],
256 uint8_t en[0 + NOISE_AUTHTAG_LEN])
258 noise_handshake_t *hs = &r->r_handshake;
259 uint8_t _key[NOISE_SYMMETRIC_KEY_LEN];
260 uint8_t e[NOISE_PUBLIC_KEY_LEN];
266 vnet_crypto_key_add (vm, VNET_CRYPTO_ALG_CHACHA20_POLY1305, _key,
267 NOISE_SYMMETRIC_KEY_LEN);
268 key = vnet_crypto_get_key (key_idx)->data;
270 if (hs->hs_state != CONSUMED_INITIATION)
274 curve25519_gen_secret (e);
275 if (!curve25519_gen_public (ue, e))
277 noise_msg_ephemeral (hs->hs_ck, hs->hs_hash, ue);
280 if (!noise_mix_dh (hs->hs_ck, NULL, e, hs->hs_e))
284 if (!noise_mix_dh (hs->hs_ck, NULL, e, r->r_public))
288 noise_mix_psk (hs->hs_ck, hs->hs_hash, key, r->r_psk);
291 noise_msg_encrypt (vm, en, NULL, 0, key_idx, hs->hs_hash);
294 hs->hs_state = CREATED_RESPONSE;
295 hs->hs_local_index = noise_remote_handshake_index_get (r);
296 *r_idx = hs->hs_remote_index;
297 *s_idx = hs->hs_local_index;
300 secure_zero_memory (key, NOISE_SYMMETRIC_KEY_LEN);
301 vnet_crypto_key_del (vm, key_idx);
302 secure_zero_memory (e, NOISE_PUBLIC_KEY_LEN);
307 noise_consume_response (vlib_main_t * vm, noise_remote_t * r, uint32_t s_idx,
308 uint32_t r_idx, uint8_t ue[NOISE_PUBLIC_KEY_LEN],
309 uint8_t en[0 + NOISE_AUTHTAG_LEN])
311 noise_local_t *l = noise_local_get (r->r_local_idx);
312 noise_handshake_t hs;
313 uint8_t _key[NOISE_SYMMETRIC_KEY_LEN];
314 uint8_t preshared_key[NOISE_PUBLIC_KEY_LEN];
320 vnet_crypto_key_add (vm, VNET_CRYPTO_ALG_CHACHA20_POLY1305, _key,
321 NOISE_SYMMETRIC_KEY_LEN);
322 key = vnet_crypto_get_key (key_idx)->data;
325 clib_memcpy (preshared_key, r->r_psk, NOISE_SYMMETRIC_KEY_LEN);
327 if (hs.hs_state != CREATED_INITIATION || hs.hs_local_index != r_idx)
331 noise_msg_ephemeral (hs.hs_ck, hs.hs_hash, ue);
334 if (!noise_mix_dh (hs.hs_ck, NULL, hs.hs_e, ue))
338 if (!noise_mix_dh (hs.hs_ck, NULL, l->l_private, ue))
342 noise_mix_psk (hs.hs_ck, hs.hs_hash, key, preshared_key);
346 if (!noise_msg_decrypt
347 (vm, NULL, en, 0 + NOISE_AUTHTAG_LEN, key_idx, hs.hs_hash))
351 hs.hs_remote_index = s_idx;
353 if (r->r_handshake.hs_state == hs.hs_state &&
354 r->r_handshake.hs_local_index == hs.hs_local_index)
357 r->r_handshake.hs_state = CONSUMED_RESPONSE;
361 secure_zero_memory (&hs, sizeof (hs));
362 secure_zero_memory (key, NOISE_SYMMETRIC_KEY_LEN);
363 vnet_crypto_key_del (vm, key_idx);
368 noise_remote_begin_session (vlib_main_t * vm, noise_remote_t * r)
370 noise_handshake_t *hs = &r->r_handshake;
371 noise_keypair_t kp, *next, *current, *previous;
373 uint8_t key_send[NOISE_SYMMETRIC_KEY_LEN];
374 uint8_t key_recv[NOISE_SYMMETRIC_KEY_LEN];
376 /* We now derive the keypair from the handshake */
377 if (hs->hs_state == CONSUMED_RESPONSE)
379 kp.kp_is_initiator = 1;
380 noise_kdf (key_send, key_recv, NULL, NULL,
381 NOISE_SYMMETRIC_KEY_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, 0,
384 else if (hs->hs_state == CREATED_RESPONSE)
386 kp.kp_is_initiator = 0;
387 noise_kdf (key_recv, key_send, NULL, NULL,
388 NOISE_SYMMETRIC_KEY_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, 0,
397 kp.kp_send_index = vnet_crypto_key_add (vm,
398 VNET_CRYPTO_ALG_CHACHA20_POLY1305,
399 key_send, NOISE_SYMMETRIC_KEY_LEN);
400 kp.kp_recv_index = vnet_crypto_key_add (vm,
401 VNET_CRYPTO_ALG_CHACHA20_POLY1305,
402 key_recv, NOISE_SYMMETRIC_KEY_LEN);
403 kp.kp_local_index = hs->hs_local_index;
404 kp.kp_remote_index = hs->hs_remote_index;
405 kp.kp_birthdate = vlib_time_now (vm);
406 clib_memset (&kp.kp_ctr, 0, sizeof (kp.kp_ctr));
408 /* Now we need to add_new_keypair */
409 clib_rwlock_writer_lock (&r->r_keypair_lock);
411 current = r->r_current;
412 previous = r->r_previous;
414 if (kp.kp_is_initiator)
419 r->r_previous = next;
420 noise_remote_keypair_free (vm, r, ¤t);
424 r->r_previous = current;
427 noise_remote_keypair_free (vm, r, &previous);
429 r->r_current = noise_remote_keypair_allocate (r);
434 noise_remote_keypair_free (vm, r, &next);
435 r->r_previous = NULL;
436 noise_remote_keypair_free (vm, r, &previous);
438 r->r_next = noise_remote_keypair_allocate (r);
441 clib_rwlock_writer_unlock (&r->r_keypair_lock);
443 secure_zero_memory (&r->r_handshake, sizeof (r->r_handshake));
445 secure_zero_memory (&kp, sizeof (kp));
450 noise_remote_clear (vlib_main_t * vm, noise_remote_t * r)
452 noise_remote_handshake_index_drop (r);
453 secure_zero_memory (&r->r_handshake, sizeof (r->r_handshake));
455 clib_rwlock_writer_lock (&r->r_keypair_lock);
456 noise_remote_keypair_free (vm, r, &r->r_next);
457 noise_remote_keypair_free (vm, r, &r->r_current);
458 noise_remote_keypair_free (vm, r, &r->r_previous);
461 r->r_previous = NULL;
462 clib_rwlock_writer_unlock (&r->r_keypair_lock);
466 noise_remote_expire_current (noise_remote_t * r)
468 clib_rwlock_writer_lock (&r->r_keypair_lock);
469 if (r->r_next != NULL)
470 r->r_next->kp_valid = 0;
471 if (r->r_current != NULL)
472 r->r_current->kp_valid = 0;
473 clib_rwlock_writer_unlock (&r->r_keypair_lock);
477 noise_remote_ready (noise_remote_t * r)
482 clib_rwlock_reader_lock (&r->r_keypair_lock);
483 if ((kp = r->r_current) == NULL ||
485 wg_birthdate_has_expired (kp->kp_birthdate, REJECT_AFTER_TIME) ||
486 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
487 kp->kp_ctr.c_send >= REJECT_AFTER_MESSAGES)
491 clib_rwlock_reader_unlock (&r->r_keypair_lock);
496 chacha20poly1305_calc (vlib_main_t * vm,
503 vnet_crypto_op_id_t op_id,
504 vnet_crypto_key_index_t key_index)
506 vnet_crypto_op_t _op, *op = &_op;
508 u8 tag_[NOISE_AUTHTAG_LEN] = { };
511 clib_memset (iv, 0, 12);
512 clib_memcpy (iv + 4, &nonce, sizeof (nonce));
514 vnet_crypto_op_init (op, op_id);
516 op->tag_len = NOISE_AUTHTAG_LEN;
517 if (op_id == VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC)
519 op->tag = src + src_len - NOISE_AUTHTAG_LEN;
520 src_len -= NOISE_AUTHTAG_LEN;
521 op->flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
526 op->src = !src ? src_ : src;
530 op->key_index = key_index;
532 op->aad_len = aad_len;
535 vnet_crypto_process_ops (vm, op, 1);
536 if (op_id == VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC)
538 clib_memcpy (dst + src_len, op->tag, NOISE_AUTHTAG_LEN);
541 return (op->status == VNET_CRYPTO_OP_STATUS_COMPLETED);
544 enum noise_state_crypt
545 noise_remote_encrypt (vlib_main_t * vm, noise_remote_t * r, uint32_t * r_idx,
546 uint64_t * nonce, uint8_t * src, size_t srclen,
550 enum noise_state_crypt ret = SC_FAILED;
552 clib_rwlock_reader_lock (&r->r_keypair_lock);
553 if ((kp = r->r_current) == NULL)
556 /* We confirm that our values are within our tolerances. We want:
558 * - our keypair to be less than REJECT_AFTER_TIME seconds old
559 * - our receive counter to be less than REJECT_AFTER_MESSAGES
560 * - our send counter to be less than REJECT_AFTER_MESSAGES
563 wg_birthdate_has_expired (kp->kp_birthdate, REJECT_AFTER_TIME) ||
564 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
565 ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
568 /* We encrypt into the same buffer, so the caller must ensure that buf
569 * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
570 * are passed back out to the caller through the provided data pointer. */
571 *r_idx = kp->kp_remote_index;
573 chacha20poly1305_calc (vm, src, srclen, dst, NULL, 0, *nonce,
574 VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC,
577 /* If our values are still within tolerances, but we are approaching
578 * the tolerances, we notify the caller with ESTALE that they should
579 * establish a new keypair. The current keypair can continue to be used
580 * until the tolerances are hit. We notify if:
581 * - our send counter is valid and not less than REKEY_AFTER_MESSAGES
582 * - we're the initiator and our keypair is older than
583 * REKEY_AFTER_TIME seconds */
584 ret = SC_KEEP_KEY_FRESH;
585 if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
586 (kp->kp_is_initiator &&
587 wg_birthdate_has_expired (kp->kp_birthdate, REKEY_AFTER_TIME)))
592 clib_rwlock_reader_unlock (&r->r_keypair_lock);
596 enum noise_state_crypt
597 noise_remote_decrypt (vlib_main_t * vm, noise_remote_t * r, uint32_t r_idx,
598 uint64_t nonce, uint8_t * src, size_t srclen,
602 enum noise_state_crypt ret = SC_FAILED;
603 clib_rwlock_reader_lock (&r->r_keypair_lock);
605 if (r->r_current != NULL && r->r_current->kp_local_index == r_idx)
609 else if (r->r_previous != NULL && r->r_previous->kp_local_index == r_idx)
613 else if (r->r_next != NULL && r->r_next->kp_local_index == r_idx)
622 /* We confirm that our values are within our tolerances. These values
623 * are the same as the encrypt routine.
625 * kp_ctr isn't locked here, we're happy to accept a racy read. */
626 if (wg_birthdate_has_expired (kp->kp_birthdate, REJECT_AFTER_TIME) ||
627 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES)
630 /* Decrypt, then validate the counter. We don't want to validate the
631 * counter before decrypting as we do not know the message is authentic
632 * prior to decryption. */
633 if (!chacha20poly1305_calc (vm, src, srclen, dst, NULL, 0, nonce,
634 VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC,
638 if (!noise_counter_recv (&kp->kp_ctr, nonce))
641 /* If we've received the handshake confirming data packet then move the
642 * next keypair into current. If we do slide the next keypair in, then
643 * we skip the REKEY_AFTER_TIME_RECV check. This is safe to do as a
644 * data packet can't confirm a session that we are an INITIATOR of. */
647 clib_rwlock_reader_unlock (&r->r_keypair_lock);
648 clib_rwlock_writer_lock (&r->r_keypair_lock);
649 if (kp == r->r_next && kp->kp_local_index == r_idx)
651 noise_remote_keypair_free (vm, r, &r->r_previous);
652 r->r_previous = r->r_current;
653 r->r_current = r->r_next;
657 clib_rwlock_writer_unlock (&r->r_keypair_lock);
658 clib_rwlock_reader_lock (&r->r_keypair_lock);
661 clib_rwlock_writer_unlock (&r->r_keypair_lock);
662 clib_rwlock_reader_lock (&r->r_keypair_lock);
665 /* Similar to when we encrypt, we want to notify the caller when we
666 * are approaching our tolerances. We notify if:
667 * - we're the initiator and the current keypair is older than
668 * REKEY_AFTER_TIME_RECV seconds. */
669 ret = SC_KEEP_KEY_FRESH;
673 kp->kp_is_initiator &&
674 wg_birthdate_has_expired (kp->kp_birthdate, REKEY_AFTER_TIME_RECV))
679 clib_rwlock_reader_unlock (&r->r_keypair_lock);
683 /* Private functions - these should not be called outside this file under any
685 static noise_keypair_t *
686 noise_remote_keypair_allocate (noise_remote_t * r)
689 kp = clib_mem_alloc (sizeof (*kp));
694 noise_remote_keypair_free (vlib_main_t * vm, noise_remote_t * r,
695 noise_keypair_t ** kp)
697 noise_local_t *local = noise_local_get (r->r_local_idx);
698 struct noise_upcall *u = &local->l_upcall;
701 u->u_index_drop ((*kp)->kp_local_index);
702 vnet_crypto_key_del (vm, (*kp)->kp_send_index);
703 vnet_crypto_key_del (vm, (*kp)->kp_recv_index);
709 noise_remote_handshake_index_get (noise_remote_t * r)
711 noise_local_t *local = noise_local_get (r->r_local_idx);
712 struct noise_upcall *u = &local->l_upcall;
713 return u->u_index_set (r);
717 noise_remote_handshake_index_drop (noise_remote_t * r)
719 noise_handshake_t *hs = &r->r_handshake;
720 noise_local_t *local = noise_local_get (r->r_local_idx);
721 struct noise_upcall *u = &local->l_upcall;
722 if (hs->hs_state != HS_ZEROED)
723 u->u_index_drop (hs->hs_local_index);
727 noise_counter_send (noise_counter_t * ctr)
735 noise_counter_recv (noise_counter_t * ctr, uint64_t recv)
737 uint64_t i, top, index_recv, index_ctr;
741 /* Check that the recv counter is valid */
742 if (ctr->c_recv >= REJECT_AFTER_MESSAGES || recv >= REJECT_AFTER_MESSAGES)
745 /* If the packet is out of the window, invalid */
746 if (recv + COUNTER_WINDOW_SIZE < ctr->c_recv)
749 /* If the new counter is ahead of the current counter, we'll need to
750 * zero out the bitmap that has previously been used */
751 index_recv = recv / COUNTER_BITS;
752 index_ctr = ctr->c_recv / COUNTER_BITS;
754 if (recv > ctr->c_recv)
756 top = clib_min (index_recv - index_ctr, COUNTER_NUM);
757 for (i = 1; i <= top; i++)
758 ctr->c_backtrack[(i + index_ctr) & (COUNTER_NUM - 1)] = 0;
762 index_recv %= COUNTER_NUM;
763 bit = 1ul << (recv % COUNTER_BITS);
765 if (ctr->c_backtrack[index_recv] & bit)
768 ctr->c_backtrack[index_recv] |= bit;
776 noise_kdf (uint8_t * a, uint8_t * b, uint8_t * c, const uint8_t * x,
777 size_t a_len, size_t b_len, size_t c_len, size_t x_len,
778 const uint8_t ck[NOISE_HASH_LEN])
780 uint8_t out[BLAKE2S_HASH_SIZE + 1];
781 uint8_t sec[BLAKE2S_HASH_SIZE];
783 /* Extract entropy from "x" into sec */
785 HMAC (EVP_blake2s256 (), ck, NOISE_HASH_LEN, x, x_len, sec, &l);
786 ASSERT (l == BLAKE2S_HASH_SIZE);
787 if (a == NULL || a_len == 0)
790 /* Expand first key: key = sec, data = 0x1 */
792 HMAC (EVP_blake2s256 (), sec, BLAKE2S_HASH_SIZE, out, 1, out, &l);
793 ASSERT (l == BLAKE2S_HASH_SIZE);
794 clib_memcpy (a, out, a_len);
796 if (b == NULL || b_len == 0)
799 /* Expand second key: key = sec, data = "a" || 0x2 */
800 out[BLAKE2S_HASH_SIZE] = 2;
801 HMAC (EVP_blake2s256 (), sec, BLAKE2S_HASH_SIZE, out, BLAKE2S_HASH_SIZE + 1,
803 ASSERT (l == BLAKE2S_HASH_SIZE);
804 clib_memcpy (b, out, b_len);
806 if (c == NULL || c_len == 0)
809 /* Expand third key: key = sec, data = "b" || 0x3 */
810 out[BLAKE2S_HASH_SIZE] = 3;
811 HMAC (EVP_blake2s256 (), sec, BLAKE2S_HASH_SIZE, out, BLAKE2S_HASH_SIZE + 1,
813 ASSERT (l == BLAKE2S_HASH_SIZE);
815 clib_memcpy (c, out, c_len);
818 /* Clear sensitive data from stack */
819 secure_zero_memory (sec, BLAKE2S_HASH_SIZE);
820 secure_zero_memory (out, BLAKE2S_HASH_SIZE + 1);
824 noise_mix_dh (uint8_t ck[NOISE_HASH_LEN],
825 uint8_t key[NOISE_SYMMETRIC_KEY_LEN],
826 const uint8_t private[NOISE_PUBLIC_KEY_LEN],
827 const uint8_t public[NOISE_PUBLIC_KEY_LEN])
829 uint8_t dh[NOISE_PUBLIC_KEY_LEN];
830 if (!curve25519_gen_shared (dh, private, public))
832 noise_kdf (ck, key, NULL, dh,
833 NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN,
835 secure_zero_memory (dh, NOISE_PUBLIC_KEY_LEN);
840 noise_mix_ss (uint8_t ck[NOISE_HASH_LEN],
841 uint8_t key[NOISE_SYMMETRIC_KEY_LEN],
842 const uint8_t ss[NOISE_PUBLIC_KEY_LEN])
844 static uint8_t null_point[NOISE_PUBLIC_KEY_LEN];
845 if (clib_memcmp (ss, null_point, NOISE_PUBLIC_KEY_LEN) == 0)
847 noise_kdf (ck, key, NULL, ss,
848 NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN,
854 noise_mix_hash (uint8_t hash[NOISE_HASH_LEN], const uint8_t * src,
857 blake2s_state_t blake;
859 blake2s_init (&blake, NOISE_HASH_LEN);
860 blake2s_update (&blake, hash, NOISE_HASH_LEN);
861 blake2s_update (&blake, src, src_len);
862 blake2s_final (&blake, hash, NOISE_HASH_LEN);
866 noise_mix_psk (uint8_t ck[NOISE_HASH_LEN], uint8_t hash[NOISE_HASH_LEN],
867 uint8_t key[NOISE_SYMMETRIC_KEY_LEN],
868 const uint8_t psk[NOISE_SYMMETRIC_KEY_LEN])
870 uint8_t tmp[NOISE_HASH_LEN];
872 noise_kdf (ck, tmp, key, psk,
873 NOISE_HASH_LEN, NOISE_HASH_LEN, NOISE_SYMMETRIC_KEY_LEN,
874 NOISE_SYMMETRIC_KEY_LEN, ck);
875 noise_mix_hash (hash, tmp, NOISE_HASH_LEN);
876 secure_zero_memory (tmp, NOISE_HASH_LEN);
880 noise_param_init (uint8_t ck[NOISE_HASH_LEN], uint8_t hash[NOISE_HASH_LEN],
881 const uint8_t s[NOISE_PUBLIC_KEY_LEN])
883 blake2s_state_t blake;
885 blake2s (ck, NOISE_HASH_LEN, (uint8_t *) NOISE_HANDSHAKE_NAME,
886 strlen (NOISE_HANDSHAKE_NAME), NULL, 0);
888 blake2s_init (&blake, NOISE_HASH_LEN);
889 blake2s_update (&blake, ck, NOISE_HASH_LEN);
890 blake2s_update (&blake, (uint8_t *) NOISE_IDENTIFIER_NAME,
891 strlen (NOISE_IDENTIFIER_NAME));
892 blake2s_final (&blake, hash, NOISE_HASH_LEN);
894 noise_mix_hash (hash, s, NOISE_PUBLIC_KEY_LEN);
898 noise_msg_encrypt (vlib_main_t * vm, uint8_t * dst, uint8_t * src,
899 size_t src_len, uint32_t key_idx,
900 uint8_t hash[NOISE_HASH_LEN])
902 /* Nonce always zero for Noise_IK */
903 chacha20poly1305_calc (vm, src, src_len, dst, hash, NOISE_HASH_LEN, 0,
904 VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC, key_idx);
905 noise_mix_hash (hash, dst, src_len + NOISE_AUTHTAG_LEN);
909 noise_msg_decrypt (vlib_main_t * vm, uint8_t * dst, uint8_t * src,
910 size_t src_len, uint32_t key_idx,
911 uint8_t hash[NOISE_HASH_LEN])
913 /* Nonce always zero for Noise_IK */
914 if (!chacha20poly1305_calc (vm, src, src_len, dst, hash, NOISE_HASH_LEN, 0,
915 VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC, key_idx))
917 noise_mix_hash (hash, src, src_len);
922 noise_msg_ephemeral (uint8_t ck[NOISE_HASH_LEN], uint8_t hash[NOISE_HASH_LEN],
923 const uint8_t src[NOISE_PUBLIC_KEY_LEN])
925 noise_mix_hash (hash, src, NOISE_PUBLIC_KEY_LEN);
926 noise_kdf (ck, NULL, NULL, src, NOISE_HASH_LEN, 0, 0,
927 NOISE_PUBLIC_KEY_LEN, ck);
931 noise_tai64n_now (uint8_t output[NOISE_TIMESTAMP_LEN])
934 uint32_t unix_nanosec;
939 unix_time_now_nsec_fraction (&unix_sec, &unix_nanosec);
941 /* Round down the nsec counter to limit precise timing leak. */
942 unix_nanosec &= REJECT_INTERVAL_MASK;
944 /* https://cr.yp.to/libtai/tai64.html */
945 sec = htobe64 (0x400000000000000aULL + unix_sec);
946 nsec = htobe32 (unix_nanosec);
948 /* memcpy to output buffer, assuming output could be unaligned. */
949 clib_memcpy (output, &sec, sizeof (sec));
950 clib_memcpy (output + sizeof (sec), &nsec, sizeof (nsec));
954 secure_zero_memory (void *v, size_t n)
956 static void *(*const volatile memset_v) (void *, int, size_t) = &memset;
961 * fd.io coding-style-patch-verification: ON
964 * eval: (c-set-style "gnu")