crypto_hmac.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright (C) 2011 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_hmac.h>
  16. #include <boost/scoped_ptr.hpp>
  17. #include <botan/botan.h>
  18. #include <botan/hmac.h>
  19. #include <botan/hash.h>
  20. #include <botan/types.h>
  21. namespace {
  22. const char*
  23. getBotanHashAlgorithmName(isc::cryptolink::HashAlgorithm algorithm) {
  24. switch (algorithm) {
  25. case isc::cryptolink::MD5:
  26. return ("MD5");
  27. break;
  28. case isc::cryptolink::SHA1:
  29. return ("SHA-1");
  30. break;
  31. case isc::cryptolink::SHA256:
  32. return ("SHA-256");
  33. break;
  34. case isc::cryptolink::UNKNOWN_HASH:
  35. return ("Unknown");
  36. break;
  37. }
  38. // compiler should have prevented us to reach this, since we have
  39. // no default. But we need a return value anyway
  40. return ("Unknown");
  41. }
  42. } // local namespace
  43. namespace isc {
  44. namespace cryptolink {
  45. class HMACImpl {
  46. public:
  47. explicit HMACImpl(const void* secret, size_t secret_len,
  48. const HashAlgorithm hash_algorithm) {
  49. Botan::HashFunction* hash;
  50. try {
  51. hash = Botan::get_hash(
  52. getBotanHashAlgorithmName(hash_algorithm));
  53. } catch (const Botan::Algorithm_Not_Found&) {
  54. isc_throw(isc::cryptolink::UnsupportedAlgorithm,
  55. "Unknown hash algorithm: " + hash_algorithm);
  56. } catch (const Botan::Exception& exc) {
  57. isc_throw(isc::cryptolink::LibraryError, exc.what());
  58. }
  59. hmac_.reset(new Botan::HMAC::HMAC(hash));
  60. // If the key length is larger than the block size, we hash the
  61. // key itself first.
  62. try {
  63. if (secret_len > hash->HASH_BLOCK_SIZE) {
  64. Botan::SecureVector<Botan::byte> hashed_key =
  65. hash->process(static_cast<const Botan::byte*>(secret),
  66. secret_len);
  67. hmac_->set_key(hashed_key.begin(), hashed_key.size());
  68. } else {
  69. hmac_->set_key(static_cast<const Botan::byte*>(secret),
  70. secret_len);
  71. }
  72. } catch (const Botan::Invalid_Key_Length& ikl) {
  73. isc_throw(BadKey, ikl.what());
  74. } catch (const Botan::Exception& exc) {
  75. isc_throw(isc::cryptolink::LibraryError, exc.what());
  76. }
  77. }
  78. ~HMACImpl() { }
  79. size_t getOutputLength() const {
  80. return (hmac_->OUTPUT_LENGTH);
  81. }
  82. void update(const void* data, const size_t len) {
  83. try {
  84. hmac_->update(static_cast<const Botan::byte*>(data), len);
  85. } catch (const Botan::Exception& exc) {
  86. isc_throw(isc::cryptolink::LibraryError, exc.what());
  87. }
  88. }
  89. void sign(isc::util::OutputBuffer& result, size_t len) {
  90. try {
  91. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  92. if (len == 0 || len > b_result.size()) {
  93. len = b_result.size();
  94. }
  95. result.writeData(b_result.begin(), len);
  96. } catch (const Botan::Exception& exc) {
  97. isc_throw(isc::cryptolink::LibraryError, exc.what());
  98. }
  99. }
  100. void sign(void* result, size_t len) {
  101. try {
  102. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  103. size_t output_size = getOutputLength();
  104. if (output_size > len) {
  105. output_size = len;
  106. }
  107. memcpy(result, b_result.begin(), output_size);
  108. } catch (const Botan::Exception& exc) {
  109. isc_throw(isc::cryptolink::LibraryError, exc.what());
  110. }
  111. }
  112. std::vector<uint8_t> sign(size_t len) {
  113. try {
  114. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  115. if (len == 0 || len > b_result.size()) {
  116. return (std::vector<uint8_t>(b_result.begin(), b_result.end()));
  117. } else {
  118. return (std::vector<uint8_t>(b_result.begin(), &b_result[len]));
  119. }
  120. } catch (const Botan::Exception& exc) {
  121. isc_throw(isc::cryptolink::LibraryError, exc.what());
  122. }
  123. }
  124. bool verify(const void* sig, size_t len) {
  125. // Botan's verify_mac checks if len matches the output_length,
  126. // which causes it to fail for truncated signatures, so we do
  127. // the check ourselves
  128. try {
  129. Botan::SecureVector<Botan::byte> our_mac = hmac_->final();
  130. if (len == 0 || len > getOutputLength()) {
  131. len = getOutputLength();
  132. }
  133. return (Botan::same_mem(&our_mac[0],
  134. static_cast<const unsigned char*>(sig),
  135. len));
  136. } catch (const Botan::Exception& exc) {
  137. isc_throw(isc::cryptolink::LibraryError, exc.what());
  138. }
  139. }
  140. private:
  141. boost::scoped_ptr<Botan::HMAC> hmac_;
  142. };
  143. HMAC::HMAC(const void* secret, size_t secret_length,
  144. const HashAlgorithm hash_algorithm)
  145. {
  146. impl_ = new HMACImpl(secret, secret_length, hash_algorithm);
  147. }
  148. HMAC::~HMAC() {
  149. delete impl_;
  150. }
  151. size_t
  152. HMAC::getOutputLength() const {
  153. return (impl_->getOutputLength());
  154. }
  155. void
  156. HMAC::update(const void* data, const size_t len) {
  157. impl_->update(data, len);
  158. }
  159. void
  160. HMAC::sign(isc::util::OutputBuffer& result, size_t len) {
  161. impl_->sign(result, len);
  162. }
  163. void
  164. HMAC::sign(void* result, size_t len) {
  165. impl_->sign(result, len);
  166. }
  167. std::vector<uint8_t>
  168. HMAC::sign(size_t len) {
  169. return impl_->sign(len);
  170. }
  171. bool
  172. HMAC::verify(const void* sig, const size_t len) {
  173. return (impl_->verify(sig, len));
  174. }
  175. void
  176. signHMAC(const void* data, size_t data_len, const void* secret,
  177. size_t secret_len, const HashAlgorithm hash_algorithm,
  178. isc::util::OutputBuffer& result, size_t len)
  179. {
  180. boost::scoped_ptr<HMAC> hmac(
  181. CryptoLink::getCryptoLink().createHMAC(secret,
  182. secret_len,
  183. hash_algorithm));
  184. hmac->update(data, data_len);
  185. hmac->sign(result, len);
  186. }
  187. bool
  188. verifyHMAC(const void* data, const size_t data_len, const void* secret,
  189. size_t secret_len, const HashAlgorithm hash_algorithm,
  190. const void* sig, const size_t sig_len)
  191. {
  192. boost::scoped_ptr<HMAC> hmac(
  193. CryptoLink::getCryptoLink().createHMAC(secret,
  194. secret_len,
  195. hash_algorithm));
  196. hmac->update(data, data_len);
  197. return (hmac->verify(sig, sig_len));
  198. }
  199. void
  200. deleteHMAC(HMAC* hmac) {
  201. delete hmac;
  202. }
  203. } // namespace cryptolink
  204. } // namespace isc