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 explicit WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsType type)
59 virtual ~WebCryptoAlgorithmParams() { }
61 WebCryptoAlgorithmParamsType type() const { return m_type; }
64 const WebCryptoAlgorithmParamsType m_type;
67 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
69 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize)
70 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCbcParams)
75 const WebVector<unsigned char>& iv() const { return m_iv; }
78 const WebVector<unsigned char> m_iv;
81 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams {
83 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize)
84 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCtrParams)
85 , m_counter(counter, counterSize)
86 , m_lengthBits(lengthBits)
90 const WebVector<unsigned char>& counter() const { return m_counter; }
91 unsigned char lengthBits() const { return m_lengthBits; }
94 const WebVector<unsigned char> m_counter;
95 const unsigned char m_lengthBits;
98 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
100 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits)
101 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesKeyGenParams)
102 , m_lengthBits(lengthBits)
106 unsigned short lengthBits() const { return m_lengthBits; }
109 const unsigned short m_lengthBits;
112 class WebCryptoHmacParams : public WebCryptoAlgorithmParams {
114 explicit WebCryptoHmacParams(const WebCryptoAlgorithm& hash)
115 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacParams)
118 BLINK_ASSERT(!hash.isNull());
121 const WebCryptoAlgorithm& hash() const { return m_hash; }
124 const WebCryptoAlgorithm m_hash;
127 class WebCryptoHmacKeyParams : public WebCryptoAlgorithmParams {
129 WebCryptoHmacKeyParams(const WebCryptoAlgorithm& hash, bool hasLengthBytes, unsigned lengthBytes)
130 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacKeyParams)
132 , m_hasLengthBytes(hasLengthBytes)
133 , m_optionalLengthBytes(lengthBytes)
135 BLINK_ASSERT(!hash.isNull());
136 BLINK_ASSERT(hasLengthBytes || !lengthBytes);
139 const WebCryptoAlgorithm& hash() const { return m_hash; }
141 bool hasLengthBytes() const { return m_hasLengthBytes; }
143 unsigned optionalLengthBytes() const { return m_optionalLengthBytes; }
146 const WebCryptoAlgorithm m_hash;
147 const bool m_hasLengthBytes;
148 const unsigned m_optionalLengthBytes;
151 class WebCryptoRsaSsaParams : public WebCryptoAlgorithmParams {
153 explicit WebCryptoRsaSsaParams(const WebCryptoAlgorithm& hash)
154 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaSsaParams)
157 BLINK_ASSERT(!hash.isNull());
160 const WebCryptoAlgorithm& hash() const { return m_hash; }
163 const WebCryptoAlgorithm m_hash;
166 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams {
168 WebCryptoRsaKeyGenParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize)
169 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaKeyGenParams)
170 , m_modulusLengthBits(modulusLengthBits)
171 , m_publicExponent(publicExponent, publicExponentSize)
175 unsigned modulusLengthBits() const { return m_modulusLengthBits; }
176 const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
179 const unsigned m_modulusLengthBits;
180 const WebVector<unsigned char> m_publicExponent;
183 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams {
185 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits)
186 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesGcmParams)
188 , m_hasAdditionalData(hasAdditionalData)
189 , m_optionalAdditionalData(additionalData, additionalDataSize)
190 , m_hasTagLengthBits(hasTagLengthBits)
191 , m_optionalTagLengthBits(tagLengthBits)
193 BLINK_ASSERT(hasAdditionalData || !additionalDataSize);
194 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits);
197 const WebVector<unsigned char>& iv() const { return m_iv; }
199 bool hasAdditionalData() const { return m_hasAdditionalData; }
200 const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; }
202 bool hasTagLengthBits() const { return m_hasTagLengthBits; }
203 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; }
206 const WebVector<unsigned char> m_iv;
207 const bool m_hasAdditionalData;
208 const WebVector<unsigned char> m_optionalAdditionalData;
209 const bool m_hasTagLengthBits;
210 const unsigned char m_optionalTagLengthBits;
213 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams {
215 WebCryptoRsaOaepParams(const WebCryptoAlgorithm& hash, bool hasLabel, const unsigned char* label, unsigned labelSize)
216 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaOaepParams)
218 , m_hasLabel(hasLabel)
219 , m_optionalLabel(label, labelSize)
221 BLINK_ASSERT(!hash.isNull());
222 BLINK_ASSERT(hasLabel || !labelSize);
225 const WebCryptoAlgorithm& hash() const { return m_hash; }
227 bool hasLabel() const { return m_hasLabel; }
228 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; }
231 const WebCryptoAlgorithm m_hash;
232 const bool m_hasLabel;
233 const WebVector<unsigned char> m_optionalLabel;