xref: /openbmc/linux/net/mptcp/crypto.c (revision b8265621)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP cryptographic functions
3  * Copyright (c) 2017 - 2019, Intel Corporation.
4  *
5  * Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
6  *       mptcp_ipv6 from multipath-tcp.org, authored by:
7  *
8  *       Sébastien Barré <sebastien.barre@uclouvain.be>
9  *       Christoph Paasch <christoph.paasch@uclouvain.be>
10  *       Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
11  *       Gregory Detal <gregory.detal@uclouvain.be>
12  *       Fabien Duchêne <fabien.duchene@uclouvain.be>
13  *       Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
14  *       Lavkesh Lahngir <lavkesh51@gmail.com>
15  *       Andreas Ripke <ripke@neclab.eu>
16  *       Vlad Dogaru <vlad.dogaru@intel.com>
17  *       Octavian Purdila <octavian.purdila@intel.com>
18  *       John Ronan <jronan@tssg.org>
19  *       Catalin Nicutar <catalin.nicutar@gmail.com>
20  *       Brandon Heller <brandonh@stanford.edu>
21  */
22 
23 #include <linux/kernel.h>
24 #include <crypto/sha.h>
25 #include <asm/unaligned.h>
26 
27 #include "protocol.h"
28 
29 #define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
30 
31 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
32 {
33 	__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
34 	__be64 input = cpu_to_be64(key);
35 	struct sha256_state state;
36 
37 	sha256_init(&state);
38 	sha256_update(&state, (__force u8 *)&input, sizeof(input));
39 	sha256_final(&state, (u8 *)mptcp_hashed_key);
40 
41 	if (token)
42 		*token = be32_to_cpu(mptcp_hashed_key[0]);
43 	if (idsn)
44 		*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
45 }
46 
47 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
48 {
49 	u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
50 	struct sha256_state state;
51 	u8 key1be[8];
52 	u8 key2be[8];
53 	int i;
54 
55 	if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
56 		len = SHA256_DIGEST_SIZE;
57 
58 	put_unaligned_be64(key1, key1be);
59 	put_unaligned_be64(key2, key2be);
60 
61 	/* Generate key xored with ipad */
62 	memset(input, 0x36, SHA256_BLOCK_SIZE);
63 	for (i = 0; i < 8; i++)
64 		input[i] ^= key1be[i];
65 	for (i = 0; i < 8; i++)
66 		input[i + 8] ^= key2be[i];
67 
68 	memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
69 
70 	sha256_init(&state);
71 	sha256_update(&state, input, SHA256_BLOCK_SIZE + len);
72 
73 	/* emit sha256(K1 || msg) on the second input block, so we can
74 	 * reuse 'input' for the last hashing
75 	 */
76 	sha256_final(&state, &input[SHA256_BLOCK_SIZE]);
77 
78 	/* Prepare second part of hmac */
79 	memset(input, 0x5C, SHA256_BLOCK_SIZE);
80 	for (i = 0; i < 8; i++)
81 		input[i] ^= key1be[i];
82 	for (i = 0; i < 8; i++)
83 		input[i + 8] ^= key2be[i];
84 
85 	sha256_init(&state);
86 	sha256_update(&state, input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE);
87 	sha256_final(&state, (u8 *)hmac);
88 }
89 
90 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TESTS)
91 EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
92 #endif
93