[go: nahoru, domu]

blob: e1c6fb7edede4026561ac121128416dd0398de23 [file] [log] [blame]
levin@chromium.org3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
joth@chromium.org70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rvargas@google.com4b559b4d2011-04-14 17:37:145#include "crypto/symmetric_key.h"
joth@chromium.org70372d42010-10-22 13:12:346
joth@chromium.orgac0f8be2010-11-12 12:03:547#include <openssl/evp.h>
8#include <openssl/rand.h>
9
10#include <algorithm>
11
joth@chromium.org70372d42010-10-22 13:12:3412#include "base/logging.h"
levin@chromium.org3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
avi@chromium.org0d8db082013-06-11 07:27:0114#include "base/strings/string_util.h"
rvargas@google.com4b559b4d2011-04-14 17:37:1415#include "crypto/openssl_util.h"
joth@chromium.org70372d42010-10-22 13:12:3416
rvargas@google.com4b559b4d2011-04-14 17:37:1417namespace crypto {
joth@chromium.org70372d42010-10-22 13:12:3418
19SymmetricKey::~SymmetricKey() {
joth@chromium.orgac0f8be2010-11-12 12:03:5420 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
joth@chromium.org70372d42010-10-22 13:12:3421}
22
23// static
24SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
25 size_t key_size_in_bits) {
joth@chromium.orgac0f8be2010-11-12 12:03:5426 DCHECK_EQ(AES, algorithm);
pkasting@chromium.orgfdce4782011-11-29 20:06:1827 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.orgac0f8be2010-11-12 12:03:5429
pkasting@chromium.orgfdce4782011-11-29 20:06:1830 if (key_size_in_bytes == 0)
joth@chromium.orgac0f8be2010-11-12 12:03:5431 return NULL;
32
joth@chromium.orgbe796bb2010-11-18 15:43:4333 OpenSSLErrStackTracer err_tracer(FROM_HERE);
joth@chromium.orgac0f8be2010-11-12 12:03:5434 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.orgfdce4782011-11-29 20:06:1838 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
joth@chromium.orgbe796bb2010-11-18 15:43:4339 return rv == 1 ? key.release() : NULL;
joth@chromium.org70372d42010-10-22 13:12:3440}
41
42// static
43SymmetricKey* 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.orgac0f8be2010-11-12 12:03:5448 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
pkasting@chromium.orgfdce4782011-11-29 20:06:1849 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.orgac0f8be2010-11-12 12:03:5454
joth@chromium.orgbe796bb2010-11-18 15:43:4355 OpenSSLErrStackTracer err_tracer(FROM_HERE);
joth@chromium.orgac0f8be2010-11-12 12:03:5456 scoped_ptr<SymmetricKey> key(new SymmetricKey);
57 uint8* key_data =
58 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
joth@chromium.orgbe796bb2010-11-18 15:43:4359 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
60 reinterpret_cast<const uint8*>(salt.data()),
61 salt.length(), iterations,
pkasting@chromium.orgfdce4782011-11-29 20:06:1862 static_cast<int>(key_size_in_bytes),
63 key_data);
joth@chromium.orgbe796bb2010-11-18 15:43:4364 return rv == 1 ? key.release() : NULL;
joth@chromium.org70372d42010-10-22 13:12:3465}
66
67// static
68SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
69 const std::string& raw_key) {
joth@chromium.orgac0f8be2010-11-12 12:03:5470 scoped_ptr<SymmetricKey> key(new SymmetricKey);
71 key->key_ = raw_key;
72 return key.release();
joth@chromium.org70372d42010-10-22 13:12:3473}
74
75bool SymmetricKey::GetRawKey(std::string* raw_key) {
joth@chromium.orgac0f8be2010-11-12 12:03:5476 *raw_key = key_;
77 return true;
joth@chromium.org70372d42010-10-22 13:12:3478}
79
rvargas@google.com4b559b4d2011-04-14 17:37:1480} // namespace crypto