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 void hash_init(const EVP_MD* md) {
38 check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
40 void hash_update(ByteRange data) {
42 1, EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
44 void hash_update(const IOBuf& data) {
49 void hash_final(MutableByteRange out) {
50 const auto size = EVP_MD_size(md_);
51 check_out_size(size_t(size), out);
53 check_libssl_result(1, EVP_DigestFinal_ex(ctx_.get(), out.data(), &len));
54 check_libssl_result(size, int(len));
58 const EVP_MD* md_ = nullptr;
59 EvpMdCtxUniquePtr ctx_{nullptr};
68 hash.hash_update(data);
77 hash.hash_update(data);
80 static void sha1(MutableByteRange out, ByteRange data) {
81 hash(out, EVP_sha1(), data);
83 static void sha1(MutableByteRange out, const IOBuf& data) {
84 hash(out, EVP_sha1(), data);
86 static void sha256(MutableByteRange out, ByteRange data) {
87 hash(out, EVP_sha256(), data);
89 static void sha256(MutableByteRange out, const IOBuf& data) {
90 hash(out, EVP_sha256(), data);
95 Hmac() : ctx_(HMAC_CTX_new()) {}
97 void hash_init(const EVP_MD* md, ByteRange key) {
101 HMAC_Init_ex(ctx_.get(), key.data(), int(key.size()), md_, nullptr));
103 void hash_update(ByteRange data) {
104 check_libssl_result(1, HMAC_Update(ctx_.get(), data.data(), data.size()));
106 void hash_update(const IOBuf& data) {
107 for (auto r : data) {
111 void hash_final(MutableByteRange out) {
112 const auto size = EVP_MD_size(md_);
113 check_out_size(size_t(size), out);
114 unsigned int len = 0;
115 check_libssl_result(1, HMAC_Final(ctx_.get(), out.data(), &len));
116 check_libssl_result(size, int(len));
120 const EVP_MD* md_ = nullptr;
121 HmacCtxUniquePtr ctx_{nullptr};
125 MutableByteRange out,
130 hmac.hash_init(md, key);
131 hmac.hash_update(data);
132 hmac.hash_final(out);
135 MutableByteRange out,
140 hmac.hash_init(md, key);
141 hmac.hash_update(data);
142 hmac.hash_final(out);
144 static void hmac_sha1(
145 MutableByteRange out, ByteRange key, ByteRange data) {
146 hmac(out, EVP_sha1(), key, data);
148 static void hmac_sha1(
149 MutableByteRange out, ByteRange key, const IOBuf& data) {
150 hmac(out, EVP_sha1(), key, data);
152 static void hmac_sha256(
153 MutableByteRange out, ByteRange key, ByteRange data) {
154 hmac(out, EVP_sha256(), key, data);
156 static void hmac_sha256(
157 MutableByteRange out, ByteRange key, const IOBuf& data) {
158 hmac(out, EVP_sha256(), key, data);
162 static inline void check_out_size(size_t size, MutableByteRange out) {
163 if (LIKELY(size == out.size())) {
166 check_out_size_throw(size, out);
168 [[noreturn]] static void check_out_size_throw(
170 MutableByteRange out);
172 static inline void check_libssl_result(int expected, int result) {
173 if (LIKELY(result == expected)) {
176 check_libssl_result_throw();
178 [[noreturn]] static void check_libssl_result_throw();