[go: nahoru, domu]

blob: 9dc71e9e43ced7b645e4e770e3bba16714b2c9cc [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
avidd373b82015-12-21 21:34:437#include <stddef.h>
8#include <stdint.h>
joth@chromium.orgac0f8be2010-11-12 12:03:549
10#include <algorithm>
rsleeviffe5a132016-06-28 01:51:5211#include <utility>
joth@chromium.orgac0f8be2010-11-12 12:03:5412
Hans Wennborg4d0e1802020-04-24 20:19:4313#include "base/check_op.h"
14#include "base/notreached.h"
avi@chromium.org0d8db082013-06-11 07:27:0115#include "base/strings/string_util.h"
rvargas@google.com4b559b4d2011-04-14 17:37:1416#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3317#include "third_party/boringssl/src/include/openssl/evp.h"
18#include "third_party/boringssl/src/include/openssl/rand.h"
joth@chromium.org70372d42010-10-22 13:12:3419
rvargas@google.com4b559b4d2011-04-14 17:37:1420namespace crypto {
joth@chromium.org70372d42010-10-22 13:12:3421
David Davidovićf8cd6a02018-08-27 14:02:5122namespace {
23
24bool CheckDerivationParameters(SymmetricKey::Algorithm algorithm,
25 size_t key_size_in_bits) {
26 switch (algorithm) {
27 case SymmetricKey::AES:
David Benjamin9f8324582022-03-01 23:40:5528 // Check for supported key sizes. Historically, NSS supported AES-192
29 // while BoringSSL did not and this check aligned their behavior.
David Davidovićf8cd6a02018-08-27 14:02:5130 return key_size_in_bits == 128 || key_size_in_bits == 256;
31 case SymmetricKey::HMAC_SHA1:
32 return key_size_in_bits % 8 == 0 && key_size_in_bits != 0;
33 }
34
35 NOTREACHED();
36 return false;
37}
38
39} // namespace
40
joth@chromium.org70372d42010-10-22 13:12:3441SymmetricKey::~SymmetricKey() {
joth@chromium.orgac0f8be2010-11-12 12:03:5442 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
joth@chromium.org70372d42010-10-22 13:12:3443}
44
45// static
rsleeviffe5a132016-06-28 01:51:5246std::unique_ptr<SymmetricKey> SymmetricKey::GenerateRandomKey(
47 Algorithm algorithm,
48 size_t key_size_in_bits) {
joth@chromium.orgac0f8be2010-11-12 12:03:5449 DCHECK_EQ(AES, algorithm);
davidben@chromium.orga534bab2014-07-25 21:04:1550
David Benjamin9f8324582022-03-01 23:40:5551 // Check for supported key sizes. Historically, NSS supported AES-192 while
52 // BoringSSL did not and this check aligned their behavior.
davidben@chromium.orga534bab2014-07-25 21:04:1553 if (key_size_in_bits != 128 && key_size_in_bits != 256)
rsleeviffe5a132016-06-28 01:51:5254 return nullptr;
davidben@chromium.orga534bab2014-07-25 21:04:1555
pkasting@chromium.orgfdce4782011-11-29 20:06:1856 size_t key_size_in_bytes = key_size_in_bits / 8;
57 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
joth@chromium.orgac0f8be2010-11-12 12:03:5458
pkasting@chromium.orgfdce4782011-11-29 20:06:1859 if (key_size_in_bytes == 0)
rsleeviffe5a132016-06-28 01:51:5260 return nullptr;
joth@chromium.orgac0f8be2010-11-12 12:03:5461
joth@chromium.orgbe796bb2010-11-18 15:43:4362 OpenSSLErrStackTracer err_tracer(FROM_HERE);
thakisd1a18472016-04-08 22:30:4163 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
avidd373b82015-12-21 21:34:4364 uint8_t* key_data = reinterpret_cast<uint8_t*>(
Brett Wilsone3c4d1a2015-07-07 23:38:0965 base::WriteInto(&key->key_, key_size_in_bytes + 1));
joth@chromium.orgac0f8be2010-11-12 12:03:5466
pkasting@chromium.orgfdce4782011-11-29 20:06:1867 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
rsleeviffe5a132016-06-28 01:51:5268 return rv == 1 ? std::move(key) : nullptr;
joth@chromium.org70372d42010-10-22 13:12:3469}
70
71// static
David Davidovićf8cd6a02018-08-27 14:02:5172std::unique_ptr<SymmetricKey> SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
rsleeviffe5a132016-06-28 01:51:5273 Algorithm algorithm,
74 const std::string& password,
75 const std::string& salt,
76 size_t iterations,
77 size_t key_size_in_bits) {
David Davidovićf8cd6a02018-08-27 14:02:5178 if (!CheckDerivationParameters(algorithm, key_size_in_bits))
79 return nullptr;
davidben@chromium.orga534bab2014-07-25 21:04:1580
pkasting@chromium.orgfdce4782011-11-29 20:06:1881 size_t key_size_in_bytes = key_size_in_bits / 8;
joth@chromium.orgac0f8be2010-11-12 12:03:5482
joth@chromium.orgbe796bb2010-11-18 15:43:4383 OpenSSLErrStackTracer err_tracer(FROM_HERE);
thakisd1a18472016-04-08 22:30:4184 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
avidd373b82015-12-21 21:34:4385 uint8_t* key_data = reinterpret_cast<uint8_t*>(
Brett Wilsone3c4d1a2015-07-07 23:38:0986 base::WriteInto(&key->key_, key_size_in_bytes + 1));
David Davidovićf8cd6a02018-08-27 14:02:5187
avidd373b82015-12-21 21:34:4388 int rv = PKCS5_PBKDF2_HMAC_SHA1(
89 password.data(), password.length(),
svaldez9c641462016-05-02 20:49:0590 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(),
91 static_cast<unsigned>(iterations),
92 key_size_in_bytes, key_data);
rsleeviffe5a132016-06-28 01:51:5293 return rv == 1 ? std::move(key) : nullptr;
joth@chromium.org70372d42010-10-22 13:12:3494}
95
96// static
David Davidovićf8cd6a02018-08-27 14:02:5197std::unique_ptr<SymmetricKey> SymmetricKey::DeriveKeyFromPasswordUsingScrypt(
98 Algorithm algorithm,
99 const std::string& password,
100 const std::string& salt,
101 size_t cost_parameter,
102 size_t block_size,
103 size_t parallelization_parameter,
104 size_t max_memory_bytes,
105 size_t key_size_in_bits) {
106 if (!CheckDerivationParameters(algorithm, key_size_in_bits))
107 return nullptr;
108
109 size_t key_size_in_bytes = key_size_in_bits / 8;
110
111 OpenSSLErrStackTracer err_tracer(FROM_HERE);
112 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
113 uint8_t* key_data = reinterpret_cast<uint8_t*>(
114 base::WriteInto(&key->key_, key_size_in_bytes + 1));
115
116 int rv = EVP_PBE_scrypt(password.data(), password.length(),
117 reinterpret_cast<const uint8_t*>(salt.data()),
118 salt.length(), cost_parameter, block_size,
119 parallelization_parameter, max_memory_bytes, key_data,
120 key_size_in_bytes);
121 return rv == 1 ? std::move(key) : nullptr;
122}
123
124// static
rsleeviffe5a132016-06-28 01:51:52125std::unique_ptr<SymmetricKey> SymmetricKey::Import(Algorithm algorithm,
126 const std::string& raw_key) {
davidben@chromium.orga534bab2014-07-25 21:04:15127 if (algorithm == AES) {
David Benjamin9f8324582022-03-01 23:40:55128 // Check for supported key sizes. Historically, NSS supported AES-192 while
129 // BoringSSL did not and this check aligned their behavior.
davidben@chromium.orga534bab2014-07-25 21:04:15130 if (raw_key.size() != 128/8 && raw_key.size() != 256/8)
rsleeviffe5a132016-06-28 01:51:52131 return nullptr;
davidben@chromium.orga534bab2014-07-25 21:04:15132 }
133
thakisd1a18472016-04-08 22:30:41134 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
joth@chromium.orgac0f8be2010-11-12 12:03:54135 key->key_ = raw_key;
rsleeviffe5a132016-06-28 01:51:52136 return key;
joth@chromium.org70372d42010-10-22 13:12:34137}
138
rsleeviffe5a132016-06-28 01:51:52139SymmetricKey::SymmetricKey() = default;
140
rvargas@google.com4b559b4d2011-04-14 17:37:14141} // namespace crypto