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
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
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 "WebVector.h"
40 // NOTE: For documentation on the meaning of each of the parameters see the
43 // http://www.w3.org/TR/WebCryptoAPI
45 // For the most part, the parameters in the spec have the same name,
46 // except that in the blink code:
48 // - Structure names are prefixed by "WebCrypto"
49 // - Optional fields are prefixed by "optional"
50 // - Data length properties are suffixed by either "Bits" or "Bytes"
52 class WebCryptoAlgorithmParams {
54 WebCryptoAlgorithmParams() { }
55 virtual ~WebCryptoAlgorithmParams() { }
56 virtual WebCryptoAlgorithmParamsType type() const = 0;
59 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
61 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize)
66 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCbcParams; }
68 const WebVector<unsigned char>& iv() const { return m_iv; }
71 const WebVector<unsigned char> m_iv;
74 class WebCryptoAlgorithmParamsWithHash : public WebCryptoAlgorithmParams {
76 explicit WebCryptoAlgorithmParamsWithHash(const WebCryptoAlgorithm& hash)
79 BLINK_ASSERT(!hash.isNull());
82 const WebCryptoAlgorithm& hash() const { return m_hash; }
85 const WebCryptoAlgorithm m_hash;
88 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams {
90 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize)
91 : WebCryptoAlgorithmParams()
92 , m_counter(counter, counterSize)
93 , m_lengthBits(lengthBits)
97 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCtrParams; }
99 const WebVector<unsigned char>& counter() const { return m_counter; }
100 unsigned char lengthBits() const { return m_lengthBits; }
103 const WebVector<unsigned char> m_counter;
104 const unsigned char m_lengthBits;
107 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
109 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits)
110 : m_lengthBits(lengthBits)
114 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesKeyGenParams; }
116 unsigned short lengthBits() const { return m_lengthBits; }
119 const unsigned short m_lengthBits;
122 class WebCryptoHmacImportParams : public WebCryptoAlgorithmParamsWithHash {
124 explicit WebCryptoHmacImportParams(const WebCryptoAlgorithm& hash)
125 : WebCryptoAlgorithmParamsWithHash(hash)
129 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacImportParams; }
132 class WebCryptoHmacKeyGenParams : public WebCryptoAlgorithmParamsWithHash {
134 WebCryptoHmacKeyGenParams(const WebCryptoAlgorithm& hash, bool hasLengthBits, unsigned lengthBits)
135 : WebCryptoAlgorithmParamsWithHash(hash)
136 , m_hasLengthBits(hasLengthBits)
137 , m_optionalLengthBits(lengthBits)
139 BLINK_ASSERT(hasLengthBits || !lengthBits);
142 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacKeyGenParams; }
144 bool hasLengthBits() const { return m_hasLengthBits; }
146 unsigned optionalLengthBits() const { return m_optionalLengthBits; }
149 const bool m_hasLengthBits;
150 const unsigned m_optionalLengthBits;
153 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams {
155 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits)
157 , m_hasAdditionalData(hasAdditionalData)
158 , m_optionalAdditionalData(additionalData, additionalDataSize)
159 , m_hasTagLengthBits(hasTagLengthBits)
160 , m_optionalTagLengthBits(tagLengthBits)
162 BLINK_ASSERT(hasAdditionalData || !additionalDataSize);
163 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits);
166 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesGcmParams; }
168 const WebVector<unsigned char>& iv() const { return m_iv; }
170 bool hasAdditionalData() const { return m_hasAdditionalData; }
171 const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; }
173 bool hasTagLengthBits() const { return m_hasTagLengthBits; }
174 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; }
177 const WebVector<unsigned char> m_iv;
178 const bool m_hasAdditionalData;
179 const WebVector<unsigned char> m_optionalAdditionalData;
180 const bool m_hasTagLengthBits;
181 const unsigned char m_optionalTagLengthBits;
184 class WebCryptoRsaHashedImportParams : public WebCryptoAlgorithmParamsWithHash {
186 explicit WebCryptoRsaHashedImportParams(const WebCryptoAlgorithm& hash)
187 : WebCryptoAlgorithmParamsWithHash(hash)
191 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedImportParams; }
194 class WebCryptoRsaHashedKeyGenParams : public WebCryptoAlgorithmParams {
196 explicit WebCryptoRsaHashedKeyGenParams(const WebCryptoAlgorithm& hash, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize)
197 : m_modulusLengthBits(modulusLengthBits)
198 , m_publicExponent(publicExponent, publicExponentSize)
201 BLINK_ASSERT(!hash.isNull());
204 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams; }
206 unsigned modulusLengthBits() const { return m_modulusLengthBits; }
207 const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
208 const WebCryptoAlgorithm& hash() const { return m_hash; }
211 const unsigned m_modulusLengthBits;
212 const WebVector<unsigned char> m_publicExponent;
213 const WebCryptoAlgorithm m_hash;
216 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams {
218 WebCryptoRsaOaepParams(bool hasLabel, const unsigned char* label, unsigned labelSize)
219 : m_hasLabel(hasLabel)
220 , m_optionalLabel(label, labelSize)
222 BLINK_ASSERT(hasLabel || !labelSize);
225 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaOaepParams; }
227 bool hasLabel() const { return m_hasLabel; }
228 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; }
231 const bool m_hasLabel;
232 const WebVector<unsigned char> m_optionalLabel;