Index: content/child/webcrypto/openssl/pbkdf2_openssl.cc |
diff --git a/content/child/webcrypto/openssl/pbkdf2_openssl.cc b/content/child/webcrypto/openssl/pbkdf2_openssl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7e71298be4e46a9f9bf90aeeda5135c8b25d22a1 |
--- /dev/null |
+++ b/content/child/webcrypto/openssl/pbkdf2_openssl.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/numerics/safe_math.h" |
+#include "content/child/webcrypto/algorithm_implementation.h" |
+#include "content/child/webcrypto/crypto_data.h" |
+#include "content/child/webcrypto/openssl/key_openssl.h" |
+#include "content/child/webcrypto/openssl/util_openssl.h" |
+#include "content/child/webcrypto/status.h" |
+#include "content/child/webcrypto/webcrypto_util.h" |
+#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
+#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
+ |
+namespace content { |
+ |
+namespace webcrypto { |
+ |
+namespace { |
+ |
+const blink::WebCryptoKeyUsageMask kAllKeyUsages = |
+ blink::WebCryptoKeyUsageDeriveKey | blink::WebCryptoKeyUsageDeriveBits; |
+ |
+class Pbkdf2Implementation : public AlgorithmImplementation { |
+ public: |
+ Pbkdf2Implementation() {} |
+ |
+ Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
eroman
2015/01/13 00:19:20
You can remove this alltogether since the default
xun.sun
2015/01/13 19:10:41
Done.
|
+ bool extractable, |
+ blink::WebCryptoKeyUsageMask usages, |
+ GenerateKeyResult* result) const override { |
+ return Status::ErrorUnsupported(); |
+ } |
+ |
+ Status VerifyKeyUsagesBeforeImportKey( |
+ blink::WebCryptoKeyFormat format, |
+ blink::WebCryptoKeyUsageMask usages) const override { |
+ switch (format) { |
+ case blink::WebCryptoKeyFormatRaw: |
+ return CheckKeyCreationUsages(kAllKeyUsages, usages, false); |
+ default: |
+ return Status::ErrorUnsupportedImportKeyFormat(); |
+ } |
+ } |
+ |
+ Status ImportKeyRaw(const CryptoData& key_data, |
+ const blink::WebCryptoAlgorithm& algorithm, |
+ bool extractable, |
+ blink::WebCryptoKeyUsageMask usages, |
+ blink::WebCryptoKey* key) const override { |
+ const blink::WebCryptoKeyAlgorithm key_algorithm = |
+ blink::WebCryptoKeyAlgorithm::createPbkdf2(algorithm.id()); |
+ |
+ return CreateWebCryptoSecretKey(key_data, key_algorithm, extractable, |
+ usages, key); |
+ } |
+ |
+ Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, |
+ const blink::WebCryptoKey& base_key, |
+ bool has_optional_length_bits, |
+ unsigned int optional_length_bits, |
+ std::vector<uint8_t>* derived_bytes) const override { |
+ if (!has_optional_length_bits || optional_length_bits % 8) |
+ return Status::ErrorPbkdf2InvalidLength(); |
+ |
eroman
2015/01/13 00:30:17
You also need a call to
crypto::OpenSSLErrStackTr
xun.sun
2015/01/13 19:10:41
Done.
|
+ const blink::WebCryptoPbkdf2Params* params = algorithm.pbkdf2Params(); |
+ |
+ const blink::WebCryptoAlgorithm& hash = params->hash(); |
+ const EVP_MD* digest_algorithm = GetDigest(hash.id()); |
+ if (!digest_algorithm) |
+ return Status::ErrorUnsupported(); |
+ |
+ unsigned int keylen_bytes = optional_length_bits / 8; |
+ derived_bytes->resize(keylen_bytes); |
+ |
+ const std::vector<uint8_t>& password = |
+ SymKeyOpenSsl::Cast(base_key)->raw_key_data(); |
+ |
+ base::CheckedNumeric<int> password_size = password.size(); |
+ if (!password_size.IsValid()) |
+ return Status::ErrorDataTooLarge(); |
+ |
+ int result = PKCS5_PBKDF2_HMAC( |
+ reinterpret_cast<const char*>(password.data()), |
eroman
2015/01/13 00:19:20
I am not sure that all chromium platforms support
xun.sun
2015/01/13 19:10:41
Yeah let's see what happens. Is making a copy the
|
+ password_size.ValueOrDie(), params->salt().data(), |
+ params->salt().size(), params->iterations(), digest_algorithm, |
+ keylen_bytes, derived_bytes->data()); |
+ |
+ if (result == 1) |
+ return Status::Success(); |
+ return Status::OperationError(); |
+ } |
+ |
+ Status SerializeKeyForClone( |
+ const blink::WebCryptoKey& key, |
+ blink::WebVector<uint8_t>* key_data) const override { |
+ key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data()); |
+ return Status::Success(); |
+ } |
+ |
+ Status DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm, |
+ blink::WebCryptoKeyType type, |
+ bool extractable, |
+ blink::WebCryptoKeyUsageMask usages, |
+ const CryptoData& key_data, |
+ blink::WebCryptoKey* key) const override { |
+ return CreateWebCryptoSecretKey(key_data, algorithm, extractable, usages, |
+ key); |
+ } |
eroman
2015/01/13 00:19:20
This algorithm also needs an implementation for Ge
xun.sun
2015/01/13 19:10:41
Implemented GetKeyLength(). How show I test it?
|
+}; |
+ |
+} // namespace |
+ |
+AlgorithmImplementation* CreatePlatformPbkdf2Implementation() { |
+ return new Pbkdf2Implementation; |
+} |
+ |
+} // namespace webcrypto |
+ |
+} // namespace content |