blob: 56abcd2c7b5bfdcf85f84845871c8d479a363471 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2011 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/message_digest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
13#include <string.h>
Jonas Olssona4d87372019-07-05 17:08:3314
Yves Gerey988cc082018-10-23 10:03:0115#include <cstdint>
16#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:2617
Ali Tofigh7fa90572022-03-17 14:47:4918#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/openssl_digest.h"
20#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2621
22namespace rtc {
23
24// From RFC 4572.
Yves Gerey665174f2018-06-19 13:03:0525const char DIGEST_MD5[] = "md5";
26const char DIGEST_SHA_1[] = "sha-1";
henrike@webrtc.orgf0488722014-05-13 18:00:2627const char DIGEST_SHA_224[] = "sha-224";
28const char DIGEST_SHA_256[] = "sha-256";
29const char DIGEST_SHA_384[] = "sha-384";
30const char DIGEST_SHA_512[] = "sha-512";
31
32static const size_t kBlockSize = 64; // valid for SHA-256 and down
33
Ali Tofigh7fa90572022-03-17 14:47:4934MessageDigest* MessageDigestFactory::Create(absl::string_view alg) {
henrike@webrtc.orgf0488722014-05-13 18:00:2635 MessageDigest* digest = new OpenSSLDigest(alg);
36 if (digest->Size() == 0) { // invalid algorithm
37 delete digest;
deadbeef37f5ecf2017-02-27 22:06:4138 digest = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2639 }
40 return digest;
henrike@webrtc.orgf0488722014-05-13 18:00:2641}
42
Ali Tofigh7fa90572022-03-17 14:47:4943bool IsFips180DigestAlgorithm(absl::string_view alg) {
henrike@webrtc.orgf0488722014-05-13 18:00:2644 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5,
45 // "Self-signed certificates (for which legacy certificates are not a
46 // consideration) MUST use one of the FIPS 180 algorithms (SHA-1,
47 // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm,
48 // and thus also MUST use it to calculate certificate fingerprints."
Yves Gerey665174f2018-06-19 13:03:0549 return alg == DIGEST_SHA_1 || alg == DIGEST_SHA_224 ||
50 alg == DIGEST_SHA_256 || alg == DIGEST_SHA_384 ||
henrike@webrtc.orgf0488722014-05-13 18:00:2651 alg == DIGEST_SHA_512;
52}
53
Yves Gerey665174f2018-06-19 13:03:0554size_t ComputeDigest(MessageDigest* digest,
55 const void* input,
56 size_t in_len,
57 void* output,
58 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:2659 digest->Update(input, in_len);
60 return digest->Finish(output, out_len);
61}
62
Ali Tofigh7fa90572022-03-17 14:47:4963size_t ComputeDigest(absl::string_view alg,
Yves Gerey665174f2018-06-19 13:03:0564 const void* input,
65 size_t in_len,
66 void* output,
67 size_t out_len) {
jbauch555604a2016-04-26 10:13:2268 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
Yves Gerey665174f2018-06-19 13:03:0569 return (digest) ? ComputeDigest(digest.get(), input, in_len, output, out_len)
70 : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:2671}
72
Ali Tofigh7fa90572022-03-17 14:47:4973std::string ComputeDigest(MessageDigest* digest, absl::string_view input) {
jbauch555604a2016-04-26 10:13:2274 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 13:03:0575 ComputeDigest(digest, input.data(), input.size(), output.get(),
76 digest->Size());
Ali Tofighfd6a4d62022-03-31 08:36:4877 return hex_encode(absl::string_view(output.get(), digest->Size()));
henrike@webrtc.orgf0488722014-05-13 18:00:2678}
79
Ali Tofigh7fa90572022-03-17 14:47:4980bool ComputeDigest(absl::string_view alg,
81 absl::string_view input,
henrike@webrtc.orgf0488722014-05-13 18:00:2682 std::string* output) {
jbauch555604a2016-04-26 10:13:2283 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:2684 if (!digest) {
85 return false;
86 }
87 *output = ComputeDigest(digest.get(), input);
88 return true;
89}
90
Ali Tofigh7fa90572022-03-17 14:47:4991std::string ComputeDigest(absl::string_view alg, absl::string_view input) {
henrike@webrtc.orgf0488722014-05-13 18:00:2692 std::string output;
93 ComputeDigest(alg, input, &output);
94 return output;
95}
96
97// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
98size_t ComputeHmac(MessageDigest* digest,
Yves Gerey665174f2018-06-19 13:03:0599 const void* key,
100 size_t key_len,
101 const void* input,
102 size_t in_len,
103 void* output,
104 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26105 // We only handle algorithms with a 64-byte blocksize.
106 // TODO: Add BlockSize() method to MessageDigest.
107 size_t block_len = kBlockSize;
108 if (digest->Size() > 32) {
109 return 0;
110 }
111 // Copy the key to a block-sized buffer to simplify padding.
112 // If the key is longer than a block, hash it and use the result instead.
jbauch555604a2016-04-26 10:13:22113 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26114 if (key_len > block_len) {
115 ComputeDigest(digest, key, key_len, new_key.get(), block_len);
116 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
117 } else {
118 memcpy(new_key.get(), key, key_len);
119 memset(new_key.get() + key_len, 0, block_len - key_len);
120 }
121 // Set up the padding from the key, salting appropriately for each padding.
jbauch555604a2016-04-26 10:13:22122 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
123 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26124 for (size_t i = 0; i < block_len; ++i) {
125 o_pad[i] = 0x5c ^ new_key[i];
126 i_pad[i] = 0x36 ^ new_key[i];
127 }
128 // Inner hash; hash the inner padding, and then the input buffer.
jbauch555604a2016-04-26 10:13:22129 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26130 digest->Update(i_pad.get(), block_len);
131 digest->Update(input, in_len);
132 digest->Finish(inner.get(), digest->Size());
133 // Outer hash; hash the outer padding, and then the result of the inner hash.
134 digest->Update(o_pad.get(), block_len);
135 digest->Update(inner.get(), digest->Size());
136 return digest->Finish(output, out_len);
137}
138
Ali Tofigh7fa90572022-03-17 14:47:49139size_t ComputeHmac(absl::string_view alg,
Yves Gerey665174f2018-06-19 13:03:05140 const void* key,
141 size_t key_len,
142 const void* input,
143 size_t in_len,
144 void* output,
145 size_t out_len) {
jbauch555604a2016-04-26 10:13:22146 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26147 if (!digest) {
148 return 0;
149 }
Yves Gerey665174f2018-06-19 13:03:05150 return ComputeHmac(digest.get(), key, key_len, input, in_len, output,
151 out_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26152}
153
Yves Gerey665174f2018-06-19 13:03:05154std::string ComputeHmac(MessageDigest* digest,
Ali Tofigh7fa90572022-03-17 14:47:49155 absl::string_view key,
156 absl::string_view input) {
jbauch555604a2016-04-26 10:13:22157 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 13:03:05158 ComputeHmac(digest, key.data(), key.size(), input.data(), input.size(),
159 output.get(), digest->Size());
Ali Tofighfd6a4d62022-03-31 08:36:48160 return hex_encode(absl::string_view(output.get(), digest->Size()));
henrike@webrtc.orgf0488722014-05-13 18:00:26161}
162
Ali Tofigh7fa90572022-03-17 14:47:49163bool ComputeHmac(absl::string_view alg,
164 absl::string_view key,
165 absl::string_view input,
Yves Gerey665174f2018-06-19 13:03:05166 std::string* output) {
jbauch555604a2016-04-26 10:13:22167 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26168 if (!digest) {
169 return false;
170 }
171 *output = ComputeHmac(digest.get(), key, input);
172 return true;
173}
174
Ali Tofigh7fa90572022-03-17 14:47:49175std::string ComputeHmac(absl::string_view alg,
176 absl::string_view key,
177 absl::string_view input) {
henrike@webrtc.orgf0488722014-05-13 18:00:26178 std::string output;
179 ComputeHmac(alg, key, input, &output);
180 return output;
181}
182
183} // namespace rtc