xref: /linux/net/mptcp/crypto.c (revision 145ff1ec090dce9beb5a9590b5dc288e7bb2e65d)
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 
36 	sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
37 
38 	if (token)
39 		*token = be32_to_cpu(mptcp_hashed_key[0]);
40 	if (idsn)
41 		*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
42 }
43 
44 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
45 {
46 	u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
47 	u8 key1be[8];
48 	u8 key2be[8];
49 	int i;
50 
51 	if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
52 		len = SHA256_DIGEST_SIZE;
53 
54 	put_unaligned_be64(key1, key1be);
55 	put_unaligned_be64(key2, key2be);
56 
57 	/* Generate key xored with ipad */
58 	memset(input, 0x36, SHA256_BLOCK_SIZE);
59 	for (i = 0; i < 8; i++)
60 		input[i] ^= key1be[i];
61 	for (i = 0; i < 8; i++)
62 		input[i + 8] ^= key2be[i];
63 
64 	memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
65 
66 	/* emit sha256(K1 || msg) on the second input block, so we can
67 	 * reuse 'input' for the last hashing
68 	 */
69 	sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
70 
71 	/* Prepare second part of hmac */
72 	memset(input, 0x5C, SHA256_BLOCK_SIZE);
73 	for (i = 0; i < 8; i++)
74 		input[i] ^= key1be[i];
75 	for (i = 0; i < 8; i++)
76 		input[i + 8] ^= key2be[i];
77 
78 	sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
79 }
80 
81 #ifdef CONFIG_MPTCP_HMAC_TEST
82 struct test_cast {
83 	char *key;
84 	char *msg;
85 	char *result;
86 };
87 
88 /* we can't reuse RFC 4231 test vectors, as we have constraint on the
89  * input and key size.
90  */
91 static struct test_cast tests[] = {
92 	{
93 		.key = "0b0b0b0b0b0b0b0b",
94 		.msg = "48692054",
95 		.result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
96 	},
97 	{
98 		.key = "aaaaaaaaaaaaaaaa",
99 		.msg = "dddddddd",
100 		.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
101 	},
102 	{
103 		.key = "0102030405060708",
104 		.msg = "cdcdcdcd",
105 		.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
106 	},
107 };
108 
109 static int __init test_mptcp_crypto(void)
110 {
111 	char hmac[32], hmac_hex[65];
112 	u32 nonce1, nonce2;
113 	u64 key1, key2;
114 	u8 msg[8];
115 	int i, j;
116 
117 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
118 		/* mptcp hmap will convert to be before computing the hmac */
119 		key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
120 		key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
121 		nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
122 		nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
123 
124 		put_unaligned_be32(nonce1, &msg[0]);
125 		put_unaligned_be32(nonce2, &msg[4]);
126 
127 		mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
128 		for (j = 0; j < 32; ++j)
129 			sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
130 		hmac_hex[64] = 0;
131 
132 		if (memcmp(hmac_hex, tests[i].result, 64))
133 			pr_err("test %d failed, got %s expected %s", i,
134 			       hmac_hex, tests[i].result);
135 		else
136 			pr_info("test %d [ ok ]", i);
137 	}
138 	return 0;
139 }
140 
141 late_initcall(test_mptcp_crypto);
142 #endif
143