Don't preload rarely seen large images
[chromium-blink-merge.git] / components / webcrypto / openssl / aes_algorithm_openssl.cc
blob05e30c5bfd56434313f58c95b7a592f74c185aec
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
3 // found in the LICENSE file.
5 #include "components/webcrypto/openssl/aes_algorithm_openssl.h"
7 #include "base/logging.h"
8 #include "components/webcrypto/crypto_data.h"
9 #include "components/webcrypto/jwk.h"
10 #include "components/webcrypto/openssl/key_openssl.h"
11 #include "components/webcrypto/openssl/util_openssl.h"
12 #include "components/webcrypto/status.h"
13 #include "components/webcrypto/webcrypto_util.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
16 namespace webcrypto {
18 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages,
19 const std::string& jwk_suffix)
20 : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) {
23 AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix)
24 : all_key_usages_(blink::WebCryptoKeyUsageEncrypt |
25 blink::WebCryptoKeyUsageDecrypt |
26 blink::WebCryptoKeyUsageWrapKey |
27 blink::WebCryptoKeyUsageUnwrapKey),
28 jwk_suffix_(jwk_suffix) {
31 Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
32 bool extractable,
33 blink::WebCryptoKeyUsageMask usages,
34 GenerateKeyResult* result) const {
35 Status status = CheckKeyCreationUsages(all_key_usages_, usages, false);
36 if (status.IsError())
37 return status;
39 unsigned int keylen_bits;
40 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
41 if (status.IsError())
42 return status;
44 return GenerateWebCryptoSecretKey(
45 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
46 extractable, usages, keylen_bits, result);
49 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
50 blink::WebCryptoKeyFormat format,
51 blink::WebCryptoKeyUsageMask usages) const {
52 switch (format) {
53 case blink::WebCryptoKeyFormatRaw:
54 case blink::WebCryptoKeyFormatJwk:
55 return CheckKeyCreationUsages(all_key_usages_, usages, false);
56 default:
57 return Status::ErrorUnsupportedImportKeyFormat();
61 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
62 const blink::WebCryptoAlgorithm& algorithm,
63 bool extractable,
64 blink::WebCryptoKeyUsageMask usages,
65 blink::WebCryptoKey* key) const {
66 const unsigned int keylen_bytes = key_data.byte_length();
67 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
68 if (status.IsError())
69 return status;
71 // No possibility of overflow.
72 unsigned int keylen_bits = keylen_bytes * 8;
74 return CreateWebCryptoSecretKey(
75 key_data,
76 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
77 extractable, usages, key);
80 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
81 const blink::WebCryptoAlgorithm& algorithm,
82 bool extractable,
83 blink::WebCryptoKeyUsageMask usages,
84 blink::WebCryptoKey* key) const {
85 std::vector<uint8_t> raw_data;
86 Status status = ReadAesSecretKeyJwk(key_data, jwk_suffix_, extractable,
87 usages, &raw_data);
88 if (status.IsError())
89 return status;
91 return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages,
92 key);
95 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
96 std::vector<uint8_t>* buffer) const {
97 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data();
98 return Status::Success();
101 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
102 std::vector<uint8_t>* buffer) const {
103 const std::vector<uint8_t>& raw_data =
104 SymKeyOpenSsl::Cast(key)->raw_key_data();
106 WriteSecretKeyJwk(CryptoData(raw_data),
107 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
108 key.extractable(), key.usages(), buffer);
110 return Status::Success();
113 Status AesAlgorithm::SerializeKeyForClone(
114 const blink::WebCryptoKey& key,
115 blink::WebVector<uint8_t>* key_data) const {
116 key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data());
117 return Status::Success();
120 Status AesAlgorithm::DeserializeKeyForClone(
121 const blink::WebCryptoKeyAlgorithm& algorithm,
122 blink::WebCryptoKeyType type,
123 bool extractable,
124 blink::WebCryptoKeyUsageMask usages,
125 const CryptoData& key_data,
126 blink::WebCryptoKey* key) const {
127 return ImportKeyRaw(key_data, CreateAlgorithm(algorithm.id()), extractable,
128 usages, key);
131 Status AesAlgorithm::GetKeyLength(
132 const blink::WebCryptoAlgorithm& key_length_algorithm,
133 bool* has_length_bits,
134 unsigned int* length_bits) const {
135 return GetAesKeyLength(key_length_algorithm, has_length_bits, length_bits);
138 } // namespace webcrypto