Kea 2.7.7
openssl_hash.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cryptolink.h>
11
12#include <boost/scoped_ptr.hpp>
13
14#include <openssl/evp.h>
15
17
18#include <cstring>
19
20namespace isc {
21namespace cryptolink {
22
23const EVP_MD*
25 switch (algorithm) {
27 return (EVP_md5());
29 return (EVP_sha1());
31 return (EVP_sha256());
33 return (EVP_sha224());
35 return (EVP_sha384());
37 return (EVP_sha512());
39 return (0);
40 }
41 // compiler should have prevented us to reach this, since we have
42 // no default. But we need a return value anyway
43 return (0);
44}
45
48class HashImpl {
49public:
50
54 explicit HashImpl(const HashAlgorithm hash_algorithm)
55 : hash_algorithm_(hash_algorithm), md_(0) {
56 const EVP_MD* algo = ossl::getHashAlgorithm(hash_algorithm);
57 if (algo == 0) {
59 "Unknown hash algorithm: " <<
60 static_cast<int>(hash_algorithm));
61 }
62
63 md_ = EVP_MD_CTX_new();
64 if (md_ == 0) {
66 "OpenSSL EVP_MD_CTX_new() failed");
67 }
68
69 EVP_DigestInit_ex(md_, algo, NULL);
70 }
71
74 if (md_) {
75 EVP_MD_CTX_free(md_);
76 }
77 md_ = 0;
78 }
79
82 return (hash_algorithm_);
83 }
84
88 size_t getOutputLength() const {
89 return (EVP_MD_CTX_size(md_));
90 }
91
95 void update(const void* data, const size_t len) {
96 EVP_DigestUpdate(md_, data, len);
97 }
98
102 void final(isc::util::OutputBuffer& result, size_t len) {
103 size_t size = getOutputLength();
104 std::vector<unsigned char> digest(size);
105 EVP_DigestFinal_ex(md_, &digest[0], NULL);
106 if (len > size) {
107 len = size;
108 }
109 result.writeData(&digest[0], len);
110 }
111
115 void final(void* result, size_t len) {
116 size_t size = getOutputLength();
117 std::vector<unsigned char> digest(size);
118 EVP_DigestFinal_ex(md_, &digest[0], NULL);
119 if (len > size) {
120 len = size;
121 }
122 std::memcpy(result, &digest[0], len);
123 }
124
128 std::vector<uint8_t> final(size_t len) {
129 size_t size = getOutputLength();
130 std::vector<unsigned char> digest(size);
131 EVP_DigestFinal_ex(md_, &digest[0], NULL);
132 if (len < size) {
133 digest.resize(len);
134 }
135 return (std::vector<uint8_t>(digest.begin(), digest.end()));
136 }
137
138private:
140 HashAlgorithm hash_algorithm_;
141
143 EVP_MD_CTX* md_;
144};
145
146Hash::Hash(const HashAlgorithm hash_algorithm) {
147 impl_ = new HashImpl(hash_algorithm);
148}
149
150Hash::~Hash() {
151 delete impl_;
152}
153
156 return (impl_->getHashAlgorithm());
157}
158
159size_t
160Hash::getOutputLength() const {
161 return (impl_->getOutputLength());
162}
163
164void
165Hash::update(const void* data, const size_t len) {
166 impl_->update(data, len);
167}
168
169void
170Hash::final(isc::util::OutputBuffer& result, size_t len) {
171 impl_->final(result, len);
172}
173
174void
175Hash::final(void* result, size_t len) {
176 impl_->final(result, len);
177}
178
179std::vector<uint8_t>
180Hash::final(size_t len) {
181 return impl_->final(len);
182}
183
184} // namespace cryptolink
185} // namespace isc
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.