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