| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef BASE_CRYPTO_SYMMETRIC_KEY_H_ | 5 #ifndef BASE_CRYPTO_SYMMETRIC_KEY_H_ |
| 6 #define BASE_CRYPTO_SYMMETRIC_KEY_H_ | 6 #define BASE_CRYPTO_SYMMETRIC_KEY_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 | 11 |
| 12 #if defined(USE_NSS) | 12 #if defined(USE_NSS) |
| 13 #include "base/crypto/scoped_nss_types.h" | 13 #include "base/crypto/scoped_nss_types.h" |
| 14 #elif defined(OS_MACOSX) | 14 #elif defined(OS_MACOSX) |
| 15 #include <Security/cssmtype.h> | 15 #include <Security/cssmtype.h> |
| 16 #elif defined(OS_WIN) |
| 17 #include "base/crypto/scoped_capi_types.h" |
| 16 #endif | 18 #endif |
| 17 | 19 |
| 18 namespace base { | 20 namespace base { |
| 19 | 21 |
| 20 // Wraps a platform-specific symmetric key and allows it to be held in a | 22 // Wraps a platform-specific symmetric key and allows it to be held in a |
| 21 // scoped_ptr. | 23 // scoped_ptr. |
| 22 class SymmetricKey { | 24 class SymmetricKey { |
| 23 public: | 25 public: |
| 24 enum Algorithm { | 26 enum Algorithm { |
| 25 AES, | 27 AES, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, | 41 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, |
| 40 const std::string& password, | 42 const std::string& password, |
| 41 const std::string& salt, | 43 const std::string& salt, |
| 42 size_t iterations, | 44 size_t iterations, |
| 43 size_t key_size_in_bits); | 45 size_t key_size_in_bits); |
| 44 | 46 |
| 45 #if defined(USE_NSS) | 47 #if defined(USE_NSS) |
| 46 PK11SymKey* key() const { return key_.get(); } | 48 PK11SymKey* key() const { return key_.get(); } |
| 47 #elif defined(OS_MACOSX) | 49 #elif defined(OS_MACOSX) |
| 48 CSSM_DATA cssm_data() const; | 50 CSSM_DATA cssm_data() const; |
| 51 #elif defined(OS_WIN) |
| 52 HCRYPTPROV prov() const { return provider_.get(); } |
| 53 HCRYPTKEY key() const { return key_.get(); } |
| 49 #endif | 54 #endif |
| 50 | 55 |
| 51 // Extracts the raw key from the platform specific data. This should only be | 56 // Extracts the raw key from the platform specific data. This should only be |
| 52 // done in unit tests to verify that keys are generated correctly. | 57 // done in unit tests to verify that keys are generated correctly. |
| 53 bool GetRawKey(std::string* raw_key); | 58 bool GetRawKey(std::string* raw_key); |
| 54 | 59 |
| 55 private: | 60 private: |
| 56 #if defined(USE_NSS) | 61 #if defined(USE_NSS) |
| 57 explicit SymmetricKey(PK11SymKey* key) : key_(key) {} | 62 explicit SymmetricKey(PK11SymKey* key) : key_(key) {} |
| 58 ScopedPK11SymKey key_; | 63 ScopedPK11SymKey key_; |
| 59 #elif defined(OS_MACOSX) | 64 #elif defined(OS_MACOSX) |
| 60 SymmetricKey(const void* key_data, size_t key_size_in_bits); | 65 SymmetricKey(const void* key_data, size_t key_size_in_bits); |
| 61 std::string key_; | 66 std::string key_; |
| 67 #elif defined(OS_WIN) |
| 68 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, const void* key_data, |
| 69 size_t key_size_in_bytes) : provider_(provider), key_(key) { |
| 70 if (key_data) { |
| 71 raw_key_.assign(reinterpret_cast<const char*>(key_data), |
| 72 key_size_in_bytes); |
| 73 } |
| 74 } |
| 75 |
| 76 ScopedHCRYPTPROV provider_; |
| 77 ScopedHCRYPTKEY key_; |
| 78 |
| 79 // Contains the raw key, if it is known during initialization and when it |
| 80 // is likely that the associated |provider_| will be unable to export the |
| 81 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
| 82 // when using the default RSA provider. |
| 83 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
| 84 // fails with NTE_BAD_KEY/NTE_BAD_LEN |
| 85 std::string raw_key_; |
| 62 #endif | 86 #endif |
| 63 | 87 |
| 64 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 88 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
| 65 }; | 89 }; |
| 66 | 90 |
| 67 } // namespace base | 91 } // namespace base |
| 68 | 92 |
| 69 #endif // BASE_CRYPTO_SYMMETRIC_KEY_H_ | 93 #endif // BASE_CRYPTO_SYMMETRIC_KEY_H_ |
| OLD | NEW |