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::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
35 blink::WebCryptoKeyUsageMask usages,
36 GenerateKeyResult* result) const {
37 Status status = CheckKeyCreationUsages(all_key_usages_, usages);
41 unsigned int keylen_bits;
42 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
46 return GenerateSecretKeyOpenSsl(
47 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
54 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
55 blink::WebCryptoKeyFormat format,
56 blink::WebCryptoKeyUsageMask usages) const {
58 case blink::WebCryptoKeyFormatRaw:
59 case blink::WebCryptoKeyFormatJwk:
60 return CheckKeyCreationUsages(all_key_usages_, usages);
62 return Status::ErrorUnsupportedImportKeyFormat();
66 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
67 const blink::WebCryptoAlgorithm& algorithm,
69 blink::WebCryptoKeyUsageMask usages,
70 blink::WebCryptoKey* key) const {
71 const unsigned int keylen_bytes = key_data.byte_length();
72 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
76 // No possibility of overflow.
77 unsigned int keylen_bits = keylen_bytes * 8;
79 return ImportKeyRawOpenSsl(
81 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
87 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
88 const blink::WebCryptoAlgorithm& algorithm,
90 blink::WebCryptoKeyUsageMask usages,
91 blink::WebCryptoKey* key) const {
92 std::vector<uint8_t> raw_data;
93 Status status = ReadAesSecretKeyJwk(
94 key_data, jwk_suffix_, extractable, usages, &raw_data);
99 CryptoData(raw_data), algorithm, extractable, usages, key);
102 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
103 std::vector<uint8_t>* buffer) const {
104 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data();
105 return Status::Success();
108 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
109 std::vector<uint8_t>* buffer) const {
110 const std::vector<uint8_t>& raw_data =
111 SymKeyOpenSsl::Cast(key)->raw_key_data();
113 WriteSecretKeyJwk(CryptoData(raw_data),
114 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
119 return Status::Success();
122 } // namespace webcrypto
124 } // namespace content