Kea 2.7.1
botan_hash.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2020 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 <botan/lookup.h>
15
17
18namespace isc {
19namespace cryptolink {
20
21const std::string
23 switch (algorithm) {
25 return ("MD5");
27 return ("SHA-1");
29 return ("SHA-256");
31 return ("SHA-224");
33 return ("SHA-384");
35 return ("SHA-512");
37 return ("Unknown");
38 }
39 // compiler should have prevented us to reach this, since we have
40 // no default. But we need a return value anyway
41 return ("Unknown");
42}
43
46class HashImpl {
47public:
48
52 explicit HashImpl(const HashAlgorithm hash_algorithm)
53 : hash_algorithm_(hash_algorithm), hash_() {
54 Botan::HashFunction* hash;
55 try {
56 const std::string& name =
57 btn::getHashAlgorithmName(hash_algorithm);
58 hash = Botan::HashFunction::create(name).release();
59 } catch (const Botan::Algorithm_Not_Found&) {
61 "Unknown hash algorithm: " <<
62 static_cast<int>(hash_algorithm));
63 } catch (const Botan::Exception& exc) {
65 "Botan error: " << exc.what());
66 }
67
68 hash_.reset(hash);
69 }
70
73
76 return (hash_algorithm_);
77 }
78
82 size_t getOutputLength() const {
83 return (hash_->output_length());
84 }
85
89 void update(const void* data, const size_t len) {
90 try {
91 hash_->update(static_cast<const Botan::byte*>(data), len);
92 } catch (const Botan::Exception& exc) {
94 "Botan error: " << exc.what());
95 }
96 }
97
101 void final(isc::util::OutputBuffer& result, size_t len) {
102 try {
103 Botan::secure_vector<Botan::byte> b_result(hash_->final());
104
105 if (len > b_result.size()) {
106 len = b_result.size();
107 }
108 result.writeData(&b_result[0], len);
109 } catch (const Botan::Exception& exc) {
111 "Botan error: " << exc.what());
112 }
113 }
114
118 void final(void* result, size_t len) {
119 try {
120 Botan::secure_vector<Botan::byte> b_result(hash_->final());
121 size_t output_size = getOutputLength();
122 if (output_size > len) {
123 output_size = len;
124 }
125 std::memcpy(result, &b_result[0], output_size);
126 } catch (const Botan::Exception& exc) {
128 "Botan error: " << exc.what());
129 }
130 }
131
135 std::vector<uint8_t> final(size_t len) {
136 try {
137 Botan::secure_vector<Botan::byte> b_result(hash_->final());
138 if (len > b_result.size()) {
139 len = b_result.size();
140 }
141 // Return vector with content. Construct &b_result[len] attempts
142 // to get an address of one element beyond the b_result. Replaced
143 // with the address of first element + len
144 return (std::vector<uint8_t>(&b_result[0], &b_result[0]+len));
145 } catch (const Botan::Exception& exc) {
147 "Botan error: " << exc.what());
148 }
149 }
150
151private:
153 HashAlgorithm hash_algorithm_;
154
156 boost::scoped_ptr<Botan::HashFunction> hash_;
157};
158
159Hash::Hash(const HashAlgorithm hash_algorithm)
160{
161 impl_ = new HashImpl(hash_algorithm);
162}
163
165 delete impl_;
166}
167
170 return (impl_->getHashAlgorithm());
171}
172
173size_t
175 return (impl_->getOutputLength());
176}
177
178void
179Hash::update(const void* data, const size_t len) {
180 impl_->update(data, len);
181}
182
183void
185 impl_->final(result, len);
186}
187
188void
189Hash::final(void* result, size_t len) {
190 impl_->final(result, len);
191}
192
193std::vector<uint8_t>
194Hash::final(size_t len) {
195 return impl_->final(len);
196}
197
198} // namespace cryptolink
199} // 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.