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

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 std::string& 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 std::string& 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::VerifyKeyUsagesBeforeGenerateKey(
42 blink::WebCryptoKeyUsageMask usage_mask) const {
43 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
44 }
45
46 Status AesAlgorithm::GenerateSecretKey(
47 const blink::WebCryptoAlgorithm& algorithm,
48 bool extractable,
49 blink::WebCryptoKeyUsageMask usage_mask,
50 blink::WebCryptoKey* key) const {
51 unsigned int keylen_bits;
52 Status status = GetAesKeyGenLength(algorithm.aesKeyGenParams(), &keylen_bits);
53 if (status.IsError())
54 return status;
55
56 return GenerateSecretKeyNss(
57 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
58 extractable,
59 usage_mask,
60 keylen_bits / 8,
61 CKM_AES_KEY_GEN,
62 key);
63 }
64
65 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
66 blink::WebCryptoKeyFormat format,
67 blink::WebCryptoKeyUsageMask usage_mask) const {
68 switch (format) {
69 case blink::WebCryptoKeyFormatRaw:
70 case blink::WebCryptoKeyFormatJwk:
71 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
72 default:
73 return Status::ErrorUnsupportedImportKeyFormat();
74 }
75 }
76 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
77 const blink::WebCryptoAlgorithm& algorithm,
78 bool extractable,
79 blink::WebCryptoKeyUsageMask usage_mask,
80 blink::WebCryptoKey* key) const {
81 const unsigned int keylen_bytes = key_data.byte_length();
82 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
83 if (status.IsError())
84 return status;
85
86 // No possibility of overflow.
87 unsigned int keylen_bits = keylen_bytes * 8;
88
89 return ImportKeyRawNss(
90 key_data,
91 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
92 extractable,
93 usage_mask,
94 import_mechanism_,
95 import_flags_,
96 key);
97 }
98
99 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
100 const blink::WebCryptoAlgorithm& algorithm,
101 bool extractable,
102 blink::WebCryptoKeyUsageMask usage_mask,
103 blink::WebCryptoKey* key) const {
104 std::vector<uint8> raw_data;
105 Status status = ReadAesSecretKeyJwk(
106 key_data, jwk_suffix_, extractable, usage_mask, &raw_data);
107 if (status.IsError())
108 return status;
109
110 return ImportKeyRaw(
111 CryptoData(raw_data), algorithm, extractable, usage_mask, key);
112 }
113
114 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
115 std::vector<uint8>* buffer) const {
116 *buffer = SymKeyNss::Cast(key)->raw_key_data();
117 return Status::Success();
118 }
119
120 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
121 std::vector<uint8>* buffer) const {
122 SymKeyNss* sym_key = SymKeyNss::Cast(key);
123 const std::vector<uint8>& raw_data = sym_key->raw_key_data();
124
125 WriteSecretKeyJwk(CryptoData(raw_data),
126 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
127 key.extractable(),
128 key.usages(),
129 buffer);
130
131 return Status::Success();
132 }
133
134 } // namespace webcrypto
135
136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698