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.
5 #include "crypto/symmetric_key.h"
10 #include "base/logging.h"
11 #include "crypto/nss_util.h"
15 SymmetricKey::~SymmetricKey() {}
18 SymmetricKey
* SymmetricKey::GenerateRandomKey(Algorithm algorithm
,
19 size_t key_size_in_bits
) {
20 DCHECK_EQ(AES
, algorithm
);
24 // Whitelist supported key sizes to avoid accidentaly relying on
25 // algorithms available in NSS but not BoringSSL and vice
26 // versa. Note that BoringSSL does not support AES-192.
27 if (key_size_in_bits
!= 128 && key_size_in_bits
!= 256)
30 ScopedPK11Slot
slot(PK11_GetInternalSlot());
34 PK11SymKey
* sym_key
= PK11_KeyGen(slot
.get(), CKM_AES_KEY_GEN
, NULL
,
35 key_size_in_bits
/ 8, NULL
);
39 return new SymmetricKey(sym_key
);
43 SymmetricKey
* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm
,
44 const std::string
& password
,
45 const std::string
& salt
,
47 size_t key_size_in_bits
) {
49 if (salt
.empty() || iterations
== 0 || key_size_in_bits
== 0)
52 if (algorithm
== AES
) {
53 // Whitelist supported key sizes to avoid accidentaly relying on
54 // algorithms available in NSS but not BoringSSL and vice
55 // versa. Note that BoringSSL does not support AES-192.
56 if (key_size_in_bits
!= 128 && key_size_in_bits
!= 256)
60 SECItem password_item
;
61 password_item
.type
= siBuffer
;
62 password_item
.data
= reinterpret_cast<unsigned char*>(
63 const_cast<char *>(password
.data()));
64 password_item
.len
= password
.size();
67 salt_item
.type
= siBuffer
;
68 salt_item
.data
= reinterpret_cast<unsigned char*>(
69 const_cast<char *>(salt
.data()));
70 salt_item
.len
= salt
.size();
72 SECOidTag cipher_algorithm
=
73 algorithm
== AES
? SEC_OID_AES_256_CBC
: SEC_OID_HMAC_SHA1
;
74 ScopedSECAlgorithmID
alg_id(PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2
,
83 ScopedPK11Slot
slot(PK11_GetInternalSlot());
87 PK11SymKey
* sym_key
= PK11_PBEKeyGen(slot
.get(), alg_id
.get(), &password_item
,
92 return new SymmetricKey(sym_key
);
96 SymmetricKey
* SymmetricKey::Import(Algorithm algorithm
,
97 const std::string
& raw_key
) {
100 if (algorithm
== AES
) {
101 // Whitelist supported key sizes to avoid accidentaly relying on
102 // algorithms available in NSS but not BoringSSL and vice
103 // versa. Note that BoringSSL does not support AES-192.
104 if (raw_key
.size() != 128/8 && raw_key
.size() != 256/8)
108 CK_MECHANISM_TYPE cipher
=
109 algorithm
== AES
? CKM_AES_CBC
: CKM_SHA_1_HMAC
;
112 key_item
.type
= siBuffer
;
113 key_item
.data
= reinterpret_cast<unsigned char*>(
114 const_cast<char *>(raw_key
.data()));
115 key_item
.len
= raw_key
.size();
117 ScopedPK11Slot
slot(PK11_GetInternalSlot());
121 // The exact value of the |origin| argument doesn't matter to NSS as long as
122 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a
124 PK11SymKey
* sym_key
= PK11_ImportSymKey(slot
.get(), cipher
, PK11_OriginUnwrap
,
125 CKA_ENCRYPT
, &key_item
, NULL
);
129 return new SymmetricKey(sym_key
);
132 bool SymmetricKey::GetRawKey(std::string
* raw_key
) {
133 SECStatus rv
= PK11_ExtractKeyValue(key_
.get());
134 if (SECSuccess
!= rv
)
137 SECItem
* key_item
= PK11_GetKeyData(key_
.get());
141 raw_key
->assign(reinterpret_cast<char*>(key_item
->data
), key_item
->len
);
145 SymmetricKey::SymmetricKey(PK11SymKey
* key
) : key_(key
) {
149 } // namespace crypto