blob: bbf39570f6062821a64f09ec69f1f8f0baa62fa0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/openssl_digest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Ali Tofigh7fa90572022-03-17 15:47:49 +010013#include "absl/strings/string_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020014#include "rtc_base/checks.h" // RTC_DCHECK, RTC_CHECK
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/openssl.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17namespace rtc {
18
Ali Tofigh7fa90572022-03-17 15:47:49 +010019OpenSSLDigest::OpenSSLDigest(absl::string_view algorithm) {
Jiawei Oueb0df082018-02-02 14:51:18 -080020 ctx_ = EVP_MD_CTX_new();
21 RTC_CHECK(ctx_ != nullptr);
22 EVP_MD_CTX_init(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023 if (GetDigestEVP(algorithm, &md_)) {
Jiawei Oueb0df082018-02-02 14:51:18 -080024 EVP_DigestInit_ex(ctx_, md_, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 } else {
deadbeef37f5ecf2017-02-27 14:06:41 -080026 md_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027 }
28}
29
30OpenSSLDigest::~OpenSSLDigest() {
Jiawei Oueb0df082018-02-02 14:51:18 -080031 EVP_MD_CTX_destroy(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032}
33
34size_t OpenSSLDigest::Size() const {
35 if (!md_) {
36 return 0;
37 }
38 return EVP_MD_size(md_);
39}
40
41void OpenSSLDigest::Update(const void* buf, size_t len) {
42 if (!md_) {
43 return;
44 }
Jiawei Oueb0df082018-02-02 14:51:18 -080045 EVP_DigestUpdate(ctx_, buf, len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046}
47
48size_t OpenSSLDigest::Finish(void* buf, size_t len) {
49 if (!md_ || len < Size()) {
50 return 0;
51 }
52 unsigned int md_len;
Jiawei Oueb0df082018-02-02 14:51:18 -080053 EVP_DigestFinal_ex(ctx_, static_cast<unsigned char*>(buf), &md_len);
54 EVP_DigestInit_ex(ctx_, md_, nullptr); // prepare for future Update()s
nisseede5da42017-01-12 05:15:36 -080055 RTC_DCHECK(md_len == Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 return md_len;
57}
58
Ali Tofigh7fa90572022-03-17 15:47:49 +010059bool OpenSSLDigest::GetDigestEVP(absl::string_view algorithm,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 const EVP_MD** mdp) {
61 const EVP_MD* md;
62 if (algorithm == DIGEST_MD5) {
63 md = EVP_md5();
64 } else if (algorithm == DIGEST_SHA_1) {
65 md = EVP_sha1();
66 } else if (algorithm == DIGEST_SHA_224) {
67 md = EVP_sha224();
68 } else if (algorithm == DIGEST_SHA_256) {
69 md = EVP_sha256();
70 } else if (algorithm == DIGEST_SHA_384) {
71 md = EVP_sha384();
72 } else if (algorithm == DIGEST_SHA_512) {
73 md = EVP_sha512();
74 } else {
75 return false;
76 }
77
78 // Can't happen
nisseede5da42017-01-12 05:15:36 -080079 RTC_DCHECK(EVP_MD_size(md) >= 16);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 *mdp = md;
81 return true;
82}
83
Yves Gerey665174f2018-06-19 15:03:05 +020084bool OpenSSLDigest::GetDigestName(const EVP_MD* md, std::string* algorithm) {
deadbeef37f5ecf2017-02-27 14:06:41 -080085 RTC_DCHECK(md != nullptr);
86 RTC_DCHECK(algorithm != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
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
Ali Tofigh7fa90572022-03-17 15:47:49 +0100109bool OpenSSLDigest::GetDigestSize(absl::string_view algorithm, size_t* length) {
Yves Gerey665174f2018-06-19 15:03:05 +0200110 const EVP_MD* md;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 if (!GetDigestEVP(algorithm, &md))
112 return false;
113
114 *length = EVP_MD_size(md);
115 return true;
116}
117
118} // namespace rtc