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 "content/child/webcrypto/openssl/aes_algorithm_openssl.h"
7 #include "base/logging.h"
8 #include "content/child/webcrypto/crypto_data.h"
9 #include "content/child/webcrypto/jwk.h"
10 #include "content/child/webcrypto/openssl/key_openssl.h"
11 #include "content/child/webcrypto/openssl/util_openssl.h"
12 #include "content/child/webcrypto/status.h"
13 #include "content/child/webcrypto/webcrypto_util.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
20 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages
,
21 const std::string
& jwk_suffix
)
22 : all_key_usages_(all_key_usages
), jwk_suffix_(jwk_suffix
) {
25 AesAlgorithm::AesAlgorithm(const std::string
& jwk_suffix
)
26 : all_key_usages_(blink::WebCryptoKeyUsageEncrypt
|
27 blink::WebCryptoKeyUsageDecrypt
|
28 blink::WebCryptoKeyUsageWrapKey
|
29 blink::WebCryptoKeyUsageUnwrapKey
),
30 jwk_suffix_(jwk_suffix
) {
33 Status
AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm
& algorithm
,
35 blink::WebCryptoKeyUsageMask usages
,
36 GenerateKeyResult
* result
) const {
37 Status status
= CheckKeyCreationUsages(all_key_usages_
, usages
, false);
41 unsigned int keylen_bits
;
42 status
= GetAesKeyGenLengthInBits(algorithm
.aesKeyGenParams(), &keylen_bits
);
46 return GenerateWebCryptoSecretKey(
47 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
48 extractable
, usages
, keylen_bits
, result
);
51 Status
AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
52 blink::WebCryptoKeyFormat format
,
53 blink::WebCryptoKeyUsageMask usages
) const {
55 case blink::WebCryptoKeyFormatRaw
:
56 case blink::WebCryptoKeyFormatJwk
:
57 return CheckKeyCreationUsages(all_key_usages_
, usages
, false);
59 return Status::ErrorUnsupportedImportKeyFormat();
63 Status
AesAlgorithm::ImportKeyRaw(const CryptoData
& key_data
,
64 const blink::WebCryptoAlgorithm
& algorithm
,
66 blink::WebCryptoKeyUsageMask usages
,
67 blink::WebCryptoKey
* key
) const {
68 const unsigned int keylen_bytes
= key_data
.byte_length();
69 Status status
= VerifyAesKeyLengthForImport(keylen_bytes
);
73 // No possibility of overflow.
74 unsigned int keylen_bits
= keylen_bytes
* 8;
76 return CreateWebCryptoSecretKey(
78 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
79 extractable
, usages
, key
);
82 Status
AesAlgorithm::ImportKeyJwk(const CryptoData
& key_data
,
83 const blink::WebCryptoAlgorithm
& algorithm
,
85 blink::WebCryptoKeyUsageMask usages
,
86 blink::WebCryptoKey
* key
) const {
87 std::vector
<uint8_t> raw_data
;
88 Status status
= ReadAesSecretKeyJwk(key_data
, jwk_suffix_
, extractable
,
93 return ImportKeyRaw(CryptoData(raw_data
), algorithm
, extractable
, usages
,
97 Status
AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey
& key
,
98 std::vector
<uint8_t>* buffer
) const {
99 *buffer
= SymKeyOpenSsl::Cast(key
)->raw_key_data();
100 return Status::Success();
103 Status
AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey
& key
,
104 std::vector
<uint8_t>* buffer
) const {
105 const std::vector
<uint8_t>& raw_data
=
106 SymKeyOpenSsl::Cast(key
)->raw_key_data();
108 WriteSecretKeyJwk(CryptoData(raw_data
),
109 MakeJwkAesAlgorithmName(jwk_suffix_
, raw_data
.size()),
110 key
.extractable(), key
.usages(), buffer
);
112 return Status::Success();
115 Status
AesAlgorithm::SerializeKeyForClone(
116 const blink::WebCryptoKey
& key
,
117 blink::WebVector
<uint8_t>* key_data
) const {
118 key_data
->assign(SymKeyOpenSsl::Cast(key
)->serialized_key_data());
119 return Status::Success();
122 Status
AesAlgorithm::DeserializeKeyForClone(
123 const blink::WebCryptoKeyAlgorithm
& algorithm
,
124 blink::WebCryptoKeyType type
,
126 blink::WebCryptoKeyUsageMask usages
,
127 const CryptoData
& key_data
,
128 blink::WebCryptoKey
* key
) const {
129 return ImportKeyRaw(key_data
, CreateAlgorithm(algorithm
.id()), extractable
,
133 Status
AesAlgorithm::GetKeyLength(
134 const blink::WebCryptoAlgorithm
& key_length_algorithm
,
135 bool* has_length_bits
,
136 unsigned int* length_bits
) const {
137 return GetAesKeyLength(key_length_algorithm
, has_length_bits
, length_bits
);
140 } // namespace webcrypto
142 } // namespace content