Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / child / webcrypto / shared_crypto.h
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 "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "content/common/content_export.h"
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
12 #include "third_party/WebKit/public/platform/WebCrypto.h"
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
14
15 namespace content {
16
17 namespace webcrypto {
18
19 class CryptoData;
20 class Status;
21
22 // Do one-time initialization. It is safe to call this multiple times.
23 CONTENT_EXPORT void Init();
24
25 // The functions exported by shared_crypto.h provide a common entry point for
26 // synchronous crypto operations.
27 //
28 // Here is how the layer cake looks.
29 //
30 //              Blink
31 //                |
32 //  ==============|==========================
33 //                |
34 //             content
35 //                |
36 //                |
37 //                v
38 //          WebCryptoImpl     (Implements the blink::WebCrypto interface for
39 //                |            asynchronous completions)
40 //                |
41 //                |      [shared_crypto_unittest.cc]
42 //                |           /
43 //                |          /   (The blink::WebCrypto interface is not
44 //                |         /     testable from the chromium side because
45 //                |        /      the result object is not mockable.
46 //                |       /       Tests are done on shared_crypto instead.
47 //                V      v
48 //        [shared_crypto.h]   (Exposes synchronous functions in the
49 //                |            webcrypto:: namespace. This does
50 //                |            common validations, infers default
51 //                |            parameters, and casts the algorithm
52 //                |            parameters to the right types)
53 //                |
54 //                V
55 //       [platform_crypto.h]  (Exposes functions in the webcrypto::platform
56 //                |            namespace)
57 //                |
58 //                |
59 //                V
60 //  [platform_crypto_{nss|openssl}.cc]  (Implements using the platform crypto
61 //                                       library)
62 //
63 // The shared_crypto.h functions are responsible for:
64 //
65 //  * Validating the key usages
66 //  * Inferring default parameters when not specified
67 //  * Validating key exportability
68 //  * Validating algorithm with key.algorithm
69 //  * Converting the blink key to a more specific platform::{PublicKey,
70 //    PrivateKey, SymKey} and making sure it was the right type.
71 //  * Validating alogorithm specific parameters (for instance, was the iv for
72 //    AES-CBC 16 bytes).
73 //  * Parse a JWK
74
75 CONTENT_EXPORT Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
76                               const blink::WebCryptoKey& key,
77                               const CryptoData& data,
78                               blink::WebArrayBuffer* buffer);
79
80 CONTENT_EXPORT Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
81                               const blink::WebCryptoKey& key,
82                               const CryptoData& data,
83                               blink::WebArrayBuffer* buffer);
84
85 CONTENT_EXPORT Status Digest(const blink::WebCryptoAlgorithm& algorithm,
86                              const CryptoData& data,
87                              blink::WebArrayBuffer* buffer);
88
89 CONTENT_EXPORT Status
90     GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
91                       bool extractable,
92                       blink::WebCryptoKeyUsageMask usage_mask,
93                       blink::WebCryptoKey* key);
94
95 CONTENT_EXPORT Status
96     GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
97                     bool extractable,
98                     blink::WebCryptoKeyUsageMask usage_mask,
99                     blink::WebCryptoKey* public_key,
100                     blink::WebCryptoKey* private_key);
101
102 CONTENT_EXPORT Status ImportKey(blink::WebCryptoKeyFormat format,
103                                 const CryptoData& key_data,
104                                 const blink::WebCryptoAlgorithm& algorithm,
105                                 bool extractable,
106                                 blink::WebCryptoKeyUsageMask usage_mask,
107                                 blink::WebCryptoKey* key);
108
109 CONTENT_EXPORT Status ExportKey(blink::WebCryptoKeyFormat format,
110                                 const blink::WebCryptoKey& key,
111                                 blink::WebArrayBuffer* buffer);
112
113 CONTENT_EXPORT Status Sign(const blink::WebCryptoAlgorithm& algorithm,
114                            const blink::WebCryptoKey& key,
115                            const CryptoData& data,
116                            blink::WebArrayBuffer* buffer);
117
118 CONTENT_EXPORT Status
119     VerifySignature(const blink::WebCryptoAlgorithm& algorithm,
120                     const blink::WebCryptoKey& key,
121                     const CryptoData& signature,
122                     const CryptoData& data,
123                     bool* signature_match);
124
125 CONTENT_EXPORT Status
126     WrapKey(blink::WebCryptoKeyFormat format,
127             const blink::WebCryptoKey& wrapping_key,
128             const blink::WebCryptoKey& key_to_wrap,
129             const blink::WebCryptoAlgorithm& wrapping_algorithm,
130             blink::WebArrayBuffer* buffer);
131
132 CONTENT_EXPORT Status
133     UnwrapKey(blink::WebCryptoKeyFormat format,
134               const CryptoData& wrapped_key_data,
135               const blink::WebCryptoKey& wrapping_key,
136               const blink::WebCryptoAlgorithm& wrapping_algorithm,
137               const blink::WebCryptoAlgorithm& algorithm,
138               bool extractable,
139               blink::WebCryptoKeyUsageMask usage_mask,
140               blink::WebCryptoKey* key);
141
142 CONTENT_EXPORT Status
143     SerializeKeyForClone(const blink::WebCryptoKey& key,
144                          blink::WebVector<unsigned char>* data);
145
146 CONTENT_EXPORT Status
147     DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm,
148                            blink::WebCryptoKeyType type,
149                            bool extractable,
150                            blink::WebCryptoKeyUsageMask usage_mask,
151                            const CryptoData& key_data,
152                            blink::WebCryptoKey* key);
153
154 }  // namespace webcrypto
155
156 }  // namespace content
157
158 #endif  // CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_