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.
5 #ifndef CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_
6 #define CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_
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"
22 // Do one-time initialization. It is safe to call this multiple times.
23 CONTENT_EXPORT void Init();
25 // The functions exported by shared_crypto.h provide a common entry point for
26 // synchronous crypto operations.
28 // Here is how the layer cake looks.
32 // ==============|==========================
38 // WebCryptoImpl (Implements the blink::WebCrypto interface for
39 // | asynchronous completions)
41 // | [shared_crypto_unittest.cc]
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.
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)
55 // [platform_crypto.h] (Exposes functions in the webcrypto::platform
60 // [platform_crypto_{nss|openssl}.cc] (Implements using the platform crypto
63 // The shared_crypto.h functions are responsible for:
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
75 CONTENT_EXPORT Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
76 const blink::WebCryptoKey& key,
77 const CryptoData& data,
78 blink::WebArrayBuffer* buffer);
80 CONTENT_EXPORT Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
81 const blink::WebCryptoKey& key,
82 const CryptoData& data,
83 blink::WebArrayBuffer* buffer);
85 CONTENT_EXPORT Status Digest(const blink::WebCryptoAlgorithm& algorithm,
86 const CryptoData& data,
87 blink::WebArrayBuffer* buffer);
90 GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
92 blink::WebCryptoKeyUsageMask usage_mask,
93 blink::WebCryptoKey* key);
96 GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
98 blink::WebCryptoKeyUsageMask usage_mask,
99 blink::WebCryptoKey* public_key,
100 blink::WebCryptoKey* private_key);
102 CONTENT_EXPORT Status ImportKey(blink::WebCryptoKeyFormat format,
103 const CryptoData& key_data,
104 const blink::WebCryptoAlgorithm& algorithm,
106 blink::WebCryptoKeyUsageMask usage_mask,
107 blink::WebCryptoKey* key);
109 CONTENT_EXPORT Status ExportKey(blink::WebCryptoKeyFormat format,
110 const blink::WebCryptoKey& key,
111 blink::WebArrayBuffer* buffer);
113 CONTENT_EXPORT Status Sign(const blink::WebCryptoAlgorithm& algorithm,
114 const blink::WebCryptoKey& key,
115 const CryptoData& data,
116 blink::WebArrayBuffer* buffer);
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);
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);
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,
139 blink::WebCryptoKeyUsageMask usage_mask,
140 blink::WebCryptoKey* key);
142 CONTENT_EXPORT Status
143 SerializeKeyForClone(const blink::WebCryptoKey& key,
144 blink::WebVector<unsigned char>* data);
146 CONTENT_EXPORT Status
147 DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm,
148 blink::WebCryptoKeyType type,
150 blink::WebCryptoKeyUsageMask usage_mask,
151 const CryptoData& key_data,
152 blink::WebCryptoKey* key);
154 } // namespace webcrypto
156 } // namespace content
158 #endif // CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_