Kea  1.5.0
botan_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>
10 #include <cryptolink/crypto_hash.h>
11 
12 #include <boost/scoped_ptr.hpp>
13 
14 #include <botan/lookup.h>
15 
17 
18 namespace isc {
19 namespace cryptolink {
20 
25 const std::string
27  switch (algorithm) {
29  return ("MD5");
31  return ("SHA-1");
33  return ("SHA-256");
35  return ("SHA-224");
37  return ("SHA-384");
39  return ("SHA-512");
41  return ("Unknown");
42  }
43  // compiler should have prevented us to reach this, since we have
44  // no default. But we need a return value anyway
45  return ("Unknown");
46 }
47 
50 class HashImpl {
51 public:
52 
56  explicit HashImpl(const HashAlgorithm hash_algorithm)
57  : hash_algorithm_(hash_algorithm), hash_() {
58  Botan::HashFunction* hash;
59  try {
60  const std::string& name =
61  btn::getHashAlgorithmName(hash_algorithm);
62  hash = Botan::HashFunction::create(name).release();
63  } catch (const Botan::Algorithm_Not_Found&) {
65  "Unknown hash algorithm: " <<
66  static_cast<int>(hash_algorithm));
67  } catch (const Botan::Exception& exc) {
69  "Botan error: " << exc.what());
70  }
71 
72  hash_.reset(hash);
73  }
74 
76  ~HashImpl() { }
77 
80  return (hash_algorithm_);
81  }
82 
86  size_t getOutputLength() const {
87  return (hash_->output_length());
88  }
89 
93  void update(const void* data, const size_t len) {
94  try {
95  hash_->update(static_cast<const Botan::byte*>(data), len);
96  } catch (const Botan::Exception& exc) {
98  "Botan error: " << exc.what());
99  }
100  }
101 
105  void final(isc::util::OutputBuffer& result, size_t len) {
106  try {
107  Botan::secure_vector<Botan::byte> b_result(hash_->final());
108 
109  if (len > b_result.size()) {
110  len = b_result.size();
111  }
112  result.writeData(&b_result[0], len);
113  } catch (const Botan::Exception& exc) {
115  "Botan error: " << exc.what());
116  }
117  }
118 
122  void final(void* result, size_t len) {
123  try {
124  Botan::secure_vector<Botan::byte> b_result(hash_->final());
125  size_t output_size = getOutputLength();
126  if (output_size > len) {
127  output_size = len;
128  }
129  std::memcpy(result, &b_result[0], output_size);
130  } catch (const Botan::Exception& exc) {
132  "Botan error: " << exc.what());
133  }
134  }
135 
139  std::vector<uint8_t> final(size_t len) {
140  try {
141  Botan::secure_vector<Botan::byte> b_result(hash_->final());
142  if (len > b_result.size()) {
143  len = b_result.size();
144  }
145  return (std::vector<uint8_t>(&b_result[0], &b_result[len]));
146  } catch (const Botan::Exception& exc) {
148  "Botan error: " << exc.what());
149  }
150  }
151 
152 private:
154  HashAlgorithm hash_algorithm_;
155 
157  boost::scoped_ptr<Botan::HashFunction> hash_;
158 };
159 
160 Hash::Hash(const HashAlgorithm hash_algorithm)
161 {
162  impl_ = new HashImpl(hash_algorithm);
163 }
164 
165 Hash::~Hash() {
166  delete impl_;
167 }
168 
170 Hash::getHashAlgorithm() const {
171  return (impl_->getHashAlgorithm());
172 }
173 
174 size_t
175 Hash::getOutputLength() const {
176  return (impl_->getOutputLength());
177 }
178 
179 void
180 Hash::update(const void* data, const size_t len) {
181  impl_->update(data, len);
182 }
183 
184 void
185 Hash::final(isc::util::OutputBuffer& result, size_t len) {
186  impl_->final(result, len);
187 }
188 
189 void
190 Hash::final(void* result, size_t len) {
191  impl_->final(result, len);
192 }
193 
194 std::vector<uint8_t>
195 Hash::final(size_t len) {
196  return impl_->final(len);
197 }
198 
199 } // namespace cryptolink
200 } // namespace isc
botan_common.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
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_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
crypto_hash.h