blob: 1cf5bc09b4c55e4ae25d675a31cc844cb7726c8b [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include "rtc_base/checks.h" // RTC_DCHECK, RTC_CHECK
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/openssl.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
16namespace rtc {
17
18OpenSSLDigest::OpenSSLDigest(const std::string& algorithm) {
Jiawei Oueb0df082018-02-02 14:51:18 -080019 ctx_ = EVP_MD_CTX_new();
20 RTC_CHECK(ctx_ != nullptr);
21 EVP_MD_CTX_init(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 if (GetDigestEVP(algorithm, &md_)) {
Jiawei Oueb0df082018-02-02 14:51:18 -080023 EVP_DigestInit_ex(ctx_, md_, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 } else {
deadbeef37f5ecf2017-02-27 14:06:41 -080025 md_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026 }
27}
28
29OpenSSLDigest::~OpenSSLDigest() {
Jiawei Oueb0df082018-02-02 14:51:18 -080030 EVP_MD_CTX_destroy(ctx_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031}
32
33size_t OpenSSLDigest::Size() const {
34 if (!md_) {
35 return 0;
36 }
37 return EVP_MD_size(md_);
38}
39
40void OpenSSLDigest::Update(const void* buf, size_t len) {
41 if (!md_) {
42 return;
43 }
Jiawei Oueb0df082018-02-02 14:51:18 -080044 EVP_DigestUpdate(ctx_, buf, len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045}
46
47size_t OpenSSLDigest::Finish(void* buf, size_t len) {
48 if (!md_ || len < Size()) {
49 return 0;
50 }
51 unsigned int md_len;
Jiawei Oueb0df082018-02-02 14:51:18 -080052 EVP_DigestFinal_ex(ctx_, static_cast<unsigned char*>(buf), &md_len);
53 EVP_DigestInit_ex(ctx_, md_, nullptr); // prepare for future Update()s
nisseede5da42017-01-12 05:15:36 -080054 RTC_DCHECK(md_len == Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 return md_len;
56}
57
58bool 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
nisseede5da42017-01-12 05:15:36 -080078 RTC_DCHECK(EVP_MD_size(md) >= 16);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 *mdp = md;
80 return true;
81}
82
Yves Gerey665174f2018-06-19 15:03:05 +020083bool OpenSSLDigest::GetDigestName(const EVP_MD* md, std::string* algorithm) {
deadbeef37f5ecf2017-02-27 14:06:41 -080084 RTC_DCHECK(md != nullptr);
85 RTC_DCHECK(algorithm != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
87 int md_type = EVP_MD_type(md);
88 if (md_type == NID_md5) {
89 *algorithm = DIGEST_MD5;
90 } else if (md_type == NID_sha1) {
91 *algorithm = DIGEST_SHA_1;
92 } else if (md_type == NID_sha224) {
93 *algorithm = DIGEST_SHA_224;
94 } else if (md_type == NID_sha256) {
95 *algorithm = DIGEST_SHA_256;
96 } else if (md_type == NID_sha384) {
97 *algorithm = DIGEST_SHA_384;
98 } else if (md_type == NID_sha512) {
99 *algorithm = DIGEST_SHA_512;
100 } else {
101 algorithm->clear();
102 return false;
103 }
104
105 return true;
106}
107
108bool OpenSSLDigest::GetDigestSize(const std::string& algorithm,
109 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