Miscellaneous fixes for virtual/override/final specifiers to match style guide.
[chromium-blink-merge.git] / content / child / webcrypto / openssl / aes_key_openssl.cc
blobea93e072050043b603e1056b59bbaeefd03a0085
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"
16 namespace content {
18 namespace webcrypto {
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,
34 bool extractable,
35 blink::WebCryptoKeyUsageMask usages,
36 GenerateKeyResult* result) const {
37 Status status = CheckKeyCreationUsages(all_key_usages_, usages, false);
38 if (status.IsError())
39 return status;
41 unsigned int keylen_bits;
42 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
43 if (status.IsError())
44 return status;
46 return GenerateSecretKeyOpenSsl(
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 {
54 switch (format) {
55 case blink::WebCryptoKeyFormatRaw:
56 case blink::WebCryptoKeyFormatJwk:
57 return CheckKeyCreationUsages(all_key_usages_, usages, false);
58 default:
59 return Status::ErrorUnsupportedImportKeyFormat();
63 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
64 const blink::WebCryptoAlgorithm& algorithm,
65 bool extractable,
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);
70 if (status.IsError())
71 return status;
73 // No possibility of overflow.
74 unsigned int keylen_bits = keylen_bytes * 8;
76 return ImportKeyRawOpenSsl(key_data, blink::WebCryptoKeyAlgorithm::createAes(
77 algorithm.id(), keylen_bits),
78 extractable, usages, key);
81 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
82 const blink::WebCryptoAlgorithm& algorithm,
83 bool extractable,
84 blink::WebCryptoKeyUsageMask usages,
85 blink::WebCryptoKey* key) const {
86 std::vector<uint8_t> raw_data;
87 Status status = ReadAesSecretKeyJwk(key_data, jwk_suffix_, extractable,
88 usages, &raw_data);
89 if (status.IsError())
90 return status;
92 return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages,
93 key);
96 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
97 std::vector<uint8_t>* buffer) const {
98 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data();
99 return Status::Success();
102 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
103 std::vector<uint8_t>* buffer) const {
104 const std::vector<uint8_t>& raw_data =
105 SymKeyOpenSsl::Cast(key)->raw_key_data();
107 WriteSecretKeyJwk(CryptoData(raw_data),
108 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
109 key.extractable(), key.usages(), buffer);
111 return Status::Success();
114 Status AesAlgorithm::SerializeKeyForClone(
115 const blink::WebCryptoKey& key,
116 blink::WebVector<uint8_t>* key_data) const {
117 key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data());
118 return Status::Success();
121 Status AesAlgorithm::DeserializeKeyForClone(
122 const blink::WebCryptoKeyAlgorithm& algorithm,
123 blink::WebCryptoKeyType type,
124 bool extractable,
125 blink::WebCryptoKeyUsageMask usages,
126 const CryptoData& key_data,
127 blink::WebCryptoKey* key) const {
128 return ImportKeyRaw(key_data, CreateAlgorithm(algorithm.id()), extractable,
129 usages, key);
132 Status AesAlgorithm::GetKeyLength(
133 const blink::WebCryptoAlgorithm& key_length_algorithm,
134 bool* has_length_bits,
135 unsigned int* length_bits) const {
136 return GetAesKeyLength(key_length_algorithm, has_length_bits, length_bits);
139 } // namespace webcrypto
141 } // namespace content