crypto_hmac.cc 7.7 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::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(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. // SEE BELOW FOR TEMPORARY CHANGE
  129. try {
  130. Botan::SecureVector<Botan::byte> our_mac = hmac_->final();
  131. if (len < getOutputLength()) {
  132. // Currently we don't support truncated signature. To avoid
  133. // validating too short signature accidently, we enforce the
  134. // standard signature size for the moment.
  135. // Once we support truncation correctly, this if-clause should
  136. // (and the capitalized comment above) be removed.
  137. len = getOutputLength();
  138. }
  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