botan_hash.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2014-2017 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. #include <cryptolink.h>
  7. #include <cryptolink/crypto_hash.h>
  8. #include <boost/scoped_ptr.hpp>
  9. #include <botan/version.h>
  10. #include <botan/botan.h>
  11. #include <botan/hash.h>
  12. #include <botan/types.h>
  13. #include <cryptolink/botan_common.h>
  14. #if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,11,0)
  15. #define secure_vector SecureVector
  16. #endif
  17. namespace isc {
  18. namespace cryptolink {
  19. /// @brief Decode the HashAlgorithm enum into a name usable by Botan
  20. ///
  21. /// @param algorithm algorithm to be converted
  22. /// @return text representation of the algorithm name
  23. const std::string
  24. btn::getHashAlgorithmName(HashAlgorithm algorithm) {
  25. switch (algorithm) {
  26. case isc::cryptolink::MD5:
  27. return ("MD5");
  28. case isc::cryptolink::SHA1:
  29. return ("SHA-1");
  30. case isc::cryptolink::SHA256:
  31. return ("SHA-256");
  32. case isc::cryptolink::SHA224:
  33. return ("SHA-224");
  34. case isc::cryptolink::SHA384:
  35. return ("SHA-384");
  36. case isc::cryptolink::SHA512:
  37. return ("SHA-512");
  38. case isc::cryptolink::UNKNOWN_HASH:
  39. return ("Unknown");
  40. }
  41. // compiler should have prevented us to reach this, since we have
  42. // no default. But we need a return value anyway
  43. return ("Unknown");
  44. }
  45. /// @brief Botan implementation of Hash. Each method is the counterpart
  46. /// of the Hash corresponding method.
  47. class HashImpl {
  48. public:
  49. /// @brief Constructor for specific hash algorithm
  50. ///
  51. /// @param hash_algorithm The hash algorithm
  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. #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,11,0)
  59. hash = Botan::HashFunction::create(name).release();
  60. #else
  61. hash = Botan::get_hash(name);
  62. #endif
  63. } catch (const Botan::Algorithm_Not_Found&) {
  64. isc_throw(isc::cryptolink::UnsupportedAlgorithm,
  65. "Unknown hash algorithm: " <<
  66. static_cast<int>(hash_algorithm));
  67. } catch (const Botan::Exception& exc) {
  68. isc_throw(isc::cryptolink::LibraryError,
  69. "Botan error: " << exc.what());
  70. }
  71. hash_.reset(hash);
  72. }
  73. /// @brief Destructor
  74. ~HashImpl() { }
  75. /// @brief Returns the HashAlgorithm of the object
  76. HashAlgorithm getHashAlgorithm() const {
  77. return (hash_algorithm_);
  78. }
  79. /// @brief Returns the output size of the digest
  80. ///
  81. /// @return output size of the digest
  82. size_t getOutputLength() const {
  83. #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
  84. return (hash_->output_length());
  85. #elif BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,8,0)
  86. return (hash_->OUTPUT_LENGTH);
  87. #else
  88. #error "Unsupported Botan version (need 1.8 or higher)"
  89. // added to suppress irrelevant compiler errors
  90. return 0;
  91. #endif
  92. }
  93. /// @brief Adds data to the digest
  94. ///
  95. /// See @ref isc::cryptolink::Hash::update() for details.
  96. void update(const void* data, const size_t len) {
  97. try {
  98. hash_->update(static_cast<const Botan::byte*>(data), len);
  99. } catch (const Botan::Exception& exc) {
  100. isc_throw(isc::cryptolink::LibraryError,
  101. "Botan error: " << exc.what());
  102. }
  103. }
  104. /// @brief Calculate the final digest
  105. ///
  106. /// See @ref isc::cryptolink::Hash::final() for details.
  107. void final(isc::util::OutputBuffer& result, size_t len) {
  108. try {
  109. Botan::secure_vector<Botan::byte> b_result(hash_->final());
  110. if (len > b_result.size()) {
  111. len = b_result.size();
  112. }
  113. result.writeData(&b_result[0], len);
  114. } catch (const Botan::Exception& exc) {
  115. isc_throw(isc::cryptolink::LibraryError,
  116. "Botan error: " << exc.what());
  117. }
  118. }
  119. /// @brief Calculate the final digest
  120. ///
  121. /// See @ref isc::cryptolink::Hash::final() for details.
  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) {
  131. isc_throw(isc::cryptolink::LibraryError,
  132. "Botan error: " << exc.what());
  133. }
  134. }
  135. /// @brief Calculate the final digest
  136. ///
  137. /// See @ref isc::cryptolink::Hash::final() for details.
  138. std::vector<uint8_t> final(size_t len) {
  139. try {
  140. Botan::secure_vector<Botan::byte> b_result(hash_->final());
  141. if (len > b_result.size()) {
  142. len = b_result.size();
  143. }
  144. return (std::vector<uint8_t>(&b_result[0], &b_result[len]));
  145. } catch (const Botan::Exception& exc) {
  146. isc_throw(isc::cryptolink::LibraryError,
  147. "Botan error: " << exc.what());
  148. }
  149. }
  150. private:
  151. /// @brief The hash algorithm
  152. HashAlgorithm hash_algorithm_;
  153. /// @brief The protected pointer to the Botan HashFunction object
  154. boost::scoped_ptr<Botan::HashFunction> hash_;
  155. };
  156. Hash::Hash(const HashAlgorithm hash_algorithm)
  157. {
  158. impl_ = new HashImpl(hash_algorithm);
  159. }
  160. Hash::~Hash() {
  161. delete impl_;
  162. }
  163. HashAlgorithm
  164. Hash::getHashAlgorithm() const {
  165. return (impl_->getHashAlgorithm());
  166. }
  167. size_t
  168. Hash::getOutputLength() const {
  169. return (impl_->getOutputLength());
  170. }
  171. void
  172. Hash::update(const void* data, const size_t len) {
  173. impl_->update(data, len);
  174. }
  175. void
  176. Hash::final(isc::util::OutputBuffer& result, size_t len) {
  177. impl_->final(result, len);
  178. }
  179. void
  180. Hash::final(void* result, size_t len) {
  181. impl_->final(result, len);
  182. }
  183. std::vector<uint8_t>
  184. Hash::final(size_t len) {
  185. return impl_->final(len);
  186. }
  187. } // namespace cryptolink
  188. } // namespace isc