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_key_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/sym_key_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::VerifyKeyUsagesBeforeGenerateKey(
34 blink::WebCryptoKeyUsageMask usage_mask
) const {
35 return CheckKeyCreationUsages(all_key_usages_
, usage_mask
);
38 Status
AesAlgorithm::GenerateSecretKey(
39 const blink::WebCryptoAlgorithm
& algorithm
,
41 blink::WebCryptoKeyUsageMask usage_mask
,
42 blink::WebCryptoKey
* key
) const {
43 unsigned int keylen_bits
;
45 GetAesKeyGenLengthInBits(algorithm
.aesKeyGenParams(), &keylen_bits
);
49 return GenerateSecretKeyOpenSsl(
50 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
57 Status
AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
58 blink::WebCryptoKeyFormat format
,
59 blink::WebCryptoKeyUsageMask usage_mask
) const {
61 case blink::WebCryptoKeyFormatRaw
:
62 case blink::WebCryptoKeyFormatJwk
:
63 return CheckKeyCreationUsages(all_key_usages_
, usage_mask
);
65 return Status::ErrorUnsupportedImportKeyFormat();
69 Status
AesAlgorithm::ImportKeyRaw(const CryptoData
& key_data
,
70 const blink::WebCryptoAlgorithm
& algorithm
,
72 blink::WebCryptoKeyUsageMask usage_mask
,
73 blink::WebCryptoKey
* key
) const {
74 const unsigned int keylen_bytes
= key_data
.byte_length();
75 Status status
= VerifyAesKeyLengthForImport(keylen_bytes
);
79 // No possibility of overflow.
80 unsigned int keylen_bits
= keylen_bytes
* 8;
82 return ImportKeyRawOpenSsl(
84 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
90 Status
AesAlgorithm::ImportKeyJwk(const CryptoData
& key_data
,
91 const blink::WebCryptoAlgorithm
& algorithm
,
93 blink::WebCryptoKeyUsageMask usage_mask
,
94 blink::WebCryptoKey
* key
) const {
95 std::vector
<uint8_t> raw_data
;
96 Status status
= ReadAesSecretKeyJwk(
97 key_data
, jwk_suffix_
, extractable
, usage_mask
, &raw_data
);
102 CryptoData(raw_data
), algorithm
, extractable
, usage_mask
, key
);
105 Status
AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey
& key
,
106 std::vector
<uint8_t>* buffer
) const {
107 *buffer
= SymKeyOpenSsl::Cast(key
)->raw_key_data();
108 return Status::Success();
111 Status
AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey
& key
,
112 std::vector
<uint8_t>* buffer
) const {
113 const std::vector
<uint8_t>& raw_data
=
114 SymKeyOpenSsl::Cast(key
)->raw_key_data();
116 WriteSecretKeyJwk(CryptoData(raw_data
),
117 MakeJwkAesAlgorithmName(jwk_suffix_
, raw_data
.size()),
122 return Status::Success();
125 } // namespace webcrypto
127 } // namespace content