crypto_hmac.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <config.h>
  15. #include <cryptolink.h>
  16. #include <cryptolink/crypto_hmac.h>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <botan/botan.h>
  19. #include <botan/hmac.h>
  20. #include <botan/hash.h>
  21. #include <botan/types.h>
  22. // [XX] remove
  23. #include <iostream>
  24. namespace {
  25. const char*
  26. getBotanHashAlgorithmName(isc::cryptolink::HashAlgorithm algorithm) {
  27. switch (algorithm) {
  28. case isc::cryptolink::MD5:
  29. return ("MD5");
  30. break;
  31. case isc::cryptolink::SHA1:
  32. return ("SHA-1");
  33. break;
  34. case isc::cryptolink::SHA256:
  35. return ("SHA-256");
  36. break;
  37. case isc::cryptolink::UNKNOWN_HASH:
  38. return ("Unknown");
  39. break;
  40. }
  41. // compiler should have prevented us to reach this, since we have
  42. // no default. But we need a return value anyway
  43. return ("Unknown");
  44. }
  45. } // local namespace
  46. namespace isc {
  47. namespace cryptolink {
  48. class HMACImpl {
  49. public:
  50. explicit HMACImpl(const void* secret, size_t secret_len,
  51. const HashAlgorithm hash_algorithm) {
  52. Botan::HashFunction* hash;
  53. try {
  54. hash = Botan::get_hash(
  55. getBotanHashAlgorithmName(hash_algorithm));
  56. } catch (const Botan::Algorithm_Not_Found&) {
  57. isc_throw(isc::cryptolink::UnsupportedAlgorithm,
  58. "Unknown hash algorithm: " + hash_algorithm);
  59. } catch (const Botan::Exception& exc) {
  60. isc_throw(isc::cryptolink::LibraryError, exc.what());
  61. }
  62. hmac_.reset(new Botan::HMAC(hash));
  63. // If the key length is larger than the block size, we hash the
  64. // key itself first.
  65. try {
  66. // use a temp var so we don't have blocks spanning
  67. // preprocessor directives
  68. size_t block_length;
  69. #if (BOTAN_API_VERSION >= 100900)
  70. block_length = hash->hash_block_size();
  71. #elif (BOTAN_API_VERSION >= 100800)
  72. block_length = hash->HASH_BLOCK_SIZE;
  73. #else
  74. #error "Unsupported BOTAN_API_VERSION"
  75. // added to suppress irrelevant compiler errors
  76. block_length = 0;
  77. #endif
  78. if (secret_len > block_length) {
  79. Botan::SecureVector<Botan::byte> hashed_key =
  80. hash->process(static_cast<const Botan::byte*>(secret),
  81. secret_len);
  82. hmac_->set_key(hashed_key.begin(), hashed_key.size());
  83. } else {
  84. // Apparently 1.9 considers 0 a valid secret length.
  85. // We do not.
  86. #if (BOTAN_API_VERSION >= 100900)
  87. if (secret_len == 0) {
  88. isc_throw(BadKey, "Bad HMAC secret length: 0");
  89. }
  90. #endif
  91. hmac_->set_key(static_cast<const Botan::byte*>(secret),
  92. secret_len);
  93. }
  94. } catch (const Botan::Invalid_Key_Length& ikl) {
  95. isc_throw(BadKey, ikl.what());
  96. } catch (const Botan::Exception& exc) {
  97. isc_throw(isc::cryptolink::LibraryError, exc.what());
  98. }
  99. }
  100. ~HMACImpl() { }
  101. size_t getOutputLength() const {
  102. #if (BOTAN_API_VERSION >= 100900)
  103. return (hmac_->output_length());
  104. #elif (BOTAN_API_VERSION >= 100800)
  105. return (hmac_->OUTPUT_LENGTH);
  106. #else
  107. #error "Unsupported BOTAN_API_VERSION"
  108. // added to suppress irrelevant compiler errors
  109. return 0;
  110. #endif
  111. }
  112. void update(const void* data, const size_t len) {
  113. try {
  114. hmac_->update(static_cast<const Botan::byte*>(data), len);
  115. } catch (const Botan::Exception& exc) {
  116. isc_throw(isc::cryptolink::LibraryError, exc.what());
  117. }
  118. }
  119. void sign(isc::util::OutputBuffer& result, size_t len) {
  120. try {
  121. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  122. if (len == 0 || len > b_result.size()) {
  123. len = b_result.size();
  124. }
  125. result.writeData(b_result.begin(), len);
  126. } catch (const Botan::Exception& exc) {
  127. isc_throw(isc::cryptolink::LibraryError, exc.what());
  128. }
  129. }
  130. void sign(void* result, size_t len) {
  131. try {
  132. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  133. size_t output_size = getOutputLength();
  134. if (output_size > len) {
  135. output_size = len;
  136. }
  137. memcpy(result, b_result.begin(), output_size);
  138. } catch (const Botan::Exception& exc) {
  139. isc_throw(isc::cryptolink::LibraryError, exc.what());
  140. }
  141. }
  142. std::vector<uint8_t> sign(size_t len) {
  143. try {
  144. Botan::SecureVector<Botan::byte> b_result(hmac_->final());
  145. if (len == 0 || len > b_result.size()) {
  146. return (std::vector<uint8_t>(b_result.begin(), b_result.end()));
  147. } else {
  148. return (std::vector<uint8_t>(b_result.begin(), &b_result[len]));
  149. }
  150. } catch (const Botan::Exception& exc) {
  151. isc_throw(isc::cryptolink::LibraryError, exc.what());
  152. }
  153. }
  154. bool verify(const void* sig, size_t len) {
  155. // Botan's verify_mac checks if len matches the output_length,
  156. // which causes it to fail for truncated signatures, so we do
  157. // the check ourselves
  158. // SEE BELOW FOR TEMPORARY CHANGE
  159. try {
  160. Botan::SecureVector<Botan::byte> our_mac = hmac_->final();
  161. if (len < getOutputLength()) {
  162. // Currently we don't support truncated signature. To avoid
  163. // validating too short signature accidently, we enforce the
  164. // standard signature size for the moment.
  165. // Once we support truncation correctly, this if-clause should
  166. // (and the capitalized comment above) be removed.
  167. return (false);
  168. }
  169. if (len == 0 || len > getOutputLength()) {
  170. len = getOutputLength();
  171. }
  172. return (Botan::same_mem(&our_mac[0],
  173. static_cast<const unsigned char*>(sig),
  174. len));
  175. } catch (const Botan::Exception& exc) {
  176. isc_throw(isc::cryptolink::LibraryError, exc.what());
  177. }
  178. }
  179. private:
  180. boost::scoped_ptr<Botan::HMAC> hmac_;
  181. };
  182. HMAC::HMAC(const void* secret, size_t secret_length,
  183. const HashAlgorithm hash_algorithm)
  184. {
  185. impl_ = new HMACImpl(secret, secret_length, hash_algorithm);
  186. }
  187. HMAC::~HMAC() {
  188. delete impl_;
  189. }
  190. size_t
  191. HMAC::getOutputLength() const {
  192. return (impl_->getOutputLength());
  193. }
  194. void
  195. HMAC::update(const void* data, const size_t len) {
  196. impl_->update(data, len);
  197. }
  198. void
  199. HMAC::sign(isc::util::OutputBuffer& result, size_t len) {
  200. impl_->sign(result, len);
  201. }
  202. void
  203. HMAC::sign(void* result, size_t len) {
  204. impl_->sign(result, len);
  205. }
  206. std::vector<uint8_t>
  207. HMAC::sign(size_t len) {
  208. return impl_->sign(len);
  209. }
  210. bool
  211. HMAC::verify(const void* sig, const size_t len) {
  212. return (impl_->verify(sig, len));
  213. }
  214. void
  215. signHMAC(const void* data, size_t data_len, const void* secret,
  216. size_t secret_len, const HashAlgorithm hash_algorithm,
  217. isc::util::OutputBuffer& result, size_t len)
  218. {
  219. boost::scoped_ptr<HMAC> hmac(
  220. CryptoLink::getCryptoLink().createHMAC(secret,
  221. secret_len,
  222. hash_algorithm));
  223. hmac->update(data, data_len);
  224. hmac->sign(result, len);
  225. }
  226. bool
  227. verifyHMAC(const void* data, const size_t data_len, const void* secret,
  228. size_t secret_len, const HashAlgorithm hash_algorithm,
  229. const void* sig, const size_t sig_len)
  230. {
  231. boost::scoped_ptr<HMAC> hmac(
  232. CryptoLink::getCryptoLink().createHMAC(secret,
  233. secret_len,
  234. hash_algorithm));
  235. hmac->update(data, data_len);
  236. return (hmac->verify(sig, sig_len));
  237. }
  238. void
  239. deleteHMAC(HMAC* hmac) {
  240. delete hmac;
  241. }
  242. } // namespace cryptolink
  243. } // namespace isc