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/io/async/ssl/OpenSSLPtrTypes.h>
20 #include <folly/portability/OpenSSL.h>
21 #include <openssl/evp.h>
22 #include <openssl/hmac.h>
23 #include <openssl/sha.h>
25 #include <folly/Range.h>
26 #include <folly/io/IOBuf.h>
32 /// These functions are not thread-safe unless you initialize OpenSSL.
38 Digest() : ctx_(EVP_MD_CTX_new()) {}
40 void hash_init(const EVP_MD* md) {
42 check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
44 void hash_update(ByteRange data) {
46 1, EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
48 void hash_update(const IOBuf& data) {
53 void hash_final(MutableByteRange out) {
54 const auto size = EVP_MD_size(md_);
55 check_out_size(size_t(size), out);
57 check_libssl_result(1, EVP_DigestFinal_ex(ctx_.get(), out.data(), &len));
58 check_libssl_result(size, int(len));
62 const EVP_MD* md_ = nullptr;
63 EvpMdCtxUniquePtr ctx_{nullptr};
72 hash.hash_update(data);
81 hash.hash_update(data);
84 static void sha1(MutableByteRange out, ByteRange data) {
85 hash(out, EVP_sha1(), data);
87 static void sha1(MutableByteRange out, const IOBuf& data) {
88 hash(out, EVP_sha1(), data);
90 static void sha256(MutableByteRange out, ByteRange data) {
91 hash(out, EVP_sha256(), data);
93 static void sha256(MutableByteRange out, const IOBuf& data) {
94 hash(out, EVP_sha256(), data);
99 Hmac() : ctx_(HMAC_CTX_new()) {}
101 void hash_init(const EVP_MD* md, ByteRange key) {
105 HMAC_Init_ex(ctx_.get(), key.data(), int(key.size()), md_, nullptr));
107 void hash_update(ByteRange data) {
108 check_libssl_result(1, HMAC_Update(ctx_.get(), data.data(), data.size()));
110 void hash_update(const IOBuf& data) {
111 for (auto r : data) {
115 void hash_final(MutableByteRange out) {
116 const auto size = EVP_MD_size(md_);
117 check_out_size(size_t(size), out);
118 unsigned int len = 0;
119 check_libssl_result(1, HMAC_Final(ctx_.get(), out.data(), &len));
120 check_libssl_result(size, int(len));
124 const EVP_MD* md_ = nullptr;
125 HmacCtxUniquePtr ctx_{nullptr};
129 MutableByteRange out,
134 hmac.hash_init(md, key);
135 hmac.hash_update(data);
136 hmac.hash_final(out);
139 MutableByteRange out,
144 hmac.hash_init(md, key);
145 hmac.hash_update(data);
146 hmac.hash_final(out);
148 static void hmac_sha1(
149 MutableByteRange out, ByteRange key, ByteRange data) {
150 hmac(out, EVP_sha1(), key, data);
152 static void hmac_sha1(
153 MutableByteRange out, ByteRange key, const IOBuf& data) {
154 hmac(out, EVP_sha1(), key, data);
156 static void hmac_sha256(
157 MutableByteRange out, ByteRange key, ByteRange data) {
158 hmac(out, EVP_sha256(), key, data);
160 static void hmac_sha256(
161 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();