henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/openssldigest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/openssl.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | OpenSSLDigest::OpenSSLDigest(const std::string& algorithm) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 19 | ctx_ = EVP_MD_CTX_new(); |
| 20 | RTC_CHECK(ctx_ != nullptr); |
| 21 | EVP_MD_CTX_init(ctx_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | if (GetDigestEVP(algorithm, &md_)) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 23 | EVP_DigestInit_ex(ctx_, md_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | } else { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 25 | md_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | OpenSSLDigest::~OpenSSLDigest() { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 30 | EVP_MD_CTX_destroy(ctx_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | size_t OpenSSLDigest::Size() const { |
| 34 | if (!md_) { |
| 35 | return 0; |
| 36 | } |
| 37 | return EVP_MD_size(md_); |
| 38 | } |
| 39 | |
| 40 | void OpenSSLDigest::Update(const void* buf, size_t len) { |
| 41 | if (!md_) { |
| 42 | return; |
| 43 | } |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 44 | EVP_DigestUpdate(ctx_, buf, len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | size_t OpenSSLDigest::Finish(void* buf, size_t len) { |
| 48 | if (!md_ || len < Size()) { |
| 49 | return 0; |
| 50 | } |
| 51 | unsigned int md_len; |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 52 | EVP_DigestFinal_ex(ctx_, static_cast<unsigned char*>(buf), &md_len); |
| 53 | EVP_DigestInit_ex(ctx_, md_, nullptr); // prepare for future Update()s |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 54 | RTC_DCHECK(md_len == Size()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | return md_len; |
| 56 | } |
| 57 | |
| 58 | bool OpenSSLDigest::GetDigestEVP(const std::string& algorithm, |
| 59 | const EVP_MD** mdp) { |
| 60 | const EVP_MD* md; |
| 61 | if (algorithm == DIGEST_MD5) { |
| 62 | md = EVP_md5(); |
| 63 | } else if (algorithm == DIGEST_SHA_1) { |
| 64 | md = EVP_sha1(); |
| 65 | } else if (algorithm == DIGEST_SHA_224) { |
| 66 | md = EVP_sha224(); |
| 67 | } else if (algorithm == DIGEST_SHA_256) { |
| 68 | md = EVP_sha256(); |
| 69 | } else if (algorithm == DIGEST_SHA_384) { |
| 70 | md = EVP_sha384(); |
| 71 | } else if (algorithm == DIGEST_SHA_512) { |
| 72 | md = EVP_sha512(); |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // Can't happen |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 78 | RTC_DCHECK(EVP_MD_size(md) >= 16); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | *mdp = md; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool OpenSSLDigest::GetDigestName(const EVP_MD* md, |
| 84 | std::string* algorithm) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 85 | RTC_DCHECK(md != nullptr); |
| 86 | RTC_DCHECK(algorithm != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | |
| 88 | int md_type = EVP_MD_type(md); |
| 89 | if (md_type == NID_md5) { |
| 90 | *algorithm = DIGEST_MD5; |
| 91 | } else if (md_type == NID_sha1) { |
| 92 | *algorithm = DIGEST_SHA_1; |
| 93 | } else if (md_type == NID_sha224) { |
| 94 | *algorithm = DIGEST_SHA_224; |
| 95 | } else if (md_type == NID_sha256) { |
| 96 | *algorithm = DIGEST_SHA_256; |
| 97 | } else if (md_type == NID_sha384) { |
| 98 | *algorithm = DIGEST_SHA_384; |
| 99 | } else if (md_type == NID_sha512) { |
| 100 | *algorithm = DIGEST_SHA_512; |
| 101 | } else { |
| 102 | algorithm->clear(); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool OpenSSLDigest::GetDigestSize(const std::string& algorithm, |
| 110 | size_t* length) { |
| 111 | const EVP_MD *md; |
| 112 | if (!GetDigestEVP(algorithm, &md)) |
| 113 | return false; |
| 114 | |
| 115 | *length = EVP_MD_size(md); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | } // namespace rtc |