Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include <openssl/err.h> | |
| 6 #include <openssl/hkdf.h> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "content/child/webcrypto/algorithm_implementation.h" | |
| 11 #include "content/child/webcrypto/crypto_data.h" | |
| 12 #include "content/child/webcrypto/openssl/key_openssl.h" | |
| 13 #include "content/child/webcrypto/openssl/util_openssl.h" | |
| 14 #include "content/child/webcrypto/status.h" | |
| 15 #include "content/child/webcrypto/webcrypto_util.h" | |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 17 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace webcrypto { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const blink::WebCryptoKeyUsageMask kValidUsages = | |
| 26 blink::WebCryptoKeyUsageDeriveKey | blink::WebCryptoKeyUsageDeriveBits; | |
| 27 | |
| 28 class HkdfImplementation : public AlgorithmImplementation { | |
| 29 public: | |
| 30 HkdfImplementation() {} | |
| 31 | |
| 32 Status VerifyKeyUsagesBeforeImportKey( | |
| 33 blink::WebCryptoKeyFormat format, | |
| 34 blink::WebCryptoKeyUsageMask usages) const override { | |
| 35 if (format != blink::WebCryptoKeyFormatRaw) | |
| 36 return Status::ErrorUnsupportedImportKeyFormat(); | |
| 37 return CheckKeyCreationUsages(kValidUsages, usages, false); | |
| 38 } | |
| 39 | |
| 40 Status ImportKeyRaw(const CryptoData& key_data, | |
| 41 const blink::WebCryptoAlgorithm& algorithm, | |
| 42 bool extractable, | |
| 43 blink::WebCryptoKeyUsageMask usages, | |
| 44 blink::WebCryptoKey* key) const override { | |
| 45 return CreateWebCryptoSecretKey( | |
| 46 key_data, blink::WebCryptoKeyAlgorithm::createWithoutParams( | |
| 47 blink::WebCryptoAlgorithmIdHkdf), | |
|
eroman
2015/01/09 21:58:07
did you run git cl format? not sure this is correc
nharper
2015/01/09 22:45:58
I did run it. I agree that it looks weird. (I trie
| |
| 48 extractable, usages, key); | |
| 49 } | |
| 50 | |
| 51 Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, | |
| 52 const blink::WebCryptoKey& base_key, | |
| 53 bool has_optional_length_bits, | |
| 54 unsigned int optional_length_bits, | |
| 55 std::vector<uint8_t>* derived_bytes) const override { | |
|
davidben
2015/01/09 22:54:46
This function should have a crypto::OpenSSLErrStac
nharper
2015/01/09 23:07:16
Done.
| |
| 56 if (!has_optional_length_bits) | |
| 57 return Status::ErrorHkdfDeriveBitsLengthNotSpecified(); | |
| 58 | |
| 59 const blink::WebCryptoHkdfParams* params = algorithm.hkdfParams(); | |
| 60 | |
| 61 const EVP_MD* digest_algorithm = GetDigest(params->hash().id()); | |
| 62 if (!digest_algorithm) | |
| 63 return Status::ErrorUnsupported(); | |
| 64 | |
| 65 // Size output to fit length | |
| 66 unsigned int derived_bytes_len = NumBitsToBytes(optional_length_bits); | |
| 67 derived_bytes->resize(derived_bytes_len); | |
| 68 | |
| 69 // Algorithm dispatch checks that the algorithm in |base_key| matches | |
| 70 // |algorithm|. | |
| 71 const std::vector<uint8_t>& raw_key = | |
| 72 SymKeyOpenSsl::Cast(base_key)->raw_key_data(); | |
| 73 const uint8_t* raw_key_ptr = raw_key.empty() ? NULL : &raw_key.front(); | |
| 74 uint8_t* derived_bytes_ptr = | |
| 75 derived_bytes->empty() ? NULL : &derived_bytes->front(); | |
| 76 if (!HKDF(derived_bytes_ptr, derived_bytes_len, digest_algorithm, | |
|
eroman
2015/01/09 21:59:23
@davidben: Could you review the use of BoringSSL (
| |
| 77 raw_key_ptr, raw_key.size(), params->salt().data(), | |
| 78 params->salt().size(), params->info().data(), | |
| 79 params->info().size())) { | |
| 80 uint32_t error = ERR_get_error(); | |
| 81 if (ERR_GET_LIB(error) == ERR_LIB_HKDF && | |
| 82 ERR_GET_REASON(error) == HKDF_R_OUTPUT_TOO_LARGE) | |
|
davidben
2015/01/09 22:54:46
Style nit: Since the conditional is multiple lines
nharper
2015/01/09 23:07:16
Done. I'm a fan of curlies.
| |
| 83 return Status::ErrorHkdfLengthTooLong(); | |
| 84 return Status::OperationError(); | |
| 85 } | |
| 86 | |
| 87 TruncateToBitLength(optional_length_bits, derived_bytes); | |
| 88 return Status::Success(); | |
| 89 } | |
| 90 | |
| 91 Status SerializeKeyForClone( | |
| 92 const blink::WebCryptoKey& key, | |
| 93 blink::WebVector<uint8_t>* key_data) const override { | |
| 94 key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data()); | |
| 95 return Status::Success(); | |
| 96 } | |
| 97 | |
| 98 Status DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm, | |
| 99 blink::WebCryptoKeyType type, | |
| 100 bool extractable, | |
| 101 blink::WebCryptoKeyUsageMask usages, | |
| 102 const CryptoData& key_data, | |
| 103 blink::WebCryptoKey* key) const override { | |
| 104 return CreateWebCryptoSecretKey(key_data, algorithm, extractable, usages, | |
| 105 key); | |
| 106 } | |
| 107 | |
| 108 Status GetKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm, | |
| 109 bool* has_length_bits, | |
| 110 unsigned int* length_bits) const override { | |
| 111 *has_length_bits = false; | |
| 112 return Status::Success(); | |
| 113 } | |
| 114 }; | |
| 115 | |
| 116 } // namespace | |
| 117 | |
| 118 AlgorithmImplementation* CreatePlatformHkdfImplementation() { | |
| 119 return new HkdfImplementation; | |
| 120 } | |
| 121 | |
| 122 } // namespace webcrypto | |
| 123 | |
| 124 } // namespace content | |
| OLD | NEW |