OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CRYPTO_SYMMETRIC_KEY_H_ | 5 #ifndef CRYPTO_SYMMETRIC_KEY_H_ |
6 #define CRYPTO_SYMMETRIC_KEY_H_ | 6 #define CRYPTO_SYMMETRIC_KEY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... | |
25 // scoped_ptr. | 25 // scoped_ptr. |
26 class CRYPTO_EXPORT SymmetricKey { | 26 class CRYPTO_EXPORT SymmetricKey { |
27 public: | 27 public: |
28 // Defines the algorithm that a key will be used with. See also | 28 // Defines the algorithm that a key will be used with. See also |
29 // classs Encrptor. | 29 // classs Encrptor. |
30 enum Algorithm { | 30 enum Algorithm { |
31 AES, | 31 AES, |
32 HMAC_SHA1, | 32 HMAC_SHA1, |
33 }; | 33 }; |
34 | 34 |
35 #if defined(OS_CHROMEOS) | |
36 explicit SymmetricKey(PK11SymKey* key); | |
wtc
2011/09/06 21:35:17
Maybe it's better to add an ImportPlatformKey or I
zel
2011/09/06 22:33:35
Added comment.
I thought about that ImportNSSKey
| |
37 #endif | |
38 | |
35 virtual ~SymmetricKey(); | 39 virtual ~SymmetricKey(); |
36 | 40 |
37 // Generates a random key suitable to be used with |algorithm| and of | 41 // Generates a random key suitable to be used with |algorithm| and of |
38 // |key_size_in_bits| bits. | 42 // |key_size_in_bits| bits. |
39 // The caller is responsible for deleting the returned SymmetricKey. | 43 // The caller is responsible for deleting the returned SymmetricKey. |
40 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, | 44 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, |
41 size_t key_size_in_bits); | 45 size_t key_size_in_bits); |
42 | 46 |
43 // Derives a key from the supplied password and salt using PBKDF2, suitable | 47 // Derives a key from the supplied password and salt using PBKDF2, suitable |
44 // for use with specified |algorithm|. Note |algorithm| is not the algorithm | 48 // for use with specified |algorithm|. Note |algorithm| is not the algorithm |
(...skipping 24 matching lines...) Expand all Loading... | |
69 // Extracts the raw key from the platform specific data. | 73 // Extracts the raw key from the platform specific data. |
70 // Warning: |raw_key| holds the raw key as bytes and thus must be handled | 74 // Warning: |raw_key| holds the raw key as bytes and thus must be handled |
71 // carefully. | 75 // carefully. |
72 bool GetRawKey(std::string* raw_key); | 76 bool GetRawKey(std::string* raw_key); |
73 | 77 |
74 private: | 78 private: |
75 #if defined(USE_OPENSSL) | 79 #if defined(USE_OPENSSL) |
76 SymmetricKey() {} | 80 SymmetricKey() {} |
77 std::string key_; | 81 std::string key_; |
78 #elif defined(USE_NSS) | 82 #elif defined(USE_NSS) |
83 #if !defined(OS_CHROMEOS) | |
79 explicit SymmetricKey(PK11SymKey* key); | 84 explicit SymmetricKey(PK11SymKey* key); |
85 #endif | |
80 ScopedPK11SymKey key_; | 86 ScopedPK11SymKey key_; |
81 #elif defined(OS_MACOSX) | 87 #elif defined(OS_MACOSX) |
82 SymmetricKey(const void* key_data, size_t key_size_in_bits); | 88 SymmetricKey(const void* key_data, size_t key_size_in_bits); |
83 std::string key_; | 89 std::string key_; |
84 #elif defined(OS_WIN) | 90 #elif defined(OS_WIN) |
85 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, | 91 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, |
86 const void* key_data, size_t key_size_in_bytes); | 92 const void* key_data, size_t key_size_in_bytes); |
87 | 93 |
88 ScopedHCRYPTPROV provider_; | 94 ScopedHCRYPTPROV provider_; |
89 ScopedHCRYPTKEY key_; | 95 ScopedHCRYPTKEY key_; |
90 | 96 |
91 // Contains the raw key, if it is known during initialization and when it | 97 // Contains the raw key, if it is known during initialization and when it |
92 // is likely that the associated |provider_| will be unable to export the | 98 // is likely that the associated |provider_| will be unable to export the |
93 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes | 99 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
94 // when using the default RSA provider. | 100 // when using the default RSA provider. |
95 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey | 101 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
96 // fails with NTE_BAD_KEY/NTE_BAD_LEN | 102 // fails with NTE_BAD_KEY/NTE_BAD_LEN |
97 std::string raw_key_; | 103 std::string raw_key_; |
98 #endif | 104 #endif |
99 | 105 |
100 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 106 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
101 }; | 107 }; |
102 | 108 |
103 } // namespace crypto | 109 } // namespace crypto |
104 | 110 |
105 #endif // CRYPTO_SYMMETRIC_KEY_H_ | 111 #endif // CRYPTO_SYMMETRIC_KEY_H_ |
OLD | NEW |