Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / webcrypto / nss / aes_algorithm_nss.cc
blobbbcc643070586b210dc15528a6abd15ced689925
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/nss/aes_algorithm_nss.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/nss/key_nss.h"
11 #include "content/child/webcrypto/nss/sym_key_nss.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(CK_MECHANISM_TYPE import_mechanism,
21 blink::WebCryptoKeyUsageMask all_key_usages,
22 const std::string& jwk_suffix)
23 : import_mechanism_(import_mechanism),
24 all_key_usages_(all_key_usages),
25 jwk_suffix_(jwk_suffix) {
28 AesAlgorithm::AesAlgorithm(CK_MECHANISM_TYPE import_mechanism,
29 const std::string& jwk_suffix)
30 : import_mechanism_(import_mechanism),
31 all_key_usages_(blink::WebCryptoKeyUsageEncrypt |
32 blink::WebCryptoKeyUsageDecrypt |
33 blink::WebCryptoKeyUsageWrapKey |
34 blink::WebCryptoKeyUsageUnwrapKey),
35 jwk_suffix_(jwk_suffix) {
38 Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
39 bool extractable,
40 blink::WebCryptoKeyUsageMask usages,
41 GenerateKeyResult* result) const {
42 Status status = CheckKeyCreationUsages(all_key_usages_, usages, false);
43 if (status.IsError())
44 return status;
46 unsigned int keylen_bits;
47 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
48 if (status.IsError())
49 return status;
51 return GenerateSecretKeyNss(
52 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
53 extractable, usages, keylen_bits, CKM_AES_KEY_GEN, result);
56 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
57 blink::WebCryptoKeyFormat format,
58 blink::WebCryptoKeyUsageMask usages) const {
59 switch (format) {
60 case blink::WebCryptoKeyFormatRaw:
61 case blink::WebCryptoKeyFormatJwk:
62 return CheckKeyCreationUsages(all_key_usages_, usages, false);
63 default:
64 return Status::ErrorUnsupportedImportKeyFormat();
67 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
68 const blink::WebCryptoAlgorithm& algorithm,
69 bool extractable,
70 blink::WebCryptoKeyUsageMask usages,
71 blink::WebCryptoKey* key) const {
72 const unsigned int keylen_bytes = key_data.byte_length();
73 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
74 if (status.IsError())
75 return status;
77 // No possibility of overflow.
78 unsigned int keylen_bits = keylen_bytes * 8;
80 return ImportKeyRawNss(key_data, blink::WebCryptoKeyAlgorithm::createAes(
81 algorithm.id(), keylen_bits),
82 extractable, usages, import_mechanism_, key);
85 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
86 const blink::WebCryptoAlgorithm& algorithm,
87 bool extractable,
88 blink::WebCryptoKeyUsageMask usages,
89 blink::WebCryptoKey* key) const {
90 std::vector<uint8_t> raw_data;
91 Status status = ReadAesSecretKeyJwk(key_data, jwk_suffix_, extractable,
92 usages, &raw_data);
93 if (status.IsError())
94 return status;
96 return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages,
97 key);
100 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
101 std::vector<uint8_t>* buffer) const {
102 *buffer = SymKeyNss::Cast(key)->raw_key_data();
103 return Status::Success();
106 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
107 std::vector<uint8_t>* buffer) const {
108 SymKeyNss* sym_key = SymKeyNss::Cast(key);
109 const std::vector<uint8_t>& raw_data = sym_key->raw_key_data();
111 WriteSecretKeyJwk(CryptoData(raw_data),
112 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
113 key.extractable(), key.usages(), buffer);
115 return Status::Success();
118 Status AesAlgorithm::SerializeKeyForClone(
119 const blink::WebCryptoKey& key,
120 blink::WebVector<uint8_t>* key_data) const {
121 key_data->assign(SymKeyNss::Cast(key)->serialized_key_data());
122 return Status::Success();
125 Status AesAlgorithm::DeserializeKeyForClone(
126 const blink::WebCryptoKeyAlgorithm& algorithm,
127 blink::WebCryptoKeyType type,
128 bool extractable,
129 blink::WebCryptoKeyUsageMask usages,
130 const CryptoData& key_data,
131 blink::WebCryptoKey* key) const {
132 return ImportKeyRaw(key_data, CreateAlgorithm(algorithm.id()), extractable,
133 usages, key);
136 Status AesAlgorithm::GetKeyLength(
137 const blink::WebCryptoAlgorithm& key_length_algorithm,
138 bool* has_length_bits,
139 unsigned int* length_bits) const {
140 return GetAesKeyLength(key_length_algorithm, has_length_bits, length_bits);
143 } // namespace webcrypto
145 } // namespace content