[email protected] | 9493ee95c | 2011-03-28 23:48:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_SYMMETRIC_KEY_H_ |
| 6 | #define CRYPTO_SYMMETRIC_KEY_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 12 | #include "crypto/crypto_export.h" |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 13 | |
| 14 | #if defined(USE_NSS) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | #include "crypto/scoped_nss_types.h" |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 16 | #elif defined(OS_MACOSX) |
| 17 | #include <Security/cssmtype.h> |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 18 | #elif defined(OS_WIN) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 19 | #include "crypto/scoped_capi_types.h" |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 20 | #endif |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 21 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 22 | namespace crypto { |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 23 | |
| 24 | // Wraps a platform-specific symmetric key and allows it to be held in a |
| 25 | // scoped_ptr. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 26 | class CRYPTO_EXPORT SymmetricKey { |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 27 | public: |
[email protected] | 03d8698 | 2010-11-16 12:28:51 | [diff] [blame] | 28 | // Defines the algorithm that a key will be used with. See also |
| 29 | // classs Encrptor. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 30 | enum Algorithm { |
| 31 | AES, |
| 32 | HMAC_SHA1, |
| 33 | }; |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 34 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 35 | virtual ~SymmetricKey(); |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 36 | |
[email protected] | 03d8698 | 2010-11-16 12:28:51 | [diff] [blame] | 37 | // Generates a random key suitable to be used with |algorithm| and of |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 38 | // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8. |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 39 | // The caller is responsible for deleting the returned SymmetricKey. |
| 40 | static SymmetricKey* GenerateRandomKey(Algorithm algorithm, |
| 41 | size_t key_size_in_bits); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 42 | |
[email protected] | 03d8698 | 2010-11-16 12:28:51 | [diff] [blame] | 43 | // Derives a key from the supplied password and salt using PBKDF2, suitable |
| 44 | // for use with specified |algorithm|. Note |algorithm| is not the algorithm |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 45 | // used to derive the key from the password. |key_size_in_bits| must be a |
| 46 | // multiple of 8. The caller is responsible for deleting the returned |
| 47 | // SymmetricKey. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 48 | static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, |
| 49 | const std::string& password, |
| 50 | const std::string& salt, |
| 51 | size_t iterations, |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 52 | size_t key_size_in_bits); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 53 | |
[email protected] | f48fdae | 2010-11-19 14:20:27 | [diff] [blame] | 54 | // Imports an array of key bytes in |raw_key|. This key may have been |
| 55 | // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with |
| 56 | // GetRawKey, or via another compatible method. The key must be of suitable |
| 57 | // size for use with |algorithm|. The caller owns the returned SymmetricKey. |
[email protected] | 896200b3 | 2010-07-20 19:21:18 | [diff] [blame] | 58 | static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 59 | |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 60 | #if defined(USE_OPENSSL) |
| 61 | const std::string& key() { return key_; } |
| 62 | #elif defined(USE_NSS) |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 63 | PK11SymKey* key() const { return key_.get(); } |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 64 | #elif defined(OS_MACOSX) |
| 65 | CSSM_DATA cssm_data() const; |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 66 | #elif defined(OS_WIN) |
| 67 | HCRYPTKEY key() const { return key_.get(); } |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 68 | #endif |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 69 | |
[email protected] | 896200b3 | 2010-07-20 19:21:18 | [diff] [blame] | 70 | // Extracts the raw key from the platform specific data. |
| 71 | // Warning: |raw_key| holds the raw key as bytes and thus must be handled |
| 72 | // carefully. |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 73 | bool GetRawKey(std::string* raw_key); |
| 74 | |
[email protected] | 6e3d9a9 | 2011-09-07 04:01:03 | [diff] [blame] | 75 | #if defined(OS_CHROMEOS) |
| 76 | // Creates symmetric key from NSS key. Takes over the ownership of |key|. |
| 77 | static SymmetricKey* CreateFromKey(PK11SymKey* key); |
| 78 | #endif |
| 79 | |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 80 | private: |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 81 | #if defined(USE_OPENSSL) |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 82 | SymmetricKey() {} |
| 83 | std::string key_; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 84 | #elif defined(USE_NSS) |
[email protected] | 896200b3 | 2010-07-20 19:21:18 | [diff] [blame] | 85 | explicit SymmetricKey(PK11SymKey* key); |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 86 | ScopedPK11SymKey key_; |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 87 | #elif defined(OS_MACOSX) |
| 88 | SymmetricKey(const void* key_data, size_t key_size_in_bits); |
| 89 | std::string key_; |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 90 | #elif defined(OS_WIN) |
| 91 | SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, |
| 92 | const void* key_data, size_t key_size_in_bytes); |
| 93 | |
| 94 | ScopedHCRYPTPROV provider_; |
| 95 | ScopedHCRYPTKEY key_; |
| 96 | |
| 97 | // Contains the raw key, if it is known during initialization and when it |
| 98 | // is likely that the associated |provider_| will be unable to export the |
| 99 | // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
| 100 | // when using the default RSA provider. |
| 101 | // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
| 102 | // fails with NTE_BAD_KEY/NTE_BAD_LEN |
| 103 | std::string raw_key_; |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 104 | #endif |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
| 107 | }; |
| 108 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 109 | } // namespace crypto |
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 110 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 111 | #endif // CRYPTO_SYMMETRIC_KEY_H_ |