LibsslMessageDigestImpl.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MessageDigestImpl.h"
  36. #include <openssl/evp.h>
  37. #include "Adler32MessageDigestImpl.h"
  38. namespace aria2 {
  39. #if defined(LIBRESSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100001L
  40. namespace {
  41. EVP_MD_CTX* EVP_MD_CTX_new() { return EVP_MD_CTX_create(); }
  42. } // namespace
  43. namespace {
  44. void EVP_MD_CTX_free(EVP_MD_CTX* ctx) { EVP_MD_CTX_destroy(ctx); }
  45. } // namespace
  46. namespace {
  47. int EVP_MD_CTX_reset(EVP_MD_CTX* ctx)
  48. {
  49. EVP_MD_CTX_init(ctx);
  50. return 1;
  51. }
  52. } // namespace
  53. #endif // defined(LIBRESSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER <
  54. // 0x10100001L
  55. template <const EVP_MD* (*init_fn)()>
  56. class MessageDigestBase : public MessageDigestImpl {
  57. public:
  58. MessageDigestBase()
  59. : ctx_(EVP_MD_CTX_new()), md_(init_fn()), len_(EVP_MD_size(md_))
  60. {
  61. EVP_MD_CTX_reset(ctx_);
  62. reset();
  63. }
  64. virtual ~MessageDigestBase() { EVP_MD_CTX_free(ctx_); }
  65. static size_t length() { return EVP_MD_size(init_fn()); }
  66. virtual size_t getDigestLength() const CXX11_OVERRIDE { return len_; }
  67. virtual void reset() CXX11_OVERRIDE { EVP_DigestInit_ex(ctx_, md_, nullptr); }
  68. virtual void update(const void* data, size_t length) CXX11_OVERRIDE
  69. {
  70. auto bytes = reinterpret_cast<const char*>(data);
  71. while (length) {
  72. size_t l = std::min(length, (size_t)std::numeric_limits<uint32_t>::max());
  73. EVP_DigestUpdate(ctx_, bytes, l);
  74. length -= l;
  75. bytes += l;
  76. }
  77. }
  78. virtual void digest(unsigned char* md) CXX11_OVERRIDE
  79. {
  80. unsigned int len;
  81. EVP_DigestFinal_ex(ctx_, md, &len);
  82. }
  83. private:
  84. EVP_MD_CTX* ctx_;
  85. const EVP_MD* md_;
  86. const size_t len_;
  87. };
  88. typedef MessageDigestBase<EVP_md5> MessageDigestMD5;
  89. typedef MessageDigestBase<EVP_sha1> MessageDigestSHA1;
  90. std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
  91. {
  92. return make_unique<MessageDigestSHA1>();
  93. }
  94. MessageDigestImpl::hashes_t MessageDigestImpl::hashes = {
  95. {"sha-1", make_hi<MessageDigestSHA1>()},
  96. #ifdef HAVE_EVP_SHA224
  97. {"sha-224", make_hi<MessageDigestBase<EVP_sha224>>()},
  98. #endif
  99. #ifdef HAVE_EVP_SHA224
  100. {"sha-256", make_hi<MessageDigestBase<EVP_sha256>>()},
  101. #endif
  102. #ifdef HAVE_EVP_SHA224
  103. {"sha-384", make_hi<MessageDigestBase<EVP_sha384>>()},
  104. #endif
  105. #ifdef HAVE_EVP_SHA224
  106. {"sha-512", make_hi<MessageDigestBase<EVP_sha512>>()},
  107. #endif
  108. {"md5", make_hi<MessageDigestMD5>()},
  109. ADLER32_MESSAGE_DIGEST};
  110. } // namespace aria2