botan_hmac.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (C) 2011-2016 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_hmac.h>
  8. #include <boost/scoped_ptr.hpp>
  9. #include <botan/version.h>
  10. #include <botan/botan.h>
  11. #include <botan/hmac.h>
  12. #include <botan/hash.h>
  13. #include <botan/types.h>
  14. #include <cryptolink/botan_common.h>
  15. #include <cstring>
  16. namespace isc {
  17. namespace cryptolink {
  18. /// @brief Botan implementation of HMAC. Each method is the counterpart
  19. /// of the HMAC corresponding method.
  20. class HMACImpl {
  21. public:
  22. /// @brief Constructor from a secret and a hash algorithm
  23. ///
  24. /// See constructor of the @ref isc::cryptolink::HMAC class for details.
  25. ///
  26. /// @param secret The secret to sign with
  27. /// @param secret_len The length of the secret
  28. /// @param hash_algorithm The hash algorithm
  29. explicit HMACImpl(const void* secret, size_t secret_len,
  30. const HashAlgorithm hash_algorithm)
  31. : hash_algorithm_(hash_algorithm), hmac_() {
  32. Botan::HashFunction* hash;
  33. try {
  34. hash = Botan::get_hash(btn::getHashAlgorithmName(hash_algorithm));
  35. } catch (const Botan::Algorithm_Not_Found&) {
  36. isc_throw(UnsupportedAlgorithm,
  37. "Unknown hash algorithm: " <<
  38. static_cast<int>(hash_algorithm));
  39. } catch (const Botan::Exception& exc) {
  40. isc_throw(LibraryError, exc.what());
  41. }
  42. hmac_.reset(new Botan::HMAC(hash));
  43. // If the key length is larger than the block size, we hash the
  44. // key itself first.
  45. try {
  46. // use a temp var so we don't have blocks spanning
  47. // preprocessor directives
  48. #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
  49. size_t block_length = hash->hash_block_size();
  50. #elif BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,8,0)
  51. size_t block_length = hash->HASH_BLOCK_SIZE;
  52. #else
  53. #error "Unsupported Botan version (need 1.8 or higher)"
  54. // added to suppress irrelevant compiler errors
  55. size_t block_length = 0;
  56. #endif
  57. if (secret_len > block_length) {
  58. Botan::SecureVector<Botan::byte> hashed_key =
  59. hash->process(static_cast<const Botan::byte*>(secret),
  60. secret_len);
  61. hmac_->set_key(hashed_key.begin(), hashed_key.size());
  62. } else {
  63. // Botan 1.8 considers len 0 a bad key. 1.9 does not,
  64. // but we won't accept it anyway, and fail early
  65. if (secret_len == 0) {
  66. isc_throw(BadKey, "Bad HMAC secret length: 0");
  67. }
  68. hmac_->set_key(static_cast<const Botan::byte*>(secret),
  69. secret_len);
  70. }
  71. } catch (const Botan::Invalid_Key_Length& ikl) {
  72. isc_throw(BadKey, ikl.what());
  73. } catch (const Botan::Exception& exc) {
  74. isc_throw(LibraryError, exc.what());
  75. }
  76. }
  77. /// @brief Destructor
  78. ~HMACImpl() {
  79. }
  80. /// @brief Returns the HashAlgorithm of the object
  81. HashAlgorithm getHashAlgorithm() const {
  82. return (hash_algorithm_);
  83. }
  84. /// @brief Returns the output size of the digest
  85. ///
  86. /// @return output size of the digest
  87. size_t getOutputLength() const {
  88. #if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,0)
  89. return (hmac_->output_length());
  90. #elif BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,8,0)
  91. return (hmac_->OUTPUT_LENGTH);
  92. #else
  93. #error "Unsupported Botan version (need 1.8 or higher)"
  94. // added to suppress irrelevant compiler errors
  95. return 0;
  96. #endif
  97. }
  98. /// @brief Add data to digest
  99. ///
  100. /// See @ref isc::cryptolink::HMAC::update() for details.
  101. void update(const void* data, const size_t len) {
  102. try {
  103. hmac_->update(static_cast<const Botan::byte*>(data), len);
  104. } catch (const Botan::Exception& exc) {
  105. isc_throw(LibraryError, exc.what());
  106. }
  107. }
  108. /// @brief Calculate the final signature
  109. ///
  110. /// See @ref isc::cryptolink::HMAC::sign() for details.
  111. void sign(isc::util::OutputBuffer& result, size_t len) {
  112. try {
  113. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  114. if (len > b_result.size()) {
  115. len = b_result.size();
  116. }
  117. result.writeData(b_result.begin(), len);
  118. } catch (const Botan::Exception& exc) {
  119. isc_throw(LibraryError, exc.what());
  120. }
  121. }
  122. /// @brief Calculate the final signature
  123. ///
  124. /// See @ref isc::cryptolink::HMAC::sign() for details.
  125. void sign(void* result, size_t len) {
  126. try {
  127. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  128. size_t output_size = getOutputLength();
  129. if (output_size > len) {
  130. output_size = len;
  131. }
  132. std::memcpy(result, b_result.begin(), output_size);
  133. } catch (const Botan::Exception& exc) {
  134. isc_throw(LibraryError, exc.what());
  135. }
  136. }
  137. /// @brief Calculate the final signature
  138. ///
  139. /// See @ref isc::cryptolink::HMAC::sign() for details.
  140. std::vector<uint8_t> sign(size_t len) {
  141. try {
  142. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  143. if (len > b_result.size()) {
  144. return (std::vector<uint8_t>(b_result.begin(), b_result.end()));
  145. } else {
  146. return (std::vector<uint8_t>(b_result.begin(), &b_result[len]));
  147. }
  148. } catch (const Botan::Exception& exc) {
  149. isc_throw(LibraryError, exc.what());
  150. }
  151. }
  152. /// @brief Verify an existing signature
  153. ///
  154. /// See @ref isc::cryptolink::HMAC::verify() for details.
  155. bool verify(const void* sig, size_t len) {
  156. /// @todo Botan's verify_mac checks if len matches the output_length,
  157. /// which causes it to fail for truncated signatures, so we do
  158. /// the check ourselves
  159. try {
  160. size_t size = getOutputLength();
  161. if (len < 10 || len < size / 2) {
  162. return (false);
  163. }
  164. if (len > size) {
  165. len = size;
  166. }
  167. if (digest_.empty()) {
  168. digest_ = hmac_->final();
  169. }
  170. return (Botan::same_mem(&digest_[0],
  171. static_cast<const unsigned char*>(sig),
  172. len));
  173. } catch (const Botan::Exception& exc) {
  174. isc_throw(LibraryError, exc.what());
  175. }
  176. }
  177. private:
  178. /// @brief The hash algorithm
  179. HashAlgorithm hash_algorithm_;
  180. /// @brief The protected pointer to the Botan HMAC object
  181. boost::scoped_ptr<Botan::HMAC> hmac_;
  182. /// @brief The digest cache for multiple verify
  183. Botan::SecureVector<Botan::byte> digest_;
  184. };
  185. HMAC::HMAC(const void* secret, size_t secret_length,
  186. const HashAlgorithm hash_algorithm)
  187. {
  188. impl_ = new HMACImpl(secret, secret_length, hash_algorithm);
  189. }
  190. HMAC::~HMAC() {
  191. delete impl_;
  192. }
  193. HashAlgorithm
  194. HMAC::getHashAlgorithm() const {
  195. return (impl_->getHashAlgorithm());
  196. }
  197. size_t
  198. HMAC::getOutputLength() const {
  199. return (impl_->getOutputLength());
  200. }
  201. void
  202. HMAC::update(const void* data, const size_t len) {
  203. impl_->update(data, len);
  204. }
  205. void
  206. HMAC::sign(isc::util::OutputBuffer& result, size_t len) {
  207. impl_->sign(result, len);
  208. }
  209. void
  210. HMAC::sign(void* result, size_t len) {
  211. impl_->sign(result, len);
  212. }
  213. std::vector<uint8_t>
  214. HMAC::sign(size_t len) {
  215. return impl_->sign(len);
  216. }
  217. bool
  218. HMAC::verify(const void* sig, const size_t len) {
  219. return (impl_->verify(sig, len));
  220. }
  221. } // namespace cryptolink
  222. } // namespace isc