OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "crypto/crypto_export.h" | 11 #include "crypto/crypto_export.h" |
12 | 12 |
13 #if defined(NACL_WIN64) | 13 #if defined(NACL_WIN64) |
14 // See comments for crypto_nacl_win64 in crypto.gyp. | 14 // See comments for crypto_nacl_win64 in crypto.gyp. |
15 // Must test for NACL_WIN64 before OS_WIN since former is a subset of latter. | 15 // Must test for NACL_WIN64 before OS_WIN since former is a subset of latter. |
16 #include "crypto/scoped_capi_types.h" | 16 #include "crypto/scoped_capi_types.h" |
17 #elif defined(USE_NSS) || \ | 17 #elif defined(USE_NSS_CERTS) || \ |
18 (!defined(USE_OPENSSL) && (defined(OS_WIN) || defined(OS_MACOSX))) | 18 (!defined(USE_OPENSSL) && (defined(OS_WIN) || defined(OS_MACOSX))) |
19 #include "crypto/scoped_nss_types.h" | 19 #include "crypto/scoped_nss_types.h" |
20 #endif | 20 #endif |
21 | 21 |
22 namespace crypto { | 22 namespace crypto { |
23 | 23 |
24 // Wraps a platform-specific symmetric key and allows it to be held in a | 24 // Wraps a platform-specific symmetric key and allows it to be held in a |
25 // scoped_ptr. | 25 // scoped_ptr. |
26 class CRYPTO_EXPORT SymmetricKey { | 26 class CRYPTO_EXPORT SymmetricKey { |
27 public: | 27 public: |
(...skipping 26 matching lines...) Expand all Loading... |
54 // Imports an array of key bytes in |raw_key|. This key may have been | 54 // Imports an array of key bytes in |raw_key|. This key may have been |
55 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with | 55 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with |
56 // GetRawKey, or via another compatible method. The key must be of suitable | 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. | 57 // size for use with |algorithm|. The caller owns the returned SymmetricKey. |
58 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); | 58 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); |
59 | 59 |
60 #if defined(NACL_WIN64) | 60 #if defined(NACL_WIN64) |
61 HCRYPTKEY key() const { return key_.get(); } | 61 HCRYPTKEY key() const { return key_.get(); } |
62 #elif defined(USE_OPENSSL) | 62 #elif defined(USE_OPENSSL) |
63 const std::string& key() { return key_; } | 63 const std::string& key() { return key_; } |
64 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | 64 #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) |
65 PK11SymKey* key() const { return key_.get(); } | 65 PK11SymKey* key() const { return key_.get(); } |
66 #endif | 66 #endif |
67 | 67 |
68 // Extracts the raw key from the platform specific data. | 68 // Extracts the raw key from the platform specific data. |
69 // Warning: |raw_key| holds the raw key as bytes and thus must be handled | 69 // Warning: |raw_key| holds the raw key as bytes and thus must be handled |
70 // carefully. | 70 // carefully. |
71 bool GetRawKey(std::string* raw_key); | 71 bool GetRawKey(std::string* raw_key); |
72 | 72 |
73 private: | 73 private: |
74 #if defined(NACL_WIN64) | 74 #if defined(NACL_WIN64) |
75 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, | 75 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, |
76 const void* key_data, size_t key_size_in_bytes); | 76 const void* key_data, size_t key_size_in_bytes); |
77 | 77 |
78 ScopedHCRYPTPROV provider_; | 78 ScopedHCRYPTPROV provider_; |
79 ScopedHCRYPTKEY key_; | 79 ScopedHCRYPTKEY key_; |
80 | 80 |
81 // Contains the raw key, if it is known during initialization and when it | 81 // Contains the raw key, if it is known during initialization and when it |
82 // is likely that the associated |provider_| will be unable to export the | 82 // is likely that the associated |provider_| will be unable to export the |
83 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes | 83 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
84 // when using the default RSA provider. | 84 // when using the default RSA provider. |
85 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey | 85 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
86 // fails with NTE_BAD_KEY/NTE_BAD_LEN | 86 // fails with NTE_BAD_KEY/NTE_BAD_LEN |
87 std::string raw_key_; | 87 std::string raw_key_; |
88 #elif defined(USE_OPENSSL) | 88 #elif defined(USE_OPENSSL) |
89 SymmetricKey() {} | 89 SymmetricKey() {} |
90 std::string key_; | 90 std::string key_; |
91 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | 91 #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) |
92 explicit SymmetricKey(PK11SymKey* key); | 92 explicit SymmetricKey(PK11SymKey* key); |
93 ScopedPK11SymKey key_; | 93 ScopedPK11SymKey key_; |
94 #endif | 94 #endif |
95 | 95 |
96 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 96 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
97 }; | 97 }; |
98 | 98 |
99 } // namespace crypto | 99 } // namespace crypto |
100 | 100 |
101 #endif // CRYPTO_SYMMETRIC_KEY_H_ | 101 #endif // CRYPTO_SYMMETRIC_KEY_H_ |
OLD | NEW |