levin@chromium.org | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rvargas@google.com | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/symmetric_key.h" |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 6 | |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 7 | #include <openssl/evp.h> |
| 8 | #include <openssl/rand.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 12 | #include "base/logging.h" |
levin@chromium.org | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
avi@chromium.org | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame^] | 14 | #include "base/strings/string_util.h" |
rvargas@google.com | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | #include "crypto/openssl_util.h" |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 16 | |
rvargas@google.com | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | namespace crypto { |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 18 | |
| 19 | SymmetricKey::~SymmetricKey() { |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 20 | std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // static |
| 24 | SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
| 25 | size_t key_size_in_bits) { |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 26 | DCHECK_EQ(AES, algorithm); |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 27 | size_t key_size_in_bytes = key_size_in_bits / 8; |
| 28 | DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 29 | |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 30 | if (key_size_in_bytes == 0) |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 31 | return NULL; |
| 32 | |
joth@chromium.org | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 33 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 34 | scoped_ptr<SymmetricKey> key(new SymmetricKey); |
| 35 | uint8* key_data = |
| 36 | reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); |
| 37 | |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 38 | int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); |
joth@chromium.org | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 39 | return rv == 1 ? key.release() : NULL; |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 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) { |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 48 | DCHECK(algorithm == AES || algorithm == HMAC_SHA1); |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 49 | size_t key_size_in_bytes = key_size_in_bits / 8; |
| 50 | DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
| 51 | |
| 52 | if (key_size_in_bytes == 0) |
| 53 | return NULL; |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 54 | |
joth@chromium.org | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 55 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 56 | scoped_ptr<SymmetricKey> key(new SymmetricKey); |
| 57 | uint8* key_data = |
| 58 | reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); |
joth@chromium.org | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 59 | int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), |
| 60 | reinterpret_cast<const uint8*>(salt.data()), |
| 61 | salt.length(), iterations, |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 62 | static_cast<int>(key_size_in_bytes), |
| 63 | key_data); |
joth@chromium.org | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 64 | return rv == 1 ? key.release() : NULL; |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // static |
| 68 | SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
| 69 | const std::string& raw_key) { |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 70 | scoped_ptr<SymmetricKey> key(new SymmetricKey); |
| 71 | key->key_ = raw_key; |
| 72 | return key.release(); |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool SymmetricKey::GetRawKey(std::string* raw_key) { |
joth@chromium.org | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 76 | *raw_key = key_; |
| 77 | return true; |
joth@chromium.org | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 78 | } |
| 79 | |
rvargas@google.com | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 80 | } // namespace crypto |