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_
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"
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();
28 // The functions exported by shared_crypto.h provide a common entry point for
29 // synchronous crypto operations.
31 // Here is how the layer cake looks.
35 // ==============|==========================
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)
46 // | [shared_crypto_unittest.cc]
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.
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)
60 // [platform_crypto.h] (Exposes functions in the webcrypto::platform
65 // [platform_crypto_{nss|openssl}.cc] (Implements using the platform crypto
68 // The shared_crypto.h functions are responsible for:
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
80 CONTENT_EXPORT Status
Encrypt(const blink::WebCryptoAlgorithm
& algorithm
,
81 const blink::WebCryptoKey
& key
,
82 const CryptoData
& data
,
83 std::vector
<uint8
>* buffer
);
85 CONTENT_EXPORT Status
Decrypt(const blink::WebCryptoAlgorithm
& algorithm
,
86 const blink::WebCryptoKey
& key
,
87 const CryptoData
& data
,
88 std::vector
<uint8
>* buffer
);
90 CONTENT_EXPORT Status
Digest(const blink::WebCryptoAlgorithm
& algorithm
,
91 const CryptoData
& data
,
92 std::vector
<uint8
>* buffer
);
94 CONTENT_EXPORT scoped_ptr
<blink::WebCryptoDigestor
> CreateDigestor(
95 blink::WebCryptoAlgorithmId algorithm
);
98 GenerateSecretKey(const blink::WebCryptoAlgorithm
& algorithm
,
100 blink::WebCryptoKeyUsageMask usage_mask
,
101 blink::WebCryptoKey
* key
);
103 CONTENT_EXPORT Status
104 GenerateKeyPair(const blink::WebCryptoAlgorithm
& algorithm
,
106 blink::WebCryptoKeyUsageMask usage_mask
,
107 blink::WebCryptoKey
* public_key
,
108 blink::WebCryptoKey
* private_key
);
110 CONTENT_EXPORT Status
ImportKey(blink::WebCryptoKeyFormat format
,
111 const CryptoData
& key_data
,
112 const blink::WebCryptoAlgorithm
& algorithm
,
114 blink::WebCryptoKeyUsageMask usage_mask
,
115 blink::WebCryptoKey
* key
);
117 CONTENT_EXPORT Status
ExportKey(blink::WebCryptoKeyFormat format
,
118 const blink::WebCryptoKey
& key
,
119 std::vector
<uint8
>* buffer
);
121 CONTENT_EXPORT Status
Sign(const blink::WebCryptoAlgorithm
& algorithm
,
122 const blink::WebCryptoKey
& key
,
123 const CryptoData
& data
,
124 std::vector
<uint8
>* buffer
);
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
);
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
);
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
,
147 blink::WebCryptoKeyUsageMask usage_mask
,
148 blink::WebCryptoKey
* key
);
150 // Called on the target Blink thread.
151 CONTENT_EXPORT
bool SerializeKeyForClone(const blink::WebCryptoKey
& key
,
152 blink::WebVector
<uint8
>* key_data
);
154 // Called on the target Blink thread.
155 CONTENT_EXPORT
bool DeserializeKeyForClone(
156 const blink::WebCryptoKeyAlgorithm
& algorithm
,
157 blink::WebCryptoKeyType type
,
159 blink::WebCryptoKeyUsageMask usage_mask
,
160 const CryptoData
& key_data
,
161 blink::WebCryptoKey
* key
);
169 Status
ToPlatformSymKey(const blink::WebCryptoKey
& key
, platform::SymKey
** out
);
171 Status
ToPlatformPublicKey(const blink::WebCryptoKey
& key
,
172 platform::PublicKey
** out
);
174 Status
ToPlatformPrivateKey(const blink::WebCryptoKey
& key
,
175 platform::PrivateKey
** out
);
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
);
183 } // namespace webcrypto
185 } // namespace content
187 #endif // CONTENT_CHILD_WEBCRYPTO_SHARED_CRYPTO_H_