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 3 // found in the LICENSE file. 4 5 #ifndef CRYPTO_SYMMETRIC_KEY_H_ 6 #define CRYPTO_SYMMETRIC_KEY_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 12 #include "base/macros.h" 13 #include "build/build_config.h" 14 #include "crypto/crypto_export.h" 15 16 #if defined(NACL_WIN64) 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. 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 24 25 namespace crypto { 26 27 // Wraps a platform-specific symmetric key and allows it to be held in a 28 // scoped_ptr. 29 class CRYPTO_EXPORT SymmetricKey { 30 public: 31 // Defines the algorithm that a key will be used with. See also 32 // classs Encrptor. 33 enum Algorithm { 34 AES, 35 HMAC_SHA1, 36 }; 37 38 virtual ~SymmetricKey(); 39 40 // Generates a random key suitable to be used with |algorithm| and of 41 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8. 42 // The caller is responsible for deleting the returned SymmetricKey. 43 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, 44 size_t key_size_in_bits); 45 46 // Derives a key from the supplied password and salt using PBKDF2, suitable 47 // for use with specified |algorithm|. Note |algorithm| is not the algorithm 48 // used to derive the key from the password. |key_size_in_bits| must be a 49 // multiple of 8. The caller is responsible for deleting the returned 50 // SymmetricKey. 51 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, 52 const std::string& password, 53 const std::string& salt, 54 size_t iterations, 55 size_t key_size_in_bits); 56 57 // Imports an array of key bytes in |raw_key|. This key may have been 58 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with 59 // GetRawKey, or via another compatible method. The key must be of suitable 60 // size for use with |algorithm|. The caller owns the returned SymmetricKey. 61 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); 62 63 #if defined(NACL_WIN64) key()64 HCRYPTKEY key() const { return key_.get(); } 65 #elif defined(USE_OPENSSL) key()66 const std::string& key() { return key_; } 67 #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) key()68 PK11SymKey* key() const { return key_.get(); } 69 #endif 70 71 // Extracts the raw key from the platform specific data. 72 // Warning: |raw_key| holds the raw key as bytes and thus must be handled 73 // carefully. 74 bool GetRawKey(std::string* raw_key); 75 76 private: 77 #if defined(NACL_WIN64) 78 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key, 79 const void* key_data, size_t key_size_in_bytes); 80 81 ScopedHCRYPTPROV provider_; 82 ScopedHCRYPTKEY key_; 83 84 // 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 86 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes 87 // when using the default RSA provider. 88 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey 89 // fails with NTE_BAD_KEY/NTE_BAD_LEN 90 std::string raw_key_; 91 #elif defined(USE_OPENSSL) 92 SymmetricKey() {} 93 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 98 99 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); 100 }; 101 102 } // namespace crypto 103 104 #endif // CRYPTO_SYMMETRIC_KEY_H_ 105