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 <openssl/err.h>
6 #include <openssl/hkdf.h>
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "components/webcrypto/algorithm_implementation.h"
11 #include "components/webcrypto/algorithms/util_openssl.h"
12 #include "components/webcrypto/crypto_data.h"
13 #include "components/webcrypto/key.h"
14 #include "components/webcrypto/status.h"
15 #include "components/webcrypto/webcrypto_util.h"
16 #include "crypto/openssl_util.h"
17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
18 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
24 const blink::WebCryptoKeyUsageMask kValidUsages
=
25 blink::WebCryptoKeyUsageDeriveKey
| blink::WebCryptoKeyUsageDeriveBits
;
27 class HkdfImplementation
: public AlgorithmImplementation
{
29 HkdfImplementation() {}
31 Status
VerifyKeyUsagesBeforeImportKey(
32 blink::WebCryptoKeyFormat format
,
33 blink::WebCryptoKeyUsageMask usages
) const override
{
34 if (format
!= blink::WebCryptoKeyFormatRaw
)
35 return Status::ErrorUnsupportedImportKeyFormat();
36 return CheckKeyCreationUsages(kValidUsages
, usages
, false);
39 Status
ImportKeyRaw(const CryptoData
& key_data
,
40 const blink::WebCryptoAlgorithm
& algorithm
,
42 blink::WebCryptoKeyUsageMask usages
,
43 blink::WebCryptoKey
* key
) const override
{
44 return CreateWebCryptoSecretKey(
45 key_data
, blink::WebCryptoKeyAlgorithm::createWithoutParams(
46 blink::WebCryptoAlgorithmIdHkdf
),
47 extractable
, usages
, key
);
50 Status
DeriveBits(const blink::WebCryptoAlgorithm
& algorithm
,
51 const blink::WebCryptoKey
& base_key
,
52 bool has_optional_length_bits
,
53 unsigned int optional_length_bits
,
54 std::vector
<uint8_t>* derived_bytes
) const override
{
55 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
56 if (!has_optional_length_bits
)
57 return Status::ErrorHkdfDeriveBitsLengthNotSpecified();
59 const blink::WebCryptoHkdfParams
* params
= algorithm
.hkdfParams();
61 const EVP_MD
* digest_algorithm
= GetDigest(params
->hash().id());
62 if (!digest_algorithm
)
63 return Status::ErrorUnsupported();
65 // Size output to fit length
66 unsigned int derived_bytes_len
= NumBitsToBytes(optional_length_bits
);
67 derived_bytes
->resize(derived_bytes_len
);
69 // Algorithm dispatch checks that the algorithm in |base_key| matches
71 const std::vector
<uint8_t>& raw_key
= GetSymmetricKeyData(base_key
);
72 if (!HKDF(vector_as_array(derived_bytes
), derived_bytes_len
,
73 digest_algorithm
, vector_as_array(&raw_key
), raw_key
.size(),
74 params
->salt().data(), params
->salt().size(),
75 params
->info().data(), params
->info().size())) {
76 uint32_t error
= ERR_get_error();
77 if (ERR_GET_LIB(error
) == ERR_LIB_HKDF
&&
78 ERR_GET_REASON(error
) == HKDF_R_OUTPUT_TOO_LARGE
) {
79 return Status::ErrorHkdfLengthTooLong();
81 return Status::OperationError();
84 TruncateToBitLength(optional_length_bits
, derived_bytes
);
85 return Status::Success();
88 Status
DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm
& algorithm
,
89 blink::WebCryptoKeyType type
,
91 blink::WebCryptoKeyUsageMask usages
,
92 const CryptoData
& key_data
,
93 blink::WebCryptoKey
* key
) const override
{
94 return CreateWebCryptoSecretKey(key_data
, algorithm
, extractable
, usages
,
98 Status
GetKeyLength(const blink::WebCryptoAlgorithm
& key_length_algorithm
,
99 bool* has_length_bits
,
100 unsigned int* length_bits
) const override
{
101 *has_length_bits
= false;
102 return Status::Success();
108 scoped_ptr
<AlgorithmImplementation
> CreateHkdfImplementation() {
109 return make_scoped_ptr(new HkdfImplementation
);
112 } // namespace webcrypto