Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 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 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 10 | #include <memory> |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "build/build_config.h" |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 15 | #include "crypto/crypto_export.h" |
| 16 | |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 17 | namespace crypto { |
| 18 | |
| 19 | // Wraps a platform-specific symmetric key and allows it to be held in a |
| 20 | // scoped_ptr. |
| 21 | class CRYPTO_EXPORT SymmetricKey { |
| 22 | public: |
| 23 | // Defines the algorithm that a key will be used with. See also |
| 24 | // classs Encrptor. |
| 25 | enum Algorithm { |
| 26 | AES, |
| 27 | HMAC_SHA1, |
| 28 | }; |
| 29 | |
| 30 | virtual ~SymmetricKey(); |
| 31 | |
| 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. |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 35 | static std::unique_ptr<SymmetricKey> GenerateRandomKey( |
| 36 | Algorithm algorithm, |
| 37 | size_t key_size_in_bits); |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 38 | |
| 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 |
| 43 | // SymmetricKey. |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 44 | static std::unique_ptr<SymmetricKey> DeriveKeyFromPassword( |
| 45 | Algorithm algorithm, |
| 46 | const std::string& password, |
| 47 | const std::string& salt, |
| 48 | size_t iterations, |
| 49 | size_t key_size_in_bits); |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 50 | |
| 51 | // Imports an array of key bytes in |raw_key|. This key may have been |
| 52 | // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame] | 53 | // key(). The key must be of suitable size for use with |algorithm|. |
| 54 | // The caller owns the returned SymmetricKey. |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 55 | static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm, |
| 56 | const std::string& raw_key); |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 57 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame] | 58 | // Returns the raw platform specific key data. |
| 59 | const std::string& key() const { return key_; } |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 60 | |
| 61 | private: |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 62 | SymmetricKey(); |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 63 | |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 64 | std::string key_; |
Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
| 67 | }; |
| 68 | |
| 69 | } // namespace crypto |
| 70 | |
| 71 | #endif // CRYPTO_SYMMETRIC_KEY_H_ |