OpenSSLHash.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/Range.h>
  18. #include <folly/io/IOBuf.h>
  19. #include <folly/portability/OpenSSL.h>
  20. #include <folly/ssl/OpenSSLPtrTypes.h>
  21. namespace folly {
  22. namespace ssl {
  23. /// Warning:
  24. /// These functions are not thread-safe unless you initialize OpenSSL.
  25. class OpenSSLHash {
  26. public:
  27. class Digest {
  28. public:
  29. Digest() : ctx_(EVP_MD_CTX_new()) {}
  30. Digest(const Digest& other) {
  31. ctx_ = EvpMdCtxUniquePtr(EVP_MD_CTX_new());
  32. if (other.md_ != nullptr) {
  33. hash_init(other.md_);
  34. check_libssl_result(
  35. 1, EVP_MD_CTX_copy_ex(ctx_.get(), other.ctx_.get()));
  36. }
  37. }
  38. Digest& operator=(const Digest& other) {
  39. this->~Digest();
  40. return *new (this) Digest(other);
  41. }
  42. void hash_init(const EVP_MD* md) {
  43. md_ = md;
  44. check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
  45. }
  46. void hash_update(ByteRange data) {
  47. check_libssl_result(
  48. 1, EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
  49. }
  50. void hash_update(const IOBuf& data) {
  51. for (auto r : data) {
  52. hash_update(r);
  53. }
  54. }
  55. void hash_final(MutableByteRange out) {
  56. const auto size = EVP_MD_size(md_);
  57. check_out_size(size_t(size), out);
  58. unsigned int len = 0;
  59. check_libssl_result(1, EVP_DigestFinal_ex(ctx_.get(), out.data(), &len));
  60. check_libssl_result(size, int(len));
  61. md_ = nullptr;
  62. }
  63. private:
  64. const EVP_MD* md_ = nullptr;
  65. EvpMdCtxUniquePtr ctx_{nullptr};
  66. };
  67. static void hash(MutableByteRange out, const EVP_MD* md, ByteRange data) {
  68. Digest hash;
  69. hash.hash_init(md);
  70. hash.hash_update(data);
  71. hash.hash_final(out);
  72. }
  73. static void hash(MutableByteRange out, const EVP_MD* md, const IOBuf& data) {
  74. Digest hash;
  75. hash.hash_init(md);
  76. hash.hash_update(data);
  77. hash.hash_final(out);
  78. }
  79. static void sha1(MutableByteRange out, ByteRange data) {
  80. hash(out, EVP_sha1(), data);
  81. }
  82. static void sha1(MutableByteRange out, const IOBuf& data) {
  83. hash(out, EVP_sha1(), data);
  84. }
  85. static void sha256(MutableByteRange out, ByteRange data) {
  86. hash(out, EVP_sha256(), data);
  87. }
  88. static void sha256(MutableByteRange out, const IOBuf& data) {
  89. hash(out, EVP_sha256(), data);
  90. }
  91. class Hmac {
  92. public:
  93. Hmac() : ctx_(HMAC_CTX_new()) {}
  94. void hash_init(const EVP_MD* md, ByteRange key) {
  95. md_ = md;
  96. check_libssl_result(
  97. 1,
  98. HMAC_Init_ex(ctx_.get(), key.data(), int(key.size()), md_, nullptr));
  99. }
  100. void hash_update(ByteRange data) {
  101. check_libssl_result(1, HMAC_Update(ctx_.get(), data.data(), data.size()));
  102. }
  103. void hash_update(const IOBuf& data) {
  104. for (auto r : data) {
  105. hash_update(r);
  106. }
  107. }
  108. void hash_final(MutableByteRange out) {
  109. const auto size = EVP_MD_size(md_);
  110. check_out_size(size_t(size), out);
  111. unsigned int len = 0;
  112. check_libssl_result(1, HMAC_Final(ctx_.get(), out.data(), &len));
  113. check_libssl_result(size, int(len));
  114. md_ = nullptr;
  115. }
  116. private:
  117. const EVP_MD* md_ = nullptr;
  118. HmacCtxUniquePtr ctx_{nullptr};
  119. };
  120. static void
  121. hmac(MutableByteRange out, const EVP_MD* md, ByteRange key, ByteRange data) {
  122. Hmac hmac;
  123. hmac.hash_init(md, key);
  124. hmac.hash_update(data);
  125. hmac.hash_final(out);
  126. }
  127. static void hmac(
  128. MutableByteRange out,
  129. const EVP_MD* md,
  130. ByteRange key,
  131. const IOBuf& data) {
  132. Hmac hmac;
  133. hmac.hash_init(md, key);
  134. hmac.hash_update(data);
  135. hmac.hash_final(out);
  136. }
  137. static void hmac_sha1(MutableByteRange out, ByteRange key, ByteRange data) {
  138. hmac(out, EVP_sha1(), key, data);
  139. }
  140. static void
  141. hmac_sha1(MutableByteRange out, ByteRange key, const IOBuf& data) {
  142. hmac(out, EVP_sha1(), key, data);
  143. }
  144. static void hmac_sha256(MutableByteRange out, ByteRange key, ByteRange data) {
  145. hmac(out, EVP_sha256(), key, data);
  146. }
  147. static void
  148. hmac_sha256(MutableByteRange out, ByteRange key, const IOBuf& data) {
  149. hmac(out, EVP_sha256(), key, data);
  150. }
  151. private:
  152. static inline void check_out_size(size_t size, MutableByteRange out) {
  153. if (LIKELY(size == out.size())) {
  154. return;
  155. }
  156. check_out_size_throw(size, out);
  157. }
  158. [[noreturn]] static void check_out_size_throw(
  159. size_t size,
  160. MutableByteRange out);
  161. static inline void check_libssl_result(int expected, int result) {
  162. if (LIKELY(result == expected)) {
  163. return;
  164. }
  165. throw_exception<std::runtime_error>("openssl crypto function failed");
  166. }
  167. };
  168. } // namespace ssl
  169. } // namespace folly