xref: /openbmc/linux/net/mptcp/crypto.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
179c0949eSPeter Krystad // SPDX-License-Identifier: GPL-2.0
279c0949eSPeter Krystad /* Multipath TCP cryptographic functions
379c0949eSPeter Krystad  * Copyright (c) 2017 - 2019, Intel Corporation.
479c0949eSPeter Krystad  *
579c0949eSPeter Krystad  * Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
679c0949eSPeter Krystad  *       mptcp_ipv6 from multipath-tcp.org, authored by:
779c0949eSPeter Krystad  *
879c0949eSPeter Krystad  *       Sébastien Barré <sebastien.barre@uclouvain.be>
979c0949eSPeter Krystad  *       Christoph Paasch <christoph.paasch@uclouvain.be>
1079c0949eSPeter Krystad  *       Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
1179c0949eSPeter Krystad  *       Gregory Detal <gregory.detal@uclouvain.be>
1279c0949eSPeter Krystad  *       Fabien Duchêne <fabien.duchene@uclouvain.be>
1379c0949eSPeter Krystad  *       Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
1479c0949eSPeter Krystad  *       Lavkesh Lahngir <lavkesh51@gmail.com>
1579c0949eSPeter Krystad  *       Andreas Ripke <ripke@neclab.eu>
1679c0949eSPeter Krystad  *       Vlad Dogaru <vlad.dogaru@intel.com>
1779c0949eSPeter Krystad  *       Octavian Purdila <octavian.purdila@intel.com>
1879c0949eSPeter Krystad  *       John Ronan <jronan@tssg.org>
1979c0949eSPeter Krystad  *       Catalin Nicutar <catalin.nicutar@gmail.com>
2079c0949eSPeter Krystad  *       Brandon Heller <brandonh@stanford.edu>
2179c0949eSPeter Krystad  */
2279c0949eSPeter Krystad 
2379c0949eSPeter Krystad #include <linux/kernel.h>
24a24d22b2SEric Biggers #include <crypto/sha2.h>
2579c0949eSPeter Krystad #include <asm/unaligned.h>
2679c0949eSPeter Krystad 
2779c0949eSPeter Krystad #include "protocol.h"
2879c0949eSPeter Krystad 
2965492c5aSPaolo Abeni #define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
3079c0949eSPeter Krystad 
mptcp_crypto_key_sha(u64 key,u32 * token,u64 * idsn)3179c0949eSPeter Krystad void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
3279c0949eSPeter Krystad {
3365492c5aSPaolo Abeni 	__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
3465492c5aSPaolo Abeni 	__be64 input = cpu_to_be64(key);
3579c0949eSPeter Krystad 
365a7a0d94SEric Biggers 	sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
3779c0949eSPeter Krystad 
3879c0949eSPeter Krystad 	if (token)
3979c0949eSPeter Krystad 		*token = be32_to_cpu(mptcp_hashed_key[0]);
4079c0949eSPeter Krystad 	if (idsn)
4165492c5aSPaolo Abeni 		*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
4279c0949eSPeter Krystad }
4379c0949eSPeter Krystad 
mptcp_crypto_hmac_sha(u64 key1,u64 key2,u8 * msg,int len,void * hmac)443df523abSPeter Krystad void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
4579c0949eSPeter Krystad {
4665492c5aSPaolo Abeni 	u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
4779c0949eSPeter Krystad 	u8 key1be[8];
4879c0949eSPeter Krystad 	u8 key2be[8];
4979c0949eSPeter Krystad 	int i;
5079c0949eSPeter Krystad 
513df523abSPeter Krystad 	if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
523df523abSPeter Krystad 		len = SHA256_DIGEST_SIZE;
533df523abSPeter Krystad 
5479c0949eSPeter Krystad 	put_unaligned_be64(key1, key1be);
5579c0949eSPeter Krystad 	put_unaligned_be64(key2, key2be);
5679c0949eSPeter Krystad 
5779c0949eSPeter Krystad 	/* Generate key xored with ipad */
58ac0ad93dSEric Biggers 	memset(input, 0x36, SHA256_BLOCK_SIZE);
5979c0949eSPeter Krystad 	for (i = 0; i < 8; i++)
6079c0949eSPeter Krystad 		input[i] ^= key1be[i];
6179c0949eSPeter Krystad 	for (i = 0; i < 8; i++)
6279c0949eSPeter Krystad 		input[i + 8] ^= key2be[i];
6379c0949eSPeter Krystad 
643df523abSPeter Krystad 	memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
6579c0949eSPeter Krystad 
6679c0949eSPeter Krystad 	/* emit sha256(K1 || msg) on the second input block, so we can
6779c0949eSPeter Krystad 	 * reuse 'input' for the last hashing
6879c0949eSPeter Krystad 	 */
695a7a0d94SEric Biggers 	sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
7079c0949eSPeter Krystad 
7179c0949eSPeter Krystad 	/* Prepare second part of hmac */
72ac0ad93dSEric Biggers 	memset(input, 0x5C, SHA256_BLOCK_SIZE);
7379c0949eSPeter Krystad 	for (i = 0; i < 8; i++)
7479c0949eSPeter Krystad 		input[i] ^= key1be[i];
7579c0949eSPeter Krystad 	for (i = 0; i < 8; i++)
7679c0949eSPeter Krystad 		input[i + 8] ^= key2be[i];
7779c0949eSPeter Krystad 
785a7a0d94SEric Biggers 	sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
7979c0949eSPeter Krystad }
8065492c5aSPaolo Abeni 
81*3fcc8a25SNico Pache #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
82a00a5822SPaolo Abeni EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
8365492c5aSPaolo Abeni #endif
84