1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4 */ 5 #ifndef _WG_NOISE_H 6 #define _WG_NOISE_H 7 8 #include "messages.h" 9 #include "peerlookup.h" 10 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> 17 18 union noise_counter { 19 struct { 20 u64 counter; 21 unsigned long backtrack[COUNTER_BITS_TOTAL / BITS_PER_LONG]; 22 spinlock_t lock; 23 } receive; 24 atomic64_t counter; 25 }; 26 27 struct noise_symmetric_key { 28 u8 key[NOISE_SYMMETRIC_KEY_LEN]; 29 union noise_counter counter; 30 u64 birthdate; 31 bool is_valid; 32 }; 33 34 struct noise_keypair { 35 struct index_hashtable_entry entry; 36 struct noise_symmetric_key sending; 37 struct noise_symmetric_key receiving; 38 __le32 remote_index; 39 bool i_am_the_initiator; 40 struct kref refcount; 41 struct rcu_head rcu; 42 u64 internal_id; 43 }; 44 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; 50 }; 51 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; 56 bool has_identity; 57 }; 58 59 enum noise_handshake_state { 60 HANDSHAKE_ZEROED, 61 HANDSHAKE_CREATED_INITIATION, 62 HANDSHAKE_CONSUMED_INITIATION, 63 HANDSHAKE_CREATED_RESPONSE, 64 HANDSHAKE_CONSUMED_RESPONSE 65 }; 66 67 struct noise_handshake { 68 struct index_hashtable_entry entry; 69 70 enum noise_handshake_state state; 71 u64 last_initiation_consumption; 72 73 struct noise_static_identity *static_identity; 74 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]; 79 80 u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN]; 81 82 u8 hash[NOISE_HASH_LEN]; 83 u8 chaining_key[NOISE_HASH_LEN]; 84 85 u8 latest_timestamp[NOISE_TIMESTAMP_LEN]; 86 __le32 remote_index; 87 88 /* Protects all members except the immutable (after noise_handshake_ 89 * init): remote_static, precomputed_static_static, static_identity. 90 */ 91 struct rw_semaphore lock; 92 }; 93 94 struct wg_device; 95 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) 104 { 105 atomic64_set(handshake_ns, ktime_get_coarse_boottime_ns() - 106 (u64)(REKEY_TIMEOUT + 1) * NSEC_PER_SEC); 107 } 108 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); 115 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); 120 121 bool 122 wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst, 123 struct noise_handshake *handshake); 124 struct wg_peer * 125 wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src, 126 struct wg_device *wg); 127 128 bool wg_noise_handshake_create_response(struct message_handshake_response *dst, 129 struct noise_handshake *handshake); 130 struct wg_peer * 131 wg_noise_handshake_consume_response(struct message_handshake_response *src, 132 struct wg_device *wg); 133 134 bool wg_noise_handshake_begin_session(struct noise_handshake *handshake, 135 struct noise_keypairs *keypairs); 136 137 #endif /* _WG_NOISE_H */ 138