2 * Copyright 2016-present 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/portability/OpenSSL.h>
22 #include <folly/ssl/OpenSSLPtrTypes.h>
28 /// These functions are not thread-safe unless you initialize OpenSSL.
33 Digest() : ctx_(EVP_MD_CTX_new()) {}
35 Digest(const Digest& other) {
36 ctx_ = EvpMdCtxUniquePtr(EVP_MD_CTX_new());
37 if (other.md_ != nullptr) {
40 1, EVP_MD_CTX_copy_ex(ctx_.get(), other.ctx_.get()));
44 Digest& operator=(const Digest& other) {
46 return *new (this) Digest(other);
49 void hash_init(const EVP_MD* md) {
51 check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
53 void hash_update(ByteRange data) {
55 1, EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
57 void hash_update(const IOBuf& data) {
62 void hash_final(MutableByteRange out) {
63 const auto size = EVP_MD_size(md_);
64 check_out_size(size_t(size), out);
66 check_libssl_result(1, EVP_DigestFinal_ex(ctx_.get(), out.data(), &len));
67 check_libssl_result(size, int(len));
72 const EVP_MD* md_ = nullptr;
73 EvpMdCtxUniquePtr ctx_{nullptr};
76 static void hash(MutableByteRange out, const EVP_MD* md, ByteRange data) {
79 hash.hash_update(data);
82 static void hash(MutableByteRange out, const EVP_MD* md, const IOBuf& data) {
85 hash.hash_update(data);
88 static void sha1(MutableByteRange out, ByteRange data) {
89 hash(out, EVP_sha1(), data);
91 static void sha1(MutableByteRange out, const IOBuf& data) {
92 hash(out, EVP_sha1(), data);
94 static void sha256(MutableByteRange out, ByteRange data) {
95 hash(out, EVP_sha256(), data);
97 static void sha256(MutableByteRange out, const IOBuf& data) {
98 hash(out, EVP_sha256(), data);
103 Hmac() : ctx_(HMAC_CTX_new()) {}
105 void hash_init(const EVP_MD* md, ByteRange key) {
109 HMAC_Init_ex(ctx_.get(), key.data(), int(key.size()), md_, nullptr));
111 void hash_update(ByteRange data) {
112 check_libssl_result(1, HMAC_Update(ctx_.get(), data.data(), data.size()));
114 void hash_update(const IOBuf& data) {
115 for (auto r : data) {
119 void hash_final(MutableByteRange out) {
120 const auto size = EVP_MD_size(md_);
121 check_out_size(size_t(size), out);
122 unsigned int len = 0;
123 check_libssl_result(1, HMAC_Final(ctx_.get(), out.data(), &len));
124 check_libssl_result(size, int(len));
129 const EVP_MD* md_ = nullptr;
130 HmacCtxUniquePtr ctx_{nullptr};
134 hmac(MutableByteRange out, const EVP_MD* md, ByteRange key, ByteRange data) {
136 hmac.hash_init(md, key);
137 hmac.hash_update(data);
138 hmac.hash_final(out);
141 MutableByteRange out,
146 hmac.hash_init(md, key);
147 hmac.hash_update(data);
148 hmac.hash_final(out);
150 static void hmac_sha1(MutableByteRange out, ByteRange key, ByteRange data) {
151 hmac(out, EVP_sha1(), key, data);
154 hmac_sha1(MutableByteRange out, ByteRange key, const IOBuf& data) {
155 hmac(out, EVP_sha1(), key, data);
157 static void hmac_sha256(MutableByteRange out, ByteRange key, ByteRange data) {
158 hmac(out, EVP_sha256(), key, data);
161 hmac_sha256(MutableByteRange out, ByteRange key, const IOBuf& data) {
162 hmac(out, EVP_sha256(), key, data);
166 static inline void check_out_size(size_t size, MutableByteRange out) {
167 if (LIKELY(size == out.size())) {
170 check_out_size_throw(size, out);
172 [[noreturn]] static void check_out_size_throw(
174 MutableByteRange out);
176 static inline void check_libssl_result(int expected, int result) {
177 if (LIKELY(result == expected)) {
180 check_libssl_result_throw();
182 [[noreturn]] static void check_libssl_result_throw();