blob: bbf39570f6062821a64f09ec69f1f8f0baa62fa0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#include "rtc_base/openssl_digest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
Ali Tofigh7fa90572022-03-17 14:47:4913#include "absl/strings/string_view.h"
Yves Gerey988cc082018-10-23 10:03:0114#include "rtc_base/checks.h" // RTC_DCHECK, RTC_CHECK
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/openssl.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2616
17namespace rtc {
18
Ali Tofigh7fa90572022-03-17 14:47:4919OpenSSLDigest::OpenSSLDigest(absl::string_view algorithm) {
Jiawei Oueb0df082018-02-02 22:51:1820 ctx_ = EVP_MD_CTX_new();
21 RTC_CHECK(ctx_ != nullptr);
22 EVP_MD_CTX_init(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:2623 if (GetDigestEVP(algorithm, &md_)) {
Jiawei Oueb0df082018-02-02 22:51:1824 EVP_DigestInit_ex(ctx_, md_, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2625 } else {
deadbeef37f5ecf2017-02-27 22:06:4126 md_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2627 }
28}
29
30OpenSSLDigest::~OpenSSLDigest() {
Jiawei Oueb0df082018-02-02 22:51:1831 EVP_MD_CTX_destroy(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:2632}
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 22:51:1845 EVP_DigestUpdate(ctx_, buf, len);
henrike@webrtc.orgf0488722014-05-13 18:00:2646}
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 22:51:1853 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 13:15:3655 RTC_DCHECK(md_len == Size());
henrike@webrtc.orgf0488722014-05-13 18:00:2656 return md_len;
57}
58
Ali Tofigh7fa90572022-03-17 14:47:4959bool OpenSSLDigest::GetDigestEVP(absl::string_view algorithm,
henrike@webrtc.orgf0488722014-05-13 18:00:2660 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 13:15:3679 RTC_DCHECK(EVP_MD_size(md) >= 16);
henrike@webrtc.orgf0488722014-05-13 18:00:2680 *mdp = md;
81 return true;
82}
83
Yves Gerey665174f2018-06-19 13:03:0584bool OpenSSLDigest::GetDigestName(const EVP_MD* md, std::string* algorithm) {
deadbeef37f5ecf2017-02-27 22:06:4185 RTC_DCHECK(md != nullptr);
86 RTC_DCHECK(algorithm != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2687
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 14:47:49109bool OpenSSLDigest::GetDigestSize(absl::string_view algorithm, size_t* length) {
Yves Gerey665174f2018-06-19 13:03:05110 const EVP_MD* md;
henrike@webrtc.orgf0488722014-05-13 18:00:26111 if (!GetDigestEVP(algorithm, &md))
112 return false;
113
114 *length = EVP_MD_size(md);
115 return true;
116}
117
118} // namespace rtc