Debian Codegraph @

src/crypto/psk.c

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Gijs Peskens <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "config.h"
9 #include "psk.h"
10 #include "log-private.h"
11 #include "crypto-private.h"
12 #include "proto/rist_time.h"
13 #include <string.h>
14
15 /* Minimum wall-time between nonce-adoption rekeys (PBKDF2) on one key.
16 * Bounds the CPU a packet flood with randomized nonces can burn. The floor
17 * is dictated by the reflector topology: a subscriber's single key sees
18 * interleaved nonces from the reflector's own RTCP and the forwarded
19 * publisher media, so legitimate rekeys arrive at the RTCP cadence
20 * (~2 per 100 ms interval); the limit must stay well above that or valid
21 * media is dropped. 10 ms still caps a flood at ~100 PBKDF2/s per key. */
22 #define RIST_REKEY_MIN_INTERVAL (ONE_SECOND / 100)
23
24 /* bad_count strikes marking decryption-failure abuse before the decrypt-path
25 * rekey rate limit engages; legitimate nonce churn keeps bad_count at 0. */
26 #define RIST_REKEY_ABUSE_STRIKES 2
27
28 #if HAVE_MBEDTLS
29 #include "mbedtls/aes.h"
30 #include "mbedtls/md.h"
31 #include "mbedtls/pkcs5.h"
32 #elif HAVE_NETTLE
33 #include <nettle/pbkdf2.h>
34 #include <nettle/aes.h>
35 #include <nettle/ctr.h>
36 #elif defined(LINUX_CRYPTO)
37 #include "linux-crypto.h"
38 #endif
39 #if !HAVE_MBEDTLS
40 #include "fastpbkdf2.h"
41 #endif
42
43 #ifndef AES_BLOCK_SIZE
44 #define AES_BLOCK_SIZE 16
45 #endif
46
47 #include <stdint.h>
48
49 //TODO: handle failures?
50 int _librist_crypto_psk_rist_key_init(struct rist_key *key, uint32_t key_size, uint32_t rotation, const char *password, bool odd)
51 {
52 key->password_len = strnlen(password, sizeof(key->password) - 1);
53 memcpy(key->password, password, key->password_len);
54 key->password[key->password_len] = '\0';
55 key->key_size = key_size;
56 key->key_rotation = rotation;
57 #if HAVE_MBEDTLS
58 mbedtls_aes_init(&key->mbedtls_aes_ctx);
59 #elif HAVE_NETTLE
60 memset(&key->nettle_ctx, 0, sizeof(key->nettle_ctx));
61 #elif defined(LINUX_CRYPTO)
62 linux_crypto_init(&key->linux_crypto_ctx);
63 #endif
64 key->odd = odd;
65 return 0;
66 }
67
68 int _librist_crypto_psk_rist_key_destroy(struct rist_key *key)
69 {
70 if (key->key_size) {
71 #if HAVE_MBEDTLS
72 mbedtls_aes_free(&key->mbedtls_aes_ctx);
73 #elif HAVE_NETTLE
74 //nothing to do here
75 #elif defined(LINUX_CRYPTO)
76 linux_crypto_free(&key->linux_crypto_ctx);
77 #endif
78 }
79 /* Wipe key material: the peer struct is freed without zeroing. */
80 _librist_crypto_secure_zero(key->password, sizeof(key->password));
81 _librist_crypto_secure_zero(key->gre_nonce, sizeof(key->gre_nonce));
82 _librist_crypto_secure_zero(key->iv, sizeof(key->iv));
83 _librist_crypto_secure_zero(key->aes_key_sched, sizeof(key->aes_key_sched));
84 return 0;
85 }
86
87 int _librist_crypto_psk_rist_key_clone(struct rist_key *key_in, struct rist_key *key_out)
88 {
89 key_out->password_len = key_in->password_len;
90 memcpy(key_out->password, key_in->password, key_in->password_len);
91 key_out->password[key_out->password_len] = '\0';
92 key_out->key_size = key_in->key_size;
93 key_out->key_rotation = key_in->key_rotation;
94 #if HAVE_MBEDTLS
95 mbedtls_aes_init(&key_out->mbedtls_aes_ctx);
96 #elif HAVE_NETTLE
97 memset(&key_out->nettle_ctx, 0, sizeof(key_out->nettle_ctx));
98 #elif defined(LINUX_CRYPTO)
99 linux_crypto_init(&key_out->linux_crypto_ctx);
100 #endif
101 key_out->odd = key_in->odd;
102 return 0;
103 }
104
105 static void _librist_crypto_aes_key(struct rist_key *key)
106 {
107 uint8_t aes_key[256 / 8];
108 /* Hard invariant: key_size drives the PBKDF2 output length into this
109 * 32-byte stack buffer. Anything but a valid AES size here means the
110 * value arrived unvalidated from the wire or config; refuse rather
111 * than overflow. */
112 if (key->key_size != 128 && key->key_size != 192 && key->key_size != 256) {
113 key->bad_decryption = true;
114 return;
115 }
116 #if HAVE_MBEDTLS
117 mbedtls_md_context_t sha_ctx;
118 const mbedtls_md_info_t *info_sha;
119 int ret = -1;
120 mbedtls_md_init(&sha_ctx);
121 info_sha = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
122 if (info_sha == NULL)
123 goto fail;
124
125 ret = mbedtls_md_setup(&sha_ctx, info_sha, 1);
126 if (ret != 0)
127 goto fail;
128
129 ret = mbedtls_pkcs5_pbkdf2_hmac(
130 &sha_ctx, (const unsigned char *)key->password, key->password_len,
131 key->gre_nonce, sizeof(key->gre_nonce),
132 RIST_PBKDF2_HMAC_SHA256_ITERATIONS, key->key_size / 8, aes_key);
133 if (ret != 0)
134 goto fail;
135 mbedtls_md_free(&sha_ctx);
136 #elif HAVE_NETTLE
137 nettle_pbkdf2_hmac_sha256(key->password_len,(const uint8_t*)key->password,
138 RIST_PBKDF2_HMAC_SHA256_ITERATIONS,
139 sizeof(key->gre_nonce), key->gre_nonce,
140 key->key_size/8, aes_key);
141 #else
142 fastpbkdf2_hmac_sha256(
143 (const void *) key->password, key->password_len,
144 (const void *) key->gre_nonce, sizeof(key->gre_nonce),
145 RIST_PBKDF2_HMAC_SHA256_ITERATIONS,
146 aes_key, key->key_size / 8);
147 #endif
148
149
150 #if HAVE_MBEDTLS
151 mbedtls_aes_setkey_enc(&key->mbedtls_aes_ctx, aes_key, key->key_size);
152 #elif HAVE_NETTLE
153 switch(key->key_size) {
154 case 256:
155 nettle_aes256_set_encrypt_key(&key->nettle_ctx.ctx256, aes_key);
156 break;
157 case 192:
158 nettle_aes192_set_encrypt_key(&key->nettle_ctx.ctx192, aes_key);
159 break;
160 case 128:
161 RIST_FALLTHROUGH;
162 default:
163 nettle_aes128_set_encrypt_key(&key->nettle_ctx.ctx128, aes_key);
164 }
165 #elif defined(LINUX_CRYPTO)
166 if (key->linux_crypto_ctx)
167 linux_crypto_set_key(aes_key, key->key_size / 8, key->linux_crypto_ctx);
168 else
169 aes_key_setup(aes_key, key->aes_key_sched, key->key_size);
170 #else
171 aes_key_setup(aes_key, key->aes_key_sched, key->key_size);
172 #endif
173 key->used_times = 0;
174 _librist_crypto_secure_zero(aes_key, sizeof(aes_key));
175 return;
176 #if HAVE_MBEDTLS
177 fail:
178 mbedtls_md_free(&sha_ctx);
179 _librist_crypto_secure_zero(aes_key, sizeof(aes_key));
180 /* Leave any prior key install in place but force the lockout flag so we
181 * don't run AES-CTR with whatever happened to be on the stack. */
182 key->bad_decryption = true;
183 return;
184 #endif
185 }
186
187 //This doesn't really belong here (not PSK related), but since all other crypto interop stuff is here it goes in here..
188 void _librist_crypto_aes_ctr(const uint8_t key[], int key_size, uint8_t iv[], const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len) {
189 #if HAVE_MBEDTLS
190 mbedtls_aes_context ctx;
191 mbedtls_aes_init(&ctx);
192 mbedtls_aes_setkey_enc(&ctx, key, key_size);
193 uint8_t stream_block[AES_BLOCK_SIZE] = {0};
194 size_t nc_off = 0;
195 mbedtls_aes_crypt_ctr(&ctx, payload_len, &nc_off, iv, stream_block, inbuf, outbuf);
196 mbedtls_aes_free(&ctx);
197 #elif HAVE_NETTLE
198 union rist_nettle_aes_ctx aes_ctx;
199 memset(&aes_ctx, 0, sizeof(aes_ctx));
200 nettle_cipher_func *f;
201 switch (key_size) {
202 case 256:
203 nettle_aes256_set_encrypt_key(&aes_ctx.ctx256, key);
204 f = (nettle_cipher_func *)nettle_aes256_encrypt;
205 break;
206 case 192:
207 nettle_aes192_set_encrypt_key(&aes_ctx.ctx192, key);
208 f = (nettle_cipher_func *)nettle_aes192_encrypt;
209 break;
210 case 128:
211 nettle_aes128_set_encrypt_key(&aes_ctx.ctx128, key);
212 f = (nettle_cipher_func *)nettle_aes128_encrypt;
213 break;
214 default:
215 return;
216 }
217 nettle_ctr_crypt(&aes_ctx, f, AES_BLOCK_SIZE, iv, payload_len, outbuf, inbuf);
218 #else
219 uint32_t aes_key_sched[60];
220 aes_key_setup(key, aes_key_sched, key_size);
221 aes_decrypt_ctr(inbuf, payload_len, outbuf, aes_key_sched, key_size, iv);
222 #endif
223 }
224
225 static void _librist_crypto_psk_aes_ctr(struct rist_key *key, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
226 {
227 #if HAVE_MBEDTLS
228 mbedtls_aes_crypt_ctr(&key->mbedtls_aes_ctx, payload_len, &key->aes_offset, key->iv, key->strean_block, inbuf, outbuf);
229 #elif HAVE_NETTLE
230 nettle_cipher_func *f;
231 switch(key->key_size) {
232 case 128:
233 f = (nettle_cipher_func *)nettle_aes128_encrypt;
234 break;
235 case 192:
236 f = (nettle_cipher_func *)nettle_aes192_encrypt;
237 break;
238 case 256:
239 f = (nettle_cipher_func *)nettle_aes256_encrypt;
240 break;
241 default:
242 return;
243 }
244 nettle_ctr_crypt(&key->nettle_ctx, f, AES_BLOCK_SIZE, key->iv,payload_len, outbuf, inbuf);
245 #elif defined(LINUX_CRYPTO)
246 if (key->linux_crypto_ctx)
247 linux_crypto_decrypt(inbuf, outbuf, payload_len, key->iv, key->linux_crypto_ctx);
248 else
249 aes_decrypt_ctr(inbuf, payload_len, outbuf, key->aes_key_sched, key->key_size, key->iv);
250 #else
251 aes_decrypt_ctr(inbuf, payload_len, outbuf, key->aes_key_sched, key->key_size, key->iv);
252 #endif
253 key->used_times++;
254 }
255
256 static void _librist_crypto_psk_prepare_iv(struct rist_key *key, uint8_t gre_version, uint32_t seq_nbe) {
257 /* Prepare AES iv */
258 // The byte array needs to be zeroes and then the seq in network byte order
259 uint8_t copy_offset = gre_version >= 1 ? 0 : 12;
260 memset(key->iv, 0, 16);
261 memcpy(key->iv + copy_offset, &seq_nbe, sizeof(seq_nbe));
262 }
263
264 static void _librist_crypto_psk_generate_nonce(struct rist_key *key) {
265 /* Fail-closed CSPRNG: on failure, mark the key locked so encrypt/decrypt
266 * short-circuit instead of running AES under a predictable nonce. */
267 uint32_t nonce_val = 0;
268 for (int attempts = 0; attempts < 8; attempts++) {
269 if (_librist_crypto_random_u32(&nonce_val) != 0) {
270 rist_log_priv3(RIST_LOG_ERROR,
271 "PSK nonce generation: CSPRNG unavailable, "
272 "PSK encrypt/decrypt locked out until passphrase rotation\n");
273 key->csprng_failed = true;
274 key->bad_decryption = true;
275 return;
276 }
277 if (nonce_val != 0)
278 break;
279 }
280 if (nonce_val == 0) {
281 /* 8 zeros in a row from a working CSPRNG is 2^-256; treat as malfunction. */
282 rist_log_priv3(RIST_LOG_ERROR,
283 "PSK nonce generation: CSPRNG returned only zeros, locking out\n");
284 key->csprng_failed = true;
285 key->bad_decryption = true;
286 return;
287 }
288
289 memcpy(key->gre_nonce, &nonce_val, sizeof(key->gre_nonce));
290
291 UNSET_BIT(key->gre_nonce[0], 7);
292 if (key->odd)
293 SET_BIT(key->gre_nonce[0], 7);
294 }
295
296 void _librist_crypto_psk_decrypt(struct rist_key *key, uint8_t nonce[4], uint32_t seq_nbe, uint8_t gre_version, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
297 {
298 uint32_t nonce_val;
299 memcpy(&nonce_val, nonce, sizeof(nonce_val));
300 /* A zero nonce never comes from a legitimate sender; drop the packet.
301 * Do NOT latch bad_decryption here: a zero nonce triggers no PBKDF2,
302 * so the latch only served as a one-packet permanent session kill. */
303 if (!nonce_val)
304 return;
305
306 if (memcmp(nonce, key->gre_nonce, sizeof(key->gre_nonce)) != 0) {
307 /* Adopt the new nonce even while locked out: a latch no fresh nonce
308 * can clear turns one spoofed packet (or six garbage decryptions)
309 * into a permanent session kill and blocks legitimate passphrase
310 * rotations. The PBKDF2-CPU-DoS protection the lockout provided
311 * becomes a rekey rate limit that engages ONLY under abuse:
312 * legitimate nonce churn (rotations, reflector RTCP/media nonce
313 * interleave) decrypts cleanly and keeps bad_count at 0, so it is
314 * never throttled; a garbage flood pushes bad_count past
315 * RIST_REKEY_ABUSE_STRIKES and is throttled to RIST_REKEY_MIN_INTERVAL. */
316 uint64_t now = timestampNTP_u64();
317 if (key->bad_count > RIST_REKEY_ABUSE_STRIKES && key->last_rekey &&
318 (now - key->last_rekey) < RIST_REKEY_MIN_INTERVAL)
319 return;
320 memcpy(key->gre_nonce, nonce, sizeof(key->gre_nonce));
321 _librist_crypto_aes_key(key);
322 /* Only clear the flag if _librist_crypto_aes_key succeeded;
323 * it sets bad_decryption=true on PBKDF2/setup failure. */
324 if (key->bad_decryption)
325 return;
326 key->bad_count = 0;
327 key->last_rekey = now;
328 }
329
330 if (key->used_times > RIST_AES_KEY_REUSE_TIMES) {
331 key->bad_decryption = true;
332 return;
333 }
334
335 _librist_crypto_psk_prepare_iv(key, gre_version, seq_nbe);
336 #if HAVE_MBEDTLS
337 key->aes_offset = 0;
338 #endif
339 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
340 return;
341 }
342
343 void _librist_crypto_psk_encrypt(struct rist_key *key, uint32_t seq_nbe, uint8_t gre_version,const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
344 {
345 uint32_t nonce_val;
346 memcpy(&nonce_val, key->gre_nonce, sizeof(nonce_val));
347 if (!nonce_val || (key->used_times +1) > RIST_AES_KEY_REUSE_TIMES || (key->key_rotation > 0 && key->used_times >= key->key_rotation)) {
348 _librist_crypto_psk_generate_nonce(key);
349 if (key->csprng_failed) {
350 memset(outbuf, 0, payload_len);
351 return;
352 }
353 _librist_crypto_aes_key(key);
354 }
355 if (key->csprng_failed) {
356 memset(outbuf, 0, payload_len);
357 return;
358 }
359 _librist_crypto_psk_prepare_iv(key, gre_version, seq_nbe);
360 #if HAVE_MBEDTLS
361 key->aes_offset = 0;
362 #endif
363 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
364 return;
365 }
366
367 int _librist_crypto_psk_set_passphrase(struct rist_key *key, const uint8_t *passsphrase, size_t passphrase_len) {
368 if (passphrase_len > sizeof(key->password) -1) {
369 return -1;
370 }
371 if (key->key_size == 0)
372 key->key_size = 256;
373 memcpy(key->password, passsphrase, passphrase_len);
374 key->password_len = passphrase_len;
375 key->used_times = 0;
376 key->csprng_failed = false; /* fresh passphrase, retry CSPRNG */
377 key->bad_decryption = false; /* fresh passphrase: clear any lockout */
378 key->bad_count = 0;
379 key->last_rekey = 0;
380 _librist_crypto_psk_generate_nonce(key);
381 _librist_crypto_aes_key(key);
382 return 0;
383 }
384
385 void _librist_crypto_psk_get_passphrase(struct rist_key *key, const uint8_t **passphrase, size_t *passphrase_len) {
386 *passphrase = key->password;
387 *passphrase_len = key->password_len;
388 }
389
390 void _librist_crypto_psk_encrypt_continue(struct rist_key *key, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len) {
391 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
392 }
393
394 void _librist_crypto_psk_preannounce_nonce(struct rist_key *key, const uint8_t nonce[4], uint32_t key_size_bits) {
395 uint32_t nonce_val;
396 memcpy(&nonce_val, nonce, sizeof(nonce_val));
397 if (!nonce_val)
398 return;
399 if (memcmp(nonce, key->gre_nonce, sizeof(key->gre_nonce)) == 0)
400 return;
401 /* key_size_bits comes straight off the wire (Advanced PSK nonce
402 * control message); accept only valid AES sizes. */
403 if (key_size_bits) {
404 if (key_size_bits != 128 && key_size_bits != 192 && key_size_bits != 256)
405 return;
406 key->key_size = key_size_bits;
407 }
408 /* This control message is reachable before authentication and carries no
409 * decrypt outcome, so there is no bad_count abuse signal to gate on (unlike
410 * the decrypt path); keep the flat RIST_REKEY_MIN_INTERVAL rate limit
411 * unconditionally. Preannounce is not in the media hot path, so the flat
412 * floor costs nothing legitimate. */
413 uint64_t now = timestampNTP_u64();
414 if (key->last_rekey && (now - key->last_rekey) < RIST_REKEY_MIN_INTERVAL)
415 return;
416 memcpy(key->gre_nonce, nonce, sizeof(key->gre_nonce));
417 _librist_crypto_aes_key(key);
418 if (!key->bad_decryption) {
419 key->bad_count = 0;
420 key->last_rekey = now;
421 }
422 }