SHA512.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_SHA512_HPP
  27. #define ZT_SHA512_HPP
  28. #include "Constants.hpp"
  29. #ifdef __APPLE__
  30. #include <CommonCrypto/CommonDigest.h>
  31. #endif
  32. #ifdef ZT_USE_LIBCRYPTO
  33. #include <openssl/sha.h>
  34. #endif
  35. #define ZT_SHA512_DIGEST_LEN 64
  36. #define ZT_SHA384_DIGEST_LEN 48
  37. namespace ZeroTier {
  38. #ifdef __APPLE__
  39. #define ZT_HAVE_NATIVE_SHA512 1
  40. static inline void SHA512(void *digest,const void *data,unsigned int len)
  41. {
  42. CC_SHA512_CTX ctx;
  43. CC_SHA512_Init(&ctx);
  44. CC_SHA512_Update(&ctx,data,len);
  45. CC_SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  46. }
  47. static inline void SHA384(void *digest,const void *data,unsigned int len)
  48. {
  49. CC_SHA512_CTX ctx;
  50. CC_SHA384_Init(&ctx);
  51. CC_SHA384_Update(&ctx,data,len);
  52. CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  53. }
  54. #endif
  55. #ifdef ZT_USE_LIBCRYPTO
  56. #define ZT_HAVE_NATIVE_SHA512 1
  57. static inline void SHA512(void *digest,const void *data,unsigned int len)
  58. {
  59. SHA512_CTX ctx;
  60. SHA512_Init(&ctx);
  61. SHA512_Update(&ctx,data,len);
  62. SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  63. }
  64. static inline void SHA384(void *digest,const void *data,unsigned int len)
  65. {
  66. SHA512_CTX ctx;
  67. SHA384_Init(&ctx);
  68. SHA384_Update(&ctx,data,len);
  69. SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
  70. }
  71. #endif
  72. #ifndef ZT_HAVE_NATIVE_SHA512
  73. void SHA512(void *digest,const void *data,unsigned int len);
  74. void SHA384(void *digest,const void *data,unsigned int len);
  75. #endif
  76. } // namespace ZeroTier
  77. #endif