OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "crypto/crypto_export.h" | 12 #include "crypto/crypto_export.h" |
13 | 13 |
14 #if defined(USE_NSS) | 14 #if defined(NACL_WIN64) |
wtc
2012/06/14 21:28:48
Please add a comment to explain NACL_WIN64, and po
ddorwin
2012/06/14 22:01:55
Done.
I explained it in crypto.gyp (I hope I got t
| |
15 #include "crypto/scoped_capi_types.h" | |
16 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
15 #include "crypto/scoped_nss_types.h" | 17 #include "crypto/scoped_nss_types.h" |
16 #elif defined(OS_MACOSX) | |
17 #include <Security/cssmtype.h> | |
18 #elif defined(OS_WIN) | |
19 #include "crypto/scoped_capi_types.h" | |
20 #endif | 18 #endif |
21 | 19 |
22 namespace crypto { | 20 namespace crypto { |
23 | 21 |
24 // 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 |
25 // scoped_ptr. | 23 // scoped_ptr. |
26 class CRYPTO_EXPORT SymmetricKey { | 24 class CRYPTO_EXPORT SymmetricKey { |
27 public: | 25 public: |
28 // Defines the algorithm that a key will be used with. See also | 26 // Defines the algorithm that a key will be used with. See also |
29 // classs Encrptor. | 27 // classs Encrptor. |
(...skipping 22 matching lines...) Expand all Loading... | |
52 size_t key_size_in_bits); | 50 size_t key_size_in_bits); |
53 | 51 |
54 // Imports an array of key bytes in |raw_key|. This key may have been | 52 // Imports an array of key bytes in |raw_key|. This key may have been |
55 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with | 53 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with |
56 // GetRawKey, or via another compatible method. The key must be of suitable | 54 // GetRawKey, or via another compatible method. The key must be of suitable |
57 // size for use with |algorithm|. The caller owns the returned SymmetricKey. | 55 // size for use with |algorithm|. The caller owns the returned SymmetricKey. |
58 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); | 56 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); |
59 | 57 |
60 #if defined(USE_OPENSSL) | 58 #if defined(USE_OPENSSL) |
61 const std::string& key() { return key_; } | 59 const std::string& key() { return key_; } |
62 #elif defined(USE_NSS) | 60 #elif defined(NACL_WIN64) |
61 HCRYPTKEY key() const { return key_.get(); } | |
62 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
63 PK11SymKey* key() const { return key_.get(); } | 63 PK11SymKey* key() const { return key_.get(); } |
64 #elif defined(OS_MACOSX) | |
65 CSSM_DATA cssm_data() const; | |
66 #elif defined(OS_WIN) | |
67 HCRYPTKEY key() const { return key_.get(); } | |
68 #endif | 64 #endif |
69 | 65 |
70 // Extracts the raw key from the platform specific data. | 66 // Extracts the raw key from the platform specific data. |
71 // 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 |
72 // carefully. | 68 // carefully. |
73 bool GetRawKey(std::string* raw_key); | 69 bool GetRawKey(std::string* raw_key); |
74 | 70 |
75 #if defined(OS_CHROMEOS) | 71 #if defined(OS_CHROMEOS) |
76 // Creates symmetric key from NSS key. Takes over the ownership of |key|. | 72 // Creates symmetric key from NSS key. Takes over the ownership of |key|. |
77 static SymmetricKey* CreateFromKey(PK11SymKey* key); | 73 static SymmetricKey* CreateFromKey(PK11SymKey* key); |
78 #endif | 74 #endif |
79 | 75 |
80 private: | 76 private: |
81 #if defined(USE_OPENSSL) | 77 #if defined(USE_OPENSSL) |
82 SymmetricKey() {} | 78 SymmetricKey() {} |
83 std::string key_; | 79 std::string key_; |
84 #elif defined(USE_NSS) | 80 #elif defined(NACL_WIN64) |
85 explicit SymmetricKey(PK11SymKey* key); | |
86 ScopedPK11SymKey key_; | |
87 #elif defined(OS_MACOSX) | |
88 SymmetricKey(const void* key_data, size_t key_size_in_bits); | |
89 std::string key_; | |
90 #elif defined(OS_WIN) | |
91 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, | 81 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, |
92 const void* key_data, size_t key_size_in_bytes); | 82 const void* key_data, size_t key_size_in_bytes); |
93 | 83 |
94 ScopedHCRYPTPROV provider_; | 84 ScopedHCRYPTPROV provider_; |
95 ScopedHCRYPTKEY key_; | 85 ScopedHCRYPTKEY key_; |
96 | 86 |
97 // Contains the raw key, if it is known during initialization and when it | 87 // Contains the raw key, if it is known during initialization and when it |
98 // is likely that the associated |provider_| will be unable to export the | 88 // is likely that the associated |provider_| will be unable to export the |
99 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes | 89 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
100 // when using the default RSA provider. | 90 // when using the default RSA provider. |
101 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey | 91 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
102 // fails with NTE_BAD_KEY/NTE_BAD_LEN | 92 // fails with NTE_BAD_KEY/NTE_BAD_LEN |
103 std::string raw_key_; | 93 std::string raw_key_; |
94 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
95 explicit SymmetricKey(PK11SymKey* key); | |
96 ScopedPK11SymKey key_; | |
104 #endif | 97 #endif |
105 | 98 |
106 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 99 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
107 }; | 100 }; |
108 | 101 |
109 } // namespace crypto | 102 } // namespace crypto |
110 | 103 |
111 #endif // CRYPTO_SYMMETRIC_KEY_H_ | 104 #endif // CRYPTO_SYMMETRIC_KEY_H_ |
OLD | NEW |