wireguard: initial implementation of wireguard protocol
[vpp.git] / src / plugins / wireguard / wireguard_messages.h
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __included_wg_messages_h__
17 #define __included_wg_messages_h__
18
19 #include <stdint.h>
20 #include <wireguard/wireguard_noise.h>
21 #include <wireguard/wireguard_cookie.h>
22
23 #define WG_TICK 0.01                            /**< WG tick period (s) */
24 #define WHZ (u32) (1/WG_TICK)           /**< WG tick frequency */
25
26 #define NOISE_KEY_LEN_BASE64 ((((NOISE_PUBLIC_KEY_LEN) + 2) / 3) * 4 + 1)
27 #define noise_encrypted_len(plain_len) ((plain_len) + NOISE_AUTHTAG_LEN)
28
29 enum limits
30 {
31   REKEY_TIMEOUT = 5,
32   REKEY_TIMEOUT_JITTER = WHZ / 3,
33   KEEPALIVE_TIMEOUT = 10,
34   MAX_TIMER_HANDSHAKES = 90 / REKEY_TIMEOUT,
35   MAX_PEERS = 1U << 20
36 };
37
38 #define foreach_wg_message_type \
39   _(INVALID, "Invalid")         \
40   _(HANDSHAKE_INITIATION, "Handshake initiation")               \
41   _(HANDSHAKE_RESPONSE, "Handshake response") \
42   _(HANDSHAKE_COOKIE, "Handshake cookie") \
43   _(DATA, "Data") \
44
45 typedef enum message_type
46 {
47 #define _(v,s) MESSAGE_##v,
48   foreach_wg_message_type
49 #undef _
50 } message_type_t;
51
52 typedef struct message_header
53 {
54   message_type_t type;
55 } message_header_t;
56
57 typedef struct message_handshake_initiation
58 {
59   message_header_t header;
60   u32 sender_index;
61   u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
62   u8 encrypted_static[noise_encrypted_len (NOISE_PUBLIC_KEY_LEN)];
63   u8 encrypted_timestamp[noise_encrypted_len (NOISE_TIMESTAMP_LEN)];
64   message_macs_t macs;
65 } message_handshake_initiation_t;
66
67 typedef struct message_handshake_response
68 {
69   message_header_t header;
70   u32 sender_index;
71   u32 receiver_index;
72   u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
73   u8 encrypted_nothing[noise_encrypted_len (0)];
74   message_macs_t macs;
75 } message_handshake_response_t;
76
77 typedef struct message_handshake_cookie
78 {
79   message_header_t header;
80   u32 receiver_index;
81   u8 nonce[COOKIE_NONCE_SIZE];
82   u8 encrypted_cookie[noise_encrypted_len (COOKIE_MAC_SIZE)];
83 } message_handshake_cookie_t;
84
85 typedef struct message_data
86 {
87   message_header_t header;
88   u32 receiver_index;
89   u64 counter;
90   u8 encrypted_data[];
91 } message_data_t;
92
93 #define message_data_len(plain_len) \
94     (noise_encrypted_len(plain_len) + sizeof(message_data_t))
95
96 #endif /* __included_wg_messages_h__ */
97
98 /*
99  * fd.io coding-style-patch-verification: ON
100  *
101  * Local Variables:
102  * eval: (c-set-style "gnu")
103  * End:
104  */