Kea 3.2.1-git
botan_hash.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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 if (len > b_result.size()) {
101 len = b_result.size();
102 }
103 result.writeData(&b_result[0], len);
104 } catch (const Botan::Exception& exc) {
106 "Botan error: " << exc.what());
107 }
108 }
109
113 void final(void* result, size_t len) {
114 try {
115 Botan::secure_vector<Botan::byte> b_result(hash_->final());
116 if (len > b_result.size()) {
117 len = b_result.size();
118 }
119 std::memcpy(result, &b_result[0], len);
120 } catch (const Botan::Exception& exc) {
122 "Botan error: " << exc.what());
123 }
124 }
125
129 std::vector<uint8_t> final(size_t len) {
130 try {
131 Botan::secure_vector<Botan::byte> b_result(hash_->final());
132 if (len > b_result.size()) {
133 len = b_result.size();
134 }
135 // Return vector with content. Construct &b_result[len] attempts
136 // to get an address of one element beyond the b_result. Replaced
137 // with the address of first element + len
138 return (std::vector<uint8_t>(&b_result[0], &b_result[0]+len));
139 } catch (const Botan::Exception& exc) {
141 "Botan error: " << exc.what());
142 }
143 }
144
145private:
147 HashAlgorithm hash_algorithm_;
148
150 std::unique_ptr<Botan::HashFunction> hash_;
151};
152
153Hash::Hash(const HashAlgorithm hash_algorithm)
154{
155 impl_ = new HashImpl(hash_algorithm);
156}
157
159 delete impl_;
160}
161
164 return (impl_->getHashAlgorithm());
165}
166
167size_t
169 return (impl_->getOutputLength());
170}
171
172void
173Hash::update(const void* data, const size_t len) {
174 impl_->update(data, len);
175}
176
177void
179 impl_->final(result, len);
180}
181
182void
183Hash::final(void* result, size_t len) {
184 impl_->final(result, len);
185}
186
187std::vector<uint8_t>
188Hash::final(size_t len) {
189 return impl_->final(len);
190}
191
192} // namespace cryptolink
193} // 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: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.