2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/Range.h>
20 #include <folly/io/IOBuf.h>
21 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
22 #include <folly/portability/OpenSSL.h>
28 /// These functions are not thread-safe unless you initialize OpenSSL.
34 Digest() : ctx_(EVP_MD_CTX_new()) {}
36 Digest(const Digest& other) {
37 ctx_ = EvpMdCtxUniquePtr(EVP_MD_CTX_new());
38 if (other.md_ != nullptr) {
41 1, EVP_MD_CTX_copy_ex(ctx_.get(), other.ctx_.get()));
45 Digest& operator=(const Digest& other) {
47 return *new (this) Digest(other);
50 void hash_init(const EVP_MD* md) {
52 check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
54 void hash_update(ByteRange data) {
56 1, EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
58 void hash_update(const IOBuf& data) {
63 void hash_final(MutableByteRange out) {
64 const auto size = EVP_MD_size(md_);
65 check_out_size(size_t(size), out);
67 check_libssl_result(1, EVP_DigestFinal_ex(ctx_.get(), out.data(), &len));
68 check_libssl_result(size, int(len));
73 const EVP_MD* md_ = nullptr;
74 EvpMdCtxUniquePtr ctx_{nullptr};
83 hash.hash_update(data);
92 hash.hash_update(data);
95 static void sha1(MutableByteRange out, ByteRange data) {
96 hash(out, EVP_sha1(), data);
98 static void sha1(MutableByteRange out, const IOBuf& data) {
99 hash(out, EVP_sha1(), data);
101 static void sha256(MutableByteRange out, ByteRange data) {
102 hash(out, EVP_sha256(), data);
104 static void sha256(MutableByteRange out, const IOBuf& data) {
105 hash(out, EVP_sha256(), data);
110 Hmac() : ctx_(HMAC_CTX_new()) {}
112 void hash_init(const EVP_MD* md, ByteRange key) {
116 HMAC_Init_ex(ctx_.get(), key.data(), int(key.size()), md_, nullptr));
118 void hash_update(ByteRange data) {
119 check_libssl_result(1, HMAC_Update(ctx_.get(), data.data(), data.size()));
121 void hash_update(const IOBuf& data) {
122 for (auto r : data) {
126 void hash_final(MutableByteRange out) {
127 const auto size = EVP_MD_size(md_);
128 check_out_size(size_t(size), out);
129 unsigned int len = 0;
130 check_libssl_result(1, HMAC_Final(ctx_.get(), out.data(), &len));
131 check_libssl_result(size, int(len));
135 const EVP_MD* md_ = nullptr;
136 HmacCtxUniquePtr ctx_{nullptr};
140 MutableByteRange out,
145 hmac.hash_init(md, key);
146 hmac.hash_update(data);
147 hmac.hash_final(out);
150 MutableByteRange out,
155 hmac.hash_init(md, key);
156 hmac.hash_update(data);
157 hmac.hash_final(out);
159 static void hmac_sha1(
160 MutableByteRange out, ByteRange key, ByteRange data) {
161 hmac(out, EVP_sha1(), key, data);
163 static void hmac_sha1(
164 MutableByteRange out, ByteRange key, const IOBuf& data) {
165 hmac(out, EVP_sha1(), key, data);
167 static void hmac_sha256(
168 MutableByteRange out, ByteRange key, ByteRange data) {
169 hmac(out, EVP_sha256(), key, data);
171 static void hmac_sha256(
172 MutableByteRange out, ByteRange key, const IOBuf& data) {
173 hmac(out, EVP_sha256(), key, data);
177 static inline void check_out_size(size_t size, MutableByteRange out) {
178 if (LIKELY(size == out.size())) {
181 check_out_size_throw(size, out);
183 [[noreturn]] static void check_out_size_throw(
185 MutableByteRange out);
187 static inline void check_libssl_result(int expected, int result) {
188 if (LIKELY(result == expected)) {
191 check_libssl_result_throw();
193 [[noreturn]] static void check_libssl_result_throw();