blob: 5a0d16a9a04b672ec69cff77fc98287450677a74 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/messagedigest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <string.h>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
15#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/openssldigest.h"
18#include "rtc_base/stringencode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
22// From RFC 4572.
Yves Gerey665174f2018-06-19 15:03:05 +020023const char DIGEST_MD5[] = "md5";
24const char DIGEST_SHA_1[] = "sha-1";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025const char DIGEST_SHA_224[] = "sha-224";
26const char DIGEST_SHA_256[] = "sha-256";
27const char DIGEST_SHA_384[] = "sha-384";
28const char DIGEST_SHA_512[] = "sha-512";
29
30static const size_t kBlockSize = 64; // valid for SHA-256 and down
31
32MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 MessageDigest* digest = new OpenSSLDigest(alg);
34 if (digest->Size() == 0) { // invalid algorithm
35 delete digest;
deadbeef37f5ecf2017-02-27 14:06:41 -080036 digest = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 }
38 return digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039}
40
41bool IsFips180DigestAlgorithm(const std::string& alg) {
42 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5,
43 // "Self-signed certificates (for which legacy certificates are not a
44 // consideration) MUST use one of the FIPS 180 algorithms (SHA-1,
45 // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm,
46 // and thus also MUST use it to calculate certificate fingerprints."
Yves Gerey665174f2018-06-19 15:03:05 +020047 return alg == DIGEST_SHA_1 || alg == DIGEST_SHA_224 ||
48 alg == DIGEST_SHA_256 || alg == DIGEST_SHA_384 ||
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 alg == DIGEST_SHA_512;
50}
51
Yves Gerey665174f2018-06-19 15:03:05 +020052size_t ComputeDigest(MessageDigest* digest,
53 const void* input,
54 size_t in_len,
55 void* output,
56 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 digest->Update(input, in_len);
58 return digest->Finish(output, out_len);
59}
60
Yves Gerey665174f2018-06-19 15:03:05 +020061size_t ComputeDigest(const std::string& alg,
62 const void* input,
63 size_t in_len,
64 void* output,
65 size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -070066 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
Yves Gerey665174f2018-06-19 15:03:05 +020067 return (digest) ? ComputeDigest(digest.get(), input, in_len, output, out_len)
68 : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069}
70
71std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -070072 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 15:03:05 +020073 ComputeDigest(digest, input.data(), input.size(), output.get(),
74 digest->Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 return hex_encode(output.get(), digest->Size());
76}
77
Yves Gerey665174f2018-06-19 15:03:05 +020078bool ComputeDigest(const std::string& alg,
79 const std::string& input,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -070081 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 if (!digest) {
83 return false;
84 }
85 *output = ComputeDigest(digest.get(), input);
86 return true;
87}
88
89std::string ComputeDigest(const std::string& alg, const std::string& input) {
90 std::string output;
91 ComputeDigest(alg, input, &output);
92 return output;
93}
94
95// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
96size_t ComputeHmac(MessageDigest* digest,
Yves Gerey665174f2018-06-19 15:03:05 +020097 const void* key,
98 size_t key_len,
99 const void* input,
100 size_t in_len,
101 void* output,
102 size_t out_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 // We only handle algorithms with a 64-byte blocksize.
104 // TODO: Add BlockSize() method to MessageDigest.
105 size_t block_len = kBlockSize;
106 if (digest->Size() > 32) {
107 return 0;
108 }
109 // Copy the key to a block-sized buffer to simplify padding.
110 // If the key is longer than a block, hash it and use the result instead.
jbauch555604a2016-04-26 03:13:22 -0700111 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 if (key_len > block_len) {
113 ComputeDigest(digest, key, key_len, new_key.get(), block_len);
114 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
115 } else {
116 memcpy(new_key.get(), key, key_len);
117 memset(new_key.get() + key_len, 0, block_len - key_len);
118 }
119 // Set up the padding from the key, salting appropriately for each padding.
jbauch555604a2016-04-26 03:13:22 -0700120 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
121 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 for (size_t i = 0; i < block_len; ++i) {
123 o_pad[i] = 0x5c ^ new_key[i];
124 i_pad[i] = 0x36 ^ new_key[i];
125 }
126 // Inner hash; hash the inner padding, and then the input buffer.
jbauch555604a2016-04-26 03:13:22 -0700127 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 digest->Update(i_pad.get(), block_len);
129 digest->Update(input, in_len);
130 digest->Finish(inner.get(), digest->Size());
131 // Outer hash; hash the outer padding, and then the result of the inner hash.
132 digest->Update(o_pad.get(), block_len);
133 digest->Update(inner.get(), digest->Size());
134 return digest->Finish(output, out_len);
135}
136
Yves Gerey665174f2018-06-19 15:03:05 +0200137size_t ComputeHmac(const std::string& alg,
138 const void* key,
139 size_t key_len,
140 const void* input,
141 size_t in_len,
142 void* output,
143 size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -0700144 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 if (!digest) {
146 return 0;
147 }
Yves Gerey665174f2018-06-19 15:03:05 +0200148 return ComputeHmac(digest.get(), key, key_len, input, in_len, output,
149 out_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150}
151
Yves Gerey665174f2018-06-19 15:03:05 +0200152std::string ComputeHmac(MessageDigest* digest,
153 const std::string& key,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -0700155 std::unique_ptr<char[]> output(new char[digest->Size()]);
Yves Gerey665174f2018-06-19 15:03:05 +0200156 ComputeHmac(digest, key.data(), key.size(), input.data(), input.size(),
157 output.get(), digest->Size());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 return hex_encode(output.get(), digest->Size());
159}
160
Yves Gerey665174f2018-06-19 15:03:05 +0200161bool ComputeHmac(const std::string& alg,
162 const std::string& key,
163 const std::string& input,
164 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -0700165 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166 if (!digest) {
167 return false;
168 }
169 *output = ComputeHmac(digest.get(), key, input);
170 return true;
171}
172
Yves Gerey665174f2018-06-19 15:03:05 +0200173std::string ComputeHmac(const std::string& alg,
174 const std::string& key,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 const std::string& input) {
176 std::string output;
177 ComputeHmac(alg, key, input, &output);
178 return output;
179}
180
181} // namespace rtc