forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.c
83 lines (68 loc) · 2.42 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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 <asm/unaligned.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)
{
u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
u8 key1be[8];
u8 key2be[8];
int i;
if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
len = SHA256_DIGEST_SIZE;
put_unaligned_be64(key1, key1be);
put_unaligned_be64(key2, key2be);
/* Generate key xored with ipad */
memset(input, 0x36, SHA256_BLOCK_SIZE);
for (i = 0; i < 8; i++)
input[i] ^= key1be[i];
for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i];
memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
/* emit sha256(K1 || msg) on the second input block, so we can
* reuse 'input' for the last hashing
*/
sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
/* Prepare second part of hmac */
memset(input, 0x5C, SHA256_BLOCK_SIZE);
for (i = 0; i < 8; i++)
input[i] ^= key1be[i];
for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i];
sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
}
#if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
#endif