blob: 3c25fab258405c224a35148888dce69fe15a35a9 [file] [log] [blame]
[email protected]9493ee95c2011-03-28 23:48:441// Copyright (c) 2011 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]41c78fa2010-03-22 20:08:418
9#include <string>
10
11#include "base/basictypes.h"
[email protected]d613a9902011-08-05 20:59:1112#include "crypto/crypto_export.h"
[email protected]41c78fa2010-03-22 20:08:4113
14#if defined(USE_NSS)
[email protected]4b559b4d2011-04-14 17:37:1415#include "crypto/scoped_nss_types.h"
[email protected]108118232010-03-29 18:22:2416#elif defined(OS_MACOSX)
17#include <Security/cssmtype.h>
[email protected]692033a2010-04-09 18:40:5018#elif defined(OS_WIN)
[email protected]4b559b4d2011-04-14 17:37:1419#include "crypto/scoped_capi_types.h"
[email protected]108118232010-03-29 18:22:2420#endif
[email protected]41c78fa2010-03-22 20:08:4121
[email protected]4b559b4d2011-04-14 17:37:1422namespace crypto {
[email protected]41c78fa2010-03-22 20:08:4123
24// Wraps a platform-specific symmetric key and allows it to be held in a
25// scoped_ptr.
[email protected]d613a9902011-08-05 20:59:1126class CRYPTO_EXPORT SymmetricKey {
[email protected]41c78fa2010-03-22 20:08:4127 public:
[email protected]03d86982010-11-16 12:28:5128 // Defines the algorithm that a key will be used with. See also
29 // classs Encrptor.
[email protected]39422e32010-03-25 19:13:0030 enum Algorithm {
31 AES,
32 HMAC_SHA1,
33 };
[email protected]41c78fa2010-03-22 20:08:4134
[email protected]692033a2010-04-09 18:40:5035 virtual ~SymmetricKey();
[email protected]41c78fa2010-03-22 20:08:4136
[email protected]03d86982010-11-16 12:28:5137 // Generates a random key suitable to be used with |algorithm| and of
[email protected]fdce4782011-11-29 20:06:1838 // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8.
[email protected]108118232010-03-29 18:22:2439 // The caller is responsible for deleting the returned SymmetricKey.
40 static SymmetricKey* GenerateRandomKey(Algorithm algorithm,
41 size_t key_size_in_bits);
[email protected]39422e32010-03-25 19:13:0042
[email protected]03d86982010-11-16 12:28:5143 // Derives a key from the supplied password and salt using PBKDF2, suitable
44 // for use with specified |algorithm|. Note |algorithm| is not the algorithm
[email protected]fdce4782011-11-29 20:06:1845 // used to derive the key from the password. |key_size_in_bits| must be a
46 // multiple of 8. The caller is responsible for deleting the returned
47 // SymmetricKey.
[email protected]39422e32010-03-25 19:13:0048 static SymmetricKey* DeriveKeyFromPassword(Algorithm algorithm,
49 const std::string& password,
50 const std::string& salt,
51 size_t iterations,
[email protected]108118232010-03-29 18:22:2452 size_t key_size_in_bits);
[email protected]39422e32010-03-25 19:13:0053
[email protected]f48fdae2010-11-19 14:20:2754 // Imports an array of key bytes in |raw_key|. This key may have been
55 // generated by GenerateRandomKey or DeriveKeyFromPassword and exported with
56 // GetRawKey, or via another compatible method. The key must be of suitable
57 // size for use with |algorithm|. The caller owns the returned SymmetricKey.
[email protected]896200b32010-07-20 19:21:1858 static SymmetricKey* Import(Algorithm algorithm, const std::string& raw_key);
[email protected]692033a2010-04-09 18:40:5059
[email protected]25007102010-11-12 16:29:0660#if defined(USE_OPENSSL)
61 const std::string& key() { return key_; }
62#elif defined(USE_NSS)
[email protected]41c78fa2010-03-22 20:08:4163 PK11SymKey* key() const { return key_.get(); }
[email protected]108118232010-03-29 18:22:2464#elif defined(OS_MACOSX)
65 CSSM_DATA cssm_data() const;
[email protected]692033a2010-04-09 18:40:5066#elif defined(OS_WIN)
67 HCRYPTKEY key() const { return key_.get(); }
[email protected]108118232010-03-29 18:22:2468#endif
[email protected]41c78fa2010-03-22 20:08:4169
[email protected]896200b32010-07-20 19:21:1870 // Extracts the raw key from the platform specific data.
71 // Warning: |raw_key| holds the raw key as bytes and thus must be handled
72 // carefully.
[email protected]41c78fa2010-03-22 20:08:4173 bool GetRawKey(std::string* raw_key);
74
[email protected]6e3d9a92011-09-07 04:01:0375#if defined(OS_CHROMEOS)
76 // Creates symmetric key from NSS key. Takes over the ownership of |key|.
77 static SymmetricKey* CreateFromKey(PK11SymKey* key);
78#endif
79
[email protected]41c78fa2010-03-22 20:08:4180 private:
[email protected]70372d42010-10-22 13:12:3481#if defined(USE_OPENSSL)
[email protected]ac0f8be2010-11-12 12:03:5482 SymmetricKey() {}
83 std::string key_;
[email protected]70372d42010-10-22 13:12:3484#elif defined(USE_NSS)
[email protected]896200b32010-07-20 19:21:1885 explicit SymmetricKey(PK11SymKey* key);
[email protected]41c78fa2010-03-22 20:08:4186 ScopedPK11SymKey key_;
[email protected]108118232010-03-29 18:22:2487#elif defined(OS_MACOSX)
88 SymmetricKey(const void* key_data, size_t key_size_in_bits);
89 std::string key_;
[email protected]692033a2010-04-09 18:40:5090#elif defined(OS_WIN)
91 SymmetricKey(HCRYPTPROV provider, HCRYPTKEY key,
92 const void* key_data, size_t key_size_in_bytes);
93
94 ScopedHCRYPTPROV provider_;
95 ScopedHCRYPTKEY key_;
96
97 // Contains the raw key, if it is known during initialization and when it
98 // is likely that the associated |provider_| will be unable to export the
99 // |key_|. This is the case of HMAC keys when the key size exceeds 16 bytes
100 // when using the default RSA provider.
101 // TODO(rsleevi): See if KP_EFFECTIVE_KEYLEN is the reason why CryptExportKey
102 // fails with NTE_BAD_KEY/NTE_BAD_LEN
103 std::string raw_key_;
[email protected]108118232010-03-29 18:22:24104#endif
[email protected]41c78fa2010-03-22 20:08:41105
106 DISALLOW_COPY_AND_ASSIGN(SymmetricKey);
107};
108
[email protected]4b559b4d2011-04-14 17:37:14109} // namespace crypto
[email protected]41c78fa2010-03-22 20:08:41110
[email protected]4b559b4d2011-04-14 17:37:14111#endif // CRYPTO_SYMMETRIC_KEY_H_