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

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

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase onto master Created 6 years, 5 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 | Annotate | Revision Log
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_key_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 CK_FLAGS import_flags,
22 blink::WebCryptoKeyUsageMask all_key_usages,
23 const char* jwk_suffix)
24 : import_mechanism_(import_mechanism),
25 import_flags_(import_flags),
26 all_key_usages_(all_key_usages),
27 jwk_suffix_(jwk_suffix) {
28 }
29
30 AesAlgorithm::AesAlgorithm(CK_MECHANISM_TYPE import_mechanism,
31 const char* jwk_suffix)
32 : import_mechanism_(import_mechanism),
33 import_flags_(CKF_ENCRYPT | CKF_DECRYPT),
34 all_key_usages_(blink::WebCryptoKeyUsageEncrypt |
35 blink::WebCryptoKeyUsageDecrypt |
36 blink::WebCryptoKeyUsageWrapKey |
37 blink::WebCryptoKeyUsageUnwrapKey),
38 jwk_suffix_(jwk_suffix) {
39 }
40
41 Status AesAlgorithm::GenerateSecretKey(
42 const blink::WebCryptoAlgorithm& algorithm,
43 bool extractable,
44 blink::WebCryptoKeyUsageMask usage_mask,
45 blink::WebCryptoKey* key) const {
46 unsigned int keylen_bits;
47 Status status = GetAesKeyGenLength(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,
54 usage_mask,
55 keylen_bits / 8,
56 CKM_AES_KEY_GEN,
57 key);
58 }
59
60 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
61 blink::WebCryptoKeyFormat format,
62 blink::WebCryptoKeyUsageMask usage_mask) const {
63 switch (format) {
64 case blink::WebCryptoKeyFormatRaw:
65 case blink::WebCryptoKeyFormatJwk:
66 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
67 default:
68 return Status::ErrorUnsupportedImportKeyFormat();
69 }
70 }
71
72 Status AesAlgorithm::VerifyKeyUsagesBeforeGenerateKey(
73 blink::WebCryptoKeyUsageMask usage_mask) const {
74 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
75 }
76
77 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
78 const blink::WebCryptoAlgorithm& algorithm,
79 bool extractable,
80 blink::WebCryptoKeyUsageMask usage_mask,
81 blink::WebCryptoKey* key) const {
82 const unsigned int keylen_bytes = key_data.byte_length();
83 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
84 if (status.IsError())
85 return status;
86
87 // No possibility of overflow.
88 unsigned int keylen_bits = keylen_bytes * 8;
89
90 return ImportKeyRawNss(
91 key_data,
92 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
93 extractable,
94 usage_mask,
95 import_mechanism_,
96 import_flags_,
97 key);
98 }
99
100 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
101 const blink::WebCryptoAlgorithm& algorithm,
102 bool extractable,
103 blink::WebCryptoKeyUsageMask usage_mask,
104 blink::WebCryptoKey* key) const {
105 std::vector<uint8> raw_data;
106 Status status = ReadAesSecretKeyJwk(
107 key_data, jwk_suffix_, extractable, usage_mask, &raw_data);
108 if (status.IsError())
109 return status;
110
111 return ImportKeyRaw(
112 CryptoData(raw_data), algorithm, extractable, usage_mask, key);
113 }
114
115 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
116 std::vector<uint8>* buffer) const {
117 *buffer = SymKeyNss::Cast(key)->raw_key_data();
118 return Status::Success();
119 }
120
121 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
122 std::vector<uint8>* buffer) const {
123 SymKeyNss* sym_key = SymKeyNss::Cast(key);
124 const std::vector<uint8>& raw_data = sym_key->raw_key_data();
125
126 WriteSecretKeyJwk(CryptoData(raw_data),
127 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
128 key.extractable(),
129 key.usages(),
130 buffer);
131
132 return Status::Success();
133 }
134
135 } // namespace webcrypto
136
137 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698