initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Utility class for calculating the HMAC for a given message. We currently |
| 31 | // only support SHA1 for the hash algorithm, but this can be extended easily. |
| 32 | |
| 33 | #ifndef BASE_HMAC_H__ |
| 34 | #define BASE_HMAC_H__ |
| 35 | |
| 36 | #include <windows.h> |
| 37 | #include <wincrypt.h> |
| 38 | |
| 39 | #include <string> |
| 40 | |
| 41 | #include "base/basictypes.h" |
| 42 | |
| 43 | class HMAC { |
| 44 | public: |
| 45 | // The set of supported hash functions. Extend as required. |
| 46 | enum HashAlgorithm { |
| 47 | SHA1 |
| 48 | }; |
| 49 | |
| 50 | HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length); |
| 51 | ~HMAC(); |
| 52 | |
| 53 | // Returns the HMAC in 'digest' for the message in 'data' and the key |
| 54 | // specified in the contructor. |
| 55 | bool Sign(const std::string& data, unsigned char* digest, int digest_length); |
| 56 | |
| 57 | private: |
| 58 | // Import the key so that we don't have to store it ourself. |
| 59 | // TODO(paulg): Bug: http://b/1084719, 'ImportKey' will not currently work on |
| 60 | // Windows 2000 since it requires special handling for importing |
| 61 | // keys. See this link for details: |
| 62 | // http://www.derkeiler.com/Newsgroups/microsoft.public.platformsdk.security/2004-06/0270.html |
| 63 | void ImportKey(const unsigned char* key, int key_length); |
| 64 | |
| 65 | // Returns the SHA1 hash of 'data' and 'key' in 'digest'. If there was any |
| 66 | // error in the calculation, this method returns false, otherwise true. |
| 67 | bool SignWithSHA1(const std::string& data, |
| 68 | unsigned char* digest, |
| 69 | int digest_length); |
| 70 | |
| 71 | // Required for the SHA1 key_blob struct. We limit this to 16 bytes since |
| 72 | // Windows 2000 doesn't support keys larger than that. |
| 73 | static const int kMaxKeySize = 16; |
| 74 | |
| 75 | // The hash algorithm to use. |
| 76 | HashAlgorithm hash_alg_; |
| 77 | |
| 78 | // Windows Crypt API resources. |
| 79 | HCRYPTPROV provider_; |
| 80 | HCRYPTHASH hash_; |
| 81 | HCRYPTKEY hkey_; |
| 82 | |
| 83 | DISALLOW_EVIL_CONSTRUCTORS(HMAC); |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | #endif // BASE_HMAC_H__ |