OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/hmac.h" | |
6 #include "base/logging.h" | |
7 | |
8 HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length) | |
9 : hash_alg_(hash_alg), | |
10 provider_(NULL), | |
11 hash_(NULL), | |
12 hkey_(NULL) { | |
13 if (!CryptAcquireContext(&provider_, NULL, NULL, | |
14 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) | |
15 provider_ = NULL; | |
16 ImportKey(key, key_length); | |
17 } | |
18 | |
19 HMAC::~HMAC() { | |
20 if (hkey_) | |
21 CryptDestroyKey(hkey_); | |
22 if (hash_) | |
23 CryptDestroyHash(hash_); | |
24 if (provider_) | |
25 CryptReleaseContext(provider_, 0); | |
26 } | |
27 | |
28 bool HMAC::Sign(const std::string& data, | |
29 unsigned char* digest, | |
30 int digest_length) { | |
31 if (!provider_ || !hkey_) | |
32 return false; | |
33 | |
34 switch (hash_alg_) { | |
35 case SHA1: | |
36 return SignWithSHA1(data, digest, digest_length); | |
37 default: | |
38 NOTREACHED(); | |
39 return false; | |
40 } | |
41 } | |
42 | |
43 void HMAC::ImportKey(const unsigned char* key, int key_length) { | |
44 if (key_length > kMaxKeySize) { | |
45 NOTREACHED(); | |
46 return; | |
47 } | |
48 | |
49 struct { | |
50 BLOBHEADER header; | |
51 DWORD key_size; | |
52 BYTE key_data[kMaxKeySize]; | |
53 } key_blob; | |
54 key_blob.header.bType = PLAINTEXTKEYBLOB; | |
55 key_blob.header.bVersion = CUR_BLOB_VERSION; | |
56 key_blob.header.reserved = 0; | |
57 key_blob.header.aiKeyAlg = CALG_RC2; | |
58 key_blob.key_size = key_length; | |
59 memcpy(key_blob.key_data, key, key_length); | |
60 | |
61 if (!CryptImportKey(provider_, | |
62 reinterpret_cast<const BYTE *>(&key_blob), | |
63 sizeof(key_blob), 0, 0, &hkey_)) | |
64 hkey_ = NULL; | |
65 | |
66 // Destroy the copy of the key. | |
67 SecureZeroMemory(key_blob.key_data, key_length); | |
68 } | |
69 | |
70 bool HMAC::SignWithSHA1(const std::string& data, | |
71 unsigned char* digest, | |
72 int digest_length) { | |
73 DCHECK(provider_); | |
74 DCHECK(hkey_); | |
75 | |
76 if (!CryptCreateHash(provider_, CALG_HMAC, hkey_, 0, &hash_)) | |
77 return false; | |
78 | |
79 HMAC_INFO hmac_info; | |
80 memset(&hmac_info, 0, sizeof(hmac_info)); | |
81 hmac_info.HashAlgid = CALG_SHA1; | |
82 if (!CryptSetHashParam(hash_, HP_HMAC_INFO, | |
83 reinterpret_cast<BYTE*>(&hmac_info), 0)) | |
84 return false; | |
85 | |
86 if (!CryptHashData(hash_, | |
87 reinterpret_cast<const BYTE*>(data.data()), | |
88 static_cast<DWORD>(data.size()), 0)) | |
89 return false; | |
90 | |
91 DWORD sha1_size = digest_length; | |
92 if (!CryptGetHashParam(hash_, HP_HASHVAL, digest, &sha1_size, 0)) | |
93 return false; | |
94 | |
95 return true; | |
96 } | |
97 | |
OLD | NEW |