blob: 88627084c6f735a396bb89694dfe63fe38c434a6 [file] [log] [blame]
Daniel Erat59c5f4b2015-08-24 12:50:25 -06001// 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 Vakulenko0d205d72016-01-15 13:02:14 -08008#include <stddef.h>
9
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070010#include <memory>
Daniel Erat59c5f4b2015-08-24 12:50:25 -060011#include <string>
12
Alex Vakulenko0d205d72016-01-15 13:02:14 -080013#include "base/macros.h"
14#include "build/build_config.h"
Daniel Erat59c5f4b2015-08-24 12:50:25 -060015#include "crypto/crypto_export.h"
16
17#if defined(NACL_WIN64)
18// See comments for crypto_nacl_win64 in crypto.gyp.
19// Must test for NACL_WIN64 before OS_WIN since former is a subset of latter.
20#include "crypto/scoped_capi_types.h"
21#elif defined(USE_NSS_CERTS) || \
22 (!defined(USE_OPENSSL) && (defined(OS_WIN) || defined(OS_MACOSX)))
23#include "crypto/scoped_nss_types.h"
24#endif
25
26namespace crypto {
27
28// Wraps a platform-specific symmetric key and allows it to be held in a
29// scoped_ptr.
30class CRYPTO_EXPORT SymmetricKey {
31 public:
32 // Defines the algorithm that a key will be used with. See also
33 // classs Encrptor.
34 enum Algorithm {
35 AES,
36 HMAC_SHA1,
37 };
38
39 virtual ~SymmetricKey();
40
41 // Generates a random key suitable to be used with |algorithm| and of
42 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8.
43 // The caller is responsible for deleting the returned SymmetricKey.
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070044 static std::unique_ptr<SymmetricKey> GenerateRandomKey(
45 Algorithm algorithm,
46 size_t key_size_in_bits);
Daniel Erat59c5f4b2015-08-24 12:50:25 -060047
48 // Derives a key from the supplied password and salt using PBKDF2, suitable
49 // for use with specified |algorithm|. Note |algorithm| is not the algorithm
50 // used to derive the key from the password. |key_size_in_bits| must be a
51 // multiple of 8. The caller is responsible for deleting the returned
52 // SymmetricKey.
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070053 static std::unique_ptr<SymmetricKey> DeriveKeyFromPassword(
54 Algorithm algorithm,
55 const std::string& password,
56 const std::string& salt,
57 size_t iterations,
58 size_t key_size_in_bits);
Daniel Erat59c5f4b2015-08-24 12:50:25 -060059
60 // Imports an array of key bytes in |raw_key|. This key may have been
61 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with
62 // GetRawKey, or via another compatible method. The key must be of suitable
63 // size for use with |algorithm|. The caller owns the returned SymmetricKey.
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070064 static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm,
65 const std::string& raw_key);
Daniel Erat59c5f4b2015-08-24 12:50:25 -060066#if defined(NACL_WIN64)
67 HCRYPTKEY key() const { return key_.get(); }
68#elif defined(USE_OPENSSL)
69 const std::string& key() { return key_; }
70#elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX)
71 PK11SymKey* key() const { return key_.get(); }
72#endif
73
74 // Extracts the raw key from the platform specific data.
75 // Warning: |raw_key| holds the raw key as bytes and thus must be handled
76 // carefully.
77 bool GetRawKey(std::string* raw_key);
78
79 private:
80#if defined(NACL_WIN64)
81 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key,
82 const void* key_data, size_t key_size_in_bytes);
83
84 ScopedHCRYPTPROV provider_;
85 ScopedHCRYPTKEY key_;
86
87 // Contains the raw key, if it is known during initialization and when it
88 // is likely that the associated |provider_| will be unable to export the
89 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes
90 // when using the default RSA provider.
91 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey
92 // fails with NTE_BAD_KEY/NTE_BAD_LEN
93 std::string raw_key_;
94#elif defined(USE_OPENSSL)
95 SymmetricKey() {}
96 std::string key_;
97#elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX)
98 explicit SymmetricKey(PK11SymKey* key);
99 ScopedPK11SymKey key_;
100#endif
101
102 DISALLOW_COPY_AND_ASSIGN(SymmetricKey);
103};
104
105} // namespace crypto
106
107#endif // CRYPTO_SYMMETRIC_KEY_H_