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.
32 #include "public/platform/WebCryptoAlgorithm.h"
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"
44 // A mapping from the algorithm ID to information about the algorithm.
45 const WebCryptoAlgorithmInfo algorithmIdToInfo[] = {
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
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
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
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
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
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
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
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
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
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
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
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
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
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);
237 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithmPrivate> {
239 WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params)
245 WebCryptoAlgorithmId id;
246 OwnPtr<WebCryptoAlgorithmParams> params;
249 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params)
250 : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, params)))
254 WebCryptoAlgorithm WebCryptoAlgorithm::createNull()
256 return WebCryptoAlgorithm();
259 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, WebCryptoAlgorithmParams* params)
261 return WebCryptoAlgorithm(id, adoptPtr(params));
264 const WebCryptoAlgorithmInfo* WebCryptoAlgorithm::lookupAlgorithmInfo(WebCryptoAlgorithmId id)
266 if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
268 return &algorithmIdToInfo[id];
271 bool WebCryptoAlgorithm::isNull() const
273 return m_private.isNull();
276 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const
279 return m_private->id;
282 WebCryptoAlgorithmParamsType WebCryptoAlgorithm::paramsType() const
285 if (!m_private->params)
286 return WebCryptoAlgorithmParamsTypeNone;
287 return m_private->params->type();
290 const WebCryptoAesCbcParams* WebCryptoAlgorithm::aesCbcParams() const
293 if (paramsType() == WebCryptoAlgorithmParamsTypeAesCbcParams)
294 return static_cast<WebCryptoAesCbcParams*>(m_private->params.get());
298 const WebCryptoAesCtrParams* WebCryptoAlgorithm::aesCtrParams() const
301 if (paramsType() == WebCryptoAlgorithmParamsTypeAesCtrParams)
302 return static_cast<WebCryptoAesCtrParams*>(m_private->params.get());
306 const WebCryptoAesKeyGenParams* WebCryptoAlgorithm::aesKeyGenParams() const
309 if (paramsType() == WebCryptoAlgorithmParamsTypeAesKeyGenParams)
310 return static_cast<WebCryptoAesKeyGenParams*>(m_private->params.get());
314 const WebCryptoHmacImportParams* WebCryptoAlgorithm::hmacImportParams() const
317 if (paramsType() == WebCryptoAlgorithmParamsTypeHmacImportParams)
318 return static_cast<WebCryptoHmacImportParams*>(m_private->params.get());
322 const WebCryptoHmacKeyGenParams* WebCryptoAlgorithm::hmacKeyGenParams() const
325 if (paramsType() == WebCryptoAlgorithmParamsTypeHmacKeyGenParams)
326 return static_cast<WebCryptoHmacKeyGenParams*>(m_private->params.get());
330 const WebCryptoAesGcmParams* WebCryptoAlgorithm::aesGcmParams() const
333 if (paramsType() == WebCryptoAlgorithmParamsTypeAesGcmParams)
334 return static_cast<WebCryptoAesGcmParams*>(m_private->params.get());
338 const WebCryptoRsaOaepParams* WebCryptoAlgorithm::rsaOaepParams() const
341 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaOaepParams)
342 return static_cast<WebCryptoRsaOaepParams*>(m_private->params.get());
346 const WebCryptoRsaHashedImportParams* WebCryptoAlgorithm::rsaHashedImportParams() const
349 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaHashedImportParams)
350 return static_cast<WebCryptoRsaHashedImportParams*>(m_private->params.get());
354 const WebCryptoRsaHashedKeyGenParams* WebCryptoAlgorithm::rsaHashedKeyGenParams() const
357 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams)
358 return static_cast<WebCryptoRsaHashedKeyGenParams*>(m_private->params.get());
362 const WebCryptoRsaPssParams* WebCryptoAlgorithm::rsaPssParams() const
365 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaPssParams)
366 return static_cast<WebCryptoRsaPssParams*>(m_private->params.get());
370 bool WebCryptoAlgorithm::isHash(WebCryptoAlgorithmId id)
373 case WebCryptoAlgorithmIdSha1:
374 case WebCryptoAlgorithmIdSha256:
375 case WebCryptoAlgorithmIdSha384:
376 case WebCryptoAlgorithmIdSha512:
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:
391 void WebCryptoAlgorithm::assign(const WebCryptoAlgorithm& other)
393 m_private = other.m_private;
396 void WebCryptoAlgorithm::reset()