Home | History | Annotate | Download | only in crypto
      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.
      4 
      5 #include "crypto/symmetric_key.h"
      6 
      7 #include <nss.h>
      8 #include <pk11pub.h>
      9 
     10 #include "base/logging.h"
     11 #include "crypto/nss_util.h"
     12 
     13 namespace crypto {
     14 
     15 SymmetricKey::~SymmetricKey() {}
     16 
     17 // static
     18 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
     19                                               size_t key_size_in_bits) {
     20   DCHECK_EQ(AES, algorithm);
     21 
     22   EnsureNSSInit();
     23 
     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;
     29 
     30   ScopedPK11Slot slot(PK11_GetInternalSlot());
     31   if (!slot.get())
     32     return NULL;
     33 
     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;
     38 
     39   return new SymmetricKey(sym_key);
     40 }
     41 
     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;
     51 
     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;
     58   }
     59 
     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();
     65 
     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();
     71 
     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;
     82 
     83   ScopedPK11Slot slot(PK11_GetInternalSlot());
     84   if (!slot.get())
     85     return NULL;
     86 
     87   PK11SymKey* sym_key = PK11_PBEKeyGen(slot.get(), alg_id.get(), &password_item,
     88                                        PR_FALSE, NULL);
     89   if (!sym_key)
     90     return NULL;
     91 
     92   return new SymmetricKey(sym_key);
     93 }
     94 
     95 // static
     96 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
     97                                    const std::string& raw_key) {
     98   EnsureNSSInit();
     99 
    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;
    106   }
    107 
    108   CK_MECHANISM_TYPE cipher =
    109       algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC;
    110 
    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();
    116 
    117   ScopedPK11Slot slot(PK11_GetInternalSlot());
    118   if (!slot.get())
    119     return NULL;
    120 
    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;
    128 
    129   return new SymmetricKey(sym_key);
    130 }
    131 
    132 bool SymmetricKey::GetRawKey(std::string* raw_key) {
    133   SECStatus rv = PK11_ExtractKeyValue(key_.get());
    134   if (SECSuccess != rv)
    135     return false;
    136 
    137   SECItem* key_item = PK11_GetKeyData(key_.get());
    138   if (!key_item)
    139     return false;
    140 
    141   raw_key->assign(reinterpret_cast<char*>(key_item->data), key_item->len);
    142   return true;
    143 }
    144 
    145 SymmetricKey::SymmetricKey(PK11SymKey* key) : key_(key) {
    146   DCHECK(key);
    147 }
    148 
    149 }  // namespace crypto
    150