OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/hmac.h" | 5 #include "crypto/hmac.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "crypto/scoped_capi_types.h" | 14 #include "crypto/scoped_capi_types.h" |
15 #include "crypto/sha2.h" | |
wtc
2011/09/24 01:39:41
BUG(kind of): Please also undo the changes in this
| |
15 #include "crypto/third_party/nss/blapi.h" | 16 #include "crypto/third_party/nss/blapi.h" |
16 #include "crypto/third_party/nss/sha256.h" | 17 #include "crypto/third_party/nss/sha256.h" |
17 | 18 |
18 namespace crypto { | 19 namespace crypto { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Implementation of HMAC-SHA-256: | 23 // Implementation of HMAC-SHA-256: |
23 // | 24 // |
24 // SHA-256 is supported in Windows XP SP3 or later. We still need to support | 25 // 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 // Windows XP SP2, so unfortunately we have to implement HMAC-SHA-256 here. |
26 | 27 |
27 enum { | 28 enum { |
28 SHA256_BLOCK_SIZE = 64 // Block size (in bytes) of the input to SHA-256. | 29 SHA256_BLOCK_SIZE = 64 // Block size (in bytes) of the input to SHA-256. |
29 }; | 30 }; |
30 | 31 |
31 // See FIPS 198: The Keyed-Hash Message Authentication Code (HMAC). | 32 // See FIPS 198: The Keyed-Hash Message Authentication Code (HMAC). |
32 void ComputeHMACSHA256(const unsigned char* key, size_t key_len, | 33 void ComputeHMACSHA256(const unsigned char* key, size_t key_len, |
33 const unsigned char* text, size_t text_len, | 34 const unsigned char* text, size_t text_len, |
34 unsigned char* output, size_t output_len) { | 35 unsigned char* output, size_t output_len) { |
35 SHA256Context ctx; | 36 SHA256Context ctx; |
36 | 37 |
37 // Pre-process the key, if necessary. | 38 // Pre-process the key, if necessary. |
38 unsigned char key0[SHA256_BLOCK_SIZE]; | 39 unsigned char key0[SHA256_BLOCK_SIZE]; |
39 if (key_len > SHA256_BLOCK_SIZE) { | 40 if (key_len > SHA256_BLOCK_SIZE) { |
40 SHA256_Begin(&ctx); | 41 SHA256_Begin(&ctx); |
41 SHA256_Update(&ctx, key, key_len); | 42 SHA256_Update(&ctx, key, key_len); |
42 SHA256_End(&ctx, key0, NULL, SHA256_LENGTH); | 43 SHA256_End(&ctx, key0, NULL, kSHA256Length); |
43 memset(key0 + SHA256_LENGTH, 0, SHA256_BLOCK_SIZE - SHA256_LENGTH); | 44 memset(key0 + kSHA256Length, 0, SHA256_BLOCK_SIZE - kSHA256Length); |
44 } else { | 45 } else { |
45 memcpy(key0, key, key_len); | 46 memcpy(key0, key, key_len); |
46 memset(key0 + key_len, 0, SHA256_BLOCK_SIZE - key_len); | 47 memset(key0 + key_len, 0, SHA256_BLOCK_SIZE - key_len); |
47 } | 48 } |
48 | 49 |
49 unsigned char padded_key[SHA256_BLOCK_SIZE]; | 50 unsigned char padded_key[SHA256_BLOCK_SIZE]; |
50 unsigned char inner_hash[SHA256_LENGTH]; | 51 unsigned char inner_hash[kSHA256Length]; |
51 | 52 |
52 // XOR key0 with ipad. | 53 // XOR key0 with ipad. |
53 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i) | 54 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i) |
54 padded_key[i] = key0[i] ^ 0x36; | 55 padded_key[i] = key0[i] ^ 0x36; |
55 | 56 |
56 // Compute the inner hash. | 57 // Compute the inner hash. |
57 SHA256_Begin(&ctx); | 58 SHA256_Begin(&ctx); |
58 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE); | 59 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE); |
59 SHA256_Update(&ctx, text, text_len); | 60 SHA256_Update(&ctx, text, text_len); |
60 SHA256_End(&ctx, inner_hash, NULL, SHA256_LENGTH); | 61 SHA256_End(&ctx, inner_hash, NULL, kSHA256Length); |
61 | 62 |
62 // XOR key0 with opad. | 63 // XOR key0 with opad. |
63 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i) | 64 for (int i = 0; i < SHA256_BLOCK_SIZE; ++i) |
64 padded_key[i] = key0[i] ^ 0x5c; | 65 padded_key[i] = key0[i] ^ 0x5c; |
65 | 66 |
66 // Compute the outer hash. | 67 // Compute the outer hash. |
67 SHA256_Begin(&ctx); | 68 SHA256_Begin(&ctx); |
68 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE); | 69 SHA256_Update(&ctx, padded_key, SHA256_BLOCK_SIZE); |
69 SHA256_Update(&ctx, inner_hash, SHA256_LENGTH); | 70 SHA256_Update(&ctx, inner_hash, kSHA256Length); |
70 SHA256_End(&ctx, output, NULL, output_len); | 71 SHA256_End(&ctx, output, NULL, output_len); |
71 } | 72 } |
72 | 73 |
73 } // namespace | 74 } // namespace |
74 | 75 |
75 struct HMACPlatformData { | 76 struct HMACPlatformData { |
76 ~HMACPlatformData() { | 77 ~HMACPlatformData() { |
77 if (!raw_key_.empty()) { | 78 if (!raw_key_.empty()) { |
78 SecureZeroMemory(&raw_key_[0], raw_key_.size()); | 79 SecureZeroMemory(&raw_key_[0], raw_key_.size()); |
79 } | 80 } |
(...skipping 16 matching lines...) Expand all Loading... | |
96 } | 97 } |
97 | 98 |
98 bool HMAC::Init(const unsigned char* key, int key_length) { | 99 bool HMAC::Init(const unsigned char* key, int key_length) { |
99 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { | 100 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { |
100 // Init must not be called more than once on the same HMAC object. | 101 // Init must not be called more than once on the same HMAC object. |
101 NOTREACHED(); | 102 NOTREACHED(); |
102 return false; | 103 return false; |
103 } | 104 } |
104 | 105 |
105 if (hash_alg_ == SHA256) { | 106 if (hash_alg_ == SHA256) { |
106 if (key_length < SHA256_LENGTH / 2) | 107 if (key_length < kSHA256Length / 2) |
107 return false; // Key is too short. | 108 return false; // Key is too short. |
108 plat_->raw_key_.assign(key, key + key_length); | 109 plat_->raw_key_.assign(key, key + key_length); |
109 return true; | 110 return true; |
110 } | 111 } |
111 | 112 |
112 if (!CryptAcquireContext(plat_->provider_.receive(), NULL, NULL, | 113 if (!CryptAcquireContext(plat_->provider_.receive(), NULL, NULL, |
113 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { | 114 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { |
114 NOTREACHED(); | 115 NOTREACHED(); |
115 return false; | 116 return false; |
116 } | 117 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 | 189 |
189 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), | 190 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), |
190 static_cast<DWORD>(data.size()), 0)) | 191 static_cast<DWORD>(data.size()), 0)) |
191 return false; | 192 return false; |
192 | 193 |
193 DWORD sha1_size = digest_length; | 194 DWORD sha1_size = digest_length; |
194 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); | 195 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); |
195 } | 196 } |
196 | 197 |
197 } // namespace crypto | 198 } // namespace crypto |
OLD | NEW |