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_ALGORITHM_DISPATCH_H_
6 #define CONTENT_CHILD_WEBCRYPTO_ALGORITHM_DISPATCH_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "content/common/content_export.h"
13 #include "third_party/WebKit/public/platform/WebCrypto.h"
19 class AlgorithmImplementation
;
21 class GenerateKeyResult
;
24 // These functions provide an entry point for synchronous webcrypto operations.
26 // The inputs to these methods come from Blink, and hence the validations done
27 // by Blink can be assumed:
29 // * The algorithm parameters are consistent with the algorithm
30 // * The key contains the required usage for the operation
32 CONTENT_EXPORT Status
Encrypt(const blink::WebCryptoAlgorithm
& algorithm
,
33 const blink::WebCryptoKey
& key
,
34 const CryptoData
& data
,
35 std::vector
<uint8_t>* buffer
);
37 CONTENT_EXPORT Status
Decrypt(const blink::WebCryptoAlgorithm
& algorithm
,
38 const blink::WebCryptoKey
& key
,
39 const CryptoData
& data
,
40 std::vector
<uint8_t>* buffer
);
42 CONTENT_EXPORT Status
Digest(const blink::WebCryptoAlgorithm
& algorithm
,
43 const CryptoData
& data
,
44 std::vector
<uint8_t>* buffer
);
46 CONTENT_EXPORT Status
GenerateKey(const blink::WebCryptoAlgorithm
& algorithm
,
48 blink::WebCryptoKeyUsageMask usages
,
49 GenerateKeyResult
* result
);
51 CONTENT_EXPORT Status
ImportKey(blink::WebCryptoKeyFormat format
,
52 const CryptoData
& key_data
,
53 const blink::WebCryptoAlgorithm
& algorithm
,
55 blink::WebCryptoKeyUsageMask usages
,
56 blink::WebCryptoKey
* key
);
58 CONTENT_EXPORT Status
ExportKey(blink::WebCryptoKeyFormat format
,
59 const blink::WebCryptoKey
& key
,
60 std::vector
<uint8_t>* buffer
);
62 CONTENT_EXPORT Status
Sign(const blink::WebCryptoAlgorithm
& algorithm
,
63 const blink::WebCryptoKey
& key
,
64 const CryptoData
& data
,
65 std::vector
<uint8_t>* buffer
);
67 CONTENT_EXPORT Status
Verify(const blink::WebCryptoAlgorithm
& algorithm
,
68 const blink::WebCryptoKey
& key
,
69 const CryptoData
& signature
,
70 const CryptoData
& data
,
71 bool* signature_match
);
74 WrapKey(blink::WebCryptoKeyFormat format
,
75 const blink::WebCryptoKey
& key_to_wrap
,
76 const blink::WebCryptoKey
& wrapping_key
,
77 const blink::WebCryptoAlgorithm
& wrapping_algorithm
,
78 std::vector
<uint8_t>* buffer
);
81 UnwrapKey(blink::WebCryptoKeyFormat format
,
82 const CryptoData
& wrapped_key_data
,
83 const blink::WebCryptoKey
& wrapping_key
,
84 const blink::WebCryptoAlgorithm
& wrapping_algorithm
,
85 const blink::WebCryptoAlgorithm
& algorithm
,
87 blink::WebCryptoKeyUsageMask usages
,
88 blink::WebCryptoKey
* key
);
90 CONTENT_EXPORT Status
DeriveBits(const blink::WebCryptoAlgorithm
& algorithm
,
91 const blink::WebCryptoKey
& base_key
,
92 unsigned int length_bits
,
93 std::vector
<uint8_t>* derived_bytes
);
95 // Derives a key by calling the underlying deriveBits/getKeyLength/importKey
98 // Note that whereas the WebCrypto spec uses a single "derivedKeyType"
99 // AlgorithmIdentifier in its specification of deriveKey(), here two separate
100 // AlgorithmIdentifiers are used:
102 // * |import_algorithm| -- The parameters required by the derived key's
103 // "importKey" operation.
105 // * |key_length_algorithm| -- The parameters required by the derived key's
106 // "get key length" operation.
108 // WebCryptoAlgorithm is not a flexible type like AlgorithmIdentifier (it cannot
109 // be easily re-interpreted as a different parameter type).
111 // Therefore being provided with separate parameter types for the import
112 // parameters and the key length parameters simplifies passing the right
113 // parameters onto ImportKey() and GetKeyLength() respectively.
114 CONTENT_EXPORT Status
115 DeriveKey(const blink::WebCryptoAlgorithm
& algorithm
,
116 const blink::WebCryptoKey
& base_key
,
117 const blink::WebCryptoAlgorithm
& import_algorithm
,
118 const blink::WebCryptoAlgorithm
& key_length_algorithm
,
120 blink::WebCryptoKeyUsageMask usages
,
121 blink::WebCryptoKey
* derived_key
);
123 CONTENT_EXPORT scoped_ptr
<blink::WebCryptoDigestor
> CreateDigestor(
124 blink::WebCryptoAlgorithmId algorithm
);
126 CONTENT_EXPORT
bool SerializeKeyForClone(const blink::WebCryptoKey
& key
,
127 blink::WebVector
<uint8_t>* key_data
);
129 CONTENT_EXPORT
bool DeserializeKeyForClone(
130 const blink::WebCryptoKeyAlgorithm
& algorithm
,
131 blink::WebCryptoKeyType type
,
133 blink::WebCryptoKeyUsageMask usages
,
134 const CryptoData
& key_data
,
135 blink::WebCryptoKey
* key
);
137 } // namespace webcrypto
139 } // namespace content
141 #endif // CONTENT_CHILD_WEBCRYPTO_ALGORITHM_DISPATCH_H_