Kea: botan_hash.cc Source File

| Kea 3.0.3-git

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>
10#include <cryptolink/crypto_hash.h>
11
12#include <botan/hash.h>
13#include <botan/exceptn.h>
14
15#include <cryptolink/botan_common.h>
16
17namespace isc {
18namespace cryptolink {
19
20const std::string
21btn::getHashAlgorithmName(HashAlgorithm algorithm) {
22 switch (algorithm) {
23 case isc::cryptolink::MD5:
24 return ("MD5");
25 case isc::cryptolink::SHA1:
26 return ("SHA-1");
27 case isc::cryptolink::SHA256:
28 return ("SHA-256");
29 case isc::cryptolink::SHA224:
30 return ("SHA-224");
31 case isc::cryptolink::SHA384:
32 return ("SHA-384");
33 case isc::cryptolink::SHA512:
34 return ("SHA-512");
35 case isc::cryptolink::UNKNOWN_HASH:
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
45 class 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&) {
58 isc_throw(isc::cryptolink::UnsupportedAlgorithm,
59 "Unknown hash algorithm: " <<
60 static_cast<int>(hash_algorithm));
61 } catch (const Botan::Exception& exc) {
62 isc_throw(isc::cryptolink::LibraryError,
63 "Botan error: " << exc.what());
64 }
65 }
66
68 ~HashImpl() = default;
69
71 HashAlgorithm getHashAlgorithm() const {
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) {
89 isc_throw(isc::cryptolink::LibraryError,
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) {
106 isc_throw(isc::cryptolink::LibraryError,
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) {
123 isc_throw(isc::cryptolink::LibraryError,
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) {
142 isc_throw(isc::cryptolink::LibraryError,
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
160Hash::~Hash() {
161 delete impl_;
162}
163
164 HashAlgorithm
165Hash::getHashAlgorithm() const {
166 return (impl_->getHashAlgorithm());
167}
168
169size_t
170Hash::getOutputLength() const {
171 return (impl_->getOutputLength());
172}
173
174void
175Hash::update(const void* data, const size_t len) {
176 impl_->update(data, len);
177}
178
179void
180Hash::final(isc::util::OutputBuffer& result, size_t len) {
181 impl_->final(result, len);
182}
183
184void
185 Hash::final(void* result, size_t len) {
186 impl_->final(result, len);
187}
188
189std::vector<uint8_t>
190 Hash::final(size_t len) {
191 return impl_->final(len);
192}
193
194} // namespace cryptolink
195} // namespace isc
botan_common.h
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::cryptolink::HashImpl::getOutputLength
size_t getOutputLength() const
Returns the output size of the digest.
Definition botan_hash.cc:78
isc::cryptolink::HashImpl::getHashAlgorithm
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
Definition botan_hash.cc:71
isc::cryptolink::HashImpl::~HashImpl
~HashImpl()=default
Destructor.
isc::cryptolink::HashImpl::update
void update(const void *data, const size_t len)
Adds data to the digest.
Definition botan_hash.cc:85
isc::cryptolink::HashImpl::HashImpl
HashImpl(const HashAlgorithm hash_algorithm)
Constructor for specific hash algorithm.
Definition botan_hash.cc:51
isc::cryptolink::Hash::update
void update(const void *data, const size_t len)
Add data to digest.
Definition botan_hash.cc:175
isc::cryptolink::Hash::~Hash
~Hash()
Destructor.
Definition botan_hash.cc:160
isc::cryptolink::Hash::getOutputLength
size_t getOutputLength() const
Returns the output size of the digest.
Definition botan_hash.cc:170
isc::cryptolink::Hash::getHashAlgorithm
HashAlgorithm getHashAlgorithm() const
Returns the HashAlgorithm of the object.
Definition botan_hash.cc:165
isc::cryptolink::Hash::final
void final(isc::util::OutputBuffer &result, size_t len)
Calculate the final digest.
Definition botan_hash.cc:180
isc::cryptolink::LibraryError
This exception is raised when a general error that was not specifically caught is thrown by the under...
Definition cryptolink.h:86
isc::cryptolink::UnsupportedAlgorithm
This exception is thrown when a cryptographic action is requested for an algorithm that is not suppor...
Definition cryptolink.h:68
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
crypto_hash.h
cryptolink.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::cryptolink::btn::getHashAlgorithmName
const std::string getHashAlgorithmName(isc::cryptolink::HashAlgorithm algorithm)
Decode the HashAlgorithm enum into a name usable by Botan.
Definition botan_hash.cc:21
isc::cryptolink
Definition botan_common.h:8
isc::cryptolink::HashAlgorithm
HashAlgorithm
Hash algorithm identifiers.
Definition cryptolink.h:23
isc::cryptolink::MD5
@ MD5
Definition cryptolink.h:30
isc::cryptolink::SHA1
@ SHA1
Definition cryptolink.h:31
isc::cryptolink::SHA512
@ SHA512
Definition cryptolink.h:35
isc::cryptolink::SHA224
@ SHA224
Definition cryptolink.h:33
isc::cryptolink::SHA256
@ SHA256
Definition cryptolink.h:32
isc::cryptolink::SHA384
@ SHA384
Definition cryptolink.h:34
isc::cryptolink::UNKNOWN_HASH
@ UNKNOWN_HASH
Definition cryptolink.h:24
isc::data
Definition base_stamped_element.cc:12
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148