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
22struct noise_local;
23struct noise_remote;
24struct noise_keypair;
25
26/* Local configuration */
27struct noise_local *
28	noise_local_alloc(void *);
29struct noise_local *
30	noise_local_ref(struct noise_local *);
31void	noise_local_put(struct noise_local *);
32void	noise_local_free(struct noise_local *, void (*)(struct noise_local *));
33void *	noise_local_arg(struct noise_local *);
34
35void	noise_local_private(struct noise_local *,
36	    const uint8_t[NOISE_PUBLIC_KEY_LEN]);
37int	noise_local_keys(struct noise_local *,
38	    uint8_t[NOISE_PUBLIC_KEY_LEN],
39	    uint8_t[NOISE_PUBLIC_KEY_LEN]);
40
41/* Remote configuration */
42struct noise_remote *
43	noise_remote_alloc(struct noise_local *, void *,
44	    const uint8_t[NOISE_PUBLIC_KEY_LEN]);
45int	noise_remote_enable(struct noise_remote *);
46void	noise_remote_disable(struct noise_remote *);
47struct noise_remote *
48	noise_remote_lookup(struct noise_local *, const uint8_t[NOISE_PUBLIC_KEY_LEN]);
49struct noise_remote *
50	noise_remote_index(struct noise_local *, uint32_t);
51struct noise_remote *
52	noise_remote_ref(struct noise_remote *);
53void	noise_remote_put(struct noise_remote *);
54void	noise_remote_free(struct noise_remote *, void (*)(struct noise_remote *));
55struct noise_local *
56	noise_remote_local(struct noise_remote *);
57void *	noise_remote_arg(struct noise_remote *);
58
59void	noise_remote_set_psk(struct noise_remote *,
60	    const uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
61int	noise_remote_keys(struct noise_remote *,
62	    uint8_t[NOISE_PUBLIC_KEY_LEN],
63	    uint8_t[NOISE_SYMMETRIC_KEY_LEN]);
64int	noise_remote_initiation_expired(struct noise_remote *);
65void	noise_remote_handshake_clear(struct noise_remote *);
66void	noise_remote_keypairs_clear(struct noise_remote *);
67
68/* Keypair functions */
69struct noise_keypair *
70	noise_keypair_lookup(struct noise_local *, uint32_t);
71struct noise_keypair *
72	noise_keypair_current(struct noise_remote *);
73struct noise_keypair *
74	noise_keypair_ref(struct noise_keypair *);
75int	noise_keypair_received_with(struct noise_keypair *);
76void	noise_keypair_put(struct noise_keypair *);
77
78struct noise_remote *
79	noise_keypair_remote(struct noise_keypair *);
80
81int	noise_keypair_nonce_next(struct noise_keypair *, uint64_t *);
82int	noise_keypair_nonce_check(struct noise_keypair *, uint64_t);
83
84int	noise_keep_key_fresh_send(struct noise_remote *);
85int	noise_keep_key_fresh_recv(struct noise_remote *);
86int	noise_keypair_encrypt(
87	    struct noise_keypair *,
88	    uint32_t *r_idx,
89	    uint64_t nonce,
90	    struct mbuf *);
91int	noise_keypair_decrypt(
92	    struct noise_keypair *,
93	    uint64_t nonce,
94	    struct mbuf *);
95
96/* Handshake functions */
97int	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
104int	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
112int	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
119int	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
128bool	noise_counter_selftest(void);
129#endif /* SELFTESTS */
130
131#endif /* __NOISE_H__ */
132