Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebCryptoAlgorithmParams.h
blobe90e98d466b11a6cea941a4d5b57e185cca118fa
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WebCryptoAlgorithmParams_h
32 #define WebCryptoAlgorithmParams_h
34 #include "WebCommon.h"
35 #include "WebCryptoAlgorithm.h"
36 #include "WebCryptoKey.h"
37 #include "WebVector.h"
39 namespace blink {
41 // NOTE: For documentation on the meaning of each of the parameters see the
42 // Web crypto spec:
44 // http://www.w3.org/TR/WebCryptoAPI
46 // For the most part, the parameters in the spec have the same name,
47 // except that in the blink code:
49 // - Structure names are prefixed by "WebCrypto"
50 // - Optional fields are prefixed by "optional"
51 // - Data length properties are suffixed by either "Bits" or "Bytes"
53 class WebCryptoAlgorithmParams {
54 public:
55 WebCryptoAlgorithmParams() { }
56 virtual ~WebCryptoAlgorithmParams() { }
57 virtual WebCryptoAlgorithmParamsType type() const = 0;
60 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
61 public:
62 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize)
63 : m_iv(iv, ivSize)
67 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCbcParams; }
69 const WebVector<unsigned char>& iv() const { return m_iv; }
71 private:
72 const WebVector<unsigned char> m_iv;
75 class WebCryptoAlgorithmParamsWithHash : public WebCryptoAlgorithmParams {
76 public:
77 explicit WebCryptoAlgorithmParamsWithHash(const WebCryptoAlgorithm& hash)
78 : m_hash(hash)
80 BLINK_ASSERT(!hash.isNull());
83 const WebCryptoAlgorithm& hash() const { return m_hash; }
85 private:
86 const WebCryptoAlgorithm m_hash;
89 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams {
90 public:
91 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize)
92 : WebCryptoAlgorithmParams()
93 , m_counter(counter, counterSize)
94 , m_lengthBits(lengthBits)
98 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCtrParams; }
100 const WebVector<unsigned char>& counter() const { return m_counter; }
101 unsigned char lengthBits() const { return m_lengthBits; }
103 private:
104 const WebVector<unsigned char> m_counter;
105 const unsigned char m_lengthBits;
108 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
109 public:
110 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits)
111 : m_lengthBits(lengthBits)
115 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesKeyGenParams; }
117 unsigned short lengthBits() const { return m_lengthBits; }
119 private:
120 const unsigned short m_lengthBits;
123 class WebCryptoHmacImportParams : public WebCryptoAlgorithmParamsWithHash {
124 public:
125 // FIXME: Remove this constructor once it is no longer used by Chromium. http://crbug.com/431085
126 explicit WebCryptoHmacImportParams(const WebCryptoAlgorithm& hash)
127 : WebCryptoAlgorithmParamsWithHash(hash)
128 , m_hasLengthBits(false)
129 , m_optionalLengthBits(0)
133 WebCryptoHmacImportParams(const WebCryptoAlgorithm& hash, bool hasLengthBits, unsigned lengthBits)
134 : WebCryptoAlgorithmParamsWithHash(hash)
135 , m_hasLengthBits(hasLengthBits)
136 , m_optionalLengthBits(lengthBits)
138 BLINK_ASSERT(hasLengthBits || !lengthBits);
141 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacImportParams; }
143 bool hasLengthBits() const { return m_hasLengthBits; }
145 unsigned optionalLengthBits() const { return m_optionalLengthBits; }
147 private:
148 const bool m_hasLengthBits;
149 const unsigned m_optionalLengthBits;
152 class WebCryptoHmacKeyGenParams : public WebCryptoAlgorithmParamsWithHash {
153 public:
154 WebCryptoHmacKeyGenParams(const WebCryptoAlgorithm& hash, bool hasLengthBits, unsigned lengthBits)
155 : WebCryptoAlgorithmParamsWithHash(hash)
156 , m_hasLengthBits(hasLengthBits)
157 , m_optionalLengthBits(lengthBits)
159 BLINK_ASSERT(hasLengthBits || !lengthBits);
162 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacKeyGenParams; }
164 bool hasLengthBits() const { return m_hasLengthBits; }
166 unsigned optionalLengthBits() const { return m_optionalLengthBits; }
168 private:
169 const bool m_hasLengthBits;
170 const unsigned m_optionalLengthBits;
173 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams {
174 public:
175 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits)
176 : m_iv(iv, ivSize)
177 , m_hasAdditionalData(hasAdditionalData)
178 , m_optionalAdditionalData(additionalData, additionalDataSize)
179 , m_hasTagLengthBits(hasTagLengthBits)
180 , m_optionalTagLengthBits(tagLengthBits)
182 BLINK_ASSERT(hasAdditionalData || !additionalDataSize);
183 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits);
186 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesGcmParams; }
188 const WebVector<unsigned char>& iv() const { return m_iv; }
190 bool hasAdditionalData() const { return m_hasAdditionalData; }
191 const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; }
193 bool hasTagLengthBits() const { return m_hasTagLengthBits; }
194 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; }
196 private:
197 const WebVector<unsigned char> m_iv;
198 const bool m_hasAdditionalData;
199 const WebVector<unsigned char> m_optionalAdditionalData;
200 const bool m_hasTagLengthBits;
201 const unsigned char m_optionalTagLengthBits;
204 class WebCryptoRsaHashedImportParams : public WebCryptoAlgorithmParamsWithHash {
205 public:
206 explicit WebCryptoRsaHashedImportParams(const WebCryptoAlgorithm& hash)
207 : WebCryptoAlgorithmParamsWithHash(hash)
211 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedImportParams; }
214 class WebCryptoRsaHashedKeyGenParams : public WebCryptoAlgorithmParams {
215 public:
216 explicit WebCryptoRsaHashedKeyGenParams(const WebCryptoAlgorithm& hash, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize)
217 : m_modulusLengthBits(modulusLengthBits)
218 , m_publicExponent(publicExponent, publicExponentSize)
219 , m_hash(hash)
221 BLINK_ASSERT(!hash.isNull());
224 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams; }
226 unsigned modulusLengthBits() const { return m_modulusLengthBits; }
227 const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
228 const WebCryptoAlgorithm& hash() const { return m_hash; }
230 private:
231 const unsigned m_modulusLengthBits;
232 const WebVector<unsigned char> m_publicExponent;
233 const WebCryptoAlgorithm m_hash;
236 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams {
237 public:
238 WebCryptoRsaOaepParams(bool hasLabel, const unsigned char* label, unsigned labelSize)
239 : m_hasLabel(hasLabel)
240 , m_optionalLabel(label, labelSize)
242 BLINK_ASSERT(hasLabel || !labelSize);
245 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaOaepParams; }
247 bool hasLabel() const { return m_hasLabel; }
248 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; }
250 private:
251 const bool m_hasLabel;
252 const WebVector<unsigned char> m_optionalLabel;
255 class WebCryptoRsaPssParams : public WebCryptoAlgorithmParams {
256 public:
257 explicit WebCryptoRsaPssParams(unsigned saltLengthBytes)
258 : m_saltLengthBytes(saltLengthBytes)
262 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaPssParams; }
264 unsigned saltLengthBytes() const { return m_saltLengthBytes; }
266 private:
267 const unsigned m_saltLengthBytes;
270 class WebCryptoEcdsaParams : public WebCryptoAlgorithmParamsWithHash {
271 public:
272 explicit WebCryptoEcdsaParams(const WebCryptoAlgorithm& hash)
273 : WebCryptoAlgorithmParamsWithHash(hash)
277 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeEcdsaParams; }
280 class WebCryptoEcKeyGenParams : public WebCryptoAlgorithmParams {
281 public:
282 explicit WebCryptoEcKeyGenParams(WebCryptoNamedCurve namedCurve)
283 : m_namedCurve(namedCurve)
287 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeEcKeyGenParams; }
289 WebCryptoNamedCurve namedCurve() const { return m_namedCurve; }
291 private:
292 const WebCryptoNamedCurve m_namedCurve;
295 class WebCryptoEcKeyImportParams : public WebCryptoAlgorithmParams {
296 public:
297 explicit WebCryptoEcKeyImportParams(WebCryptoNamedCurve namedCurve)
298 : m_namedCurve(namedCurve)
302 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeEcKeyImportParams; }
304 WebCryptoNamedCurve namedCurve() const { return m_namedCurve; }
306 private:
307 const WebCryptoNamedCurve m_namedCurve;
310 class WebCryptoEcdhKeyDeriveParams : public WebCryptoAlgorithmParams {
311 public:
312 explicit WebCryptoEcdhKeyDeriveParams(const WebCryptoKey& publicKey)
313 : m_publicKey(publicKey)
317 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeEcdhKeyDeriveParams; }
319 const WebCryptoKey& publicKey() const { return m_publicKey; }
321 private:
322 const WebCryptoKey m_publicKey;
325 class WebCryptoAesDerivedKeyParams : public WebCryptoAlgorithmParams {
326 public:
327 explicit WebCryptoAesDerivedKeyParams(unsigned short lengthBits)
328 : m_lengthBits(lengthBits)
332 virtual WebCryptoAlgorithmParamsType type() const override { return WebCryptoAlgorithmParamsTypeAesDerivedKeyParams; }
334 unsigned short lengthBits() const { return m_lengthBits; }
336 private:
337 const unsigned short m_lengthBits;
340 class WebCryptoHkdfParams : public WebCryptoAlgorithmParamsWithHash {
341 public:
342 WebCryptoHkdfParams(const WebCryptoAlgorithm& hash, const unsigned char* salt, unsigned saltSize, const unsigned char* info, unsigned infoSize)
343 : WebCryptoAlgorithmParamsWithHash(hash)
344 , m_salt(salt, saltSize)
345 , m_info(info, infoSize)
349 const WebVector<unsigned char>& salt() const { return m_salt; }
351 const WebVector<unsigned char>& info() const { return m_info; }
353 virtual WebCryptoAlgorithmParamsType type() const
355 return WebCryptoAlgorithmParamsTypeHkdfParams;
358 private:
359 const WebVector<unsigned char> m_salt;
360 const WebVector<unsigned char> m_info;
363 class WebCryptoPbkdf2Params : public WebCryptoAlgorithmParamsWithHash {
364 public:
365 WebCryptoPbkdf2Params(const WebCryptoAlgorithm& hash, const unsigned char* salt, unsigned saltLength, unsigned iterations)
366 : WebCryptoAlgorithmParamsWithHash(hash)
367 , m_salt(salt, saltLength)
368 , m_iterations(iterations)
372 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypePbkdf2Params; }
374 const WebVector<unsigned char>& salt() const { return m_salt; }
375 unsigned iterations() const { return m_iterations; }
377 private:
378 const WebVector<unsigned char> m_salt;
379 const unsigned m_iterations;
382 } // namespace blink
384 #endif