| 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) |
| 15 // See comments for crypto_nacl_win64 in crypto.gyp. |
| 16 // Must test for NACL_WIN64 before OS_WIN since former is a subset of latter. |
| 17 #include "crypto/scoped_capi_types.h" |
| 18 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 15 #include "crypto/scoped_nss_types.h" | 19 #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 | 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: |
| 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. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 size_t key_size_in_bits); | 52 size_t key_size_in_bits); |
| 53 | 53 |
| 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(USE_OPENSSL) | 60 #if defined(USE_OPENSSL) |
| 61 const std::string& key() { return key_; } | 61 const std::string& key() { return key_; } |
| 62 #elif defined(USE_NSS) | 62 #elif defined(NACL_WIN64) |
| 63 HCRYPTKEY key() const { return key_.get(); } |
| 64 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 63 PK11SymKey* key() const { return key_.get(); } | 65 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 | 66 #endif |
| 69 | 67 |
| 70 // Extracts the raw key from the platform specific data. | 68 // Extracts the raw key from the platform specific data. |
| 71 // 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 |
| 72 // carefully. | 70 // carefully. |
| 73 bool GetRawKey(std::string* raw_key); | 71 bool GetRawKey(std::string* raw_key); |
| 74 | 72 |
| 75 #if defined(OS_CHROMEOS) | 73 #if defined(OS_CHROMEOS) |
| 76 // Creates symmetric key from NSS key. Takes over the ownership of |key|. | 74 // Creates symmetric key from NSS key. Takes over the ownership of |key|. |
| 77 static SymmetricKey* CreateFromKey(PK11SymKey* key); | 75 static SymmetricKey* CreateFromKey(PK11SymKey* key); |
| 78 #endif | 76 #endif |
| 79 | 77 |
| 80 private: | 78 private: |
| 81 #if defined(USE_OPENSSL) | 79 #if defined(USE_OPENSSL) |
| 82 SymmetricKey() {} | 80 SymmetricKey() {} |
| 83 std::string key_; | 81 std::string key_; |
| 84 #elif defined(USE_NSS) | 82 #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, | 83 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, |
| 92 const void* key_data, size_t key_size_in_bytes); | 84 const void* key_data, size_t key_size_in_bytes); |
| 93 | 85 |
| 94 ScopedHCRYPTPROV provider_; | 86 ScopedHCRYPTPROV provider_; |
| 95 ScopedHCRYPTKEY key_; | 87 ScopedHCRYPTKEY key_; |
| 96 | 88 |
| 97 // Contains the raw key, if it is known during initialization and when it | 89 // 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 | 90 // 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 | 91 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes |
| 100 // when using the default RSA provider. | 92 // when using the default RSA provider. |
| 101 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey | 93 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey |
| 102 // fails with NTE_BAD_KEY/NTE_BAD_LEN | 94 // fails with NTE_BAD_KEY/NTE_BAD_LEN |
| 103 std::string raw_key_; | 95 std::string raw_key_; |
| 96 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 97 explicit SymmetricKey(PK11SymKey* key); |
| 98 ScopedPK11SymKey key_; |
| 104 #endif | 99 #endif |
| 105 | 100 |
| 106 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 101 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
| 107 }; | 102 }; |
| 108 | 103 |
| 109 } // namespace crypto | 104 } // namespace crypto |
| 110 | 105 |
| 111 #endif // CRYPTO_SYMMETRIC_KEY_H_ | 106 #endif // CRYPTO_SYMMETRIC_KEY_H_ |
| OLD | NEW |