| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/symmetric_key.h" | 5 #include "crypto/symmetric_key.h" |
| 6 | 6 |
| 7 #include <nss.h> | 7 #include <nss.h> |
| 8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "crypto/nss_util.h" | 11 #include "crypto/nss_util.h" |
| 12 | 12 |
| 13 namespace crypto { | 13 namespace crypto { |
| 14 | 14 |
| 15 SymmetricKey::~SymmetricKey() {} | 15 SymmetricKey::~SymmetricKey() {} |
| 16 | 16 |
| 17 // static | 17 // static |
| 18 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 18 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
| 19 size_t key_size_in_bits) { | 19 size_t key_size_in_bits) { |
| 20 DCHECK_EQ(AES, algorithm); | 20 DCHECK_EQ(AES, algorithm); |
| 21 | 21 |
| 22 LOG(ERROR) << "HIIIIIIIIIIIIIIIIIIIIIIIIIII"; |
| 22 EnsureNSSInit(); | 23 EnsureNSSInit(); |
| 23 if (key_size_in_bits == 0) | 24 if (key_size_in_bits == 0) |
| 24 return NULL; | 25 return NULL; |
| 25 | 26 |
| 26 ScopedPK11Slot slot(PK11_GetInternalSlot()); | 27 ScopedPK11Slot slot(PK11_GetInternalSlot()); |
| 27 if (!slot.get()) | 28 if (!slot.get()) |
| 28 return NULL; | 29 return NULL; |
| 29 | 30 |
| 30 PK11SymKey* sym_key = PK11_KeyGen(slot.get(), CKM_AES_KEY_GEN, NULL, | 31 PK11SymKey* sym_key = PK11_KeyGen(slot.get(), CKM_AES_KEY_GEN, NULL, |
| 31 key_size_in_bits / 8, NULL); | 32 key_size_in_bits / 8, NULL); |
| 32 if (!sym_key) | 33 if (!sym_key) |
| 33 return NULL; | 34 return NULL; |
| 34 | 35 |
| 35 return new SymmetricKey(sym_key); | 36 return new SymmetricKey(sym_key); |
| 36 } | 37 } |
| 37 | 38 |
| 38 // static | 39 // static |
| 39 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 40 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
| 40 const std::string& password, | 41 const std::string& password, |
| 41 const std::string& salt, | 42 const std::string& salt, |
| 42 size_t iterations, | 43 size_t iterations, |
| 43 size_t key_size_in_bits) { | 44 size_t key_size_in_bits) { |
| 45 LOG(ERROR) << "HIIIIIIIIIIIIIIIIIIIIIIIIIII"; |
| 44 EnsureNSSInit(); | 46 EnsureNSSInit(); |
| 45 if (salt.empty() || iterations == 0 || key_size_in_bits == 0) | 47 if (salt.empty() || iterations == 0 || key_size_in_bits == 0) |
| 46 return NULL; | 48 return NULL; |
| 47 | 49 |
| 48 SECItem password_item; | 50 SECItem password_item; |
| 49 password_item.type = siBuffer; | 51 password_item.type = siBuffer; |
| 50 password_item.data = reinterpret_cast<unsigned char*>( | 52 password_item.data = reinterpret_cast<unsigned char*>( |
| 51 const_cast<char *>(password.data())); | 53 const_cast<char *>(password.data())); |
| 52 password_item.len = password.size(); | 54 password_item.len = password.size(); |
| 53 | 55 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 76 PR_FALSE, NULL); | 78 PR_FALSE, NULL); |
| 77 if (!sym_key) | 79 if (!sym_key) |
| 78 return NULL; | 80 return NULL; |
| 79 | 81 |
| 80 return new SymmetricKey(sym_key); | 82 return new SymmetricKey(sym_key); |
| 81 } | 83 } |
| 82 | 84 |
| 83 // static | 85 // static |
| 84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 86 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
| 85 const std::string& raw_key) { | 87 const std::string& raw_key) { |
| 88 LOG(ERROR) << "HIIIIIIIIIIIIIIIIIIIIIIIIIII"; |
| 86 EnsureNSSInit(); | 89 EnsureNSSInit(); |
| 87 CK_MECHANISM_TYPE cipher = | 90 CK_MECHANISM_TYPE cipher = |
| 88 algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC; | 91 algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC; |
| 89 | 92 |
| 90 SECItem key_item; | 93 SECItem key_item; |
| 91 key_item.type = siBuffer; | 94 key_item.type = siBuffer; |
| 92 key_item.data = reinterpret_cast<unsigned char*>( | 95 key_item.data = reinterpret_cast<unsigned char*>( |
| 93 const_cast<char *>(raw_key.data())); | 96 const_cast<char *>(raw_key.data())); |
| 94 key_item.len = raw_key.size(); | 97 key_item.len = raw_key.size(); |
| 95 | 98 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 126 SymmetricKey* SymmetricKey::CreateFromKey(PK11SymKey* key) { | 129 SymmetricKey* SymmetricKey::CreateFromKey(PK11SymKey* key) { |
| 127 return new SymmetricKey(key); | 130 return new SymmetricKey(key); |
| 128 } | 131 } |
| 129 #endif | 132 #endif |
| 130 | 133 |
| 131 SymmetricKey::SymmetricKey(PK11SymKey* key) : key_(key) { | 134 SymmetricKey::SymmetricKey(PK11SymKey* key) : key_(key) { |
| 132 DCHECK(key); | 135 DCHECK(key); |
| 133 } | 136 } |
| 134 | 137 |
| 135 } // namespace crypto | 138 } // namespace crypto |
| OLD | NEW |