crypto.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include <linux/kernel.h>
  23. #include <crypto/sha2.h>
  24. #include "protocol.h"
  25. #define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
  26. void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
  27. {
  28. __be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
  29. __be64 input = cpu_to_be64(key);
  30. sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
  31. if (token)
  32. *token = be32_to_cpu(mptcp_hashed_key[0]);
  33. if (idsn)
  34. *idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
  35. }
  36. void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
  37. {
  38. __be64 key[2] = { cpu_to_be64(key1), cpu_to_be64(key2) };
  39. hmac_sha256_usingrawkey((const u8 *)key, sizeof(key), msg, len, hmac);
  40. }
  41. #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
  42. EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
  43. #endif