1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/kernel.h>
24#include <crypto/sha2.h>
25#include <asm/unaligned.h>
26
27#include "protocol.h"
28
29#define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
30
31void 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
44void 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
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
67
68
69 sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
70
71
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#if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
82EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
83#endif
84