wireguard: Fix for tunnel encap
[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.h>
24
25 static void cookie_precompute_key (uint8_t *,
26                                    const uint8_t[COOKIE_INPUT_SIZE],
27                                    const char *);
28 static void cookie_macs_mac1 (message_macs_t *, const void *, size_t,
29                               const uint8_t[COOKIE_KEY_SIZE]);
30 static void cookie_macs_mac2 (message_macs_t *, const void *, size_t,
31                               const uint8_t[COOKIE_COOKIE_SIZE]);
32 static void cookie_checker_make_cookie (vlib_main_t * vm, cookie_checker_t *,
33                                         uint8_t[COOKIE_COOKIE_SIZE],
34                                         ip4_address_t ip4, u16 udp_port);
35
36 /* Public Functions */
37 void
38 cookie_maker_init (cookie_maker_t * cp, const uint8_t key[COOKIE_INPUT_SIZE])
39 {
40   clib_memset (cp, 0, sizeof (*cp));
41   cookie_precompute_key (cp->cp_mac1_key, key, COOKIE_MAC1_KEY_LABEL);
42   cookie_precompute_key (cp->cp_cookie_key, key, COOKIE_COOKIE_KEY_LABEL);
43 }
44
45 void
46 cookie_checker_update (cookie_checker_t * cc, uint8_t key[COOKIE_INPUT_SIZE])
47 {
48   if (key)
49     {
50       cookie_precompute_key (cc->cc_mac1_key, key, COOKIE_MAC1_KEY_LABEL);
51       cookie_precompute_key (cc->cc_cookie_key, key, COOKIE_COOKIE_KEY_LABEL);
52     }
53   else
54     {
55       clib_memset (cc->cc_mac1_key, 0, sizeof (cc->cc_mac1_key));
56       clib_memset (cc->cc_cookie_key, 0, sizeof (cc->cc_cookie_key));
57     }
58 }
59
60 void
61 cookie_maker_mac (cookie_maker_t * cp, message_macs_t * cm, void *buf,
62                   size_t len)
63 {
64   len = len - sizeof (message_macs_t);
65   cookie_macs_mac1 (cm, buf, len, cp->cp_mac1_key);
66
67   clib_memcpy (cp->cp_mac1_last, cm->mac1, COOKIE_MAC_SIZE);
68   cp->cp_mac1_valid = 1;
69
70   if (!wg_birthdate_has_expired (cp->cp_birthdate,
71                                  COOKIE_SECRET_MAX_AGE -
72                                  COOKIE_SECRET_LATENCY))
73     cookie_macs_mac2 (cm, buf, len, cp->cp_cookie);
74   else
75     clib_memset (cm->mac2, 0, COOKIE_MAC_SIZE);
76 }
77
78 enum cookie_mac_state
79 cookie_checker_validate_macs (vlib_main_t * vm, cookie_checker_t * cc,
80                               message_macs_t * cm, void *buf, size_t len,
81                               bool busy, ip4_address_t ip4, u16 udp_port)
82 {
83   message_macs_t our_cm;
84   uint8_t cookie[COOKIE_COOKIE_SIZE];
85
86   len = len - sizeof (message_macs_t);
87   cookie_macs_mac1 (&our_cm, buf, len, cc->cc_mac1_key);
88
89   /* If mac1 is invalid, we want to drop the packet */
90   if (clib_memcmp (our_cm.mac1, cm->mac1, COOKIE_MAC_SIZE) != 0)
91     return INVALID_MAC;
92
93   if (!busy)
94     return VALID_MAC_BUT_NO_COOKIE;
95
96   cookie_checker_make_cookie (vm, cc, cookie, ip4, udp_port);
97   cookie_macs_mac2 (&our_cm, buf, len, cookie);
98
99   /* If the mac2 is invalid, we want to send a cookie response */
100   if (clib_memcmp (our_cm.mac2, cm->mac2, COOKIE_MAC_SIZE) != 0)
101     return VALID_MAC_BUT_NO_COOKIE;
102
103   return VALID_MAC_WITH_COOKIE;
104 }
105
106 /* Private functions */
107 static void
108 cookie_precompute_key (uint8_t * key, const uint8_t input[COOKIE_INPUT_SIZE],
109                        const char *label)
110 {
111   blake2s_state_t blake;
112
113   blake2s_init (&blake, COOKIE_KEY_SIZE);
114   blake2s_update (&blake, (const uint8_t *) label, strlen (label));
115   blake2s_update (&blake, input, COOKIE_INPUT_SIZE);
116   blake2s_final (&blake, key, COOKIE_KEY_SIZE);
117 }
118
119 static void
120 cookie_macs_mac1 (message_macs_t * cm, const void *buf, size_t len,
121                   const uint8_t key[COOKIE_KEY_SIZE])
122 {
123   blake2s_state_t state;
124   blake2s_init_key (&state, COOKIE_MAC_SIZE, key, COOKIE_KEY_SIZE);
125   blake2s_update (&state, buf, len);
126   blake2s_final (&state, cm->mac1, COOKIE_MAC_SIZE);
127
128 }
129
130 static void
131 cookie_macs_mac2 (message_macs_t * cm, const void *buf, size_t len,
132                   const uint8_t key[COOKIE_COOKIE_SIZE])
133 {
134   blake2s_state_t state;
135   blake2s_init_key (&state, COOKIE_MAC_SIZE, key, COOKIE_COOKIE_SIZE);
136   blake2s_update (&state, buf, len);
137   blake2s_update (&state, cm->mac1, COOKIE_MAC_SIZE);
138   blake2s_final (&state, cm->mac2, COOKIE_MAC_SIZE);
139 }
140
141 static void
142 cookie_checker_make_cookie (vlib_main_t * vm, cookie_checker_t * cc,
143                             uint8_t cookie[COOKIE_COOKIE_SIZE],
144                             ip4_address_t ip4, u16 udp_port)
145 {
146   blake2s_state_t state;
147
148   if (wg_birthdate_has_expired (cc->cc_secret_birthdate,
149                                 COOKIE_SECRET_MAX_AGE))
150     {
151       cc->cc_secret_birthdate = vlib_time_now (vm);
152       RAND_bytes (cc->cc_secret, COOKIE_SECRET_SIZE);
153     }
154
155   blake2s_init_key (&state, COOKIE_COOKIE_SIZE, cc->cc_secret,
156                     COOKIE_SECRET_SIZE);
157
158   blake2s_update (&state, ip4.as_u8, sizeof (ip4_address_t));   //TODO: IP6
159   blake2s_update (&state, (u8 *) & udp_port, sizeof (u16));
160   blake2s_final (&state, cookie, COOKIE_COOKIE_SIZE);
161 }
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */