license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 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. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | // |
| 5 | // Utility class for calculating the HMAC for a given message. We currently |
| 6 | // only support SHA1 for the hash algorithm, but this can be extended easily. |
| 7 | |
| 8 | #ifndef BASE_HMAC_H__ |
| 9 | #define BASE_HMAC_H__ |
| 10 | |
| 11 | #include <windows.h> |
| 12 | #include <wincrypt.h> |
| 13 | |
| 14 | #include <string> |
| 15 | |
| 16 | #include "base/basictypes.h" |
| 17 | |
| 18 | class HMAC { |
| 19 | public: |
| 20 | // The set of supported hash functions. Extend as required. |
| 21 | enum HashAlgorithm { |
| 22 | SHA1 |
| 23 | }; |
| 24 | |
| 25 | HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length); |
| 26 | ~HMAC(); |
| 27 | |
| 28 | // Returns the HMAC in 'digest' for the message in 'data' and the key |
| 29 | // specified in the contructor. |
| 30 | bool Sign(const std::string& data, unsigned char* digest, int digest_length); |
| 31 | |
| 32 | private: |
| 33 | // Import the key so that we don't have to store it ourself. |
| 34 | // TODO(paulg): Bug: http://b/1084719, 'ImportKey' will not currently work on |
| 35 | // Windows 2000 since it requires special handling for importing |
| 36 | // keys. See this link for details: |
| 37 | // http://www.derkeiler.com/Newsgroups/microsoft.public.platformsdk.security/2004-06/0270.html |
| 38 | void ImportKey(const unsigned char* key, int key_length); |
| 39 | |
| 40 | // Returns the SHA1 hash of 'data' and 'key' in 'digest'. If there was any |
| 41 | // error in the calculation, this method returns false, otherwise true. |
| 42 | bool SignWithSHA1(const std::string& data, |
| 43 | unsigned char* digest, |
| 44 | int digest_length); |
| 45 | |
| 46 | // Required for the SHA1 key_blob struct. We limit this to 16 bytes since |
| 47 | // Windows 2000 doesn't support keys larger than that. |
| 48 | static const int kMaxKeySize = 16; |
| 49 | |
| 50 | // The hash algorithm to use. |
| 51 | HashAlgorithm hash_alg_; |
| 52 | |
| 53 | // Windows Crypt API resources. |
| 54 | HCRYPTPROV provider_; |
| 55 | HCRYPTHASH hash_; |
| 56 | HCRYPTKEY hkey_; |
| 57 | |
| 58 | DISALLOW_EVIL_CONSTRUCTORS(HMAC); |
| 59 | }; |
| 60 | |
| 61 | |
[email protected] | 2292150 | 2008-08-14 11:44:17 | [diff] [blame] | 62 | #endif // BASE_HMAC_H__ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 63 | |