OLD | NEW |
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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 return Status::ErrorUnexpected(); | 268 return Status::ErrorUnexpected(); |
269 | 269 |
270 if (algorithm.id() != base_key.algorithm().id()) | 270 if (algorithm.id() != base_key.algorithm().id()) |
271 return Status::ErrorUnexpected(); | 271 return Status::ErrorUnexpected(); |
272 | 272 |
273 const AlgorithmImplementation* impl = NULL; | 273 const AlgorithmImplementation* impl = NULL; |
274 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 274 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
275 if (status.IsError()) | 275 if (status.IsError()) |
276 return status; | 276 return status; |
277 | 277 |
278 return impl->DeriveBits(algorithm, base_key, length_bits, derived_bytes); | 278 return impl->DeriveBits(algorithm, base_key, true, length_bits, |
| 279 derived_bytes); |
| 280 } |
| 281 |
| 282 Status DeriveKey(const blink::WebCryptoAlgorithm& algorithm, |
| 283 const blink::WebCryptoKey& base_key, |
| 284 const blink::WebCryptoAlgorithm& import_algorithm, |
| 285 const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 286 bool extractable, |
| 287 blink::WebCryptoKeyUsageMask usages, |
| 288 blink::WebCryptoKey* derived_key) { |
| 289 if (!KeyUsageAllows(base_key, blink::WebCryptoKeyUsageDeriveKey)) |
| 290 return Status::ErrorUnexpected(); |
| 291 |
| 292 if (algorithm.id() != base_key.algorithm().id()) |
| 293 return Status::ErrorUnexpected(); |
| 294 |
| 295 if (import_algorithm.id() != key_length_algorithm.id()) |
| 296 return Status::ErrorUnexpected(); |
| 297 |
| 298 const AlgorithmImplementation* import_impl = NULL; |
| 299 Status status = |
| 300 GetAlgorithmImplementation(import_algorithm.id(), &import_impl); |
| 301 if (status.IsError()) |
| 302 return status; |
| 303 |
| 304 // Fail fast if the requested key usages are incorect. |
| 305 status = import_impl->VerifyKeyUsagesBeforeImportKey( |
| 306 blink::WebCryptoKeyFormatRaw, usages); |
| 307 if (status.IsError()) |
| 308 return status; |
| 309 |
| 310 // Determine how many bits long the derived key should be. |
| 311 unsigned int length_bits = 0; |
| 312 bool has_length_bits = false; |
| 313 status = import_impl->GetKeyLength(key_length_algorithm, &has_length_bits, |
| 314 &length_bits); |
| 315 if (status.IsError()) |
| 316 return status; |
| 317 |
| 318 // Derive the key bytes. |
| 319 const AlgorithmImplementation* derive_impl = NULL; |
| 320 status = GetAlgorithmImplementation(algorithm.id(), &derive_impl); |
| 321 if (status.IsError()) |
| 322 return status; |
| 323 |
| 324 std::vector<uint8_t> derived_bytes; |
| 325 status = derive_impl->DeriveBits(algorithm, base_key, has_length_bits, |
| 326 length_bits, &derived_bytes); |
| 327 if (status.IsError()) |
| 328 return status; |
| 329 |
| 330 // Create the key using the derived bytes. |
| 331 return ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(derived_bytes), |
| 332 import_algorithm, extractable, usages, derived_key); |
279 } | 333 } |
280 | 334 |
281 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( | 335 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( |
282 blink::WebCryptoAlgorithmId algorithm) { | 336 blink::WebCryptoAlgorithmId algorithm) { |
283 PlatformInit(); | 337 PlatformInit(); |
284 return CreatePlatformDigestor(algorithm); | 338 return CreatePlatformDigestor(algorithm); |
285 } | 339 } |
286 | 340 |
287 bool SerializeKeyForClone(const blink::WebCryptoKey& key, | 341 bool SerializeKeyForClone(const blink::WebCryptoKey& key, |
288 blink::WebVector<uint8_t>* key_data) { | 342 blink::WebVector<uint8_t>* key_data) { |
(...skipping 18 matching lines...) Expand all Loading... |
307 return false; | 361 return false; |
308 | 362 |
309 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages, | 363 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages, |
310 key_data, key); | 364 key_data, key); |
311 return status.IsSuccess(); | 365 return status.IsSuccess(); |
312 } | 366 } |
313 | 367 |
314 } // namespace webcrypto | 368 } // namespace webcrypto |
315 | 369 |
316 } // namespace content | 370 } // namespace content |
OLD | NEW |