blob: d802241a0a0f66ad944dd608c62810b37f01efb6 [file] [log] [blame]
[email protected]45a445212012-06-15 08:11:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]41c78fa2010-03-22 20:08:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_SYMMETRIC_KEY_H_
6#define CRYPTO_SYMMETRIC_KEY_H_
[email protected]41c78fa2010-03-22 20:08:417
avidd373b8b2015-12-21 21:34:438#include <stddef.h>
9
rsleeviffe5a132016-06-28 01:51:5210#include <memory>
[email protected]41c78fa2010-03-22 20:08:4111#include <string>
12
avidd373b8b2015-12-21 21:34:4313#include "base/macros.h"
14#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1115#include "crypto/crypto_export.h"
[email protected]41c78fa2010-03-22 20:08:4116
[email protected]4b559b4d2011-04-14 17:37:1417namespace crypto {
[email protected]41c78fa2010-03-22 20:08:4118
19// Wraps a platform-specific symmetric key and allows it to be held in a
20// scoped_ptr.
[email protected]d613a9902011-08-05 20:59:1121class CRYPTO_EXPORT SymmetricKey {
[email protected]41c78fa2010-03-22 20:08:4122 public:
[email protected]03d86982010-11-16 12:28:5123 // Defines the algorithm that a key will be used with. See also
24 // classs Encrptor.
[email protected]39422e32010-03-25 19:13:0025 enum Algorithm {
26 AES,
27 HMAC_SHA1,
28 };
[email protected]41c78fa2010-03-22 20:08:4129
[email protected]692033a2010-04-09 18:40:5030 virtual ~SymmetricKey();
[email protected]41c78fa2010-03-22 20:08:4131
[email protected]03d86982010-11-16 12:28:5132 // Generates a random key suitable to be used with |algorithm| and of
[email protected]fdce4782011-11-29 20:06:1833 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8.
[email protected]108118232010-03-29 18:22:2434 // The caller is responsible for deleting the returned SymmetricKey.
rsleeviffe5a132016-06-28 01:51:5235 static std::unique_ptr<SymmetricKey> GenerateRandomKey(
36 Algorithm algorithm,
37 size_t key_size_in_bits);
[email protected]39422e32010-03-25 19:13:0038
[email protected]03d86982010-11-16 12:28:5139 // Derives a key from the supplied password and salt using PBKDF2, suitable
40 // for use with specified |algorithm|. Note |algorithm| is not the algorithm
[email protected]fdce4782011-11-29 20:06:1841 // 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
43 // SymmetricKey.
David Davidovićf8cd6a02018-08-27 14:02:5144 static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingPbkdf2(
rsleeviffe5a132016-06-28 01:51:5245 Algorithm algorithm,
46 const std::string& password,
47 const std::string& salt,
48 size_t iterations,
49 size_t key_size_in_bits);
[email protected]39422e32010-03-25 19:13:0050
David Davidovićf8cd6a02018-08-27 14:02:5151 // 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
57 // SymmetricKey.
58 static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingScrypt(
59 Algorithm algorithm,
60 const std::string& password,
61 const std::string& salt,
62 size_t cost_parameter,
63 size_t block_size,
64 size_t parallelization_parameter,
65 size_t max_memory_bytes,
66 size_t key_size_in_bits);
67
[email protected]f48fdae2010-11-19 14:20:2768 // Imports an array of key bytes in |raw_key|. This key may have been
David Davidovićf8cd6a02018-08-27 14:02:5169 // 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.
rsleeviffe5a132016-06-28 01:51:5272 static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm,
73 const std::string& raw_key);
[email protected]692033a2010-04-09 18:40:5074
Chris Mumford7bdfcab2017-06-20 17:15:1475 // Returns the raw platform specific key data.
Chris Mumfordea3b6c192017-06-09 18:33:1376 const std::string& key() const { return key_; }
[email protected]41c78fa2010-03-22 20:08:4177
[email protected]41c78fa2010-03-22 20:08:4178 private:
rsleeviffe5a132016-06-28 01:51:5279 SymmetricKey();
80
davidben365d6962014-10-29 17:51:2581 std::string key_;
[email protected]41c78fa2010-03-22 20:08:4182
83 DISALLOW_COPY_AND_ASSIGN(SymmetricKey);
84};
85
[email protected]4b559b4d2011-04-14 17:37:1486} // namespace crypto
[email protected]41c78fa2010-03-22 20:08:4187
[email protected]4b559b4d2011-04-14 17:37:1488#endif // CRYPTO_SYMMETRIC_KEY_H_