OLD | NEW |
| (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 #ifndef CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ | |
6 #define CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "content/common/content_export.h" | |
14 #include "third_party/WebKit/public/platform/WebCrypto.h" | |
15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
16 | |
17 namespace content { | |
18 | |
19 namespace webcrypto { | |
20 | |
21 class CryptoData; | |
22 class Status; | |
23 | |
24 // Do one-time initialization. It is safe to call this multiple times. | |
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 // | |
31 // Here is how the layer cake looks. | |
32 // | |
33 // Blink | |
34 // | | |
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 | |
80 CONTENT_EXPORT Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
81 const blink::WebCryptoKey& key, | |
82 const CryptoData& data, | |
83 std::vector<uint8>* buffer); | |
84 | |
85 CONTENT_EXPORT Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
86 const blink::WebCryptoKey& key, | |
87 const CryptoData& data, | |
88 std::vector<uint8>* buffer); | |
89 | |
90 CONTENT_EXPORT Status Digest(const blink::WebCryptoAlgorithm& algorithm, | |
91 const CryptoData& data, | |
92 std::vector<uint8>* buffer); | |
93 | |
94 CONTENT_EXPORT scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( | |
95 blink::WebCryptoAlgorithmId algorithm); | |
96 | |
97 CONTENT_EXPORT Status | |
98 GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | |
99 bool extractable, | |
100 blink::WebCryptoKeyUsageMask usage_mask, | |
101 blink::WebCryptoKey* key); | |
102 | |
103 CONTENT_EXPORT Status | |
104 GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm, | |
105 bool extractable, | |
106 blink::WebCryptoKeyUsageMask usage_mask, | |
107 blink::WebCryptoKey* public_key, | |
108 blink::WebCryptoKey* private_key); | |
109 | |
110 CONTENT_EXPORT Status ImportKey(blink::WebCryptoKeyFormat format, | |
111 const CryptoData& key_data, | |
112 const blink::WebCryptoAlgorithm& algorithm, | |
113 bool extractable, | |
114 blink::WebCryptoKeyUsageMask usage_mask, | |
115 blink::WebCryptoKey* key); | |
116 | |
117 CONTENT_EXPORT Status ExportKey(blink::WebCryptoKeyFormat format, | |
118 const blink::WebCryptoKey& key, | |
119 std::vector<uint8>* buffer); | |
120 | |
121 CONTENT_EXPORT Status Sign(const blink::WebCryptoAlgorithm& algorithm, | |
122 const blink::WebCryptoKey& key, | |
123 const CryptoData& data, | |
124 std::vector<uint8>* buffer); | |
125 | |
126 CONTENT_EXPORT Status | |
127 VerifySignature(const blink::WebCryptoAlgorithm& algorithm, | |
128 const blink::WebCryptoKey& key, | |
129 const CryptoData& signature, | |
130 const CryptoData& data, | |
131 bool* signature_match); | |
132 | |
133 CONTENT_EXPORT Status | |
134 WrapKey(blink::WebCryptoKeyFormat format, | |
135 const blink::WebCryptoKey& key_to_wrap, | |
136 const blink::WebCryptoKey& wrapping_key, | |
137 const blink::WebCryptoAlgorithm& wrapping_algorithm, | |
138 std::vector<uint8>* buffer); | |
139 | |
140 CONTENT_EXPORT Status | |
141 UnwrapKey(blink::WebCryptoKeyFormat format, | |
142 const CryptoData& wrapped_key_data, | |
143 const blink::WebCryptoKey& wrapping_key, | |
144 const blink::WebCryptoAlgorithm& wrapping_algorithm, | |
145 const blink::WebCryptoAlgorithm& algorithm, | |
146 bool extractable, | |
147 blink::WebCryptoKeyUsageMask usage_mask, | |
148 blink::WebCryptoKey* key); | |
149 | |
150 // Called on the target Blink thread. | |
151 CONTENT_EXPORT bool SerializeKeyForClone(const blink::WebCryptoKey& key, | |
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 | |
183 } // namespace webcrypto | |
184 | |
185 } // namespace content | |
186 | |
187 #endif // CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_ | |
OLD | NEW |