blob: 62b4a6bc97045c20b4b8143b15cab136cb6a1c83 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#include "rtc_base/message_digest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
16#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/openssl_digest.h"
19#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
21namespace rtc {
22
23// From RFC 4572.
Yves Gerey665174f2018-06-19 15:03:05 +020024const char DIGEST_MD5[] = "md5";
25const char DIGEST_SHA_1[] = "sha-1";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026const char DIGEST_SHA_224[] = "sha-224";
27const char DIGEST_SHA_256[] = "sha-256";
28const char DIGEST_SHA_384[] = "sha-384";
29const char DIGEST_SHA_512[] = "sha-512";
30
31static const size_t kBlockSize = 64; // valid for SHA-256 and down
32
33MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 MessageDigest* digest = new OpenSSLDigest(alg);
35 if (digest->Size() == 0) { // invalid algorithm
36 delete digest;
deadbeef37f5ecf2017-02-27 14:06:41 -080037 digest = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 }
39 return digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040}
41
42bool IsFips180DigestAlgorithm(const std::string& alg) {
43 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5,
44 // "Self-signed certificates (for which legacy certificates are not a
45 // consideration) MUST use one of the FIPS 180 algorithms (SHA-1,
46 // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm,
47 // and thus also MUST use it to calculate certificate fingerprints."
Yves Gerey665174f2018-06-19 15:03:05 +020048 return alg == DIGEST_SHA_1 || alg == DIGEST_SHA_224 ||
49 alg == DIGEST_SHA_256 || alg == DIGEST_SHA_384 ||
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 alg == DIGEST_SHA_512;
51}
52
Yves Gerey665174f2018-06-19 15:03:05 +020053size_t ComputeDigest(MessageDigest* digest,
54 const void* input,
55 size_t in_len,
56 void* output,
57 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 digest->Update(input, in_len);
59 return digest->Finish(output, out_len);
60}
61
Yves Gerey665174f2018-06-19 15:03:05 +020062size_t ComputeDigest(const std::string& alg,
63 const void* input,
64 size_t in_len,
65 void* output,
66 size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -070067 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
Yves Gerey665174f2018-06-19 15:03:05 +020068 return (digest) ? ComputeDigest(digest.get(), input, in_len, output, out_len)
69 : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070}
71
72std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -070073 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 15:03:05 +020074 ComputeDigest(digest, input.data(), input.size(), output.get(),
75 digest->Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 return hex_encode(output.get(), digest->Size());
77}
78
Yves Gerey665174f2018-06-19 15:03:05 +020079bool ComputeDigest(const std::string& alg,
80 const std::string& input,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -070082 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 if (!digest) {
84 return false;
85 }
86 *output = ComputeDigest(digest.get(), input);
87 return true;
88}
89
90std::string ComputeDigest(const std::string& alg, const std::string& input) {
91 std::string output;
92 ComputeDigest(alg, input, &output);
93 return output;
94}
95
96// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
97size_t ComputeHmac(MessageDigest* digest,
Yves Gerey665174f2018-06-19 15:03:05 +020098 const void* key,
99 size_t key_len,
100 const void* input,
101 size_t in_len,
102 void* output,
103 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 // We only handle algorithms with a 64-byte blocksize.
105 // TODO: Add BlockSize() method to MessageDigest.
106 size_t block_len = kBlockSize;
107 if (digest->Size() > 32) {
108 return 0;
109 }
110 // Copy the key to a block-sized buffer to simplify padding.
111 // If the key is longer than a block, hash it and use the result instead.
jbauch555604a2016-04-26 03:13:22 -0700112 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 if (key_len > block_len) {
114 ComputeDigest(digest, key, key_len, new_key.get(), block_len);
115 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
116 } else {
117 memcpy(new_key.get(), key, key_len);
118 memset(new_key.get() + key_len, 0, block_len - key_len);
119 }
120 // Set up the padding from the key, salting appropriately for each padding.
jbauch555604a2016-04-26 03:13:22 -0700121 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
122 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 for (size_t i = 0; i < block_len; ++i) {
124 o_pad[i] = 0x5c ^ new_key[i];
125 i_pad[i] = 0x36 ^ new_key[i];
126 }
127 // Inner hash; hash the inner padding, and then the input buffer.
jbauch555604a2016-04-26 03:13:22 -0700128 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 digest->Update(i_pad.get(), block_len);
130 digest->Update(input, in_len);
131 digest->Finish(inner.get(), digest->Size());
132 // Outer hash; hash the outer padding, and then the result of the inner hash.
133 digest->Update(o_pad.get(), block_len);
134 digest->Update(inner.get(), digest->Size());
135 return digest->Finish(output, out_len);
136}
137
Yves Gerey665174f2018-06-19 15:03:05 +0200138size_t ComputeHmac(const std::string& alg,
139 const void* key,
140 size_t key_len,
141 const void* input,
142 size_t in_len,
143 void* output,
144 size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -0700145 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 if (!digest) {
147 return 0;
148 }
Yves Gerey665174f2018-06-19 15:03:05 +0200149 return ComputeHmac(digest.get(), key, key_len, input, in_len, output,
150 out_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151}
152
Yves Gerey665174f2018-06-19 15:03:05 +0200153std::string ComputeHmac(MessageDigest* digest,
154 const std::string& key,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155 const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -0700156 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 15:03:05 +0200157 ComputeHmac(digest, key.data(), key.size(), input.data(), input.size(),
158 output.get(), digest->Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159 return hex_encode(output.get(), digest->Size());
160}
161
Yves Gerey665174f2018-06-19 15:03:05 +0200162bool ComputeHmac(const std::string& alg,
163 const std::string& key,
164 const std::string& input,
165 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -0700166 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 if (!digest) {
168 return false;
169 }
170 *output = ComputeHmac(digest.get(), key, input);
171 return true;
172}
173
Yves Gerey665174f2018-06-19 15:03:05 +0200174std::string ComputeHmac(const std::string& alg,
175 const std::string& key,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 const std::string& input) {
177 std::string output;
178 ComputeHmac(alg, key, input, &output);
179 return output;
180}
181
182} // namespace rtc