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.
5 #ifndef CRYPTO_SYMMETRIC_KEY_H_
6 #define CRYPTO_SYMMETRIC_KEY_H_
13 #include "base/macros.h"
14 #include "build/build_config.h"
15 #include "crypto/crypto_export.h"
19 // Wraps a platform-specific symmetric key and allows it to be held in a
21 class CRYPTO_EXPORT SymmetricKey {
23 // Defines the algorithm that a key will be used with. See also
30 virtual ~SymmetricKey();
32 // Generates a random key suitable to be used with |algorithm| and of
33 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8.
34 // The caller is responsible for deleting the returned SymmetricKey.
35 static std::unique_ptr<SymmetricKey> GenerateRandomKey(
37 size_t key_size_in_bits);
39 // Derives a key from the supplied password and salt using PBKDF2, suitable
40 // for use with specified |algorithm|. Note |algorithm| is not the algorithm
41 // used to derive the key from the password. |key_size_in_bits| must be a
42 // multiple of 8. The caller is responsible for deleting the returned
44 static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingPbkdf2(
46 const std::string& password,
47 const std::string& salt,
49 size_t key_size_in_bits);
51 // Derives a key from the supplied password and salt using scrypt, suitable
52 // for use with specified |algorithm|. Note |algorithm| is not the algorithm
53 // used to derive the key from the password. |cost_parameter|, |block_size|,
54 // and |parallelization_parameter| correspond to the parameters |N|, |r|, and
55 // |p| from the scrypt specification (see RFC 7914). |key_size_in_bits| must
56 // be a multiple of 8. The caller is responsible for deleting the returned
58 static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingScrypt(
60 const std::string& password,
61 const std::string& salt,
62 size_t cost_parameter,
64 size_t parallelization_parameter,
65 size_t max_memory_bytes,
66 size_t key_size_in_bits);
68 // Imports an array of key bytes in |raw_key|. This key may have been
69 // generated by GenerateRandomKey or DeriveKeyFromPassword{Pbkdf2,Scrypt} and
70 // exported with key(). The key must be of suitable size for use with
71 // |algorithm|. The caller owns the returned SymmetricKey.
72 static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm,
73 const std::string& raw_key);
75 // Returns the raw platform specific key data.
76 const std::string& key() const { return key_; }
83 DISALLOW_COPY_AND_ASSIGN(SymmetricKey);
88 #endif // CRYPTO_SYMMETRIC_KEY_H_