^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _WG_COOKIE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _WG_COOKIE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "messages.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct wg_peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct cookie_checker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u8 secret[NOISE_HASH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u8 cookie_encryption_key[NOISE_SYMMETRIC_KEY_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u8 message_mac1_key[NOISE_SYMMETRIC_KEY_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u64 secret_birthdate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct rw_semaphore secret_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct wg_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct cookie {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u64 birthdate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool is_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u8 cookie[COOKIE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) bool have_sent_mac1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u8 last_mac1_sent[COOKIE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u8 cookie_decryption_key[NOISE_SYMMETRIC_KEY_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u8 message_mac1_key[NOISE_SYMMETRIC_KEY_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct rw_semaphore lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) enum cookie_mac_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) INVALID_MAC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) VALID_MAC_BUT_NO_COOKIE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) VALID_MAC_WITH_COOKIE_BUT_RATELIMITED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) VALID_MAC_WITH_COOKIE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void wg_cookie_checker_init(struct cookie_checker *checker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct wg_device *wg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void wg_cookie_checker_precompute_device_keys(struct cookie_checker *checker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void wg_cookie_checker_precompute_peer_keys(struct wg_peer *peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void wg_cookie_init(struct cookie *cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) enum cookie_mac_state wg_cookie_validate_packet(struct cookie_checker *checker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) bool check_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void wg_cookie_add_mac_to_packet(void *message, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct wg_peer *peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void wg_cookie_message_create(struct message_handshake_cookie *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct sk_buff *skb, __le32 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct cookie_checker *checker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void wg_cookie_message_consume(struct message_handshake_cookie *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct wg_device *wg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #endif /* _WG_COOKIE_H */