OLD | NEW |
1 // Copyright (c) 2012 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 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
14 #include "crypto/crypto_export.h" | 15 #include "crypto/crypto_export.h" |
15 | 16 |
16 namespace crypto { | 17 namespace crypto { |
17 | 18 |
18 // Wraps a platform-specific symmetric key and allows it to be held in a | 19 // Wraps a platform-specific symmetric key and allows it to be held in a |
19 // scoped_ptr. | 20 // scoped_ptr. |
20 class CRYPTO_EXPORT SymmetricKey { | 21 class CRYPTO_EXPORT SymmetricKey { |
21 public: | 22 public: |
22 // Defines the algorithm that a key will be used with. See also | 23 // Defines the algorithm that a key will be used with. See also |
23 // classs Encrptor. | 24 // classs Encrptor. |
24 enum Algorithm { | 25 enum Algorithm { |
25 AES, | 26 AES, |
26 HMAC_SHA1, | 27 HMAC_SHA1, |
27 }; | 28 }; |
28 | 29 |
29 virtual ~SymmetricKey(); | 30 virtual ~SymmetricKey(); |
30 | 31 |
31 // Generates a random key suitable to be used with |algorithm| and of | 32 // Generates a random key suitable to be used with |algorithm| and of |
32 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8. | 33 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8. |
33 // The caller is responsible for deleting the returned SymmetricKey. | 34 // The caller is responsible for deleting the returned SymmetricKey. |
34 static SymmetricKey* GenerateRandomKey(Algorithm algorithm, | 35 static std::unique_ptr<SymmetricKey> GenerateRandomKey( |
35 size_t key_size_in_bits); | 36 Algorithm algorithm, |
| 37 size_t key_size_in_bits); |
36 | 38 |
37 // Derives a key from the supplied password and salt using PBKDF2, suitable | 39 // Derives a key from the supplied password and salt using PBKDF2, suitable |
38 // for use with specified |algorithm|. Note |algorithm| is not the algorithm | 40 // for use with specified |algorithm|. Note |algorithm| is not the algorithm |
39 // used to derive the key from the password. |key_size_in_bits| must be a | 41 // used to derive the key from the password. |key_size_in_bits| must be a |
40 // multiple of 8. The caller is responsible for deleting the returned | 42 // multiple of 8. The caller is responsible for deleting the returned |
41 // SymmetricKey. | 43 // SymmetricKey. |
42 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm, | 44 static std::unique_ptr<SymmetricKey> DeriveKeyFromPassword( |
43 const std::string& password, | 45 Algorithm algorithm, |
44 const std::string& salt, | 46 const std::string& password, |
45 size_t iterations, | 47 const std::string& salt, |
46 size_t key_size_in_bits); | 48 size_t iterations, |
| 49 size_t key_size_in_bits); |
47 | 50 |
48 // Imports an array of key bytes in |raw_key|. This key may have been | 51 // Imports an array of key bytes in |raw_key|. This key may have been |
49 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with | 52 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with |
50 // GetRawKey, or via another compatible method. The key must be of suitable | 53 // GetRawKey, or via another compatible method. The key must be of suitable |
51 // size for use with |algorithm|. The caller owns the returned SymmetricKey. | 54 // size for use with |algorithm|. The caller owns the returned SymmetricKey. |
52 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key); | 55 static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm, |
| 56 const std::string& raw_key); |
53 | 57 |
54 const std::string& key() { return key_; } | 58 const std::string& key() { return key_; } |
55 | 59 |
56 // Extracts the raw key from the platform specific data. | 60 // Extracts the raw key from the platform specific data. |
57 // Warning: |raw_key| holds the raw key as bytes and thus must be handled | 61 // Warning: |raw_key| holds the raw key as bytes and thus must be handled |
58 // carefully. | 62 // carefully. |
59 bool GetRawKey(std::string* raw_key); | 63 bool GetRawKey(std::string* raw_key); |
60 | 64 |
61 private: | 65 private: |
62 SymmetricKey() {} | 66 SymmetricKey(); |
| 67 |
63 std::string key_; | 68 std::string key_; |
64 | 69 |
65 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); | 70 DISALLOW_COPY_AND_ASSIGN(SymmetricKey); |
66 }; | 71 }; |
67 | 72 |
68 } // namespace crypto | 73 } // namespace crypto |
69 | 74 |
70 #endif // CRYPTO_SYMMETRIC_KEY_H_ | 75 #endif // CRYPTO_SYMMETRIC_KEY_H_ |
OLD | NEW |