botan_hash.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cryptolink.h>
  15. #include <cryptolink/crypto_hash.h>
  16. #include <boost/scoped_ptr.hpp>
  17. #include <botan/version.h>
  18. #include <botan/botan.h>
  19. #include <botan/hash.h>
  20. #include <botan/types.h>
  21. #include <cryptolink/botan_common.h>
  22. #include <cstring>
  23. namespace isc {
  24. namespace cryptolink {
  25. /// @brief Decode the HashAlgorithm enum into a name usable by Botan
  26. ///
  27. /// @param algorithm algorithm to be converted
  28. /// @return text representation of the algorithm name
  29. const char*
  30. btn::getHashAlgorithmName(HashAlgorithm algorithm) {
  31. switch (algorithm) {
  32. case isc::cryptolink::MD5:
  33. return ("MD5");
  34. case isc::cryptolink::SHA1:
  35. return ("SHA-1");
  36. case isc::cryptolink::SHA256:
  37. return ("SHA-256");
  38. case isc::cryptolink::SHA224:
  39. return ("SHA-224");
  40. case isc::cryptolink::SHA384:
  41. return ("SHA-384");
  42. case isc::cryptolink::SHA512:
  43. return ("SHA-512");
  44. case isc::cryptolink::UNKNOWN_HASH:
  45. return ("Unknown");
  46. }
  47. // compiler should have prevented us to reach this, since we have
  48. // no default. But we need a return value anyway
  49. return ("Unknown");
  50. }
  51. /// @brief Botan implementation of Hash. Each method is the counterpart
  52. /// of the Hash corresponding method.
  53. class HashImpl {
  54. public:
  55. /// @brief Constructor for specific hash algorithm
  56. ///
  57. /// @param hash_algorithm The hash algorithm
  58. explicit HashImpl(const HashAlgorithm hash_algorithm) {
  59. Botan::HashFunction* hash;
  60. try {
  61. hash = Botan::get_hash(btn::getHashAlgorithmName(hash_algorithm));
  62. } catch (const Botan::Algorithm_Not_Found&) {
  63. isc_throw(isc::cryptolink::UnsupportedAlgorithm,
  64. "Unknown hash algorithm: " <<
  65. static_cast<int>(hash_algorithm));
  66. } catch (const Botan::Exception& exc) {
  67. isc_throw(isc::cryptolink::LibraryError, exc.what());
  68. }
  69. hash_.reset(hash);
  70. }
  71. /// @brief Destructor
  72. ~HashImpl() { }
  73. /// @brief Returns the output size of the digest
  74. ///
  75. /// @return output size of the digest
  76. size_t getOutputLength() const {
  77. #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
  78. return (hash_->output_length());
  79. #elif BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,8,0)
  80. return (hash_->OUTPUT_LENGTH);
  81. #else
  82. #error "Unsupported Botan version (need 1.8 or higher)"
  83. // added to suppress irrelevant compiler errors
  84. return 0;
  85. #endif
  86. }
  87. /// @brief Adds data to the digest
  88. ///
  89. /// See @ref isc::cryptolink::Hash::update() for details.
  90. void update(const void* data, const size_t len) {
  91. try {
  92. hash_->update(static_cast<const Botan::byte*>(data), len);
  93. } catch (const Botan::Exception& exc) {
  94. isc_throw(isc::cryptolink::LibraryError, exc.what());
  95. }
  96. }
  97. /// @brief Calculate the final digest
  98. ///
  99. /// See @ref isc::cryptolink::Hash::final() for details.
  100. void final(isc::util::OutputBuffer& result, size_t len) {
  101. try {
  102. Botan::SecureVector<Botan::byte> b_result(hash_->final());
  103. if (len > b_result.size()) {
  104. len = b_result.size();
  105. }
  106. result.writeData(b_result.begin(), len);
  107. } catch (const Botan::Exception& exc) {
  108. isc_throw(isc::cryptolink::LibraryError, exc.what());
  109. }
  110. }
  111. /// @brief Calculate the final digest
  112. ///
  113. /// See @ref isc::cryptolink::Hash::final() for details.
  114. void final(void* result, size_t len) {
  115. try {
  116. Botan::SecureVector<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.begin(), output_size);
  122. } catch (const Botan::Exception& exc) {
  123. isc_throw(isc::cryptolink::LibraryError, exc.what());
  124. }
  125. }
  126. /// @brief Calculate the final digest
  127. ///
  128. /// See @ref isc::cryptolink::Hash::final() for details.
  129. std::vector<uint8_t> final(size_t len) {
  130. try {
  131. Botan::SecureVector<Botan::byte> b_result(hash_->final());
  132. if (len > b_result.size()) {
  133. return (std::vector<uint8_t>(b_result.begin(), b_result.end()));
  134. } else {
  135. return (std::vector<uint8_t>(b_result.begin(), &b_result[len]));
  136. }
  137. } catch (const Botan::Exception& exc) {
  138. isc_throw(isc::cryptolink::LibraryError, exc.what());
  139. }
  140. }
  141. private:
  142. /// \brief The protected pointer to the Botan HashFunction object
  143. boost::scoped_ptr<Botan::HashFunction> hash_;
  144. };
  145. Hash::Hash(const HashAlgorithm hash_algorithm)
  146. {
  147. impl_ = new HashImpl(hash_algorithm);
  148. }
  149. Hash::~Hash() {
  150. delete impl_;
  151. }
  152. size_t
  153. Hash::getOutputLength() const {
  154. return (impl_->getOutputLength());
  155. }
  156. void
  157. Hash::update(const void* data, const size_t len) {
  158. impl_->update(data, len);
  159. }
  160. void
  161. Hash::final(isc::util::OutputBuffer& result, size_t len) {
  162. impl_->final(result, len);
  163. }
  164. void
  165. Hash::final(void* result, size_t len) {
  166. impl_->final(result, len);
  167. }
  168. std::vector<uint8_t>
  169. Hash::final(size_t len) {
  170. return impl_->final(len);
  171. }
  172. } // namespace cryptolink
  173. } // namespace isc