OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "public/platform/WebCryptoAlgorithm.h" | 32 #include "public/platform/WebCryptoAlgorithm.h" |
33 | 33 |
34 #include "public/platform/WebCryptoAlgorithmParams.h" | 34 #include "public/platform/WebCryptoAlgorithmParams.h" |
| 35 #include "wtf/Assertions.h" |
35 #include "wtf/OwnPtr.h" | 36 #include "wtf/OwnPtr.h" |
| 37 #include "wtf/StdLibExtras.h" |
36 #include "wtf/ThreadSafeRefCounted.h" | 38 #include "wtf/ThreadSafeRefCounted.h" |
37 | 39 |
38 namespace blink { | 40 namespace blink { |
39 | 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 }, |
| 201 }; |
| 202 |
| 203 // Initializing the algorithmIdToInfo table above depends on knowing the enum |
| 204 // values for algorithm IDs. If those ever change, the table will need to be |
| 205 // updated. |
| 206 COMPILE_ASSERT(WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch); |
| 207 COMPILE_ASSERT(WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch); |
| 208 COMPILE_ASSERT(WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_idDoesn
tMatch); |
| 209 COMPILE_ASSERT(WebCryptoAlgorithmIdSha1 == 3, Sha1_idDoesntMatch); |
| 210 COMPILE_ASSERT(WebCryptoAlgorithmIdSha256 == 4, Sha256_idDoesntMatch); |
| 211 COMPILE_ASSERT(WebCryptoAlgorithmIdSha384 == 5, Sha384_idDoesntMatch); |
| 212 COMPILE_ASSERT(WebCryptoAlgorithmIdSha512 == 6, Sha512_idDoesntMatch); |
| 213 COMPILE_ASSERT(WebCryptoAlgorithmIdAesGcm == 7, AesGcm_idDoesntMatch); |
| 214 COMPILE_ASSERT(WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch); |
| 215 COMPILE_ASSERT(WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch); |
| 216 COMPILE_ASSERT(WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch); |
| 217 COMPILE_ASSERT(WebCryptoAlgorithmIdLast == 10, Last_idDoesntMatch); |
| 218 COMPILE_ASSERT(10 == WebCryptoOperationLast, UpdateParamsMapping); |
| 219 |
| 220 } // namespace |
| 221 |
40 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithm
Private> { | 222 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithm
Private> { |
41 public: | 223 public: |
42 WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgor
ithmParams> params) | 224 WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgor
ithmParams> params) |
43 : id(id) | 225 : id(id) |
44 , params(params) | 226 , params(params) |
45 { | 227 { |
46 } | 228 } |
47 | 229 |
48 WebCryptoAlgorithmId id; | 230 WebCryptoAlgorithmId id; |
49 OwnPtr<WebCryptoAlgorithmParams> params; | 231 OwnPtr<WebCryptoAlgorithmParams> params; |
50 }; | 232 }; |
51 | 233 |
52 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCr
yptoAlgorithmParams> params) | 234 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCr
yptoAlgorithmParams> params) |
53 : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, params))) | 235 : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, params))) |
54 { | 236 { |
55 } | 237 } |
56 | 238 |
57 WebCryptoAlgorithm WebCryptoAlgorithm::createNull() | 239 WebCryptoAlgorithm WebCryptoAlgorithm::createNull() |
58 { | 240 { |
59 return WebCryptoAlgorithm(); | 241 return WebCryptoAlgorithm(); |
60 } | 242 } |
61 | 243 |
62 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId
id, WebCryptoAlgorithmParams* params) | 244 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId
id, WebCryptoAlgorithmParams* params) |
63 { | 245 { |
64 return WebCryptoAlgorithm(id, adoptPtr(params)); | 246 return WebCryptoAlgorithm(id, adoptPtr(params)); |
65 } | 247 } |
66 | 248 |
| 249 const WebCryptoAlgorithmInfo* WebCryptoAlgorithm::lookupAlgorithmInfo(WebCryptoA
lgorithmId id) |
| 250 { |
| 251 if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo)) |
| 252 return 0; |
| 253 return &algorithmIdToInfo[id]; |
| 254 } |
| 255 |
67 bool WebCryptoAlgorithm::isNull() const | 256 bool WebCryptoAlgorithm::isNull() const |
68 { | 257 { |
69 return m_private.isNull(); | 258 return m_private.isNull(); |
70 } | 259 } |
71 | 260 |
72 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const | 261 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const |
73 { | 262 { |
74 ASSERT(!isNull()); | 263 ASSERT(!isNull()); |
75 return m_private->id; | 264 return m_private->id; |
76 } | 265 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 { | 368 { |
180 m_private = other.m_private; | 369 m_private = other.m_private; |
181 } | 370 } |
182 | 371 |
183 void WebCryptoAlgorithm::reset() | 372 void WebCryptoAlgorithm::reset() |
184 { | 373 { |
185 m_private.reset(); | 374 m_private.reset(); |
186 } | 375 } |
187 | 376 |
188 } // namespace blink | 377 } // namespace blink |
OLD | NEW |