Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mptcp: use HMAC-SHA256 library instead of open-coded HMAC

Now that there are easy-to-use HMAC-SHA256 library functions, use these
in net/mptcp/crypto.c instead of open-coding the HMAC algorithm.

Remove the WARN_ON_ONCE() for messages longer than SHA256_DIGEST_SIZE.
The new implementation handles all message lengths correctly.

The mptcp-crypto KUnit test still passes after this change.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Reviewed-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20250902-net-next-mptcp-misc-feat-6-18-v2-1-fa02bb3188b1@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Biggers and committed by
Jakub Kicinski
2d5be562 0e2a5208

+2 -33
+2 -33
net/mptcp/crypto.c
··· 22 22 23 23 #include <linux/kernel.h> 24 24 #include <crypto/sha2.h> 25 - #include <linux/unaligned.h> 26 25 27 26 #include "protocol.h" 28 27 ··· 42 43 43 44 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac) 44 45 { 45 - u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE]; 46 - u8 key1be[8]; 47 - u8 key2be[8]; 48 - int i; 46 + __be64 key[2] = { cpu_to_be64(key1), cpu_to_be64(key2) }; 49 47 50 - if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE)) 51 - len = SHA256_DIGEST_SIZE; 52 - 53 - put_unaligned_be64(key1, key1be); 54 - put_unaligned_be64(key2, key2be); 55 - 56 - /* Generate key xored with ipad */ 57 - memset(input, 0x36, SHA256_BLOCK_SIZE); 58 - for (i = 0; i < 8; i++) 59 - input[i] ^= key1be[i]; 60 - for (i = 0; i < 8; i++) 61 - input[i + 8] ^= key2be[i]; 62 - 63 - memcpy(&input[SHA256_BLOCK_SIZE], msg, len); 64 - 65 - /* emit sha256(K1 || msg) on the second input block, so we can 66 - * reuse 'input' for the last hashing 67 - */ 68 - sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]); 69 - 70 - /* Prepare second part of hmac */ 71 - memset(input, 0x5C, SHA256_BLOCK_SIZE); 72 - for (i = 0; i < 8; i++) 73 - input[i] ^= key1be[i]; 74 - for (i = 0; i < 8; i++) 75 - input[i + 8] ^= key2be[i]; 76 - 77 - sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac); 48 + hmac_sha256_usingrawkey((const u8 *)key, sizeof(key), msg, len, hmac); 78 49 } 79 50 80 51 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)