You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/openssl_digest.h"
  11. #include "rtc_base/checks.h" // RTC_DCHECK, RTC_CHECK
  12. #include "rtc_base/openssl.h"
  13. namespace rtc {
  14. OpenSSLDigest::OpenSSLDigest(const std::string& algorithm) {
  15. ctx_ = EVP_MD_CTX_new();
  16. RTC_CHECK(ctx_ != nullptr);
  17. EVP_MD_CTX_init(ctx_);
  18. if (GetDigestEVP(algorithm, &md_)) {
  19. EVP_DigestInit_ex(ctx_, md_, nullptr);
  20. } else {
  21. md_ = nullptr;
  22. }
  23. }
  24. OpenSSLDigest::~OpenSSLDigest() {
  25. EVP_MD_CTX_destroy(ctx_);
  26. }
  27. size_t OpenSSLDigest::Size() const {
  28. if (!md_) {
  29. return 0;
  30. }
  31. return EVP_MD_size(md_);
  32. }
  33. void OpenSSLDigest::Update(const void* buf, size_t len) {
  34. if (!md_) {
  35. return;
  36. }
  37. EVP_DigestUpdate(ctx_, buf, len);
  38. }
  39. size_t OpenSSLDigest::Finish(void* buf, size_t len) {
  40. if (!md_ || len < Size()) {
  41. return 0;
  42. }
  43. unsigned int md_len;
  44. EVP_DigestFinal_ex(ctx_, static_cast<unsigned char*>(buf), &md_len);
  45. EVP_DigestInit_ex(ctx_, md_, nullptr); // prepare for future Update()s
  46. RTC_DCHECK(md_len == Size());
  47. return md_len;
  48. }
  49. bool OpenSSLDigest::GetDigestEVP(const std::string& algorithm,
  50. const EVP_MD** mdp) {
  51. const EVP_MD* md;
  52. if (algorithm == DIGEST_MD5) {
  53. md = EVP_md5();
  54. } else if (algorithm == DIGEST_SHA_1) {
  55. md = EVP_sha1();
  56. } else if (algorithm == DIGEST_SHA_224) {
  57. md = EVP_sha224();
  58. } else if (algorithm == DIGEST_SHA_256) {
  59. md = EVP_sha256();
  60. } else if (algorithm == DIGEST_SHA_384) {
  61. md = EVP_sha384();
  62. } else if (algorithm == DIGEST_SHA_512) {
  63. md = EVP_sha512();
  64. } else {
  65. return false;
  66. }
  67. // Can't happen
  68. RTC_DCHECK(EVP_MD_size(md) >= 16);
  69. *mdp = md;
  70. return true;
  71. }
  72. bool OpenSSLDigest::GetDigestName(const EVP_MD* md, std::string* algorithm) {
  73. RTC_DCHECK(md != nullptr);
  74. RTC_DCHECK(algorithm != nullptr);
  75. int md_type = EVP_MD_type(md);
  76. if (md_type == NID_md5) {
  77. *algorithm = DIGEST_MD5;
  78. } else if (md_type == NID_sha1) {
  79. *algorithm = DIGEST_SHA_1;
  80. } else if (md_type == NID_sha224) {
  81. *algorithm = DIGEST_SHA_224;
  82. } else if (md_type == NID_sha256) {
  83. *algorithm = DIGEST_SHA_256;
  84. } else if (md_type == NID_sha384) {
  85. *algorithm = DIGEST_SHA_384;
  86. } else if (md_type == NID_sha512) {
  87. *algorithm = DIGEST_SHA_512;
  88. } else {
  89. algorithm->clear();
  90. return false;
  91. }
  92. return true;
  93. }
  94. bool OpenSSLDigest::GetDigestSize(const std::string& algorithm,
  95. size_t* length) {
  96. const EVP_MD* md;
  97. if (!GetDigestEVP(algorithm, &md))
  98. return false;
  99. *length = EVP_MD_size(md);
  100. return true;
  101. }
  102. } // namespace rtc