Fix wrong enum for Tab.Discarding histogram.
[chromium-blink-merge.git] / crypto / symmetric_key_nss.cc
blob95ca9bd073ed8bbedbdbe547b5342f645ad38892
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"
7 #include <nss.h>
8 #include <pk11pub.h>
10 #include "base/logging.h"
11 #include "crypto/nss_util.h"
13 namespace crypto {
15 SymmetricKey::~SymmetricKey() {}
17 // static
18 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
19 size_t key_size_in_bits) {
20 DCHECK_EQ(AES, algorithm);
22 EnsureNSSInit();
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)
28 return NULL;
30 ScopedPK11Slot slot(PK11_GetInternalSlot());
31 if (!slot.get())
32 return NULL;
34 PK11SymKey* sym_key = PK11_KeyGen(slot.get(), CKM_AES_KEY_GEN, NULL,
35 key_size_in_bits / 8, NULL);
36 if (!sym_key)
37 return NULL;
39 return new SymmetricKey(sym_key);
42 // static
43 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
44 const std::string& password,
45 const std::string& salt,
46 size_t iterations,
47 size_t key_size_in_bits) {
48 EnsureNSSInit();
49 if (salt.empty() || iterations == 0 || key_size_in_bits == 0)
50 return NULL;
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)
57 return NULL;
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();
66 SECItem salt_item;
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,
75 cipher_algorithm,
76 SEC_OID_HMAC_SHA1,
77 key_size_in_bits / 8,
78 iterations,
79 &salt_item));
80 if (!alg_id.get())
81 return NULL;
83 ScopedPK11Slot slot(PK11_GetInternalSlot());
84 if (!slot.get())
85 return NULL;
87 PK11SymKey* sym_key = PK11_PBEKeyGen(slot.get(), alg_id.get(), &password_item,
88 PR_FALSE, NULL);
89 if (!sym_key)
90 return NULL;
92 return new SymmetricKey(sym_key);
95 // static
96 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
97 const std::string& raw_key) {
98 EnsureNSSInit();
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)
105 return NULL;
108 CK_MECHANISM_TYPE cipher =
109 algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC;
111 SECItem key_item;
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());
118 if (!slot.get())
119 return NULL;
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
123 // placeholder.
124 PK11SymKey* sym_key = PK11_ImportSymKey(slot.get(), cipher, PK11_OriginUnwrap,
125 CKA_ENCRYPT, &key_item, NULL);
126 if (!sym_key)
127 return 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)
135 return false;
137 SECItem* key_item = PK11_GetKeyData(key_.get());
138 if (!key_item)
139 return false;
141 raw_key->assign(reinterpret_cast<char*>(key_item->data), key_item->len);
142 return true;
145 SymmetricKey::SymmetricKey(PK11SymKey* key) : key_(key) {
146 DCHECK(key);
149 } // namespace crypto