hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/net/wireguard/noise.c
....@@ -111,7 +111,7 @@
111111
112112 static void keypair_free_rcu(struct rcu_head *rcu)
113113 {
114
- kzfree(container_of(rcu, struct noise_keypair, rcu));
114
+ kfree_sensitive(container_of(rcu, struct noise_keypair, rcu));
115115 }
116116
117117 static void keypair_free_kref(struct kref *kref)
....@@ -302,6 +302,41 @@
302302 static_identity->static_public, private_key);
303303 }
304304
305
+static void hmac(u8 *out, const u8 *in, const u8 *key, const size_t inlen, const size_t keylen)
306
+{
307
+ struct blake2s_state state;
308
+ u8 x_key[BLAKE2S_BLOCK_SIZE] __aligned(__alignof__(u32)) = { 0 };
309
+ u8 i_hash[BLAKE2S_HASH_SIZE] __aligned(__alignof__(u32));
310
+ int i;
311
+
312
+ if (keylen > BLAKE2S_BLOCK_SIZE) {
313
+ blake2s_init(&state, BLAKE2S_HASH_SIZE);
314
+ blake2s_update(&state, key, keylen);
315
+ blake2s_final(&state, x_key);
316
+ } else
317
+ memcpy(x_key, key, keylen);
318
+
319
+ for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
320
+ x_key[i] ^= 0x36;
321
+
322
+ blake2s_init(&state, BLAKE2S_HASH_SIZE);
323
+ blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
324
+ blake2s_update(&state, in, inlen);
325
+ blake2s_final(&state, i_hash);
326
+
327
+ for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
328
+ x_key[i] ^= 0x5c ^ 0x36;
329
+
330
+ blake2s_init(&state, BLAKE2S_HASH_SIZE);
331
+ blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
332
+ blake2s_update(&state, i_hash, BLAKE2S_HASH_SIZE);
333
+ blake2s_final(&state, i_hash);
334
+
335
+ memcpy(out, i_hash, BLAKE2S_HASH_SIZE);
336
+ memzero_explicit(x_key, BLAKE2S_BLOCK_SIZE);
337
+ memzero_explicit(i_hash, BLAKE2S_HASH_SIZE);
338
+}
339
+
305340 /* This is Hugo Krawczyk's HKDF:
306341 * - https://eprint.iacr.org/2010/264.pdf
307342 * - https://tools.ietf.org/html/rfc5869
....@@ -322,14 +357,14 @@
322357 ((third_len || third_dst) && (!second_len || !second_dst))));
323358
324359 /* Extract entropy from data into secret */
325
- blake2s256_hmac(secret, data, chaining_key, data_len, NOISE_HASH_LEN);
360
+ hmac(secret, data, chaining_key, data_len, NOISE_HASH_LEN);
326361
327362 if (!first_dst || !first_len)
328363 goto out;
329364
330365 /* Expand first key: key = secret, data = 0x1 */
331366 output[0] = 1;
332
- blake2s256_hmac(output, output, secret, 1, BLAKE2S_HASH_SIZE);
367
+ hmac(output, output, secret, 1, BLAKE2S_HASH_SIZE);
333368 memcpy(first_dst, output, first_len);
334369
335370 if (!second_dst || !second_len)
....@@ -337,8 +372,7 @@
337372
338373 /* Expand second key: key = secret, data = first-key || 0x2 */
339374 output[BLAKE2S_HASH_SIZE] = 2;
340
- blake2s256_hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1,
341
- BLAKE2S_HASH_SIZE);
375
+ hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1, BLAKE2S_HASH_SIZE);
342376 memcpy(second_dst, output, second_len);
343377
344378 if (!third_dst || !third_len)
....@@ -346,8 +380,7 @@
346380
347381 /* Expand third key: key = secret, data = second-key || 0x3 */
348382 output[BLAKE2S_HASH_SIZE] = 3;
349
- blake2s256_hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1,
350
- BLAKE2S_HASH_SIZE);
383
+ hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1, BLAKE2S_HASH_SIZE);
351384 memcpy(third_dst, output, third_len);
352385
353386 out:
....@@ -818,7 +851,7 @@
818851 handshake->entry.peer->device->index_hashtable,
819852 &handshake->entry, &new_keypair->entry);
820853 } else {
821
- kzfree(new_keypair);
854
+ kfree_sensitive(new_keypair);
822855 }
823856 rcu_read_unlock_bh();
824857