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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <openssl/rand.h>
20 #include <vlib/vlib.h>
22 #include <wireguard/wireguard_cookie.h>
23 #include <wireguard/wireguard.h>
25 static void cookie_precompute_key (uint8_t *,
26 const uint8_t[COOKIE_INPUT_SIZE],
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);
36 /* Public Functions */
38 cookie_maker_init (cookie_maker_t * cp, const uint8_t key[COOKIE_INPUT_SIZE])
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);
46 cookie_checker_update (cookie_checker_t * cc, uint8_t key[COOKIE_INPUT_SIZE])
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);
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));
61 cookie_maker_mac (cookie_maker_t * cp, message_macs_t * cm, void *buf,
64 len = len - sizeof (message_macs_t);
65 cookie_macs_mac1 (cm, buf, len, cp->cp_mac1_key);
67 clib_memcpy (cp->cp_mac1_last, cm->mac1, COOKIE_MAC_SIZE);
68 cp->cp_mac1_valid = 1;
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);
75 clib_memset (cm->mac2, 0, COOKIE_MAC_SIZE);
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)
83 message_macs_t our_cm;
84 uint8_t cookie[COOKIE_COOKIE_SIZE];
86 len = len - sizeof (message_macs_t);
87 cookie_macs_mac1 (&our_cm, buf, len, cc->cc_mac1_key);
89 /* If mac1 is invald, we want to drop the packet */
90 if (clib_memcmp (our_cm.mac1, cm->mac1, COOKIE_MAC_SIZE) != 0)
94 return VALID_MAC_BUT_NO_COOKIE;
96 cookie_checker_make_cookie (vm, cc, cookie, ip4, udp_port);
97 cookie_macs_mac2 (&our_cm, buf, len, cookie);
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;
103 return VALID_MAC_WITH_COOKIE;
106 /* Private functions */
108 cookie_precompute_key (uint8_t * key, const uint8_t input[COOKIE_INPUT_SIZE],
111 blake2s_state_t blake;
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);
120 cookie_macs_mac1 (message_macs_t * cm, const void *buf, size_t len,
121 const uint8_t key[COOKIE_KEY_SIZE])
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);
131 cookie_macs_mac2 (message_macs_t * cm, const void *buf, size_t len,
132 const uint8_t key[COOKIE_COOKIE_SIZE])
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);
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)
146 blake2s_state_t state;
148 if (wg_birthdate_has_expired (cc->cc_secret_birthdate,
149 COOKIE_SECRET_MAX_AGE))
151 cc->cc_secret_birthdate = vlib_time_now (vm);
152 RAND_bytes (cc->cc_secret, COOKIE_SECRET_SIZE);
155 blake2s_init_key (&state, COOKIE_COOKIE_SIZE, cc->cc_secret,
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);
164 * fd.io coding-style-patch-verification: ON
167 * eval: (c-set-style "gnu")