Fix build when webrtc is disabled; i.e. when enable_webrtc is set to 0 in build/commo...
[chromium-blink-merge.git] / crypto / symmetric_key_nss.cc
blob546c457d5555362b59fb0dffc1d49815428d0056
1 // Copyright (c) 2011 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();
23 if (key_size_in_bits == 0)
24 return NULL;
26 ScopedPK11Slot slot(PK11_GetBestSlot(CKM_AES_KEY_GEN, NULL));
27 if (!slot.get())
28 return NULL;
30 PK11SymKey* sym_key = PK11_KeyGen(slot.get(), CKM_AES_KEY_GEN, NULL,
31 key_size_in_bits / 8, NULL);
32 if (!sym_key)
33 return NULL;
35 return new SymmetricKey(sym_key);
38 // static
39 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
40 const std::string& password,
41 const std::string& salt,
42 size_t iterations,
43 size_t key_size_in_bits) {
44 EnsureNSSInit();
45 if (salt.empty() || iterations == 0 || key_size_in_bits == 0)
46 return NULL;
48 SECItem password_item;
49 password_item.type = siBuffer;
50 password_item.data = reinterpret_cast<unsigned char*>(
51 const_cast<char *>(password.data()));
52 password_item.len = password.size();
54 SECItem salt_item;
55 salt_item.type = siBuffer;
56 salt_item.data = reinterpret_cast<unsigned char*>(
57 const_cast<char *>(salt.data()));
58 salt_item.len = salt.size();
60 SECOidTag cipher_algorithm =
61 algorithm == AES ? SEC_OID_AES_256_CBC : SEC_OID_HMAC_SHA1;
62 ScopedSECAlgorithmID alg_id(PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2,
63 cipher_algorithm,
64 SEC_OID_HMAC_SHA1,
65 key_size_in_bits / 8,
66 iterations,
67 &salt_item));
68 if (!alg_id.get())
69 return NULL;
71 ScopedPK11Slot slot(PK11_GetBestSlot(SEC_OID_PKCS5_PBKDF2, NULL));
72 if (!slot.get())
73 return NULL;
75 PK11SymKey* sym_key = PK11_PBEKeyGen(slot.get(), alg_id.get(), &password_item,
76 PR_FALSE, NULL);
77 if (!sym_key)
78 return NULL;
80 return new SymmetricKey(sym_key);
83 // static
84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
85 const std::string& raw_key) {
86 CK_MECHANISM_TYPE cipher =
87 algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC;
89 SECItem key_item;
90 key_item.type = siBuffer;
91 key_item.data = reinterpret_cast<unsigned char*>(
92 const_cast<char *>(raw_key.data()));
93 key_item.len = raw_key.size();
95 ScopedPK11Slot slot(PK11_GetBestSlot(cipher, NULL));
96 if (!slot.get())
97 return NULL;
99 // The exact value of the |origin| argument doesn't matter to NSS as long as
100 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a
101 // placeholder.
102 PK11SymKey* sym_key = PK11_ImportSymKey(slot.get(), cipher, PK11_OriginUnwrap,
103 CKA_ENCRYPT, &key_item, NULL);
104 if (!sym_key)
105 return NULL;
107 return new SymmetricKey(sym_key);
110 bool SymmetricKey::GetRawKey(std::string* raw_key) {
111 SECStatus rv = PK11_ExtractKeyValue(key_.get());
112 if (SECSuccess != rv)
113 return false;
115 SECItem* key_item = PK11_GetKeyData(key_.get());
116 if (!key_item)
117 return false;
119 raw_key->assign(reinterpret_cast<char*>(key_item->data), key_item->len);
120 return true;
123 #if defined(OS_CHROMEOS)
124 // static
125 SymmetricKey* SymmetricKey::CreateFromKey(PK11SymKey* key) {
126 return new SymmetricKey(key);
128 #endif
130 SymmetricKey::SymmetricKey(PK11SymKey* key) : key_(key) {
131 DCHECK(key);
134 } // namespace crypto