xref: /freebsd/sys/dev/wg/wg_noise.h (revision e0c4386e)
1 /* SPDX-License-Identifier: ISC
2  *
3  * Copyright (C) 2015-2021 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  * Copyright (C) 2019-2021 Matt Dunwoodie <ncon@noconroy.net>
5  */
6 
7 #ifndef __NOISE_H__
8 #define __NOISE_H__
9 
10 #include "crypto.h"
11 
12 #define NOISE_PUBLIC_KEY_LEN	CURVE25519_KEY_SIZE
13 #define NOISE_SYMMETRIC_KEY_LEN	CHACHA20POLY1305_KEY_SIZE
14 #define NOISE_TIMESTAMP_LEN	(sizeof(uint64_t) + sizeof(uint32_t))
15 #define NOISE_AUTHTAG_LEN	CHACHA20POLY1305_AUTHTAG_SIZE
16 #define NOISE_HASH_LEN		BLAKE2S_HASH_SIZE
17 
18 #define REJECT_AFTER_TIME	180
19 #define REKEY_TIMEOUT		5
20 #define KEEPALIVE_TIMEOUT	10
21 
22 struct noise_local;
23 struct noise_remote;
24 struct noise_keypair;
25 
26 /* Local configuration */
27 struct noise_local *
28 	noise_local_alloc(void *);
29 struct noise_local *
30 	noise_local_ref(struct noise_local *);
31 void	noise_local_put(struct noise_local *);
32 void	noise_local_free(struct noise_local *, void (*)(struct noise_local *));
33 void *	noise_local_arg(struct noise_local *);
34 
35 void	noise_local_private(struct noise_local *,
36 	    const uint8_t[NOISE_PUBLIC_KEY_LEN]);
37 int	noise_local_keys(struct noise_local *,
38 	    uint8_t[NOISE_PUBLIC_KEY_LEN],
39 	    uint8_t[NOISE_PUBLIC_KEY_LEN]);
40 
41 /* Remote configuration */
42 struct noise_remote *
43 	noise_remote_alloc(struct noise_local *, void *,
44 	    const uint8_t[NOISE_PUBLIC_KEY_LEN]);
45 int	noise_remote_enable(struct noise_remote *);
46 void	noise_remote_disable(struct noise_remote *);
47 struct noise_remote *
48 	noise_remote_lookup(struct noise_local *, const uint8_t[NOISE_PUBLIC_KEY_LEN]);
49 struct noise_remote *
50 	noise_remote_index(struct noise_local *, uint32_t);
51 struct noise_remote *
52 	noise_remote_ref(struct noise_remote *);
53 void	noise_remote_put(struct noise_remote *);
54 void	noise_remote_free(struct noise_remote *, void (*)(struct noise_remote *));
55 struct noise_local *
56 	noise_remote_local(struct noise_remote *);
57 void *	noise_remote_arg(struct noise_remote *);
58 
59 void	noise_remote_set_psk(struct noise_remote *,
60 	    const uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
61 int	noise_remote_keys(struct noise_remote *,
62 	    uint8_t[NOISE_PUBLIC_KEY_LEN],
63 	    uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
64 int	noise_remote_initiation_expired(struct noise_remote *);
65 void	noise_remote_handshake_clear(struct noise_remote *);
66 void	noise_remote_keypairs_clear(struct noise_remote *);
67 
68 /* Keypair functions */
69 struct noise_keypair *
70 	noise_keypair_lookup(struct noise_local *, uint32_t);
71 struct noise_keypair *
72 	noise_keypair_current(struct noise_remote *);
73 struct noise_keypair *
74 	noise_keypair_ref(struct noise_keypair *);
75 int	noise_keypair_received_with(struct noise_keypair *);
76 void	noise_keypair_put(struct noise_keypair *);
77 
78 struct noise_remote *
79 	noise_keypair_remote(struct noise_keypair *);
80 
81 int	noise_keypair_nonce_next(struct noise_keypair *, uint64_t *);
82 int	noise_keypair_nonce_check(struct noise_keypair *, uint64_t);
83 
84 int	noise_keep_key_fresh_send(struct noise_remote *);
85 int	noise_keep_key_fresh_recv(struct noise_remote *);
86 int	noise_keypair_encrypt(
87 	    struct noise_keypair *,
88 	    uint32_t *r_idx,
89 	    uint64_t nonce,
90 	    struct mbuf *);
91 int	noise_keypair_decrypt(
92 	    struct noise_keypair *,
93 	    uint64_t nonce,
94 	    struct mbuf *);
95 
96 /* Handshake functions */
97 int	noise_create_initiation(
98 	    struct noise_remote *,
99 	    uint32_t *s_idx,
100 	    uint8_t ue[NOISE_PUBLIC_KEY_LEN],
101 	    uint8_t es[NOISE_PUBLIC_KEY_LEN + NOISE_AUTHTAG_LEN],
102 	    uint8_t ets[NOISE_TIMESTAMP_LEN + NOISE_AUTHTAG_LEN]);
103 
104 int	noise_consume_initiation(
105 	    struct noise_local *,
106 	    struct noise_remote **,
107 	    uint32_t s_idx,
108 	    uint8_t ue[NOISE_PUBLIC_KEY_LEN],
109 	    uint8_t es[NOISE_PUBLIC_KEY_LEN + NOISE_AUTHTAG_LEN],
110 	    uint8_t ets[NOISE_TIMESTAMP_LEN + NOISE_AUTHTAG_LEN]);
111 
112 int	noise_create_response(
113 	    struct noise_remote *,
114 	    uint32_t *s_idx,
115 	    uint32_t *r_idx,
116 	    uint8_t ue[NOISE_PUBLIC_KEY_LEN],
117 	    uint8_t en[0 + NOISE_AUTHTAG_LEN]);
118 
119 int	noise_consume_response(
120 	    struct noise_local *,
121 	    struct noise_remote **,
122 	    uint32_t s_idx,
123 	    uint32_t r_idx,
124 	    uint8_t ue[NOISE_PUBLIC_KEY_LEN],
125 	    uint8_t en[0 + NOISE_AUTHTAG_LEN]);
126 
127 #ifdef SELFTESTS
128 bool	noise_counter_selftest(void);
129 #endif /* SELFTESTS */
130 
131 #endif /* __NOISE_H__ */
132