Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / public / platform / WebCryptoAlgorithmParams.h
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
17  *
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.
29  */
30
31 #ifndef WebCryptoAlgorithmParams_h
32 #define WebCryptoAlgorithmParams_h
33
34 #include "WebCommon.h"
35 #include "WebCryptoAlgorithm.h"
36 #include "WebVector.h"
37
38 namespace blink {
39
40 // NOTE: For documentation on the meaning of each of the parameters see the
41 //       Web crypto spec:
42 //
43 //       http://www.w3.org/TR/WebCryptoAPI
44 //
45 //       For the most part, the parameters in the spec have the same name,
46 //       except that in the blink code:
47 //
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"
51
52 class WebCryptoAlgorithmParams {
53 public:
54     explicit WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsType type)
55         : m_type(type)
56     {
57     }
58
59     virtual ~WebCryptoAlgorithmParams() { }
60
61     WebCryptoAlgorithmParamsType type() const { return m_type; }
62
63 private:
64     const WebCryptoAlgorithmParamsType m_type;
65 };
66
67 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
68 public:
69     WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize)
70         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCbcParams)
71         , m_iv(iv, ivSize)
72     {
73     }
74
75     const WebVector<unsigned char>& iv() const { return m_iv; }
76
77 private:
78     const WebVector<unsigned char> m_iv;
79 };
80
81 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams {
82 public:
83     WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize)
84         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCtrParams)
85         , m_counter(counter, counterSize)
86         , m_lengthBits(lengthBits)
87     {
88     }
89
90     const WebVector<unsigned char>& counter() const { return m_counter; }
91     unsigned char lengthBits() const { return m_lengthBits; }
92
93 private:
94     const WebVector<unsigned char> m_counter;
95     const unsigned char m_lengthBits;
96 };
97
98 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
99 public:
100     explicit WebCryptoAesKeyGenParams(unsigned short lengthBits)
101         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesKeyGenParams)
102         , m_lengthBits(lengthBits)
103     {
104     }
105
106     unsigned short lengthBits() const { return m_lengthBits; }
107
108 private:
109     const unsigned short m_lengthBits;
110 };
111
112 class WebCryptoHmacParams : public WebCryptoAlgorithmParams {
113 public:
114     explicit WebCryptoHmacParams(const WebCryptoAlgorithm& hash)
115         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacParams)
116         , m_hash(hash)
117     {
118         BLINK_ASSERT(!hash.isNull());
119     }
120
121     const WebCryptoAlgorithm& hash() const { return m_hash; }
122
123 private:
124     const WebCryptoAlgorithm m_hash;
125 };
126
127 class WebCryptoHmacKeyParams : public WebCryptoAlgorithmParams {
128 public:
129     WebCryptoHmacKeyParams(const WebCryptoAlgorithm& hash, bool hasLengthBytes, unsigned lengthBytes)
130         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacKeyParams)
131         , m_hash(hash)
132         , m_hasLengthBytes(hasLengthBytes)
133         , m_optionalLengthBytes(lengthBytes)
134     {
135         BLINK_ASSERT(!hash.isNull());
136         BLINK_ASSERT(hasLengthBytes || !lengthBytes);
137     }
138
139     const WebCryptoAlgorithm& hash() const { return m_hash; }
140
141     bool hasLengthBytes() const { return m_hasLengthBytes; }
142
143     unsigned optionalLengthBytes() const { return m_optionalLengthBytes; }
144
145 private:
146     const WebCryptoAlgorithm m_hash;
147     const bool m_hasLengthBytes;
148     const unsigned m_optionalLengthBytes;
149 };
150
151 class WebCryptoRsaSsaParams : public WebCryptoAlgorithmParams {
152 public:
153     explicit WebCryptoRsaSsaParams(const WebCryptoAlgorithm& hash)
154         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaSsaParams)
155         , m_hash(hash)
156     {
157         BLINK_ASSERT(!hash.isNull());
158     }
159
160     const WebCryptoAlgorithm& hash() const { return m_hash; }
161
162 private:
163     const WebCryptoAlgorithm m_hash;
164 };
165
166 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams {
167 public:
168     WebCryptoRsaKeyGenParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize)
169         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaKeyGenParams)
170         , m_modulusLengthBits(modulusLengthBits)
171         , m_publicExponent(publicExponent, publicExponentSize)
172     {
173     }
174
175     unsigned modulusLengthBits() const { return m_modulusLengthBits; }
176     const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
177
178 private:
179     const unsigned m_modulusLengthBits;
180     const WebVector<unsigned char> m_publicExponent;
181 };
182
183 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams {
184 public:
185     WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits)
186         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesGcmParams)
187         , m_iv(iv, ivSize)
188         , m_hasAdditionalData(hasAdditionalData)
189         , m_optionalAdditionalData(additionalData, additionalDataSize)
190         , m_hasTagLengthBits(hasTagLengthBits)
191         , m_optionalTagLengthBits(tagLengthBits)
192     {
193         BLINK_ASSERT(hasAdditionalData || !additionalDataSize);
194         BLINK_ASSERT(hasTagLengthBits || !tagLengthBits);
195     }
196
197     const WebVector<unsigned char>& iv() const { return m_iv; }
198
199     bool hasAdditionalData() const { return m_hasAdditionalData; }
200     const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; }
201
202     bool hasTagLengthBits() const { return m_hasTagLengthBits; }
203     unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; }
204
205 private:
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;
211 };
212
213 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams {
214 public:
215     WebCryptoRsaOaepParams(const WebCryptoAlgorithm& hash, bool hasLabel, const unsigned char* label, unsigned labelSize)
216         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaOaepParams)
217         , m_hash(hash)
218         , m_hasLabel(hasLabel)
219         , m_optionalLabel(label, labelSize)
220     {
221         BLINK_ASSERT(!hash.isNull());
222         BLINK_ASSERT(hasLabel || !labelSize);
223     }
224
225     const WebCryptoAlgorithm& hash() const { return m_hash; }
226
227     bool hasLabel() const { return m_hasLabel; }
228     const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; }
229
230 private:
231     const WebCryptoAlgorithm m_hash;
232     const bool m_hasLabel;
233     const WebVector<unsigned char> m_optionalLabel;
234 };
235
236 } // namespace blink
237
238 #endif