Files
erofs-linux/net/mptcp/crypto.c
T
Eric Biggers 2d5be5629c 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 <[email protected]>
Reviewed-by: Matthieu Baerts (NGI0) <[email protected]>
Signed-off-by: Matthieu Baerts (NGI0) <[email protected]>
Link: https://patch.msgid.link/20250902-net-next-mptcp-misc-feat-6-18-v2-1-fa02bb3188b1@kernel.org
Signed-off-by: Jakub Kicinski <[email protected]>
2025-09-03 15:08:20 -07:00

53 lines
1.7 KiB
C

// SPDX-License-Identifier: GPL-2.0
/* Multipath TCP cryptographic functions
* Copyright (c) 2017 - 2019, Intel Corporation.
*
* Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
* mptcp_ipv6 from multipath-tcp.org, authored by:
*
* Sébastien Barré <[email protected]>
* Christoph Paasch <[email protected]>
* Jaakko Korkeaniemi <[email protected]>
* Gregory Detal <[email protected]>
* Fabien Duchêne <[email protected]>
* Andreas Seelinger <[email protected]>
* Lavkesh Lahngir <[email protected]>
* Andreas Ripke <[email protected]>
* Vlad Dogaru <[email protected]>
* Octavian Purdila <[email protected]>
* John Ronan <[email protected]>
* Catalin Nicutar <[email protected]>
* Brandon Heller <[email protected]>
*/
#include <linux/kernel.h>
#include <crypto/sha2.h>
#include "protocol.h"
#define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
{
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
__be64 input = cpu_to_be64(key);
sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
if (token)
*token = be32_to_cpu(mptcp_hashed_key[0]);
if (idsn)
*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
}
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
{
__be64 key[2] = { cpu_to_be64(key1), cpu_to_be64(key2) };
hmac_sha256_usingrawkey((const u8 *)key, sizeof(key), msg, len, hmac);
}
#if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
#endif