Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / exported / WebCryptoAlgorithm.cpp
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 #include "config.h"
32 #include "public/platform/WebCryptoAlgorithm.h"
33
34 #include "public/platform/WebCryptoAlgorithmParams.h"
35 #include "wtf/Assertions.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/StdLibExtras.h"
38 #include "wtf/ThreadSafeRefCounted.h"
39
40 namespace blink {
41
42 namespace {
43
44 // A mapping from the algorithm ID to information about the algorithm.
45 const WebCryptoAlgorithmInfo algorithmIdToInfo[] = {
46     { // Index 0
47         "AES-CBC", {
48             WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
49             WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
50             WebCryptoAlgorithmInfo::Undefined, // Sign
51             WebCryptoAlgorithmInfo::Undefined, // Verify
52             WebCryptoAlgorithmInfo::Undefined, // Digest
53             WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
54             WebCryptoAlgorithmParamsTypeNone, // ImportKey
55             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
56             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
57             WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
58             WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
59         }
60     }, { // Index 1
61         "HMAC", {
62             WebCryptoAlgorithmInfo::Undefined, // Encrypt
63             WebCryptoAlgorithmInfo::Undefined, // Decrypt
64             WebCryptoAlgorithmParamsTypeNone, // Sign
65             WebCryptoAlgorithmParamsTypeNone, // Verify
66             WebCryptoAlgorithmInfo::Undefined, // Digest
67             WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
68             WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
69             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
70             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
71             WebCryptoAlgorithmInfo::Undefined, // WrapKey
72             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
73         }
74     }, { // Index 2
75         "RSASSA-PKCS1-v1_5", {
76             WebCryptoAlgorithmInfo::Undefined, // Encrypt
77             WebCryptoAlgorithmInfo::Undefined, // Decrypt
78             WebCryptoAlgorithmParamsTypeNone, // Sign
79             WebCryptoAlgorithmParamsTypeNone, // Verify
80             WebCryptoAlgorithmInfo::Undefined, // Digest
81             WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // GenerateKey
82             WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportKey
83             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
84             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
85             WebCryptoAlgorithmInfo::Undefined, // WrapKey
86             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
87         }
88     }, { // Index 3
89         "SHA-1", {
90             WebCryptoAlgorithmInfo::Undefined, // Encrypt
91             WebCryptoAlgorithmInfo::Undefined, // Decrypt
92             WebCryptoAlgorithmInfo::Undefined, // Sign
93             WebCryptoAlgorithmInfo::Undefined, // Verify
94             WebCryptoAlgorithmParamsTypeNone, // Digest
95             WebCryptoAlgorithmInfo::Undefined, // GenerateKey
96             WebCryptoAlgorithmInfo::Undefined, // ImportKey
97             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
98             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
99             WebCryptoAlgorithmInfo::Undefined, // WrapKey
100             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
101         }
102     }, { // Index 4
103         "SHA-256", {
104             WebCryptoAlgorithmInfo::Undefined, // Encrypt
105             WebCryptoAlgorithmInfo::Undefined, // Decrypt
106             WebCryptoAlgorithmInfo::Undefined, // Sign
107             WebCryptoAlgorithmInfo::Undefined, // Verify
108             WebCryptoAlgorithmParamsTypeNone, // Digest
109             WebCryptoAlgorithmInfo::Undefined, // GenerateKey
110             WebCryptoAlgorithmInfo::Undefined, // ImportKey
111             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
112             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
113             WebCryptoAlgorithmInfo::Undefined, // WrapKey
114             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
115         }
116     }, { // Index 5
117         "SHA-384", {
118             WebCryptoAlgorithmInfo::Undefined, // Encrypt
119             WebCryptoAlgorithmInfo::Undefined, // Decrypt
120             WebCryptoAlgorithmInfo::Undefined, // Sign
121             WebCryptoAlgorithmInfo::Undefined, // Verify
122             WebCryptoAlgorithmParamsTypeNone, // Digest
123             WebCryptoAlgorithmInfo::Undefined, // GenerateKey
124             WebCryptoAlgorithmInfo::Undefined, // ImportKey
125             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
126             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
127             WebCryptoAlgorithmInfo::Undefined, // WrapKey
128             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
129         }
130     }, { // Index 6
131         "SHA-512", {
132             WebCryptoAlgorithmInfo::Undefined, // Encrypt
133             WebCryptoAlgorithmInfo::Undefined, // Decrypt
134             WebCryptoAlgorithmInfo::Undefined, // Sign
135             WebCryptoAlgorithmInfo::Undefined, // Verify
136             WebCryptoAlgorithmParamsTypeNone, // Digest
137             WebCryptoAlgorithmInfo::Undefined, // GenerateKey
138             WebCryptoAlgorithmInfo::Undefined, // ImportKey
139             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
140             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
141             WebCryptoAlgorithmInfo::Undefined, // WrapKey
142             WebCryptoAlgorithmInfo::Undefined // UnwrapKey
143         }
144     }, { // Index 7
145         "AES-GCM", {
146             WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
147             WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
148             WebCryptoAlgorithmInfo::Undefined, // Sign
149             WebCryptoAlgorithmInfo::Undefined, // Verify
150             WebCryptoAlgorithmInfo::Undefined, // Digest
151             WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
152             WebCryptoAlgorithmParamsTypeNone, // ImportKey
153             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
154             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
155             WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
156             WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
157         }
158     }, { // Index 8
159         "RSA-OAEP", {
160             WebCryptoAlgorithmParamsTypeRsaOaepParams, // Encrypt
161             WebCryptoAlgorithmParamsTypeRsaOaepParams, // Decrypt
162             WebCryptoAlgorithmInfo::Undefined, // Sign
163             WebCryptoAlgorithmInfo::Undefined, // Verify
164             WebCryptoAlgorithmInfo::Undefined, // Digest
165             WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // GenerateKey
166             WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportKey
167             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
168             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
169             WebCryptoAlgorithmParamsTypeRsaOaepParams, // WrapKey
170             WebCryptoAlgorithmParamsTypeRsaOaepParams // UnwrapKey
171         }
172     }, { // Index 9
173         "AES-CTR", {
174             WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
175             WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
176             WebCryptoAlgorithmInfo::Undefined, // Sign
177             WebCryptoAlgorithmInfo::Undefined, // Verify
178             WebCryptoAlgorithmInfo::Undefined, // Digest
179             WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
180             WebCryptoAlgorithmParamsTypeNone, // ImportKey
181             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
182             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
183             WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
184             WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
185         }
186     }, { // Index 10
187         "AES-KW", {
188             WebCryptoAlgorithmInfo::Undefined, // Encrypt
189             WebCryptoAlgorithmInfo::Undefined, // Decrypt
190             WebCryptoAlgorithmInfo::Undefined, // Sign
191             WebCryptoAlgorithmInfo::Undefined, // Verify
192             WebCryptoAlgorithmInfo::Undefined, // Digest
193             WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
194             WebCryptoAlgorithmParamsTypeNone, // ImportKey
195             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
196             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
197             WebCryptoAlgorithmParamsTypeNone, // WrapKey
198             WebCryptoAlgorithmParamsTypeNone // UnwrapKey
199         }
200     }, { // Index 11
201         "RSA-PSS", {
202             WebCryptoAlgorithmInfo::Undefined, // Encrypt
203             WebCryptoAlgorithmInfo::Undefined, // Decrypt
204             WebCryptoAlgorithmParamsTypeRsaPssParams, // Sign
205             WebCryptoAlgorithmParamsTypeRsaPssParams, // Verify
206             WebCryptoAlgorithmInfo::Undefined, // Digest
207             WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // GenerateKey
208             WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportKey
209             WebCryptoAlgorithmInfo::Undefined, // DeriveKey
210             WebCryptoAlgorithmInfo::Undefined, // DeriveBits
211             WebCryptoAlgorithmParamsTypeNone, // WrapKey
212             WebCryptoAlgorithmParamsTypeNone // UnwrapKey
213         }
214     },
215 };
216
217 // Initializing the algorithmIdToInfo table above depends on knowing the enum
218 // values for algorithm IDs. If those ever change, the table will need to be
219 // updated.
220 COMPILE_ASSERT(WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch);
221 COMPILE_ASSERT(WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch);
222 COMPILE_ASSERT(WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_idDoesntMatch);
223 COMPILE_ASSERT(WebCryptoAlgorithmIdSha1 == 3, Sha1_idDoesntMatch);
224 COMPILE_ASSERT(WebCryptoAlgorithmIdSha256 == 4, Sha256_idDoesntMatch);
225 COMPILE_ASSERT(WebCryptoAlgorithmIdSha384 == 5, Sha384_idDoesntMatch);
226 COMPILE_ASSERT(WebCryptoAlgorithmIdSha512 == 6, Sha512_idDoesntMatch);
227 COMPILE_ASSERT(WebCryptoAlgorithmIdAesGcm == 7, AesGcm_idDoesntMatch);
228 COMPILE_ASSERT(WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch);
229 COMPILE_ASSERT(WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch);
230 COMPILE_ASSERT(WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch);
231 COMPILE_ASSERT(WebCryptoAlgorithmIdRsaPss == 11, RsaPss_idDoesntMatch);
232 COMPILE_ASSERT(WebCryptoAlgorithmIdLast == 11, Last_idDoesntMatch);
233 COMPILE_ASSERT(10 == WebCryptoOperationLast, UpdateParamsMapping);
234
235 } // namespace
236
237 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithmPrivate> {
238 public:
239     WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params)
240         : id(id)
241         , params(params)
242     {
243     }
244
245     WebCryptoAlgorithmId id;
246     OwnPtr<WebCryptoAlgorithmParams> params;
247 };
248
249 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params)
250     : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, params)))
251 {
252 }
253
254 WebCryptoAlgorithm WebCryptoAlgorithm::createNull()
255 {
256     return WebCryptoAlgorithm();
257 }
258
259 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, WebCryptoAlgorithmParams* params)
260 {
261     return WebCryptoAlgorithm(id, adoptPtr(params));
262 }
263
264 const WebCryptoAlgorithmInfo* WebCryptoAlgorithm::lookupAlgorithmInfo(WebCryptoAlgorithmId id)
265 {
266     if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
267         return 0;
268     return &algorithmIdToInfo[id];
269 }
270
271 bool WebCryptoAlgorithm::isNull() const
272 {
273     return m_private.isNull();
274 }
275
276 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const
277 {
278     ASSERT(!isNull());
279     return m_private->id;
280 }
281
282 WebCryptoAlgorithmParamsType WebCryptoAlgorithm::paramsType() const
283 {
284     ASSERT(!isNull());
285     if (!m_private->params)
286         return WebCryptoAlgorithmParamsTypeNone;
287     return m_private->params->type();
288 }
289
290 const WebCryptoAesCbcParams* WebCryptoAlgorithm::aesCbcParams() const
291 {
292     ASSERT(!isNull());
293     if (paramsType() == WebCryptoAlgorithmParamsTypeAesCbcParams)
294         return static_cast<WebCryptoAesCbcParams*>(m_private->params.get());
295     return 0;
296 }
297
298 const WebCryptoAesCtrParams* WebCryptoAlgorithm::aesCtrParams() const
299 {
300     ASSERT(!isNull());
301     if (paramsType() == WebCryptoAlgorithmParamsTypeAesCtrParams)
302         return static_cast<WebCryptoAesCtrParams*>(m_private->params.get());
303     return 0;
304 }
305
306 const WebCryptoAesKeyGenParams* WebCryptoAlgorithm::aesKeyGenParams() const
307 {
308     ASSERT(!isNull());
309     if (paramsType() == WebCryptoAlgorithmParamsTypeAesKeyGenParams)
310         return static_cast<WebCryptoAesKeyGenParams*>(m_private->params.get());
311     return 0;
312 }
313
314 const WebCryptoHmacImportParams* WebCryptoAlgorithm::hmacImportParams() const
315 {
316     ASSERT(!isNull());
317     if (paramsType() == WebCryptoAlgorithmParamsTypeHmacImportParams)
318         return static_cast<WebCryptoHmacImportParams*>(m_private->params.get());
319     return 0;
320 }
321
322 const WebCryptoHmacKeyGenParams* WebCryptoAlgorithm::hmacKeyGenParams() const
323 {
324     ASSERT(!isNull());
325     if (paramsType() == WebCryptoAlgorithmParamsTypeHmacKeyGenParams)
326         return static_cast<WebCryptoHmacKeyGenParams*>(m_private->params.get());
327     return 0;
328 }
329
330 const WebCryptoAesGcmParams* WebCryptoAlgorithm::aesGcmParams() const
331 {
332     ASSERT(!isNull());
333     if (paramsType() == WebCryptoAlgorithmParamsTypeAesGcmParams)
334         return static_cast<WebCryptoAesGcmParams*>(m_private->params.get());
335     return 0;
336 }
337
338 const WebCryptoRsaOaepParams* WebCryptoAlgorithm::rsaOaepParams() const
339 {
340     ASSERT(!isNull());
341     if (paramsType() == WebCryptoAlgorithmParamsTypeRsaOaepParams)
342         return static_cast<WebCryptoRsaOaepParams*>(m_private->params.get());
343     return 0;
344 }
345
346 const WebCryptoRsaHashedImportParams* WebCryptoAlgorithm::rsaHashedImportParams() const
347 {
348     ASSERT(!isNull());
349     if (paramsType() == WebCryptoAlgorithmParamsTypeRsaHashedImportParams)
350         return static_cast<WebCryptoRsaHashedImportParams*>(m_private->params.get());
351     return 0;
352 }
353
354 const WebCryptoRsaHashedKeyGenParams* WebCryptoAlgorithm::rsaHashedKeyGenParams() const
355 {
356     ASSERT(!isNull());
357     if (paramsType() == WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams)
358         return static_cast<WebCryptoRsaHashedKeyGenParams*>(m_private->params.get());
359     return 0;
360 }
361
362 const WebCryptoRsaPssParams* WebCryptoAlgorithm::rsaPssParams() const
363 {
364     ASSERT(!isNull());
365     if (paramsType() == WebCryptoAlgorithmParamsTypeRsaPssParams)
366         return static_cast<WebCryptoRsaPssParams*>(m_private->params.get());
367     return 0;
368 }
369
370 bool WebCryptoAlgorithm::isHash(WebCryptoAlgorithmId id)
371 {
372     switch (id) {
373     case WebCryptoAlgorithmIdSha1:
374     case WebCryptoAlgorithmIdSha256:
375     case WebCryptoAlgorithmIdSha384:
376     case WebCryptoAlgorithmIdSha512:
377         return true;
378     case WebCryptoAlgorithmIdAesCbc:
379     case WebCryptoAlgorithmIdHmac:
380     case WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
381     case WebCryptoAlgorithmIdAesGcm:
382     case WebCryptoAlgorithmIdRsaOaep:
383     case WebCryptoAlgorithmIdAesCtr:
384     case WebCryptoAlgorithmIdAesKw:
385     case WebCryptoAlgorithmIdRsaPss:
386         break;
387     }
388     return false;
389 }
390
391 void WebCryptoAlgorithm::assign(const WebCryptoAlgorithm& other)
392 {
393     m_private = other.m_private;
394 }
395
396 void WebCryptoAlgorithm::reset()
397 {
398     m_private.reset();
399 }
400
401 } // namespace blink