MySQL 9.1.0
Source Code Documentation
openssl_digest.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_ROUTING_OPENSSL_DIGEST_H
27#define MYSQLROUTER_ROUTING_OPENSSL_DIGEST_H
28
29#include <memory> // unique_ptr
30#include <string>
31#include <string_view>
32
33#include <openssl/evp.h>
34#include <openssl/opensslv.h>
35
36namespace openssl {
38 public:
39 DigestFunc(const EVP_MD *func) : func_{func} {}
40
41 int size() const { return EVP_MD_size(func_); }
42
43 const EVP_MD *native_func() const { return func_; }
44
45 private:
46 const EVP_MD *func_;
47};
48
49class DigestCtx {
50 public:
51 class Deleter {
52 public:
53 void operator()(EVP_MD_CTX *ctx) {
54#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
55 EVP_MD_CTX_free(ctx);
56#else
57 EVP_MD_CTX_destroy(ctx);
58#endif
59 }
60 };
61
62 DigestCtx(const EVP_MD *func) : digest_func_(func) {}
63 DigestCtx(const DigestFunc &func) : digest_func_(func.native_func()) {}
64
65 // reinit digest-ctx with the same digest-function after finalize()
66 bool init() { return init(digest_func_); }
67
68 // init digest-ctx with the digest-function.
69 bool init(const EVP_MD *digest_func) {
70 if (digest_func == nullptr) return false;
71
72 auto res = EVP_DigestInit_ex(ctx_.get(), digest_func, nullptr);
73 if (res) digest_func_ = digest_func;
74
75 return res;
76 }
77
78 bool init(DigestFunc func) { return init(func.native_func()); }
79
80 template <class T>
81 bool update(const T &data) {
82 return EVP_DigestUpdate(ctx_.get(), data.data(), data.size());
83 }
84
85 template <class T>
86 bool finalize(T &out) {
87 unsigned int written;
88
89 return EVP_DigestFinal_ex(
90 ctx_.get(), reinterpret_cast<unsigned char *>(out.data()), &written);
91 }
92
93 private:
94 const EVP_MD *digest_func_{};
95
96 std::unique_ptr<EVP_MD_CTX, Deleter> ctx_ {
97#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
98 EVP_MD_CTX_new()
99#else
100 EVP_MD_CTX_create()
101#endif
102 };
103};
104} // namespace openssl
105
106#endif
Definition: openssl_digest.h:51
void operator()(EVP_MD_CTX *ctx)
Definition: openssl_digest.h:53
Definition: openssl_digest.h:49
std::unique_ptr< EVP_MD_CTX, Deleter > ctx_
Definition: openssl_digest.h:96
bool update(const T &data)
Definition: openssl_digest.h:81
bool init(DigestFunc func)
Definition: openssl_digest.h:78
const EVP_MD * digest_func_
Definition: openssl_digest.h:94
bool init(const EVP_MD *digest_func)
Definition: openssl_digest.h:69
DigestCtx(const DigestFunc &func)
Definition: openssl_digest.h:63
bool init()
Definition: openssl_digest.h:66
bool finalize(T &out)
Definition: openssl_digest.h:86
DigestCtx(const EVP_MD *func)
Definition: openssl_digest.h:62
Definition: openssl_digest.h:37
const EVP_MD * func_
Definition: openssl_digest.h:46
DigestFunc(const EVP_MD *func)
Definition: openssl_digest.h:39
int size() const
Definition: openssl_digest.h:41
const EVP_MD * native_func() const
Definition: openssl_digest.h:43
Definition: openssl_digest.h:36