Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / webcrypto / algorithms / pbkdf2.cc
blob609e744ea8a3a4f5aa9ce83029b9f8b4b1f75bda
1 // Copyright 2015 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.
5 #include "base/stl_util.h"
6 #include "components/webcrypto/algorithm_implementation.h"
7 #include "components/webcrypto/algorithms/secret_key_util.h"
8 #include "components/webcrypto/algorithms/util_openssl.h"
9 #include "components/webcrypto/crypto_data.h"
10 #include "components/webcrypto/key.h"
11 #include "components/webcrypto/status.h"
12 #include "components/webcrypto/webcrypto_util.h"
13 #include "crypto/openssl_util.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
17 namespace webcrypto {
19 namespace {
21 const blink::WebCryptoKeyUsageMask kAllKeyUsages =
22 blink::WebCryptoKeyUsageDeriveKey | blink::WebCryptoKeyUsageDeriveBits;
24 class Pbkdf2Implementation : public AlgorithmImplementation {
25 public:
26 Pbkdf2Implementation() {}
28 Status VerifyKeyUsagesBeforeImportKey(
29 blink::WebCryptoKeyFormat format,
30 blink::WebCryptoKeyUsageMask usages) const override {
31 switch (format) {
32 case blink::WebCryptoKeyFormatRaw:
33 return CheckKeyCreationUsages(kAllKeyUsages, usages, false);
34 default:
35 return Status::ErrorUnsupportedImportKeyFormat();
39 Status ImportKeyRaw(const CryptoData& key_data,
40 const blink::WebCryptoAlgorithm& algorithm,
41 bool extractable,
42 blink::WebCryptoKeyUsageMask usages,
43 blink::WebCryptoKey* key) const override {
44 const blink::WebCryptoKeyAlgorithm key_algorithm =
45 blink::WebCryptoKeyAlgorithm::createWithoutParams(
46 blink::WebCryptoAlgorithmIdPbkdf2);
48 return CreateWebCryptoSecretKey(key_data, key_algorithm, extractable,
49 usages, key);
52 Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm,
53 const blink::WebCryptoKey& base_key,
54 bool has_optional_length_bits,
55 unsigned int optional_length_bits,
56 std::vector<uint8_t>* derived_bytes) const override {
57 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
59 if (!has_optional_length_bits)
60 return Status::ErrorPbkdf2DeriveBitsLengthNotSpecified();
62 if (optional_length_bits % 8)
63 return Status::ErrorPbkdf2InvalidLength();
65 const blink::WebCryptoPbkdf2Params* params = algorithm.pbkdf2Params();
67 const blink::WebCryptoAlgorithm& hash = params->hash();
68 const EVP_MD* digest_algorithm = GetDigest(hash.id());
69 if (!digest_algorithm)
70 return Status::ErrorUnsupported();
72 unsigned int keylen_bytes = optional_length_bits / 8;
73 derived_bytes->resize(keylen_bytes);
75 const std::vector<uint8_t>& password = GetSymmetricKeyData(base_key);
77 if (!PKCS5_PBKDF2_HMAC(
78 reinterpret_cast<const char*>(vector_as_array(&password)),
79 password.size(), params->salt().data(), params->salt().size(),
80 params->iterations(), digest_algorithm, keylen_bytes,
81 vector_as_array(derived_bytes))) {
82 return Status::OperationError();
84 return Status::Success();
87 Status DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm,
88 blink::WebCryptoKeyType type,
89 bool extractable,
90 blink::WebCryptoKeyUsageMask usages,
91 const CryptoData& key_data,
92 blink::WebCryptoKey* key) const override {
93 return CreateWebCryptoSecretKey(key_data, algorithm, extractable, usages,
94 key);
97 Status GetKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm,
98 bool* has_length_bits,
99 unsigned int* length_bits) const override {
100 *has_length_bits = false;
101 return Status::Success();
105 } // namespace
107 scoped_ptr<AlgorithmImplementation> CreatePbkdf2Implementation() {
108 return make_scoped_ptr(new Pbkdf2Implementation);
111 } // namespace webcrypto