Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Multipath TCP cryptographic functions
0003  * Copyright (c) 2017 - 2019, Intel Corporation.
0004  *
0005  * Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
0006  *       mptcp_ipv6 from multipath-tcp.org, authored by:
0007  *
0008  *       Sébastien Barré <sebastien.barre@uclouvain.be>
0009  *       Christoph Paasch <christoph.paasch@uclouvain.be>
0010  *       Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
0011  *       Gregory Detal <gregory.detal@uclouvain.be>
0012  *       Fabien Duchêne <fabien.duchene@uclouvain.be>
0013  *       Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
0014  *       Lavkesh Lahngir <lavkesh51@gmail.com>
0015  *       Andreas Ripke <ripke@neclab.eu>
0016  *       Vlad Dogaru <vlad.dogaru@intel.com>
0017  *       Octavian Purdila <octavian.purdila@intel.com>
0018  *       John Ronan <jronan@tssg.org>
0019  *       Catalin Nicutar <catalin.nicutar@gmail.com>
0020  *       Brandon Heller <brandonh@stanford.edu>
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     /* Generate key xored with ipad */
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     /* emit sha256(K1 || msg) on the second input block, so we can
0067      * reuse 'input' for the last hashing
0068      */
0069     sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
0070 
0071     /* Prepare second part of hmac */
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