Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(813)

Side by Side Diff: content/child/webcrypto/nss/aes_algorithm_nss.cc

Issue 1077273002: html_viewer: Move webcrypto to a place where html_viewer can use it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/child/webcrypto/nss/aes_algorithm_nss.h"
6
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"
15
16 namespace content {
17
18 namespace webcrypto {
19
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) {
26 }
27
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) {
36 }
37
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;
45
46 unsigned int keylen_bits;
47 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
48 if (status.IsError())
49 return status;
50
51 return GenerateSecretKeyNss(
52 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
53 extractable, usages, keylen_bits, CKM_AES_KEY_GEN, result);
54 }
55
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();
65 }
66 }
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;
76
77 // No possibility of overflow.
78 unsigned int keylen_bits = keylen_bytes * 8;
79
80 return ImportKeyRawNss(key_data, blink::WebCryptoKeyAlgorithm::createAes(
81 algorithm.id(), keylen_bits),
82 extractable, usages, import_mechanism_, key);
83 }
84
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;
95
96 return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages,
97 key);
98 }
99
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();
104 }
105
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();
110
111 WriteSecretKeyJwk(CryptoData(raw_data),
112 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
113 key.extractable(), key.usages(), buffer);
114
115 return Status::Success();
116 }
117
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();
123 }
124
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);
134 }
135
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);
141 }
142
143 } // namespace webcrypto
144
145 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/nss/aes_algorithm_nss.h ('k') | content/child/webcrypto/nss/aes_cbc_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698