1 /* SPDX-License-Identifier: GPL-2.0 */
9 #include "peerlookup.h"
11 #include <linux/types.h>
12 #include <linux/spinlock.h>
13 #include <linux/atomic.h>
14 #include <linux/rwsem.h>
15 #include <linux/mutex.h>
16 #include <linux/kref.h>
21 unsigned long backtrack[COUNTER_BITS_TOTAL / BITS_PER_LONG];
27 struct noise_symmetric_key {
28 u8 key[NOISE_SYMMETRIC_KEY_LEN];
29 union noise_counter counter;
34 struct noise_keypair {
35 struct index_hashtable_entry entry;
36 struct noise_symmetric_key sending;
37 struct noise_symmetric_key receiving;
39 bool i_am_the_initiator;
45 struct noise_keypairs {
46 struct noise_keypair __rcu *current_keypair;
47 struct noise_keypair __rcu *previous_keypair;
48 struct noise_keypair __rcu *next_keypair;
49 spinlock_t keypair_update_lock;
52 struct noise_static_identity {
53 u8 static_public[NOISE_PUBLIC_KEY_LEN];
54 u8 static_private[NOISE_PUBLIC_KEY_LEN];
55 struct rw_semaphore lock;
59 enum noise_handshake_state {
61 HANDSHAKE_CREATED_INITIATION,
62 HANDSHAKE_CONSUMED_INITIATION,
63 HANDSHAKE_CREATED_RESPONSE,
64 HANDSHAKE_CONSUMED_RESPONSE
67 struct noise_handshake {
68 struct index_hashtable_entry entry;
70 enum noise_handshake_state state;
71 u64 last_initiation_consumption;
73 struct noise_static_identity *static_identity;
75 u8 ephemeral_private[NOISE_PUBLIC_KEY_LEN];
76 u8 remote_static[NOISE_PUBLIC_KEY_LEN];
77 u8 remote_ephemeral[NOISE_PUBLIC_KEY_LEN];
78 u8 precomputed_static_static[NOISE_PUBLIC_KEY_LEN];
80 u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN];
82 u8 hash[NOISE_HASH_LEN];
83 u8 chaining_key[NOISE_HASH_LEN];
85 u8 latest_timestamp[NOISE_TIMESTAMP_LEN];
88 /* Protects all members except the immutable (after noise_handshake_
89 * init): remote_static, precomputed_static_static, static_identity.
91 struct rw_semaphore lock;
96 void wg_noise_init(void);
97 bool wg_noise_handshake_init(struct noise_handshake *handshake,
98 struct noise_static_identity *static_identity,
99 const u8 peer_public_key[NOISE_PUBLIC_KEY_LEN],
100 const u8 peer_preshared_key[NOISE_SYMMETRIC_KEY_LEN],
101 struct wg_peer *peer);
102 void wg_noise_handshake_clear(struct noise_handshake *handshake);
103 static inline void wg_noise_reset_last_sent_handshake(atomic64_t *handshake_ns)
105 atomic64_set(handshake_ns, ktime_get_coarse_boottime_ns() -
106 (u64)(REKEY_TIMEOUT + 1) * NSEC_PER_SEC);
109 void wg_noise_keypair_put(struct noise_keypair *keypair, bool unreference_now);
110 struct noise_keypair *wg_noise_keypair_get(struct noise_keypair *keypair);
111 void wg_noise_keypairs_clear(struct noise_keypairs *keypairs);
112 bool wg_noise_received_with_keypair(struct noise_keypairs *keypairs,
113 struct noise_keypair *received_keypair);
114 void wg_noise_expire_current_peer_keypairs(struct wg_peer *peer);
116 void wg_noise_set_static_identity_private_key(
117 struct noise_static_identity *static_identity,
118 const u8 private_key[NOISE_PUBLIC_KEY_LEN]);
119 bool wg_noise_precompute_static_static(struct wg_peer *peer);
122 wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst,
123 struct noise_handshake *handshake);
125 wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,
126 struct wg_device *wg);
128 bool wg_noise_handshake_create_response(struct message_handshake_response *dst,
129 struct noise_handshake *handshake);
131 wg_noise_handshake_consume_response(struct message_handshake_response *src,
132 struct wg_device *wg);
134 bool wg_noise_handshake_begin_session(struct noise_handshake *handshake,
135 struct noise_keypairs *keypairs);
137 #endif /* _WG_NOISE_H */