blob: ba7d827503513cb2b6fe40c60c328457beb3a979 [file] [log] [blame]
[email protected]4a0141b2012-03-27 01:15:301// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/hmac.h"
[email protected]301415e2008-09-04 19:00:376
[email protected]fbcfafe2008-09-08 13:58:107#include <windows.h>
8#include <wincrypt.h>
9
[email protected]301415e2008-09-04 19:00:3710#include <algorithm>
11#include <vector>
12
initial.commitd7cae122008-07-26 21:49:3813#include "base/logging.h"
[email protected]4b559b4d2011-04-14 17:37:1414#include "crypto/scoped_capi_types.h"
[email protected]e3ff8ee22011-11-15 01:36:4515#include "crypto/third_party/nss/chromium-blapi.h"
16#include "crypto/third_party/nss/chromium-sha256.h"
initial.commitd7cae122008-07-26 21:49:3817
[email protected]4b559b4d2011-04-14 17:37:1418namespace crypto {
[email protected]301415e2008-09-04 19:00:3719
[email protected]db163f82010-04-02 21:01:3520namespace {
21
22// Implementation of HMAC-SHA-256:
23//
24// SHA-256 is supported in Windows XP SP3 or later. We still need to support
25// Windows XP SP2, so unfortunately we have to implement HMAC-SHA-256 here.
26
27enum {
28 SHA256_BLOCK_SIZE = 64 // Block size (in bytes) of the input to SHA-256.
29};
30
[email protected]4a0141b2012-03-27 01:15:3031// NSS doesn't accept size_t for text size, divide the data into smaller
32// chunks as needed.
33void Wrapped_SHA256_Update(SHA256Context* ctx, const unsigned char* text,
34 size_t text_len) {
35 const unsigned int kChunkSize = 1 << 30;
36 while (text_len > kChunkSize) {
37 SHA256_Update(ctx, text, kChunkSize);
38 text += kChunkSize;
39 text_len -= kChunkSize;
40 }
41 SHA256_Update(ctx, text, (unsigned int)text_len);
42}
43
[email protected]db163f82010-04-02 21:01:3544// See FIPS 198: The Keyed-Hash Message Authentication Code (HMAC).
45void ComputeHMACSHA256(const unsigned char* key, size_t key_len,
46 const unsigned char* text, size_t text_len,
47 unsigned char* output, size_t output_len) {
48 SHA256Context ctx;
49
50 // Pre-process the key, if necessary.
51 unsigned char key0[SHA256_BLOCK_SIZE];
52 if (key_len > SHA256_BLOCK_SIZE) {
53 SHA256_Begin(&ctx);
[email protected]4a0141b2012-03-27 01:15:3054 Wrapped_SHA256_Update(&ctx, key, key_len);
[email protected]db163f82010-04-02 21:01:3555 SHA256_End(&ctx, key0, NULL, SHA256_LENGTH);
56 memset(key0 + SHA256_LENGTH, 0, SHA256_BLOCK_SIZE - SHA256_LENGTH);
57 } else {
58 memcpy(key0, key, key_len);
[email protected]cfd607fb2011-12-22 07:40:4459 if (key_len < SHA256_BLOCK_SIZE)
60 memset(key0 + key_len, 0, SHA256_BLOCK_SIZE - key_len);
[email protected]db163f82010-04-02 21:01:3561 }
62
63 unsigned char padded_key[SHA256_BLOCK_SIZE];
64 unsigned char inner_hash[SHA256_LENGTH];
65
66 // XOR key0 with ipad.
67 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i)
68 padded_key[i] = key0[i] ^ 0x36;
69
70 // Compute the inner hash.
71 SHA256_Begin(&ctx);
72 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE);
[email protected]4a0141b2012-03-27 01:15:3073 Wrapped_SHA256_Update(&ctx, text, text_len);
[email protected]db163f82010-04-02 21:01:3574 SHA256_End(&ctx, inner_hash, NULL, SHA256_LENGTH);
75
76 // XOR key0 with opad.
77 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i)
78 padded_key[i] = key0[i] ^ 0x5c;
79
80 // Compute the outer hash.
81 SHA256_Begin(&ctx);
82 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE);
83 SHA256_Update(&ctx, inner_hash, SHA256_LENGTH);
[email protected]4a0141b2012-03-27 01:15:3084 SHA256_End(&ctx, output, NULL, (unsigned int) output_len);
[email protected]db163f82010-04-02 21:01:3585}
86
87} // namespace
88
[email protected]fbcfafe2008-09-08 13:58:1089struct HMACPlatformData {
[email protected]08ce4d42010-10-21 00:31:1990 ~HMACPlatformData() {
91 if (!raw_key_.empty()) {
92 SecureZeroMemory(&raw_key_[0], raw_key_.size());
93 }
[email protected]db163f82010-04-02 21:01:3594
[email protected]08ce4d42010-10-21 00:31:1995 // Destroy the key before releasing the provider.
96 key_.reset();
97 }
98
99 ScopedHCRYPTPROV provider_;
100 ScopedHCRYPTKEY key_;
[email protected]db163f82010-04-02 21:01:35101
102 // For HMAC-SHA-256 only.
103 std::vector<unsigned char> raw_key_;
[email protected]fbcfafe2008-09-08 13:58:10104};
105
[email protected]d91f8432009-05-05 23:55:59106HMAC::HMAC(HashAlgorithm hash_alg)
[email protected]fbcfafe2008-09-08 13:58:10107 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
[email protected]db163f82010-04-02 21:01:35108 // Only SHA-1 and SHA-256 hash algorithms are supported now.
109 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256);
[email protected]d91f8432009-05-05 23:55:59110}
111
[email protected]673266c42012-12-04 00:50:35112bool HMAC::Init(const unsigned char* key, size_t key_length) {
[email protected]08ce4d42010-10-21 00:31:19113 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) {
[email protected]d91f8432009-05-05 23:55:59114 // Init must not be called more than once on the same HMAC object.
115 NOTREACHED();
116 return false;
117 }
118
[email protected]db163f82010-04-02 21:01:35119 if (hash_alg_ == SHA256) {
[email protected]db163f82010-04-02 21:01:35120 plat_->raw_key_.assign(key, key + key_length);
121 return true;
122 }
123
[email protected]08ce4d42010-10-21 00:31:19124 if (!CryptAcquireContext(plat_->provider_.receive(), NULL, NULL,
[email protected]d91f8432009-05-05 23:55:59125 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
126 NOTREACHED();
[email protected]d91f8432009-05-05 23:55:59127 return false;
128 }
initial.commitd7cae122008-07-26 21:49:38129
[email protected]301415e2008-09-04 19:00:37130 // This code doesn't work on Win2k because PLAINTEXTKEYBLOB and
131 // CRYPT_IPSEC_HMAC_KEY are not supported on Windows 2000. PLAINTEXTKEYBLOB
132 // allows the import of an unencrypted key. For Win2k support, a cubmbersome
133 // exponent-of-one key procedure must be used:
134 // http://support.microsoft.com/kb/228786/en-us
135 // CRYPT_IPSEC_HMAC_KEY allows keys longer than 16 bytes.
initial.commitd7cae122008-07-26 21:49:38136
[email protected]301415e2008-09-04 19:00:37137 struct KeyBlob {
initial.commitd7cae122008-07-26 21:49:38138 BLOBHEADER header;
139 DWORD key_size;
[email protected]301415e2008-09-04 19:00:37140 BYTE key_data[1];
141 };
142 size_t key_blob_size = std::max(offsetof(KeyBlob, key_data) + key_length,
143 sizeof(KeyBlob));
144 std::vector<BYTE> key_blob_storage = std::vector<BYTE>(key_blob_size);
145 KeyBlob* key_blob = reinterpret_cast<KeyBlob*>(&key_blob_storage[0]);
146 key_blob->header.bType = PLAINTEXTKEYBLOB;
147 key_blob->header.bVersion = CUR_BLOB_VERSION;
148 key_blob->header.reserved = 0;
149 key_blob->header.aiKeyAlg = CALG_RC2;
[email protected]673266c42012-12-04 00:50:35150 key_blob->key_size = static_cast<DWORD>(key_length);
[email protected]301415e2008-09-04 19:00:37151 memcpy(key_blob->key_data, key, key_length);
initial.commitd7cae122008-07-26 21:49:38152
[email protected]fbcfafe2008-09-08 13:58:10153 if (!CryptImportKey(plat_->provider_, &key_blob_storage[0],
[email protected]4a0141b2012-03-27 01:15:30154 (DWORD)key_blob_storage.size(), 0,
155 CRYPT_IPSEC_HMAC_KEY, plat_->key_.receive())) {
[email protected]d91f8432009-05-05 23:55:59156 NOTREACHED();
[email protected]d91f8432009-05-05 23:55:59157 return false;
[email protected]301415e2008-09-04 19:00:37158 }
initial.commitd7cae122008-07-26 21:49:38159
160 // Destroy the copy of the key.
[email protected]301415e2008-09-04 19:00:37161 SecureZeroMemory(key_blob->key_data, key_length);
[email protected]d91f8432009-05-05 23:55:59162
163 return true;
initial.commitd7cae122008-07-26 21:49:38164}
165
[email protected]fbcfafe2008-09-08 13:58:10166HMAC::~HMAC() {
[email protected]fbcfafe2008-09-08 13:58:10167}
168
[email protected]c28986ee2011-06-06 22:00:11169bool HMAC::Sign(const base::StringPiece& data,
[email protected]fbcfafe2008-09-08 13:58:10170 unsigned char* digest,
[email protected]673266c42012-12-04 00:50:35171 size_t digest_length) const {
[email protected]db163f82010-04-02 21:01:35172 if (hash_alg_ == SHA256) {
173 if (plat_->raw_key_.empty())
174 return false;
175 ComputeHMACSHA256(&plat_->raw_key_[0], plat_->raw_key_.size(),
176 reinterpret_cast<const unsigned char*>(data.data()),
177 data.size(), digest, digest_length);
178 return true;
179 }
180
[email protected]08ce4d42010-10-21 00:31:19181 if (!plat_->provider_ || !plat_->key_)
[email protected]fbcfafe2008-09-08 13:58:10182 return false;
183
184 if (hash_alg_ != SHA1) {
185 NOTREACHED();
186 return false;
187 }
188
[email protected]08ce4d42010-10-21 00:31:19189 ScopedHCRYPTHASH hash;
190 if (!CryptCreateHash(plat_->provider_, CALG_HMAC, plat_->key_, 0,
191 hash.receive()))
initial.commitd7cae122008-07-26 21:49:38192 return false;
193
194 HMAC_INFO hmac_info;
195 memset(&hmac_info, 0, sizeof(hmac_info));
196 hmac_info.HashAlgid = CALG_SHA1;
[email protected]08ce4d42010-10-21 00:31:19197 if (!CryptSetHashParam(hash, HP_HMAC_INFO,
initial.commitd7cae122008-07-26 21:49:38198 reinterpret_cast<BYTE*>(&hmac_info), 0))
199 return false;
200
[email protected]08ce4d42010-10-21 00:31:19201 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()),
initial.commitd7cae122008-07-26 21:49:38202 static_cast<DWORD>(data.size()), 0))
203 return false;
204
[email protected]673266c42012-12-04 00:50:35205 DWORD sha1_size = static_cast<DWORD>(digest_length);
[email protected]08ce4d42010-10-21 00:31:19206 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0);
initial.commitd7cae122008-07-26 21:49:38207}
license.botbf09a502008-08-24 00:55:55208
[email protected]4b559b4d2011-04-14 17:37:14209} // namespace crypto