crypto_hmac.cc 9.1 KB

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