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

Side by Side Diff: content/child/webcrypto/algorithm_dispatch.h

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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ 5 #ifndef CONTENT_CHILD_WEBCRYPTO_ALGORITHM_DISPATCH_H_
6 #define CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ 6 #define CONTENT_CHILD_WEBCRYPTO_ALGORITHM_DISPATCH_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
13 #include "content/common/content_export.h" 12 #include "content/common/content_export.h"
14 #include "third_party/WebKit/public/platform/WebCrypto.h" 13 #include "third_party/WebKit/public/platform/WebCrypto.h"
15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
16 14
17 namespace content { 15 namespace content {
18 16
19 namespace webcrypto { 17 namespace webcrypto {
20 18
19 class AlgorithmImplementation;
21 class CryptoData; 20 class CryptoData;
22 class Status; 21 class Status;
23 22
24 // Do one-time initialization. It is safe to call this multiple times. 23 // These functions provide an entry point for synchronous webcrypto operations.
25 // May be called concurrently from multiple threads.
26 CONTENT_EXPORT void Init();
27
28 // The functions exported by shared_crypto.h provide a common entry point for
29 // synchronous crypto operations.
30 // 24 //
31 // Here is how the layer cake looks. 25 // The inputs to these methods come from Blink, and hence the validations done
26 // by blink can be assumed:
Ryan Sleevi 2014/07/12 00:55:27 inconsistent capitalization of Blink (25 vs 26)
eroman 2014/07/12 01:59:30 Done.
32 // 27 //
33 // Blink 28 // * The algorithm parameters are consistent with the algorithm
34 // | 29 // * The key contains the required usage for the operation
35 // ==============|==========================
36 // |
37 // content
38 // |
39 // |
40 // v
41 // WebCryptoImpl (Implements the blink::WebCrypto interface for
42 // | asynchronous completions; posts tasks to
43 // | the webcrypto worker pool to fulfill the request
44 // using the synchronous methods of shared_crypto.h)
45 // |
46 // | [shared_crypto_unittest.cc]
47 // | /
48 // | / (The blink::WebCrypto interface is not
49 // | / testable from the chromium side because
50 // | / the result object is not mockable.
51 // | / Tests are done on shared_crypto instead.
52 // V v
53 // [shared_crypto.h] (Exposes synchronous functions in the
54 // | webcrypto:: namespace. This does
55 // | common validations, infers default
56 // | parameters, and casts the algorithm
57 // | parameters to the right types)
58 // |
59 // V
60 // [platform_crypto.h] (Exposes functions in the webcrypto::platform
61 // | namespace)
62 // |
63 // |
64 // V
65 // [platform_crypto_{nss|openssl}.cc] (Implements using the platform crypto
66 // library)
67 //
68 // The shared_crypto.h functions are responsible for:
69 //
70 // * Validating the key usages
71 // * Inferring default parameters when not specified
72 // * Validating key exportability
73 // * Validating algorithm with key.algorithm
74 // * Converting the Blink key to a more specific platform::{PublicKey,
75 // PrivateKey, SymKey} and making sure it was the right type.
76 // * Validating alogorithm specific parameters (for instance, was the iv for
77 // AES-CBC 16 bytes).
78 // * Parse a JWK
79 30
80 CONTENT_EXPORT Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, 31 CONTENT_EXPORT Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
81 const blink::WebCryptoKey& key, 32 const blink::WebCryptoKey& key,
82 const CryptoData& data, 33 const CryptoData& data,
83 std::vector<uint8>* buffer); 34 std::vector<uint8>* buffer);
84 35
85 CONTENT_EXPORT Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, 36 CONTENT_EXPORT Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
86 const blink::WebCryptoKey& key, 37 const blink::WebCryptoKey& key,
87 const CryptoData& data, 38 const CryptoData& data,
88 std::vector<uint8>* buffer); 39 std::vector<uint8>* buffer);
89 40
90 CONTENT_EXPORT Status Digest(const blink::WebCryptoAlgorithm& algorithm, 41 CONTENT_EXPORT Status Digest(const blink::WebCryptoAlgorithm& algorithm,
91 const CryptoData& data, 42 const CryptoData& data,
92 std::vector<uint8>* buffer); 43 std::vector<uint8>* buffer);
93 44
94 CONTENT_EXPORT scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
95 blink::WebCryptoAlgorithmId algorithm);
96
97 CONTENT_EXPORT Status 45 CONTENT_EXPORT Status
98 GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, 46 GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
99 bool extractable, 47 bool extractable,
100 blink::WebCryptoKeyUsageMask usage_mask, 48 blink::WebCryptoKeyUsageMask usage_mask,
101 blink::WebCryptoKey* key); 49 blink::WebCryptoKey* key);
102 50
103 CONTENT_EXPORT Status 51 CONTENT_EXPORT Status
104 GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm, 52 GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
105 bool extractable, 53 bool extractable,
106 blink::WebCryptoKeyUsageMask usage_mask, 54 blink::WebCryptoKeyUsageMask usage_mask,
107 blink::WebCryptoKey* public_key, 55 blink::WebCryptoKey* public_key,
108 blink::WebCryptoKey* private_key); 56 blink::WebCryptoKey* private_key);
109 57
110 CONTENT_EXPORT Status ImportKey(blink::WebCryptoKeyFormat format, 58 CONTENT_EXPORT Status ImportKey(blink::WebCryptoKeyFormat format,
111 const CryptoData& key_data, 59 const CryptoData& key_data,
112 const blink::WebCryptoAlgorithm& algorithm, 60 const blink::WebCryptoAlgorithm& algorithm,
113 bool extractable, 61 bool extractable,
114 blink::WebCryptoKeyUsageMask usage_mask, 62 blink::WebCryptoKeyUsageMask usage_mask,
115 blink::WebCryptoKey* key); 63 blink::WebCryptoKey* key);
116 64
117 CONTENT_EXPORT Status ExportKey(blink::WebCryptoKeyFormat format, 65 CONTENT_EXPORT Status ExportKey(blink::WebCryptoKeyFormat format,
118 const blink::WebCryptoKey& key, 66 const blink::WebCryptoKey& key,
119 std::vector<uint8>* buffer); 67 std::vector<uint8>* buffer);
120 68
121 CONTENT_EXPORT Status Sign(const blink::WebCryptoAlgorithm& algorithm, 69 CONTENT_EXPORT Status Sign(const blink::WebCryptoAlgorithm& algorithm,
122 const blink::WebCryptoKey& key, 70 const blink::WebCryptoKey& key,
123 const CryptoData& data, 71 const CryptoData& data,
124 std::vector<uint8>* buffer); 72 std::vector<uint8>* buffer);
125 73
126 CONTENT_EXPORT Status 74 CONTENT_EXPORT Status Verify(const blink::WebCryptoAlgorithm& algorithm,
127 VerifySignature(const blink::WebCryptoAlgorithm& algorithm, 75 const blink::WebCryptoKey& key,
128 const blink::WebCryptoKey& key, 76 const CryptoData& signature,
129 const CryptoData& signature, 77 const CryptoData& data,
130 const CryptoData& data, 78 bool* signature_match);
131 bool* signature_match);
132 79
133 CONTENT_EXPORT Status 80 CONTENT_EXPORT Status
134 WrapKey(blink::WebCryptoKeyFormat format, 81 WrapKey(blink::WebCryptoKeyFormat format,
135 const blink::WebCryptoKey& key_to_wrap, 82 const blink::WebCryptoKey& key_to_wrap,
136 const blink::WebCryptoKey& wrapping_key, 83 const blink::WebCryptoKey& wrapping_key,
137 const blink::WebCryptoAlgorithm& wrapping_algorithm, 84 const blink::WebCryptoAlgorithm& wrapping_algorithm,
138 std::vector<uint8>* buffer); 85 std::vector<uint8>* buffer);
139 86
140 CONTENT_EXPORT Status 87 CONTENT_EXPORT Status
141 UnwrapKey(blink::WebCryptoKeyFormat format, 88 UnwrapKey(blink::WebCryptoKeyFormat format,
142 const CryptoData& wrapped_key_data, 89 const CryptoData& wrapped_key_data,
143 const blink::WebCryptoKey& wrapping_key, 90 const blink::WebCryptoKey& wrapping_key,
144 const blink::WebCryptoAlgorithm& wrapping_algorithm, 91 const blink::WebCryptoAlgorithm& wrapping_algorithm,
145 const blink::WebCryptoAlgorithm& algorithm, 92 const blink::WebCryptoAlgorithm& algorithm,
146 bool extractable, 93 bool extractable,
147 blink::WebCryptoKeyUsageMask usage_mask, 94 blink::WebCryptoKeyUsageMask usage_mask,
148 blink::WebCryptoKey* key); 95 blink::WebCryptoKey* key);
149 96
150 // Called on the target Blink thread. 97 CONTENT_EXPORT scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
151 CONTENT_EXPORT bool SerializeKeyForClone(const blink::WebCryptoKey& key, 98 blink::WebCryptoAlgorithmId algorithm);
152 blink::WebVector<uint8>* key_data);
153
154 // Called on the target Blink thread.
155 CONTENT_EXPORT bool DeserializeKeyForClone(
156 const blink::WebCryptoKeyAlgorithm& algorithm,
157 blink::WebCryptoKeyType type,
158 bool extractable,
159 blink::WebCryptoKeyUsageMask usage_mask,
160 const CryptoData& key_data,
161 blink::WebCryptoKey* key);
162
163 namespace platform {
164 class SymKey;
165 class PublicKey;
166 class PrivateKey;
167 }
168
169 Status ToPlatformSymKey(const blink::WebCryptoKey& key, platform::SymKey** out);
170
171 Status ToPlatformPublicKey(const blink::WebCryptoKey& key,
172 platform::PublicKey** out);
173
174 Status ToPlatformPrivateKey(const blink::WebCryptoKey& key,
175 platform::PrivateKey** out);
176
177 // Returns Staus::Success() if |usages| is valid for |key_type| and |algorithm|.
178 // Otherwise returns a failure
179 Status CheckKeyUsages(blink::WebCryptoAlgorithmId algorithm,
180 blink::WebCryptoKeyType key_type,
181 blink::WebCryptoKeyUsageMask usages);
182 99
183 } // namespace webcrypto 100 } // namespace webcrypto
184 101
185 } // namespace content 102 } // namespace content
186 103
187 #endif // CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ 104 #endif // CONTENT_CHILD_WEBCRYPTO_ALGORITHM_DISPATCH_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698