Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 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> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 24 | #include <crypto/sha2.h> |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
| 26 | |
| 27 | #include "protocol.h" |
| 28 | |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 29 | #define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4) |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 30 | |
| 31 | void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn) |
| 32 | { |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 33 | __be32 mptcp_hashed_key[SHA256_DIGEST_WORDS]; |
| 34 | __be64 input = cpu_to_be64(key); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 35 | |
Eric Biggers | 5a7a0d9 | 2020-07-08 09:39:42 -0700 | [diff] [blame] | 36 | sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 37 | |
| 38 | if (token) |
| 39 | *token = be32_to_cpu(mptcp_hashed_key[0]); |
| 40 | if (idsn) |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 41 | *idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6])); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 44 | void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac) |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 45 | { |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 46 | u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE]; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 47 | u8 key1be[8]; |
| 48 | u8 key2be[8]; |
| 49 | int i; |
| 50 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 51 | if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE)) |
| 52 | len = SHA256_DIGEST_SIZE; |
| 53 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 54 | put_unaligned_be64(key1, key1be); |
| 55 | put_unaligned_be64(key2, key2be); |
| 56 | |
| 57 | /* Generate key xored with ipad */ |
Eric Biggers | ac0ad93 | 2020-05-02 11:24:21 -0700 | [diff] [blame] | 58 | memset(input, 0x36, SHA256_BLOCK_SIZE); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 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 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 64 | memcpy(&input[SHA256_BLOCK_SIZE], msg, len); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 65 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 66 | /* emit sha256(K1 || msg) on the second input block, so we can |
| 67 | * reuse 'input' for the last hashing |
| 68 | */ |
Eric Biggers | 5a7a0d9 | 2020-07-08 09:39:42 -0700 | [diff] [blame] | 69 | sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 70 | |
| 71 | /* Prepare second part of hmac */ |
Eric Biggers | ac0ad93 | 2020-05-02 11:24:21 -0700 | [diff] [blame] | 72 | memset(input, 0x5C, SHA256_BLOCK_SIZE); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 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 | |
Eric Biggers | 5a7a0d9 | 2020-07-08 09:39:42 -0700 | [diff] [blame] | 78 | sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 79 | } |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 80 | |
Nico Pache | 3fcc8a2 | 2021-04-16 15:38:01 -0700 | [diff] [blame] | 81 | #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST) |
Paolo Abeni | a00a582 | 2020-06-26 19:30:01 +0200 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha); |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 83 | #endif |