LibsslMessageDigestImpl.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 OPENSSL_VERSION_NUMBER < 0x10100001L
  40. namespace {
  41. EVP_MD_CTX* EVP_MD_CTX_new() {
  42. return EVP_MD_CTX_create();
  43. }
  44. } // namespace
  45. namespace {
  46. void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
  47. EVP_MD_CTX_destroy(ctx);
  48. }
  49. } // namespace
  50. namespace {
  51. int EVP_MD_CTX_reset(EVP_MD_CTX* ctx) {
  52. EVP_MD_CTX_init(ctx);
  53. return 1;
  54. }
  55. } // namespace
  56. #endif // OPENSSL_VERSION_NUMBER < 0x10100001L
  57. template<const EVP_MD* (*init_fn)()>
  58. class MessageDigestBase : public MessageDigestImpl {
  59. public:
  60. MessageDigestBase()
  61. : ctx_(EVP_MD_CTX_new()), md_(init_fn()), len_(EVP_MD_size(md_)) {
  62. EVP_MD_CTX_reset(ctx_);
  63. reset();
  64. }
  65. virtual ~MessageDigestBase() {
  66. EVP_MD_CTX_free(ctx_);
  67. }
  68. static size_t length() {
  69. return EVP_MD_size(init_fn());
  70. }
  71. virtual size_t getDigestLength() const CXX11_OVERRIDE {
  72. return len_;
  73. }
  74. virtual void reset() CXX11_OVERRIDE {
  75. EVP_DigestInit_ex(ctx_, md_, nullptr);
  76. }
  77. virtual void update(const void* data, size_t length) CXX11_OVERRIDE {
  78. auto bytes = reinterpret_cast<const char*>(data);
  79. while (length) {
  80. size_t l = std::min(length, (size_t)std::numeric_limits<uint32_t>::max());
  81. EVP_DigestUpdate(ctx_, bytes, l);
  82. length -= l;
  83. bytes += l;
  84. }
  85. }
  86. virtual void digest(unsigned char* md) CXX11_OVERRIDE {
  87. unsigned int len;
  88. EVP_DigestFinal_ex(ctx_, md, &len);
  89. }
  90. private:
  91. EVP_MD_CTX *ctx_;
  92. const EVP_MD* md_;
  93. const size_t len_;
  94. };
  95. typedef MessageDigestBase<EVP_md5> MessageDigestMD5;
  96. typedef MessageDigestBase<EVP_sha1> MessageDigestSHA1;
  97. std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
  98. {
  99. return make_unique<MessageDigestSHA1>();
  100. }
  101. MessageDigestImpl::hashes_t MessageDigestImpl::hashes = {
  102. { "sha-1", make_hi<MessageDigestSHA1>() },
  103. #ifdef HAVE_EVP_SHA224
  104. { "sha-224", make_hi<MessageDigestBase<EVP_sha224> >() },
  105. #endif
  106. #ifdef HAVE_EVP_SHA224
  107. { "sha-256", make_hi<MessageDigestBase<EVP_sha256> >() },
  108. #endif
  109. #ifdef HAVE_EVP_SHA224
  110. { "sha-384", make_hi<MessageDigestBase<EVP_sha384> >() },
  111. #endif
  112. #ifdef HAVE_EVP_SHA224
  113. { "sha-512", make_hi<MessageDigestBase<EVP_sha512> >() },
  114. #endif
  115. { "md5", make_hi<MessageDigestMD5>() },
  116. ADLER32_MESSAGE_DIGEST
  117. };
  118. } // namespace aria2