SHA512.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c)2019 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: 2023-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. #ifdef ZT_USE_LIBCRYPTO
  20. #include <openssl/sha.h>
  21. #endif
  22. #define ZT_SHA512_DIGEST_LEN 64
  23. #define ZT_SHA384_DIGEST_LEN 48
  24. #define ZT_SHA512_BLOCK_SIZE 128
  25. #define ZT_SHA384_BLOCK_SIZE 128
  26. #define ZT_HMACSHA384_LEN 48
  27. #define ZT_PROTO_KBKDF_LABEL_KEY_USE_HMAC_SHA_384 'H'
  28. #define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K1 '1'
  29. #define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K2 '2'
  30. #define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K3 '3'
  31. #define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K4 '4'
  32. namespace ZeroTier {
  33. #ifdef __APPLE__
  34. #define ZT_HAVE_NATIVE_SHA512 1
  35. static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len)
  36. {
  37. CC_SHA512_CTX ctx;
  38. CC_SHA512_Init(&ctx);
  39. CC_SHA512_Update(&ctx,data,len);
  40. CC_SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  41. }
  42. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len)
  43. {
  44. CC_SHA512_CTX ctx;
  45. CC_SHA384_Init(&ctx);
  46. CC_SHA384_Update(&ctx,data,len);
  47. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  48. }
  49. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
  50. {
  51. CC_SHA512_CTX ctx;
  52. CC_SHA384_Init(&ctx);
  53. CC_SHA384_Update(&ctx,data0,len0);
  54. CC_SHA384_Update(&ctx,data1,len1);
  55. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  56. }
  57. #endif
  58. #ifdef ZT_USE_LIBCRYPTO
  59. #define ZT_HAVE_NATIVE_SHA512 1
  60. static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len)
  61. {
  62. SHA512_CTX ctx;
  63. SHA512_Init(&ctx);
  64. SHA512_Update(&ctx,data,len);
  65. SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  66. }
  67. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len)
  68. {
  69. SHA512_CTX ctx;
  70. SHA384_Init(&ctx);
  71. SHA384_Update(&ctx,data,len);
  72. SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  73. }
  74. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
  75. {
  76. SHA512_CTX ctx;
  77. SHA384_Init(&ctx);
  78. SHA384_Update(&ctx,data0,len0);
  79. SHA384_Update(&ctx,data1,len1);
  80. SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  81. }
  82. #endif
  83. #ifndef ZT_HAVE_NATIVE_SHA512
  84. void SHA512(void *digest,const void *data,unsigned int len);
  85. void SHA384(void *digest,const void *data,unsigned int len);
  86. void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1);
  87. #endif
  88. /**
  89. * Compute HMAC SHA-384 using a 256-bit key
  90. *
  91. * @param key Secret key
  92. * @param msg Message to HMAC
  93. * @param msglen Length of message
  94. * @param mac Buffer to fill with result
  95. */
  96. void HMACSHA384(const uint8_t key[32],const void *msg,const unsigned int msglen,uint8_t mac[48]);
  97. /**
  98. * Compute KBKDF (key-based key derivation function) using HMAC-SHA-384 as a PRF
  99. *
  100. * @param key Source master key
  101. * @param label A label indicating the key's purpose in the ZeroTier system
  102. * @param context An arbitrary "context" or zero if not applicable
  103. * @param iter Key iteration for generation of multiple keys for the same label/context
  104. * @param out Output to receive derived key
  105. */
  106. void KBKDFHMACSHA384(const uint8_t key[32],const char label,const char context,const uint32_t iter,uint8_t out[32]);
  107. } // namespace ZeroTier
  108. #endif