blob: 1e4c3850fb3b7b1703b11518ba78cdeec82e85bb [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
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <string.h>
16
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.
23const char DIGEST_MD5[] = "md5";
24const char DIGEST_SHA_1[] = "sha-1";
25const 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."
47 return alg == DIGEST_SHA_1 ||
48 alg == DIGEST_SHA_224 ||
49 alg == DIGEST_SHA_256 ||
50 alg == DIGEST_SHA_384 ||
51 alg == DIGEST_SHA_512;
52}
53
54size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
55 void* output, size_t out_len) {
56 digest->Update(input, in_len);
57 return digest->Finish(output, out_len);
58}
59
60size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
61 void* output, size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -070062 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 return (digest) ?
64 ComputeDigest(digest.get(), input, in_len, output, out_len) :
65 0;
66}
67
68std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -070069 std::unique_ptr<char[]> output(new char[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 ComputeDigest(digest, input.data(), input.size(),
71 output.get(), digest->Size());
72 return hex_encode(output.get(), digest->Size());
73}
74
75bool ComputeDigest(const std::string& alg, const std::string& input,
76 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -070077 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 if (!digest) {
79 return false;
80 }
81 *output = ComputeDigest(digest.get(), input);
82 return true;
83}
84
85std::string ComputeDigest(const std::string& alg, const std::string& input) {
86 std::string output;
87 ComputeDigest(alg, input, &output);
88 return output;
89}
90
91// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
92size_t ComputeHmac(MessageDigest* digest,
93 const void* key, size_t key_len,
94 const void* input, size_t in_len,
95 void* output, size_t out_len) {
96 // We only handle algorithms with a 64-byte blocksize.
97 // TODO: Add BlockSize() method to MessageDigest.
98 size_t block_len = kBlockSize;
99 if (digest->Size() > 32) {
100 return 0;
101 }
102 // Copy the key to a block-sized buffer to simplify padding.
103 // If the key is longer than a block, hash it and use the result instead.
jbauch555604a2016-04-26 03:13:22 -0700104 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 if (key_len > block_len) {
106 ComputeDigest(digest, key, key_len, new_key.get(), block_len);
107 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
108 } else {
109 memcpy(new_key.get(), key, key_len);
110 memset(new_key.get() + key_len, 0, block_len - key_len);
111 }
112 // Set up the padding from the key, salting appropriately for each padding.
jbauch555604a2016-04-26 03:13:22 -0700113 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
114 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 for (size_t i = 0; i < block_len; ++i) {
116 o_pad[i] = 0x5c ^ new_key[i];
117 i_pad[i] = 0x36 ^ new_key[i];
118 }
119 // Inner hash; hash the inner padding, and then the input buffer.
jbauch555604a2016-04-26 03:13:22 -0700120 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 digest->Update(i_pad.get(), block_len);
122 digest->Update(input, in_len);
123 digest->Finish(inner.get(), digest->Size());
124 // Outer hash; hash the outer padding, and then the result of the inner hash.
125 digest->Update(o_pad.get(), block_len);
126 digest->Update(inner.get(), digest->Size());
127 return digest->Finish(output, out_len);
128}
129
130size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
131 const void* input, size_t in_len,
132 void* output, size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -0700133 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 if (!digest) {
135 return 0;
136 }
137 return ComputeHmac(digest.get(), key, key_len,
138 input, in_len, output, out_len);
139}
140
141std::string ComputeHmac(MessageDigest* digest, const std::string& key,
142 const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -0700143 std::unique_ptr<char[]> output(new char[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 ComputeHmac(digest, key.data(), key.size(),
145 input.data(), input.size(), output.get(), digest->Size());
146 return hex_encode(output.get(), digest->Size());
147}
148
149bool ComputeHmac(const std::string& alg, const std::string& key,
150 const std::string& input, std::string* output) {
jbauch555604a2016-04-26 03:13:22 -0700151 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 if (!digest) {
153 return false;
154 }
155 *output = ComputeHmac(digest.get(), key, input);
156 return true;
157}
158
159std::string ComputeHmac(const std::string& alg, const std::string& key,
160 const std::string& input) {
161 std::string output;
162 ComputeHmac(alg, key, input, &output);
163 return output;
164}
165
166} // namespace rtc