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

Side by Side Diff: content/child/webcrypto/openssl/aes_key_openssl.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/openssl/aes_key_openssl.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/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"
15
16 namespace content {
17
18 namespace webcrypto {
19
20 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages,
21 const char* jwk_suffix)
22 : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) {
23 }
24
25 AesAlgorithm::AesAlgorithm(const char* jwk_suffix)
26 : all_key_usages_(blink::WebCryptoKeyUsageEncrypt |
27 blink::WebCryptoKeyUsageDecrypt |
28 blink::WebCryptoKeyUsageWrapKey |
29 blink::WebCryptoKeyUsageUnwrapKey),
30 jwk_suffix_(jwk_suffix) {
31 }
32
33 Status AesAlgorithm::GenerateSecretKey(
34 const blink::WebCryptoAlgorithm& algorithm,
35 bool extractable,
36 blink::WebCryptoKeyUsageMask usage_mask,
37 blink::WebCryptoKey* key) const {
38 unsigned int keylen_bits;
39 Status status = GetAesKeyGenLength(algorithm.aesKeyGenParams(), &keylen_bits);
40 if (status.IsError())
41 return status;
42
43 return GenerateSecretKeyOpenSsl(
44 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
45 extractable,
46 usage_mask,
47 keylen_bits / 8,
48 key);
49 }
50
51 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
52 blink::WebCryptoKeyFormat format,
53 blink::WebCryptoKeyUsageMask usage_mask) const {
54 switch (format) {
55 case blink::WebCryptoKeyFormatRaw:
56 case blink::WebCryptoKeyFormatJwk:
57 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
58 default:
59 return Status::ErrorUnsupportedImportKeyFormat();
60 }
61 }
62
63 Status AesAlgorithm::VerifyKeyUsagesBeforeGenerateKey(
64 blink::WebCryptoKeyUsageMask usage_mask) const {
65 return CheckKeyCreationUsages(all_key_usages_, usage_mask);
66 }
67
68 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
69 const blink::WebCryptoAlgorithm& algorithm,
70 bool extractable,
71 blink::WebCryptoKeyUsageMask usage_mask,
72 blink::WebCryptoKey* key) const {
73 const unsigned int keylen_bytes = key_data.byte_length();
74 Status status = VerifyAesKeyLengthForImport(keylen_bytes);
75 if (status.IsError())
76 return status;
77
78 // No possibility of overflow.
79 unsigned int keylen_bits = keylen_bytes * 8;
80
81 return ImportKeyRawOpenSsl(
82 key_data,
83 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
84 extractable,
85 usage_mask,
86 key);
87 }
88
89 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
90 const blink::WebCryptoAlgorithm& algorithm,
91 bool extractable,
92 blink::WebCryptoKeyUsageMask usage_mask,
93 blink::WebCryptoKey* key) const {
94 std::vector<uint8> raw_data;
95 Status status = ReadAesSecretKeyJwk(
96 key_data, jwk_suffix_, extractable, usage_mask, &raw_data);
97 if (status.IsError())
98 return status;
99
100 return ImportKeyRaw(
101 CryptoData(raw_data), algorithm, extractable, usage_mask, key);
102 }
103
104 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
105 std::vector<uint8>* buffer) const {
106 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data();
107 return Status::Success();
108 }
109
110 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
111 std::vector<uint8>* buffer) const {
112 const std::vector<uint8>& raw_data = SymKeyOpenSsl::Cast(key)->raw_key_data();
113
114 WriteSecretKeyJwk(CryptoData(raw_data),
115 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
116 key.extractable(),
117 key.usages(),
118 buffer);
119
120 return Status::Success();
121 }
122
123 } // namespace webcrypto
124
125 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698