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 WebCryptoAlgorithm_h
32 #define WebCryptoAlgorithm_h
34 #include "WebCommon.h"
35 #include "WebPrivatePtr.h"
38 #include "wtf/PassOwnPtr.h"
43 enum WebCryptoOperation
{
44 WebCryptoOperationEncrypt
,
45 WebCryptoOperationDecrypt
,
46 WebCryptoOperationSign
,
47 WebCryptoOperationVerify
,
48 WebCryptoOperationDigest
,
49 WebCryptoOperationGenerateKey
,
50 WebCryptoOperationImportKey
,
51 WebCryptoOperationGetKeyLength
,
52 WebCryptoOperationDeriveBits
,
53 WebCryptoOperationWrapKey
,
54 WebCryptoOperationUnwrapKey
,
55 WebCryptoOperationLast
= WebCryptoOperationUnwrapKey
,
58 enum WebCryptoAlgorithmId
{
59 WebCryptoAlgorithmIdAesCbc
,
60 WebCryptoAlgorithmIdHmac
,
61 WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
,
62 WebCryptoAlgorithmIdSha1
,
63 WebCryptoAlgorithmIdSha256
,
64 WebCryptoAlgorithmIdSha384
,
65 WebCryptoAlgorithmIdSha512
,
66 WebCryptoAlgorithmIdAesGcm
,
67 WebCryptoAlgorithmIdRsaOaep
,
68 WebCryptoAlgorithmIdAesCtr
,
69 WebCryptoAlgorithmIdAesKw
,
70 WebCryptoAlgorithmIdRsaPss
,
71 WebCryptoAlgorithmIdEcdsa
,
72 WebCryptoAlgorithmIdEcdh
,
73 WebCryptoAlgorithmIdHkdf
,
74 WebCryptoAlgorithmIdPbkdf2
,
76 WebCryptoAlgorithmIdLast
= WebCryptoAlgorithmIdPbkdf2
,
80 enum WebCryptoNamedCurve
{
81 WebCryptoNamedCurveP256
,
82 WebCryptoNamedCurveP384
,
83 WebCryptoNamedCurveP521
,
85 WebCryptoNamedCurveLast
= WebCryptoNamedCurveP521
,
89 enum WebCryptoAlgorithmParamsType
{
90 WebCryptoAlgorithmParamsTypeNone
,
91 WebCryptoAlgorithmParamsTypeAesCbcParams
,
92 WebCryptoAlgorithmParamsTypeAesKeyGenParams
,
93 WebCryptoAlgorithmParamsTypeHmacImportParams
,
94 WebCryptoAlgorithmParamsTypeHmacKeyGenParams
,
95 WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams
,
96 WebCryptoAlgorithmParamsTypeRsaHashedImportParams
,
97 WebCryptoAlgorithmParamsTypeAesGcmParams
,
98 WebCryptoAlgorithmParamsTypeRsaOaepParams
,
99 WebCryptoAlgorithmParamsTypeAesCtrParams
,
100 WebCryptoAlgorithmParamsTypeRsaPssParams
,
101 WebCryptoAlgorithmParamsTypeEcdsaParams
,
102 WebCryptoAlgorithmParamsTypeEcKeyGenParams
,
103 WebCryptoAlgorithmParamsTypeEcKeyImportParams
,
104 WebCryptoAlgorithmParamsTypeEcdhKeyDeriveParams
,
105 WebCryptoAlgorithmParamsTypeAesDerivedKeyParams
,
106 WebCryptoAlgorithmParamsTypeHkdfParams
,
107 WebCryptoAlgorithmParamsTypePbkdf2Params
,
110 struct WebCryptoAlgorithmInfo
{
111 typedef char ParamsTypeOrUndefined
;
112 static const ParamsTypeOrUndefined Undefined
= -1;
114 // The canonical (case-sensitive) name for the algorithm as a
115 // null-terminated C-string literal.
118 // A map from the operation to the expected parameter type of the algorithm.
119 // If an operation is not applicable for the algorithm, set to Undefined.
120 const ParamsTypeOrUndefined operationToParamsType
[WebCryptoOperationLast
+ 1];
124 class WebCryptoAesCbcParams
;
125 class WebCryptoAesKeyGenParams
;
126 class WebCryptoHmacImportParams
;
127 class WebCryptoHmacKeyGenParams
;
128 class WebCryptoAesGcmParams
;
129 class WebCryptoRsaOaepParams
;
130 class WebCryptoAesCtrParams
;
131 class WebCryptoRsaHashedKeyGenParams
;
132 class WebCryptoRsaHashedImportParams
;
133 class WebCryptoRsaPssParams
;
134 class WebCryptoEcdsaParams
;
135 class WebCryptoEcKeyGenParams
;
136 class WebCryptoEcKeyImportParams
;
137 class WebCryptoEcdhKeyDeriveParams
;
138 class WebCryptoAesDerivedKeyParams
;
139 class WebCryptoHkdfParams
;
140 class WebCryptoPbkdf2Params
;
142 class WebCryptoAlgorithmParams
;
143 class WebCryptoAlgorithmPrivate
;
145 // The WebCryptoAlgorithm represents a normalized algorithm and its parameters.
148 // * Copiable (cheaply)
150 // If WebCryptoAlgorithm "isNull()" then it is invalid to call any of the other
151 // methods on it (other than destruction, assignment, or isNull()).
152 class WebCryptoAlgorithm
{
155 WebCryptoAlgorithm() { }
156 BLINK_PLATFORM_EXPORT
WebCryptoAlgorithm(WebCryptoAlgorithmId
, PassOwnPtr
<WebCryptoAlgorithmParams
>);
159 BLINK_PLATFORM_EXPORT
static WebCryptoAlgorithm
createNull();
160 BLINK_PLATFORM_EXPORT
static WebCryptoAlgorithm
adoptParamsAndCreate(WebCryptoAlgorithmId
, WebCryptoAlgorithmParams
*);
162 // Returns a WebCryptoAlgorithmInfo for the algorithm with the given ID. If
163 // the ID is invalid, return 0. The caller can assume the pointer will be
164 // valid for the program's entire runtime.
165 BLINK_PLATFORM_EXPORT
static const WebCryptoAlgorithmInfo
* lookupAlgorithmInfo(WebCryptoAlgorithmId
);
167 ~WebCryptoAlgorithm() { reset(); }
169 WebCryptoAlgorithm(const WebCryptoAlgorithm
& other
) { assign(other
); }
170 WebCryptoAlgorithm
& operator=(const WebCryptoAlgorithm
& other
)
176 BLINK_PLATFORM_EXPORT
bool isNull() const;
178 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmId
id() const;
180 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmParamsType
paramsType() const;
182 // Retrieves the type-specific parameters. The algorithm contains at most 1
183 // type of parameters. Retrieving an invalid parameter will return 0.
184 BLINK_PLATFORM_EXPORT
const WebCryptoAesCbcParams
* aesCbcParams() const;
185 BLINK_PLATFORM_EXPORT
const WebCryptoAesKeyGenParams
* aesKeyGenParams() const;
186 BLINK_PLATFORM_EXPORT
const WebCryptoHmacImportParams
* hmacImportParams() const;
187 BLINK_PLATFORM_EXPORT
const WebCryptoHmacKeyGenParams
* hmacKeyGenParams() const;
188 BLINK_PLATFORM_EXPORT
const WebCryptoAesGcmParams
* aesGcmParams() const;
189 BLINK_PLATFORM_EXPORT
const WebCryptoRsaOaepParams
* rsaOaepParams() const;
190 BLINK_PLATFORM_EXPORT
const WebCryptoAesCtrParams
* aesCtrParams() const;
191 BLINK_PLATFORM_EXPORT
const WebCryptoRsaHashedImportParams
* rsaHashedImportParams() const;
192 BLINK_PLATFORM_EXPORT
const WebCryptoRsaHashedKeyGenParams
* rsaHashedKeyGenParams() const;
193 BLINK_PLATFORM_EXPORT
const WebCryptoRsaPssParams
* rsaPssParams() const;
194 BLINK_PLATFORM_EXPORT
const WebCryptoEcdsaParams
* ecdsaParams() const;
195 BLINK_PLATFORM_EXPORT
const WebCryptoEcKeyGenParams
* ecKeyGenParams() const;
196 BLINK_PLATFORM_EXPORT
const WebCryptoEcKeyImportParams
* ecKeyImportParams() const;
197 BLINK_PLATFORM_EXPORT
const WebCryptoEcdhKeyDeriveParams
* ecdhKeyDeriveParams() const;
198 BLINK_PLATFORM_EXPORT
const WebCryptoAesDerivedKeyParams
* aesDerivedKeyParams() const;
199 BLINK_PLATFORM_EXPORT
const WebCryptoHkdfParams
* hkdfParams() const;
200 BLINK_PLATFORM_EXPORT
const WebCryptoPbkdf2Params
* pbkdf2Params() const;
202 // Returns true if the provided algorithm ID is for a hash (in other words, SHA-*)
203 BLINK_PLATFORM_EXPORT
static bool isHash(WebCryptoAlgorithmId
);
204 // Returns true if the provided algorithm ID is for a key derivation function
205 BLINK_PLATFORM_EXPORT
static bool isKdf(WebCryptoAlgorithmId
);
208 BLINK_PLATFORM_EXPORT
void assign(const WebCryptoAlgorithm
& other
);
209 BLINK_PLATFORM_EXPORT
void reset();
211 WebPrivatePtr
<WebCryptoAlgorithmPrivate
> m_private
;