Kea 2.7.6
botan_hash.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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 <botan/hash.h>
13#include <botan/exceptn.h>
14
16
17namespace isc {
18namespace cryptolink {
19
20const std::string
22 switch (algorithm) {
24 return ("MD5");
26 return ("SHA-1");
28 return ("SHA-256");
30 return ("SHA-224");
32 return ("SHA-384");
34 return ("SHA-512");
36 return ("Unknown");
37 }
38 // compiler should have prevented us to reach this, since we have
39 // no default. But we need a return value anyway
40 return ("Unknown");
41}
42
45class HashImpl {
46public:
47
51 explicit HashImpl(const HashAlgorithm hash_algorithm)
52 : hash_algorithm_(hash_algorithm), hash_() {
53 try {
54 const std::string& name =
55 btn::getHashAlgorithmName(hash_algorithm);
56 hash_ = Botan::HashFunction::create_or_throw(name);
57 } catch (const Botan::Lookup_Error&) {
59 "Unknown hash algorithm: " <<
60 static_cast<int>(hash_algorithm));
61 } catch (const Botan::Exception& exc) {
63 "Botan error: " << exc.what());
64 }
65 }
66
68 ~HashImpl() = default;
69
72 return (hash_algorithm_);
73 }
74
78 size_t getOutputLength() const {
79 return (hash_->output_length());
80 }
81
85 void update(const void* data, const size_t len) {
86 try {
87 hash_->update(static_cast<const Botan::byte*>(data), len);
88 } catch (const Botan::Exception& exc) {
90 "Botan error: " << exc.what());
91 }
92 }
93
97 void final(isc::util::OutputBuffer& result, size_t len) {
98 try {
99 Botan::secure_vector<Botan::byte> b_result(hash_->final());
100
101 if (len > b_result.size()) {
102 len = b_result.size();
103 }
104 result.writeData(&b_result[0], len);
105 } catch (const Botan::Exception& exc) {
107 "Botan error: " << exc.what());
108 }
109 }
110
114 void final(void* result, size_t len) {
115 try {
116 Botan::secure_vector<Botan::byte> b_result(hash_->final());
117 size_t output_size = getOutputLength();
118 if (output_size > len) {
119 output_size = len;
120 }
121 std::memcpy(result, &b_result[0], output_size);
122 } catch (const Botan::Exception& exc) {
124 "Botan error: " << exc.what());
125 }
126 }
127
131 std::vector<uint8_t> final(size_t len) {
132 try {
133 Botan::secure_vector<Botan::byte> b_result(hash_->final());
134 if (len > b_result.size()) {
135 len = b_result.size();
136 }
137 // Return vector with content. Construct &b_result[len] attempts
138 // to get an address of one element beyond the b_result. Replaced
139 // with the address of first element + len
140 return (std::vector<uint8_t>(&b_result[0], &b_result[0]+len));
141 } catch (const Botan::Exception& exc) {
143 "Botan error: " << exc.what());
144 }
145 }
146
147private:
149 HashAlgorithm hash_algorithm_;
150
152 std::unique_ptr<Botan::HashFunction> hash_;
153};
154
155Hash::Hash(const HashAlgorithm hash_algorithm)
156{
157 impl_ = new HashImpl(hash_algorithm);
158}
159
161 delete impl_;
162}
163
166 return (impl_->getHashAlgorithm());
167}
168
169size_t
171 return (impl_->getOutputLength());
172}
173
174void
175Hash::update(const void* data, const size_t len) {
176 impl_->update(data, len);
177}
178
179void
181 impl_->final(result, len);
182}
183
184void
185Hash::final(void* result, size_t len) {
186 impl_->final(result, len);
187}
188
189std::vector<uint8_t>
190Hash::final(size_t len) {
191 return impl_->final(len);
192}
193
194} // namespace cryptolink
195} // namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
#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.