1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/crypto/symmetric_key.h"
#include <nss.h>
#include <pk11pub.h>
#include "base/nss_util.h"
#include "base/logging.h"
namespace base {
SymmetricKey::~SymmetricKey() {}
// static
SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
size_t key_size_in_bits) {
DCHECK_EQ(AES, algorithm);
EnsureNSSInit();
if (key_size_in_bits == 0)
return NULL;
ScopedPK11Slot slot(PK11_GetBestSlot(CKM_AES_KEY_GEN, NULL));
if (!slot.get())
return NULL;
PK11SymKey* sym_key = PK11_KeyGen(slot.get(), CKM_AES_KEY_GEN, NULL,
key_size_in_bits / 8, NULL);
if (!sym_key)
return NULL;
return new SymmetricKey(sym_key);
}
// static
SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
const std::string& password,
const std::string& salt,
size_t iterations,
size_t key_size_in_bits) {
EnsureNSSInit();
if (salt.empty() || iterations == 0 || key_size_in_bits == 0)
return NULL;
SECItem password_item;
password_item.type = siBuffer;
password_item.data = reinterpret_cast<unsigned char*>(
const_cast<char *>(password.data()));
password_item.len = password.size();
SECItem salt_item;
salt_item.type = siBuffer;
salt_item.data = reinterpret_cast<unsigned char*>(
const_cast<char *>(salt.data()));
salt_item.len = salt.size();
SECOidTag cipher_algorithm =
algorithm == AES ? SEC_OID_AES_256_CBC : SEC_OID_HMAC_SHA1;
ScopedSECAlgorithmID alg_id(PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2,
cipher_algorithm,
SEC_OID_HMAC_SHA1,
key_size_in_bits / 8,
iterations,
&salt_item));
if (!alg_id.get())
return NULL;
ScopedPK11Slot slot(PK11_GetBestSlot(SEC_OID_PKCS5_PBKDF2, NULL));
if (!slot.get())
return NULL;
PK11SymKey* sym_key = PK11_PBEKeyGen(slot.get(), alg_id.get(), &password_item,
PR_FALSE, NULL);
if (!sym_key)
return NULL;
return new SymmetricKey(sym_key);
}
bool SymmetricKey::GetRawKey(std::string* raw_key) {
SECStatus rv = PK11_ExtractKeyValue(key_.get());
if (SECSuccess != rv)
return false;
SECItem* key_item = PK11_GetKeyData(key_.get());
if (!key_item)
return false;
raw_key->assign(reinterpret_cast<char*>(key_item->data), key_item->len);
return true;
}
} // namespace base
|