wireguard: add dos mitigation support
[vpp.git] / src / plugins / wireguard / wireguard_cookie.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>.
4  * Copyright (c) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stddef.h>
19 #include <openssl/rand.h>
20 #include <vlib/vlib.h>
21
22 #include <wireguard/wireguard_cookie.h>
23 #include <wireguard/wireguard_chachapoly.h>
24 #include <wireguard/wireguard.h>
25
26 static void cookie_precompute_key (uint8_t *,
27                                    const uint8_t[COOKIE_INPUT_SIZE],
28                                    const char *);
29 static void cookie_macs_mac1 (message_macs_t *, const void *, size_t,
30                               const uint8_t[COOKIE_KEY_SIZE]);
31 static void cookie_macs_mac2 (message_macs_t *, const void *, size_t,
32                               const uint8_t[COOKIE_COOKIE_SIZE]);
33 static void cookie_checker_make_cookie (vlib_main_t *vm, cookie_checker_t *,
34                                         uint8_t[COOKIE_COOKIE_SIZE],
35                                         ip46_address_t *ip, u16 udp_port);
36
37 /* Public Functions */
38 void
39 cookie_maker_init (cookie_maker_t * cp, const uint8_t key[COOKIE_INPUT_SIZE])
40 {
41   clib_memset (cp, 0, sizeof (*cp));
42   cookie_precompute_key (cp->cp_mac1_key, key, COOKIE_MAC1_KEY_LABEL);
43   cookie_precompute_key (cp->cp_cookie_key, key, COOKIE_COOKIE_KEY_LABEL);
44 }
45
46 void
47 cookie_checker_update (cookie_checker_t * cc, uint8_t key[COOKIE_INPUT_SIZE])
48 {
49   if (key)
50     {
51       cookie_precompute_key (cc->cc_mac1_key, key, COOKIE_MAC1_KEY_LABEL);
52       cookie_precompute_key (cc->cc_cookie_key, key, COOKIE_COOKIE_KEY_LABEL);
53     }
54   else
55     {
56       clib_memset (cc->cc_mac1_key, 0, sizeof (cc->cc_mac1_key));
57       clib_memset (cc->cc_cookie_key, 0, sizeof (cc->cc_cookie_key));
58     }
59 }
60
61 void
62 cookie_checker_create_payload (vlib_main_t *vm, cookie_checker_t *cc,
63                                message_macs_t *cm,
64                                uint8_t nonce[COOKIE_NONCE_SIZE],
65                                uint8_t ecookie[COOKIE_ENCRYPTED_SIZE],
66                                ip46_address_t *ip, u16 udp_port)
67 {
68   uint8_t cookie[COOKIE_COOKIE_SIZE];
69
70   cookie_checker_make_cookie (vm, cc, cookie, ip, udp_port);
71   RAND_bytes (nonce, COOKIE_NONCE_SIZE);
72
73   wg_xchacha20poly1305_encrypt (vm, cookie, COOKIE_COOKIE_SIZE, ecookie,
74                                 cm->mac1, COOKIE_MAC_SIZE, nonce,
75                                 cc->cc_cookie_key);
76
77   wg_secure_zero_memory (cookie, sizeof (cookie));
78 }
79
80 bool
81 cookie_maker_consume_payload (vlib_main_t *vm, cookie_maker_t *cp,
82                               uint8_t nonce[COOKIE_NONCE_SIZE],
83                               uint8_t ecookie[COOKIE_ENCRYPTED_SIZE])
84 {
85   uint8_t cookie[COOKIE_COOKIE_SIZE];
86
87   if (cp->cp_mac1_valid == 0)
88     {
89       return false;
90     }
91
92   if (!wg_xchacha20poly1305_decrypt (vm, ecookie, COOKIE_ENCRYPTED_SIZE,
93                                      cookie, cp->cp_mac1_last, COOKIE_MAC_SIZE,
94                                      nonce, cp->cp_cookie_key))
95     {
96       return false;
97     }
98
99   clib_memcpy (cp->cp_cookie, cookie, COOKIE_COOKIE_SIZE);
100   cp->cp_birthdate = vlib_time_now (vm);
101   cp->cp_mac1_valid = 0;
102
103   return true;
104 }
105
106 void
107 cookie_maker_mac (cookie_maker_t * cp, message_macs_t * cm, void *buf,
108                   size_t len)
109 {
110   len = len - sizeof (message_macs_t);
111   cookie_macs_mac1 (cm, buf, len, cp->cp_mac1_key);
112
113   clib_memcpy (cp->cp_mac1_last, cm->mac1, COOKIE_MAC_SIZE);
114   cp->cp_mac1_valid = 1;
115
116   if (!wg_birthdate_has_expired (cp->cp_birthdate,
117                                  COOKIE_SECRET_MAX_AGE -
118                                  COOKIE_SECRET_LATENCY))
119     cookie_macs_mac2 (cm, buf, len, cp->cp_cookie);
120   else
121     clib_memset (cm->mac2, 0, COOKIE_MAC_SIZE);
122 }
123
124 enum cookie_mac_state
125 cookie_checker_validate_macs (vlib_main_t *vm, cookie_checker_t *cc,
126                               message_macs_t *cm, void *buf, size_t len,
127                               bool busy, ip46_address_t *ip, u16 udp_port)
128 {
129   message_macs_t our_cm;
130   uint8_t cookie[COOKIE_COOKIE_SIZE];
131
132   len = len - sizeof (message_macs_t);
133   cookie_macs_mac1 (&our_cm, buf, len, cc->cc_mac1_key);
134
135   /* If mac1 is invalid, we want to drop the packet */
136   if (clib_memcmp (our_cm.mac1, cm->mac1, COOKIE_MAC_SIZE) != 0)
137     return INVALID_MAC;
138
139   if (!busy)
140     return VALID_MAC_BUT_NO_COOKIE;
141
142   cookie_checker_make_cookie (vm, cc, cookie, ip, udp_port);
143   cookie_macs_mac2 (&our_cm, buf, len, cookie);
144
145   /* If the mac2 is invalid, we want to send a cookie response */
146   if (clib_memcmp (our_cm.mac2, cm->mac2, COOKIE_MAC_SIZE) != 0)
147     return VALID_MAC_BUT_NO_COOKIE;
148
149   return VALID_MAC_WITH_COOKIE;
150 }
151
152 /* Private functions */
153 static void
154 cookie_precompute_key (uint8_t * key, const uint8_t input[COOKIE_INPUT_SIZE],
155                        const char *label)
156 {
157   blake2s_state_t blake;
158
159   blake2s_init (&blake, COOKIE_KEY_SIZE);
160   blake2s_update (&blake, (const uint8_t *) label, strlen (label));
161   blake2s_update (&blake, input, COOKIE_INPUT_SIZE);
162   blake2s_final (&blake, key, COOKIE_KEY_SIZE);
163 }
164
165 static void
166 cookie_macs_mac1 (message_macs_t * cm, const void *buf, size_t len,
167                   const uint8_t key[COOKIE_KEY_SIZE])
168 {
169   blake2s_state_t state;
170   blake2s_init_key (&state, COOKIE_MAC_SIZE, key, COOKIE_KEY_SIZE);
171   blake2s_update (&state, buf, len);
172   blake2s_final (&state, cm->mac1, COOKIE_MAC_SIZE);
173
174 }
175
176 static void
177 cookie_macs_mac2 (message_macs_t * cm, const void *buf, size_t len,
178                   const uint8_t key[COOKIE_COOKIE_SIZE])
179 {
180   blake2s_state_t state;
181   blake2s_init_key (&state, COOKIE_MAC_SIZE, key, COOKIE_COOKIE_SIZE);
182   blake2s_update (&state, buf, len);
183   blake2s_update (&state, cm->mac1, COOKIE_MAC_SIZE);
184   blake2s_final (&state, cm->mac2, COOKIE_MAC_SIZE);
185 }
186
187 static void
188 cookie_checker_make_cookie (vlib_main_t *vm, cookie_checker_t *cc,
189                             uint8_t cookie[COOKIE_COOKIE_SIZE],
190                             ip46_address_t *ip, u16 udp_port)
191 {
192   blake2s_state_t state;
193
194   if (wg_birthdate_has_expired (cc->cc_secret_birthdate,
195                                 COOKIE_SECRET_MAX_AGE))
196     {
197       cc->cc_secret_birthdate = vlib_time_now (vm);
198       RAND_bytes (cc->cc_secret, COOKIE_SECRET_SIZE);
199     }
200
201   blake2s_init_key (&state, COOKIE_COOKIE_SIZE, cc->cc_secret,
202                     COOKIE_SECRET_SIZE);
203
204   if (ip46_address_is_ip4 (ip))
205     {
206       blake2s_update (&state, ip->ip4.as_u8, sizeof (ip4_address_t));
207     }
208   else
209     {
210       blake2s_update (&state, ip->ip6.as_u8, sizeof (ip6_address_t));
211     }
212   blake2s_update (&state, (u8 *) & udp_port, sizeof (u16));
213   blake2s_final (&state, cookie, COOKIE_COOKIE_SIZE);
214 }
215
216 /*
217  * fd.io coding-style-patch-verification: ON
218  *
219  * Local Variables:
220  * eval: (c-set-style "gnu")
221  * End:
222  */