0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <linux/kernel.h>
0024 #include <crypto/sha2.h>
0025 #include <asm/unaligned.h>
0026
0027 #include "protocol.h"
0028
0029 #define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
0030
0031 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
0032 {
0033 __be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
0034 __be64 input = cpu_to_be64(key);
0035
0036 sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
0037
0038 if (token)
0039 *token = be32_to_cpu(mptcp_hashed_key[0]);
0040 if (idsn)
0041 *idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
0042 }
0043
0044 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
0045 {
0046 u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
0047 u8 key1be[8];
0048 u8 key2be[8];
0049 int i;
0050
0051 if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
0052 len = SHA256_DIGEST_SIZE;
0053
0054 put_unaligned_be64(key1, key1be);
0055 put_unaligned_be64(key2, key2be);
0056
0057
0058 memset(input, 0x36, SHA256_BLOCK_SIZE);
0059 for (i = 0; i < 8; i++)
0060 input[i] ^= key1be[i];
0061 for (i = 0; i < 8; i++)
0062 input[i + 8] ^= key2be[i];
0063
0064 memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
0065
0066
0067
0068
0069 sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
0070
0071
0072 memset(input, 0x5C, SHA256_BLOCK_SIZE);
0073 for (i = 0; i < 8; i++)
0074 input[i] ^= key1be[i];
0075 for (i = 0; i < 8; i++)
0076 input[i + 8] ^= key2be[i];
0077
0078 sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
0079 }
0080
0081 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
0082 EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
0083 #endif