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_PLATFORM_CRYPTO_H_
6 #define CONTENT_CHILD_WEBCRYPTO_PLATFORM_CRYPTO_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "third_party/WebKit/public/platform/WebCrypto.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
23 enum EncryptOrDecrypt { ENCRYPT, DECRYPT };
30 // Functions in the webcrypto::platform namespace are intended to be those
31 // which are OpenSSL/NSS specific.
33 // The general purpose code which applies to both OpenSSL and NSS
34 // implementations of webcrypto should live in the outter webcrypto namespace,
35 // and the crypto library specific bits in the "platform" namespace.
41 // Unless otherwise noted, functions in webcrypto::platform are called
42 // exclusively from a sequenced worker pool.
44 // This means that operations using a given key cannot occur in
45 // parallel and it is not necessary to guard against concurrent usage.
47 // The exceptions are:
49 // * Key::ThreadSafeSerializeForClone(), which is called from the
50 // target Blink thread during structured clone.
52 // * ImportKeyRaw(), ImportKeySpki(), ImportKeyPkcs8(), which can be
53 // called from the target Blink thread during structured clone
54 // deserialization, as well as from the webcrypto worker pool.
56 // TODO(eroman): Change it so import happens in worker pool too.
57 // http://crbug.com/366834
64 // Base key class for all platform keys, used to safely cast between types.
65 class Key : public blink::WebCryptoKeyHandle {
67 virtual SymKey* AsSymKey() = 0;
68 virtual PublicKey* AsPublicKey() = 0;
69 virtual PrivateKey* AsPrivateKey() = 0;
71 virtual bool ThreadSafeSerializeForClone(
72 blink::WebVector<uint8>* key_data) = 0;
75 // Do any one-time initialization. Note that this can be called MULTIPLE times
76 // (once per instantiation of WebCryptoImpl).
80 // * |key| is a non-null AES-CBC key.
81 // * |iv| is exactly 16 bytes long
82 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode,
84 const CryptoData& data,
86 std::vector<uint8>* buffer);
89 // * |key| is a non-null AES-GCM key.
90 // * |tag_length_bits| is one of {32, 64, 96, 104, 112, 120, 128}
91 Status EncryptDecryptAesGcm(EncryptOrDecrypt mode,
93 const CryptoData& data,
95 const CryptoData& additional_data,
96 unsigned int tag_length_bits,
97 std::vector<uint8>* buffer);
100 // * |key| is non-null.
101 // * |data| is not empty.
102 Status EncryptRsaEsPkcs1v1_5(PublicKey* key,
103 const CryptoData& data,
104 std::vector<uint8>* buffer);
107 // * |key| is non-null.
108 Status DecryptRsaEsPkcs1v1_5(PrivateKey* key,
109 const CryptoData& data,
110 std::vector<uint8>* buffer);
113 // * |key| is a non-null HMAC key.
114 // * |hash| is a digest algorithm.
115 Status SignHmac(SymKey* key,
116 const blink::WebCryptoAlgorithm& hash,
117 const CryptoData& data,
118 std::vector<uint8>* buffer);
121 // * |algorithm| is a SHA function.
122 Status DigestSha(blink::WebCryptoAlgorithmId algorithm,
123 const CryptoData& data,
124 std::vector<uint8>* buffer);
127 // * |algorithm| is a SHA function.
128 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
129 blink::WebCryptoAlgorithmId algorithm);
132 // * |key| is non-null.
133 // * |hash| is a digest algorithm.
134 Status SignRsaSsaPkcs1v1_5(PrivateKey* key,
135 const blink::WebCryptoAlgorithm& hash,
136 const CryptoData& data,
137 std::vector<uint8>* buffer);
140 // * |key| is non-null.
141 // * |hash| is a digest algorithm.
142 Status VerifyRsaSsaPkcs1v1_5(PublicKey* key,
143 const blink::WebCryptoAlgorithm& hash,
144 const CryptoData& signature,
145 const CryptoData& data,
146 bool* signature_match);
148 // |keylen_bytes| is the desired length of the key in bits.
151 // * algorithm.id() is for a symmetric key algorithm.
152 // * keylen_bytes is non-zero (TODO(eroman): revisit this).
153 // * For AES algorithms |keylen_bytes| is either 16, 24, or 32 bytes long.
154 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
156 blink::WebCryptoKeyUsageMask usage_mask,
157 unsigned keylen_bytes,
158 blink::WebCryptoKey* key);
161 // * algorithm.id() is for an RSA algorithm.
162 // * public_exponent, modulus_length_bits and hash_or_null are the same as what
163 // is in algorithm. They are split out for convenience.
164 // * hash_or_null.isNull() may be true if a hash is not applicable to the
166 // * modulus_length_bits is not 0
167 // * public_exponent is not empty.
168 Status GenerateRsaKeyPair(const blink::WebCryptoAlgorithm& algorithm,
170 blink::WebCryptoKeyUsageMask usage_mask,
171 unsigned int modulus_length_bits,
172 const CryptoData& public_exponent,
173 const blink::WebCryptoAlgorithm& hash,
174 blink::WebCryptoKey* public_key,
175 blink::WebCryptoKey* private_key);
178 // * |key| is non-null.
179 // * |algorithm.id()| is for a symmetric key algorithm.
180 // * For AES algorithms |key_data| is either 16, 24, or 32 bytes long.
181 // Note that this may be called from target Blink thread.
182 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm,
183 const CryptoData& key_data,
185 blink::WebCryptoKeyUsageMask usage_mask,
186 blink::WebCryptoKey* key);
189 // * algorithm.id() is for an RSA algorithm.
190 Status ImportRsaPublicKey(const blink::WebCryptoAlgorithm& algorithm,
192 blink::WebCryptoKeyUsageMask usage_mask,
193 const CryptoData& modulus_data,
194 const CryptoData& exponent_data,
195 blink::WebCryptoKey* key);
197 // Note that this may be called from target Blink thread.
198 Status ImportKeySpki(const blink::WebCryptoAlgorithm& algorithm,
199 const CryptoData& key_data,
201 blink::WebCryptoKeyUsageMask usage_mask,
202 blink::WebCryptoKey* key);
204 // Note that this may be called from target Blink thread.
205 Status ImportKeyPkcs8(const blink::WebCryptoAlgorithm& algorithm,
206 const CryptoData& key_data,
208 blink::WebCryptoKeyUsageMask usage_mask,
209 blink::WebCryptoKey* key);
212 // * |key| is non-null.
213 Status ExportKeyRaw(SymKey* key, std::vector<uint8>* buffer);
216 // * |key| is non-null.
217 Status ExportKeySpki(PublicKey* key, std::vector<uint8>* buffer);
220 // * |key| is non-null.
221 Status ExportRsaPublicKey(PublicKey* key,
222 std::vector<uint8>* modulus,
223 std::vector<uint8>* public_exponent);
226 // * |key| is non-null.
227 Status ExportKeyPkcs8(PrivateKey* key,
228 const blink::WebCryptoKeyAlgorithm& key_algorithm,
229 std::vector<uint8>* buffer);
232 // * |wrapping_key| is non-null
233 // * |key| is non-null
234 Status WrapSymKeyAesKw(SymKey* wrapping_key,
236 std::vector<uint8>* buffer);
238 // Unwraps (decrypts) |wrapped_key_data| using AES-KW and places the results in
239 // a WebCryptoKey. Raw key data remains inside NSS. This function should be used
240 // when the input |wrapped_key_data| is known to result in symmetric raw key
241 // data after AES-KW decryption.
243 // * |wrapping_key| is non-null
244 // * |key| is non-null
245 // * |wrapped_key_data| is at least 24 bytes and a multiple of 8 bytes
246 // * |algorithm.id()| is for a symmetric key algorithm.
247 Status UnwrapSymKeyAesKw(const CryptoData& wrapped_key_data,
248 SymKey* wrapping_key,
249 const blink::WebCryptoAlgorithm& algorithm,
251 blink::WebCryptoKeyUsageMask usage_mask,
252 blink::WebCryptoKey* key);
254 // Performs AES-KW decryption on the input |data|. This function should be used
255 // when the input |data| does not directly represent a key and should instead be
256 // interpreted as generic bytes.
258 // * |key| is non-null
259 // * |data| is at least 24 bytes and a multiple of 8 bytes
260 // * |buffer| is non-null.
261 Status DecryptAesKw(SymKey* key,
262 const CryptoData& data,
263 std::vector<uint8>* buffer);
266 // * |wrapping_key| is non-null
267 // * |key| is non-null
268 Status WrapSymKeyRsaEs(PublicKey* wrapping_key,
270 std::vector<uint8>* buffer);
273 // * |wrapping_key| is non-null
274 // * |key| is non-null
275 // * |algorithm.id()| is for a symmetric key algorithm.
276 Status UnwrapSymKeyRsaEs(const CryptoData& wrapped_key_data,
277 PrivateKey* wrapping_key,
278 const blink::WebCryptoAlgorithm& algorithm,
280 blink::WebCryptoKeyUsageMask usage_mask,
281 blink::WebCryptoKey* key);
283 } // namespace platform
285 } // namespace webcrypto
287 } // namespace content
289 #endif // CONTENT_CHILD_WEBCRYPTO_PLATFORM_CRYPTO_H_