SHA512.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace ZeroTier {
  28. #ifdef __APPLE__
  29. #define ZT_HAVE_NATIVE_SHA512 1
  30. static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len)
  31. {
  32. CC_SHA512_CTX ctx;
  33. CC_SHA512_Init(&ctx);
  34. CC_SHA512_Update(&ctx,data,len);
  35. CC_SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  36. }
  37. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len)
  38. {
  39. CC_SHA512_CTX ctx;
  40. CC_SHA384_Init(&ctx);
  41. CC_SHA384_Update(&ctx,data,len);
  42. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  43. }
  44. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
  45. {
  46. CC_SHA512_CTX ctx;
  47. CC_SHA384_Init(&ctx);
  48. CC_SHA384_Update(&ctx,data0,len0);
  49. CC_SHA384_Update(&ctx,data1,len1);
  50. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  51. }
  52. #endif
  53. #ifdef ZT_USE_LIBCRYPTO
  54. #define ZT_HAVE_NATIVE_SHA512 1
  55. static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len)
  56. {
  57. SHA512_CTX ctx;
  58. SHA512_Init(&ctx);
  59. SHA512_Update(&ctx,data,len);
  60. SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  61. }
  62. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len)
  63. {
  64. SHA512_CTX ctx;
  65. SHA384_Init(&ctx);
  66. SHA384_Update(&ctx,data,len);
  67. SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  68. }
  69. static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
  70. {
  71. SHA512_CTX ctx;
  72. SHA384_Init(&ctx);
  73. SHA384_Update(&ctx,data0,len0);
  74. SHA384_Update(&ctx,data1,len1);
  75. SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  76. }
  77. #endif
  78. #ifndef ZT_HAVE_NATIVE_SHA512
  79. void SHA512(void *digest,const void *data,unsigned int len);
  80. void SHA384(void *digest,const void *data,unsigned int len);
  81. void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1);
  82. #endif
  83. static inline void HMACSHA384(const uint8_t key[32],const void *msg,const unsigned int msglen,uint8_t mac[48])
  84. {
  85. uint64_t kInPadded[16];
  86. uint64_t outer[22]; // output padded key | H(input padded key | msg)
  87. #ifdef ZT_NO_TYPE_PUNNING
  88. for(int i=0;i<32;++i) ((uint8_t *)kInPadded)[i] = key[i] ^ 0x36;
  89. for(int i=4;i<16;++i) kInPadded[i] = 0x3636363636363636ULL;
  90. for(int i=0;i<32;++i) ((uint8_t *)outer)[i] = key[i] ^ 0x5c;
  91. for(int i=4;i<16;++i) outer[i] = 0x5c5c5c5c5c5c5c5cULL;
  92. #else
  93. {
  94. const uint64_t k0 = ((const uint64_t *)key)[0];
  95. const uint64_t k1 = ((const uint64_t *)key)[1];
  96. const uint64_t k2 = ((const uint64_t *)key)[2];
  97. const uint64_t k3 = ((const uint64_t *)key)[3];
  98. kInPadded[0] = k0 ^ 0x3636363636363636ULL;
  99. kInPadded[0] = k1 ^ 0x3636363636363636ULL;
  100. kInPadded[0] = k2 ^ 0x3636363636363636ULL;
  101. kInPadded[0] = k3 ^ 0x3636363636363636ULL;
  102. for(int i=4;i<16;++i) kInPadded[i] = 0x3636363636363636ULL;
  103. outer[0] = k0 ^ 0x5c5c5c5c5c5c5c5cULL;
  104. outer[1] = k1 ^ 0x5c5c5c5c5c5c5c5cULL;
  105. outer[2] = k2 ^ 0x5c5c5c5c5c5c5c5cULL;
  106. outer[3] = k3 ^ 0x5c5c5c5c5c5c5c5cULL;
  107. for(int i=4;i<16;++i) outer[i] = 0x5c5c5c5c5c5c5c5cULL;
  108. }
  109. #endif
  110. SHA384(((uint8_t *)outer) + 128,kInPadded,128,msg,msglen); // H(input padded key | msg)
  111. SHA384(mac,outer,176); // H(output padded key | H(input padded key | msg))
  112. }
  113. } // namespace ZeroTier
  114. #endif