Kea 1.5.0
botan1_hash.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2018 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/version.h>
15#include <botan/botan.h>
16#include <botan/hash.h>
17#include <botan/types.h>
18
20
21namespace isc {
22namespace cryptolink {
23
28const std::string
30 switch (algorithm) {
32 return ("MD5");
34 return ("SHA-1");
36 return ("SHA-256");
38 return ("SHA-224");
40 return ("SHA-384");
42 return ("SHA-512");
44 return ("Unknown");
45 }
46 // compiler should have prevented us to reach this, since we have
47 // no default. But we need a return value anyway
48 return ("Unknown");
49}
50
53class HashImpl {
54public:
55
59 explicit HashImpl(const HashAlgorithm hash_algorithm)
60 : hash_algorithm_(hash_algorithm), hash_() {
61 Botan::HashFunction* hash;
62 try {
63 const std::string& name =
64 btn::getHashAlgorithmName(hash_algorithm);
65 hash = Botan::get_hash(name);
66 } catch (const Botan::Algorithm_Not_Found&) {
68 "Unknown hash algorithm: " <<
69 static_cast<int>(hash_algorithm));
70 } catch (const Botan::Exception& exc) {
72 "Botan error: " << exc.what());
73 }
74
75 hash_.reset(hash);
76 }
77
80
83 return (hash_algorithm_);
84 }
85
89 size_t getOutputLength() const {
90#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
91 return (hash_->output_length());
92#else
93#error "Unsupported Botan version (need 1.9 or higher)"
94 // added to suppress irrelevant compiler errors
95 return 0;
96#endif
97 }
98
102 void update(const void* data, const size_t len) {
103 try {
104 hash_->update(static_cast<const Botan::byte*>(data), len);
105 } catch (const Botan::Exception& exc) {
107 "Botan error: " << exc.what());
108 }
109 }
110
114 void final(isc::util::OutputBuffer& result, size_t len) {
115 try {
116 Botan::SecureVector<Botan::byte> b_result(hash_->final());
117
118 if (len > b_result.size()) {
119 len = b_result.size();
120 }
121 result.writeData(&b_result[0], len);
122 } catch (const Botan::Exception& exc) {
124 "Botan error: " << exc.what());
125 }
126 }
127
131 void final(void* result, size_t len) {
132 try {
133 Botan::SecureVector<Botan::byte> b_result(hash_->final());
134 size_t output_size = getOutputLength();
135 if (output_size > len) {
136 output_size = len;
137 }
138 std::memcpy(result, &b_result[0], output_size);
139 } catch (const Botan::Exception& exc) {
141 "Botan error: " << exc.what());
142 }
143 }
144
148 std::vector<uint8_t> final(size_t len) {
149 try {
150 Botan::SecureVector<Botan::byte> b_result(hash_->final());
151 if (len > b_result.size()) {
152 len = b_result.size();
153 }
154 return (std::vector<uint8_t>(&b_result[0], &b_result[len]));
155 } catch (const Botan::Exception& exc) {
157 "Botan error: " << exc.what());
158 }
159 }
160
161private:
163 HashAlgorithm hash_algorithm_;
164
166 boost::scoped_ptr<Botan::HashFunction> hash_;
167};
168
169Hash::Hash(const HashAlgorithm hash_algorithm)
170{
171 impl_ = new HashImpl(hash_algorithm);
172}
173
175 delete impl_;
176}
177
180 return (impl_->getHashAlgorithm());
181}
182
183size_t
185 return (impl_->getOutputLength());
186}
187
188void
189Hash::update(const void* data, const size_t len) {
190 impl_->update(data, len);
191}
192
193void
195 impl_->final(result, len);
196}
197
198void
199Hash::final(void* result, size_t len) {
200 impl_->final(result, len);
201}
202
203std::vector<uint8_t>
204Hash::final(size_t len) {
205 return impl_->final(len);
206}
207
208} // namespace cryptolink
209} // 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:294
#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-dhcp-ddns.