| 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 #endif // USE_NSS | 14 #elif defined(OS_MACOSX) |
| 15 #include <Security/cssmtype.h> |
| 16 #endif |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 17 | 19 |
| 18 // Wraps a platform-specific symmetric key and allows it to be held in a | 20 // Wraps a platform-specific symmetric key and allows it to be held in a |
| 19 // scoped_ptr. | 21 // scoped_ptr. |
| 20 class SymmetricKey { | 22 class SymmetricKey { |
| 21 public: | 23 public: |
| 22 enum Algorithm { | 24 enum Algorithm { |
| 23 AES, | 25 AES, |
| 24 HMAC_SHA1, | 26 HMAC_SHA1, |
| 25 }; | 27 }; |
| 26 | 28 |
| 27 virtual ~SymmetricKey() {} | 29 virtual ~SymmetricKey() {} |
| 28 | 30 |
| 29 // Generates a random key suitable to be used with |cipher| and of |key_size| | 31 // Generates a random key suitable to be used with |cipher| and of |
| 30 // bytes. The caller is responsible for deleting the returned SymmetricKey. | 32 // |key_size_in_bits| bits. |
| 31 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, size_t key_size); | 33 // The caller is responsible for deleting the returned SymmetricKey. |
| 34 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, |
| 35 size_t key_size_in_bits); |
| 32 | 36 |
| 33 // Derives a key from the supplied password and salt using PBKDF2. The caller | 37 // Derives a key from the supplied password and salt using PBKDF2. The caller |
| 34 // is respnosible for deleting the returned SymmetricKey. | 38 // is responsible for deleting the returned SymmetricKey. |
| 35 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, | 39 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, |
| 36 const std::string& password, | 40 const std::string& password, |
| 37 const std::string& salt, | 41 const std::string& salt, |
| 38 size_t iterations, | 42 size_t iterations, |
| 39 size_t key_size); | 43 size_t key_size_in_bits); |
| 40 | 44 |
| 41 #if defined(USE_NSS) | 45 #if defined(USE_NSS) |
| 42 PK11SymKey* key() const { return key_.get(); } | 46 PK11SymKey* key() const { return key_.get(); } |
| 43 #endif // USE_NSS | 47 #elif defined(OS_MACOSX) |
| 48 CSSM_DATA cssm_data() const; |
| 49 #endif |
| 44 | 50 |
| 45 // Extracts the raw key from the platform specific data. This should only be | 51 // Extracts the raw key from the platform specific data. This should only be |
| 46 // done in unit tests to verify that keys are generated correctly. | 52 // done in unit tests to verify that keys are generated correctly. |
| 47 bool GetRawKey(std::string* raw_key); | 53 bool GetRawKey(std::string* raw_key); |
| 48 | 54 |
| 49 private: | 55 private: |
| 50 #if defined(USE_NSS) | 56 #if defined(USE_NSS) |
| 51 explicit SymmetricKey(PK11SymKey* key) : key_(key) {} | 57 explicit SymmetricKey(PK11SymKey* key) : key_(key) {} |
| 52 ScopedPK11SymKey key_; | 58 ScopedPK11SymKey key_; |
| 53 #endif // USE_NSS | 59 #elif defined(OS_MACOSX) |
| 60 SymmetricKey(const void* key_data, size_t key_size_in_bits); |
| 61 std::string key_; |
| 62 #endif |
| 54 | 63 |
| 55 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 64 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
| 56 }; | 65 }; |
| 57 | 66 |
| 58 } // namespace base | 67 } // namespace base |
| 59 | 68 |
| 60 #endif // BASE_CRYPTO_SYMMETRIC_KEY_H_ | 69 #endif // BASE_CRYPTO_SYMMETRIC_KEY_H_ |
| OLD | NEW |