SHA512.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SHA512_HPP
  14. #define ZT_SHA512_HPP
  15. #include "Constants.hpp"
  16. #ifdef __APPLE__
  17. #include <CommonCrypto/CommonDigest.h>
  18. #endif
  19. #define ZT_SHA512_DIGEST_SIZE 64
  20. #define ZT_SHA384_DIGEST_SIZE 48
  21. #define ZT_SHA512_BLOCK_SIZE 128
  22. #define ZT_SHA384_BLOCK_SIZE 128
  23. #define ZT_HMACSHA384_LEN 48
  24. namespace ZeroTier {
  25. // SHA384 and SHA512 are actually in the standard libraries on MacOS and iOS
  26. #ifdef __APPLE__
  27. #define ZT_HAVE_NATIVE_SHA512 1
  28. static ZT_INLINE void SHA512(void *digest,const void *data,unsigned int len)
  29. {
  30. CC_SHA512_CTX ctx;
  31. CC_SHA512_Init(&ctx);
  32. CC_SHA512_Update(&ctx,data,len);
  33. CC_SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  34. }
  35. static ZT_INLINE void SHA384(void *digest,const void *data,unsigned int len)
  36. {
  37. CC_SHA512_CTX ctx;
  38. CC_SHA384_Init(&ctx);
  39. CC_SHA384_Update(&ctx,data,len);
  40. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  41. }
  42. static ZT_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
  43. {
  44. CC_SHA512_CTX ctx;
  45. CC_SHA384_Init(&ctx);
  46. CC_SHA384_Update(&ctx,data0,len0);
  47. CC_SHA384_Update(&ctx,data1,len1);
  48. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  49. }
  50. #endif
  51. #ifndef ZT_HAVE_NATIVE_SHA512
  52. void SHA512(void *digest,const void *data,unsigned int len);
  53. void SHA384(void *digest,const void *data,unsigned int len);
  54. void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1);
  55. #endif
  56. /**
  57. * Compute HMAC SHA-384 using a 256-bit key
  58. *
  59. * @param key Secret key
  60. * @param msg Message to HMAC
  61. * @param msglen Length of message
  62. * @param mac Buffer to fill with result
  63. */
  64. void HMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE],const void *msg,unsigned int msglen,uint8_t mac[48]);
  65. /**
  66. * Compute KBKDF (key-based key derivation function) using HMAC-SHA-384 as a PRF
  67. *
  68. * @param key Source master key
  69. * @param label A label indicating the key's purpose in the ZeroTier system
  70. * @param context An arbitrary "context" or zero if not applicable
  71. * @param iter Key iteration for generation of multiple keys for the same label/context
  72. * @param out Output to receive derived key
  73. */
  74. void KBKDFHMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE],char label,char context,uint32_t iter,uint8_t out[ZT_SYMMETRIC_KEY_SIZE]);
  75. } // namespace ZeroTier
  76. #endif