3
* Copyright 2011, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/messagedigest.h"
33
#include "talk/base/openssldigest.h"
35
#include "talk/base/md5digest.h"
36
#include "talk/base/sha1digest.h"
38
#include "talk/base/scoped_ptr.h"
39
#include "talk/base/stringencode.h"
44
const char DIGEST_MD5[] = "md5";
45
const char DIGEST_SHA_1[] = "sha-1";
46
const char DIGEST_SHA_224[] = "sha-224";
47
const char DIGEST_SHA_256[] = "sha-256";
48
const char DIGEST_SHA_384[] = "sha-384";
49
const char DIGEST_SHA_512[] = "sha-512";
51
static const size_t kBlockSize = 64; // valid for SHA-256 and down
53
MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
55
MessageDigest* digest = new OpenSSLDigest(alg);
56
if (digest->Size() == 0) { // invalid algorithm
62
MessageDigest* digest = NULL;
63
if (alg == DIGEST_MD5) {
64
digest = new Md5Digest();
65
} else if (alg == DIGEST_SHA_1) {
66
digest = new Sha1Digest();
72
size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
73
void* output, size_t out_len) {
74
digest->Update(input, in_len);
75
return digest->Finish(output, out_len);
78
size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
79
void* output, size_t out_len) {
80
scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
81
return (digest.get()) ?
82
ComputeDigest(digest.get(), input, in_len, output, out_len) :
86
std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
87
scoped_array<char> output(new char[digest->Size()]);
88
ComputeDigest(digest, input.data(), input.size(),
89
output.get(), digest->Size());
90
return hex_encode(output.get(), digest->Size());
93
bool ComputeDigest(const std::string& alg, const std::string& input,
94
std::string* output) {
95
scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
99
*output = ComputeDigest(digest.get(), input);
103
std::string ComputeDigest(const std::string& alg, const std::string& input) {
105
ComputeDigest(alg, input, &output);
109
// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
110
size_t ComputeHmac(MessageDigest* digest,
111
const void* key, size_t key_len,
112
const void* input, size_t in_len,
113
void* output, size_t out_len) {
114
// We only handle algorithms with a 64-byte blocksize.
115
// TODO: Add BlockSize() method to MessageDigest.
116
size_t block_len = kBlockSize;
117
if (digest->Size() > 32) {
120
// Copy the key to a block-sized buffer to simplify padding.
121
// If the key is longer than a block, hash it and use the result instead.
122
scoped_array<uint8> new_key(new uint8[block_len]);
123
if (key_len > block_len) {
124
ComputeDigest(digest, key, key_len, new_key.get(), block_len);
125
memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
127
memcpy(new_key.get(), key, key_len);
128
memset(new_key.get() + key_len, 0, block_len - key_len);
130
// Set up the padding from the key, salting appropriately for each padding.
131
scoped_array<uint8> o_pad(new uint8[block_len]), i_pad(new uint8[block_len]);
132
for (size_t i = 0; i < block_len; ++i) {
133
o_pad[i] = 0x5c ^ new_key[i];
134
i_pad[i] = 0x36 ^ new_key[i];
136
// Inner hash; hash the inner padding, and then the input buffer.
137
scoped_array<uint8> inner(new uint8[digest->Size()]);
138
digest->Update(i_pad.get(), block_len);
139
digest->Update(input, in_len);
140
digest->Finish(inner.get(), digest->Size());
141
// Outer hash; hash the outer padding, and then the result of the inner hash.
142
digest->Update(o_pad.get(), block_len);
143
digest->Update(inner.get(), digest->Size());
144
return digest->Finish(output, out_len);
147
size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
148
const void* input, size_t in_len,
149
void* output, size_t out_len) {
150
scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
154
return ComputeHmac(digest.get(), key, key_len,
155
input, in_len, output, out_len);
158
std::string ComputeHmac(MessageDigest* digest, const std::string& key,
159
const std::string& input) {
160
scoped_array<char> output(new char[digest->Size()]);
161
ComputeHmac(digest, key.data(), key.size(),
162
input.data(), input.size(), output.get(), digest->Size());
163
return hex_encode(output.get(), digest->Size());
166
bool ComputeHmac(const std::string& alg, const std::string& key,
167
const std::string& input, std::string* output) {
168
scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
172
*output = ComputeHmac(digest.get(), key, input);
176
std::string ComputeHmac(const std::string& alg, const std::string& key,
177
const std::string& input) {
179
ComputeHmac(alg, key, input, &output);
183
} // namespace talk_base