Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
0004  */
0005 #ifndef _WG_NOISE_H
0006 #define _WG_NOISE_H
0007 
0008 #include "messages.h"
0009 #include "peerlookup.h"
0010 
0011 #include <linux/types.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/atomic.h>
0014 #include <linux/rwsem.h>
0015 #include <linux/mutex.h>
0016 #include <linux/kref.h>
0017 
0018 struct noise_replay_counter {
0019     u64 counter;
0020     spinlock_t lock;
0021     unsigned long backtrack[COUNTER_BITS_TOTAL / BITS_PER_LONG];
0022 };
0023 
0024 struct noise_symmetric_key {
0025     u8 key[NOISE_SYMMETRIC_KEY_LEN];
0026     u64 birthdate;
0027     bool is_valid;
0028 };
0029 
0030 struct noise_keypair {
0031     struct index_hashtable_entry entry;
0032     struct noise_symmetric_key sending;
0033     atomic64_t sending_counter;
0034     struct noise_symmetric_key receiving;
0035     struct noise_replay_counter receiving_counter;
0036     __le32 remote_index;
0037     bool i_am_the_initiator;
0038     struct kref refcount;
0039     struct rcu_head rcu;
0040     u64 internal_id;
0041 };
0042 
0043 struct noise_keypairs {
0044     struct noise_keypair __rcu *current_keypair;
0045     struct noise_keypair __rcu *previous_keypair;
0046     struct noise_keypair __rcu *next_keypair;
0047     spinlock_t keypair_update_lock;
0048 };
0049 
0050 struct noise_static_identity {
0051     u8 static_public[NOISE_PUBLIC_KEY_LEN];
0052     u8 static_private[NOISE_PUBLIC_KEY_LEN];
0053     struct rw_semaphore lock;
0054     bool has_identity;
0055 };
0056 
0057 enum noise_handshake_state {
0058     HANDSHAKE_ZEROED,
0059     HANDSHAKE_CREATED_INITIATION,
0060     HANDSHAKE_CONSUMED_INITIATION,
0061     HANDSHAKE_CREATED_RESPONSE,
0062     HANDSHAKE_CONSUMED_RESPONSE
0063 };
0064 
0065 struct noise_handshake {
0066     struct index_hashtable_entry entry;
0067 
0068     enum noise_handshake_state state;
0069     u64 last_initiation_consumption;
0070 
0071     struct noise_static_identity *static_identity;
0072 
0073     u8 ephemeral_private[NOISE_PUBLIC_KEY_LEN];
0074     u8 remote_static[NOISE_PUBLIC_KEY_LEN];
0075     u8 remote_ephemeral[NOISE_PUBLIC_KEY_LEN];
0076     u8 precomputed_static_static[NOISE_PUBLIC_KEY_LEN];
0077 
0078     u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN];
0079 
0080     u8 hash[NOISE_HASH_LEN];
0081     u8 chaining_key[NOISE_HASH_LEN];
0082 
0083     u8 latest_timestamp[NOISE_TIMESTAMP_LEN];
0084     __le32 remote_index;
0085 
0086     /* Protects all members except the immutable (after noise_handshake_
0087      * init): remote_static, precomputed_static_static, static_identity.
0088      */
0089     struct rw_semaphore lock;
0090 };
0091 
0092 struct wg_device;
0093 
0094 void wg_noise_init(void);
0095 void wg_noise_handshake_init(struct noise_handshake *handshake,
0096                  struct noise_static_identity *static_identity,
0097                  const u8 peer_public_key[NOISE_PUBLIC_KEY_LEN],
0098                  const u8 peer_preshared_key[NOISE_SYMMETRIC_KEY_LEN],
0099                  struct wg_peer *peer);
0100 void wg_noise_handshake_clear(struct noise_handshake *handshake);
0101 static inline void wg_noise_reset_last_sent_handshake(atomic64_t *handshake_ns)
0102 {
0103     atomic64_set(handshake_ns, ktime_get_coarse_boottime_ns() -
0104                        (u64)(REKEY_TIMEOUT + 1) * NSEC_PER_SEC);
0105 }
0106 
0107 void wg_noise_keypair_put(struct noise_keypair *keypair, bool unreference_now);
0108 struct noise_keypair *wg_noise_keypair_get(struct noise_keypair *keypair);
0109 void wg_noise_keypairs_clear(struct noise_keypairs *keypairs);
0110 bool wg_noise_received_with_keypair(struct noise_keypairs *keypairs,
0111                     struct noise_keypair *received_keypair);
0112 void wg_noise_expire_current_peer_keypairs(struct wg_peer *peer);
0113 
0114 void wg_noise_set_static_identity_private_key(
0115     struct noise_static_identity *static_identity,
0116     const u8 private_key[NOISE_PUBLIC_KEY_LEN]);
0117 void wg_noise_precompute_static_static(struct wg_peer *peer);
0118 
0119 bool
0120 wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst,
0121                      struct noise_handshake *handshake);
0122 struct wg_peer *
0123 wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,
0124                       struct wg_device *wg);
0125 
0126 bool wg_noise_handshake_create_response(struct message_handshake_response *dst,
0127                     struct noise_handshake *handshake);
0128 struct wg_peer *
0129 wg_noise_handshake_consume_response(struct message_handshake_response *src,
0130                     struct wg_device *wg);
0131 
0132 bool wg_noise_handshake_begin_session(struct noise_handshake *handshake,
0133                       struct noise_keypairs *keypairs);
0134 
0135 #endif /* _WG_NOISE_H */