crypto_hmac.cc 7.4 KB

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