Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: content/child/webcrypto/algorithm_dispatch.cc

Issue 749183004: WebCrypto: Implement crypto.subtle.deriveKey (chromium-side). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ecdh
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/webcrypto/algorithm_dispatch.h" 5 #include "content/child/webcrypto/algorithm_dispatch.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "content/child/webcrypto/algorithm_implementation.h" 8 #include "content/child/webcrypto/algorithm_implementation.h"
9 #include "content/child/webcrypto/algorithm_registry.h" 9 #include "content/child/webcrypto/algorithm_registry.h"
10 #include "content/child/webcrypto/crypto_data.h" 10 #include "content/child/webcrypto/crypto_data.h"
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 return Status::ErrorUnexpected(); 249 return Status::ErrorUnexpected();
250 250
251 if (algorithm.id() != base_key.algorithm().id()) 251 if (algorithm.id() != base_key.algorithm().id())
252 return Status::ErrorUnexpected(); 252 return Status::ErrorUnexpected();
253 253
254 const AlgorithmImplementation* impl = NULL; 254 const AlgorithmImplementation* impl = NULL;
255 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); 255 Status status = GetAlgorithmImplementation(algorithm.id(), &impl);
256 if (status.IsError()) 256 if (status.IsError())
257 return status; 257 return status;
258 258
259 return impl->DeriveBits(algorithm, base_key, length_bits, derived_bytes); 259 return impl->DeriveBits(algorithm, base_key, true, length_bits,
260 derived_bytes);
261 }
262
263 Status DeriveKey(const blink::WebCryptoAlgorithm& algorithm,
264 const blink::WebCryptoKey& base_key,
265 const blink::WebCryptoAlgorithm& import_algorithm,
266 const blink::WebCryptoAlgorithm& key_length_algorithm,
267 bool extractable,
268 blink::WebCryptoKeyUsageMask usages,
269 blink::WebCryptoKey* derived_key) {
270 if (!KeyUsageAllows(base_key, blink::WebCryptoKeyUsageDeriveKey))
271 return Status::ErrorUnexpected();
272
273 if (algorithm.id() != base_key.algorithm().id())
274 return Status::ErrorUnexpected();
275
276 if (import_algorithm.id() != key_length_algorithm.id())
277 return Status::ErrorUnexpected();
278
279 const AlgorithmImplementation* import_impl = NULL;
280 Status status =
281 GetAlgorithmImplementation(import_algorithm.id(), &import_impl);
282 if (status.IsError())
283 return status;
284
285 // Fail fast if the requested key usages are incorect.
286 status = import_impl->VerifyKeyUsagesBeforeImportKey(
287 blink::WebCryptoKeyFormatRaw, usages);
288 if (status.IsError())
289 return status;
290
291 // Determine how many bits long the derived key should be.
292 unsigned int length_bits = 0;
293 bool has_length_bits = false;
294 status = import_impl->GetKeyLength(key_length_algorithm, &has_length_bits,
295 &length_bits);
296 if (status.IsError())
297 return status;
298
299 // Derive the key bytes.
300 const AlgorithmImplementation* derive_impl = NULL;
301 status = GetAlgorithmImplementation(algorithm.id(), &derive_impl);
302 if (status.IsError())
303 return status;
304
305 std::vector<uint8_t> derived_bytes;
306 status = derive_impl->DeriveBits(algorithm, base_key, has_length_bits,
307 length_bits, &derived_bytes);
308 if (status.IsError())
309 return status;
310
311 // Create the key using the derived bytes.
312 return ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(derived_bytes),
313 import_algorithm, extractable, usages, derived_key);
260 } 314 }
261 315
262 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( 316 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
263 blink::WebCryptoAlgorithmId algorithm) { 317 blink::WebCryptoAlgorithmId algorithm) {
264 PlatformInit(); 318 PlatformInit();
265 return CreatePlatformDigestor(algorithm); 319 return CreatePlatformDigestor(algorithm);
266 } 320 }
267 321
268 bool SerializeKeyForClone(const blink::WebCryptoKey& key, 322 bool SerializeKeyForClone(const blink::WebCryptoKey& key,
269 blink::WebVector<uint8_t>* key_data) { 323 blink::WebVector<uint8_t>* key_data) {
(...skipping 18 matching lines...) Expand all
288 return false; 342 return false;
289 343
290 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages, 344 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages,
291 key_data, key); 345 key_data, key);
292 return status.IsSuccess(); 346 return status.IsSuccess();
293 } 347 }
294 348
295 } // namespace webcrypto 349 } // namespace webcrypto
296 350
297 } // namespace content 351 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698