SHA512.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_SHA512_HPP
  9. #define ZT_SHA512_HPP
  10. #include "Constants.hpp"
  11. #ifdef __APPLE__
  12. #include <CommonCrypto/CommonDigest.h>
  13. #endif
  14. #define ZT_SHA512_DIGEST_SIZE 64
  15. #define ZT_SHA384_DIGEST_SIZE 48
  16. #define ZT_SHA512_BLOCK_SIZE 128
  17. #define ZT_SHA384_BLOCK_SIZE 128
  18. #define ZT_HMACSHA384_LEN 48
  19. namespace ZeroTier {
  20. // SHA384 and SHA512 are actually in the standard libraries on MacOS and iOS
  21. #ifdef __APPLE__
  22. #define ZT_HAVE_NATIVE_SHA512 1
  23. static ZT_INLINE void SHA512(void* digest, const void* data, unsigned int len)
  24. {
  25. CC_SHA512_CTX ctx;
  26. CC_SHA512_Init(&ctx);
  27. CC_SHA512_Update(&ctx, data, len);
  28. CC_SHA512_Final(reinterpret_cast<unsigned char*>(digest), &ctx);
  29. }
  30. static ZT_INLINE void SHA384(void* digest, const void* data, unsigned int len)
  31. {
  32. CC_SHA512_CTX ctx;
  33. CC_SHA384_Init(&ctx);
  34. CC_SHA384_Update(&ctx, data, len);
  35. CC_SHA384_Final(reinterpret_cast<unsigned char*>(digest), &ctx);
  36. }
  37. static ZT_INLINE void SHA384(void* digest, const void* data0, unsigned int len0, const void* data1, unsigned int len1)
  38. {
  39. CC_SHA512_CTX ctx;
  40. CC_SHA384_Init(&ctx);
  41. CC_SHA384_Update(&ctx, data0, len0);
  42. CC_SHA384_Update(&ctx, data1, len1);
  43. CC_SHA384_Final(reinterpret_cast<unsigned char*>(digest), &ctx);
  44. }
  45. #endif
  46. #ifndef ZT_HAVE_NATIVE_SHA512
  47. void SHA512(void* digest, const void* data, unsigned int len);
  48. void SHA384(void* digest, const void* data, unsigned int len);
  49. void SHA384(void* digest, const void* data0, unsigned int len0, const void* data1, unsigned int len1);
  50. #endif
  51. /**
  52. * Compute HMAC SHA-384 using a 256-bit key
  53. *
  54. * @param key Secret key
  55. * @param msg Message to HMAC
  56. * @param msglen Length of message
  57. * @param mac Buffer to fill with result
  58. */
  59. void HMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE], const void* msg, unsigned int msglen, uint8_t mac[48]);
  60. /**
  61. * Compute KBKDF (key-based key derivation function) using HMAC-SHA-384 as a PRF
  62. *
  63. * @param key Source master key
  64. * @param label A label indicating the key's purpose in the ZeroTier system
  65. * @param context An arbitrary "context" or zero if not applicable
  66. * @param iter Key iteration for generation of multiple keys for the same label/context
  67. * @param out Output to receive derived key
  68. */
  69. void KBKDFHMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE], char label, char context, uint32_t iter, uint8_t out[ZT_SYMMETRIC_KEY_SIZE]);
  70. } // namespace ZeroTier
  71. #endif