Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: Source/modules/crypto/NormalizeAlgorithm.cpp

Issue 255453002: [refactor] Use a lookup table rather than binary search for algorithm normalization. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/crypto/NormalizeAlgorithm.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 const char* const algorithmName; 53 const char* const algorithmName;
54 // Must be strlen(algorithmName). 54 // Must be strlen(algorithmName).
55 unsigned char algorithmNameLength; 55 unsigned char algorithmNameLength;
56 blink::WebCryptoAlgorithmId algorithmId; 56 blink::WebCryptoAlgorithmId algorithmId;
57 57
58 #if ASSERT_ENABLED 58 #if ASSERT_ENABLED
59 bool operator<(const AlgorithmNameMapping&) const; 59 bool operator<(const AlgorithmNameMapping&) const;
60 #endif 60 #endif
61 }; 61 };
62 62
63 struct OperationParamsMapping {
64 blink::WebCryptoAlgorithmId algorithmId;
65 AlgorithmOperation operation;
66 blink::WebCryptoAlgorithmParamsType params;
67
68 bool operator<(const OperationParamsMapping&) const;
69 };
70
71 // Must be sorted by length, and then by reverse string. 63 // Must be sorted by length, and then by reverse string.
72 // Also all names must be upper case ASCII. 64 // Also all names must be upper case ASCII.
73 const AlgorithmNameMapping algorithmNameMappings[] = { 65 const AlgorithmNameMapping algorithmNameMappings[] = {
74 {"HMAC", 4, blink::WebCryptoAlgorithmIdHmac}, 66 {"HMAC", 4, blink::WebCryptoAlgorithmIdHmac},
75 {"SHA-1", 5, blink::WebCryptoAlgorithmIdSha1}, 67 {"SHA-1", 5, blink::WebCryptoAlgorithmIdSha1},
76 {"AES-KW", 6, blink::WebCryptoAlgorithmIdAesKw}, 68 {"AES-KW", 6, blink::WebCryptoAlgorithmIdAesKw},
77 {"SHA-512", 7, blink::WebCryptoAlgorithmIdSha512}, 69 {"SHA-512", 7, blink::WebCryptoAlgorithmIdSha512},
78 {"SHA-384", 7, blink::WebCryptoAlgorithmIdSha384}, 70 {"SHA-384", 7, blink::WebCryptoAlgorithmIdSha384},
79 {"SHA-256", 7, blink::WebCryptoAlgorithmIdSha256}, 71 {"SHA-256", 7, blink::WebCryptoAlgorithmIdSha256},
80 {"AES-CBC", 7, blink::WebCryptoAlgorithmIdAesCbc}, 72 {"AES-CBC", 7, blink::WebCryptoAlgorithmIdAesCbc},
81 {"AES-GCM", 7, blink::WebCryptoAlgorithmIdAesGcm}, 73 {"AES-GCM", 7, blink::WebCryptoAlgorithmIdAesGcm},
82 {"AES-CTR", 7, blink::WebCryptoAlgorithmIdAesCtr}, 74 {"AES-CTR", 7, blink::WebCryptoAlgorithmIdAesCtr},
83 {"RSAES-PKCS1-V1_5", 16, blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5}, 75 {"RSAES-PKCS1-V1_5", 16, blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5},
84 {"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, 76 {"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5},
85 }; 77 };
86 78
87 // What operations each algorithm supports, and what parameters it expects. 79 typedef char ParamsTypeOrUndefined;
88 // Must be sorted by algorithm id and then operation. 80 const ParamsTypeOrUndefined Undefined = -1;
89 const OperationParamsMapping operationParamsMappings[] = { 81
90 // AES-CBC 82 struct AlgorithmInfo {
eroman 2014/04/23 23:26:23 Rather than introduce another switch statement on
91 {blink::WebCryptoAlgorithmIdAesCbc, Encrypt, blink::WebCryptoAlgorithmParams TypeAesCbcParams}, 83 // The canonical (case-sensitive) name for the algorithm.
92 {blink::WebCryptoAlgorithmIdAesCbc, Decrypt, blink::WebCryptoAlgorithmParams TypeAesCbcParams}, 84 const char* name;
93 {blink::WebCryptoAlgorithmIdAesCbc, GenerateKey, blink::WebCryptoAlgorithmPa ramsTypeAesKeyGenParams}, 85
94 {blink::WebCryptoAlgorithmIdAesCbc, ImportKey, blink::WebCryptoAlgorithmPara msTypeNone}, 86 // The full set of key usages allowed by the algorithm. In the case of
95 {blink::WebCryptoAlgorithmIdAesCbc, WrapKey, blink::WebCryptoAlgorithmParams TypeAesCbcParams}, 87 // asymmetric algorithms, it is the union of key usages for public and
96 {blink::WebCryptoAlgorithmIdAesCbc, UnwrapKey, blink::WebCryptoAlgorithmPara msTypeAesCbcParams}, 88 // private keys.
97 89 blink::WebCryptoKeyUsageMask allKeyUsages;
98 // HMAC 90
99 {blink::WebCryptoAlgorithmIdHmac, Sign, blink::WebCryptoAlgorithmParamsTypeN one}, 91 // A map from the operation to the expected parameter type of the algorithm.
100 {blink::WebCryptoAlgorithmIdHmac, Verify, blink::WebCryptoAlgorithmParamsTyp eNone}, 92 // If an operation is not applicable for the algorithm, set to Undefined.
101 {blink::WebCryptoAlgorithmIdHmac, GenerateKey, blink::WebCryptoAlgorithmPara msTypeHmacKeyGenParams}, 93 const ParamsTypeOrUndefined operationToParamsType[LastAlgorithmOperation + 1 ];
102 {blink::WebCryptoAlgorithmIdHmac, ImportKey, blink::WebCryptoAlgorithmParams TypeHmacImportParams},
103
104 // RSASSA-PKCS1-v1_5
105 {blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, Sign, blink::WebCryptoAlgorithm ParamsTypeNone},
106 {blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, Verify, blink::WebCryptoAlgorit hmParamsTypeNone},
107 {blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, GenerateKey, blink::WebCryptoAl gorithmParamsTypeRsaHashedKeyGenParams},
108 {blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, ImportKey, blink::WebCryptoAlgo rithmParamsTypeRsaHashedImportParams},
109
110 // RSAES-PKCS1-v1_5
111 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, Encrypt, blink::WebCryptoAlgorit hmParamsTypeNone},
112 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, Decrypt, blink::WebCryptoAlgorit hmParamsTypeNone},
113 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, GenerateKey, blink::WebCryptoAlg orithmParamsTypeRsaKeyGenParams},
114 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, ImportKey, blink::WebCryptoAlgor ithmParamsTypeNone},
115 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, WrapKey, blink::WebCryptoAlgorit hmParamsTypeNone},
116 {blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, UnwrapKey, blink::WebCryptoAlgor ithmParamsTypeNone},
117
118 // SHA-*
119 {blink::WebCryptoAlgorithmIdSha1, Digest, blink::WebCryptoAlgorithmParamsTyp eNone},
120 {blink::WebCryptoAlgorithmIdSha256, Digest, blink::WebCryptoAlgorithmParamsT ypeNone},
121 {blink::WebCryptoAlgorithmIdSha384, Digest, blink::WebCryptoAlgorithmParamsT ypeNone},
122 {blink::WebCryptoAlgorithmIdSha512, Digest, blink::WebCryptoAlgorithmParamsT ypeNone},
123
124 // AES-GCM
125 {blink::WebCryptoAlgorithmIdAesGcm, Encrypt, blink::WebCryptoAlgorithmParams TypeAesGcmParams},
126 {blink::WebCryptoAlgorithmIdAesGcm, Decrypt, blink::WebCryptoAlgorithmParams TypeAesGcmParams},
127 {blink::WebCryptoAlgorithmIdAesGcm, GenerateKey, blink::WebCryptoAlgorithmPa ramsTypeAesKeyGenParams},
128 {blink::WebCryptoAlgorithmIdAesGcm, ImportKey, blink::WebCryptoAlgorithmPara msTypeNone},
129 {blink::WebCryptoAlgorithmIdAesGcm, WrapKey, blink::WebCryptoAlgorithmParams TypeAesGcmParams},
130 {blink::WebCryptoAlgorithmIdAesGcm, UnwrapKey, blink::WebCryptoAlgorithmPara msTypeAesGcmParams},
131
132 // AES-CTR
133 {blink::WebCryptoAlgorithmIdAesCtr, Encrypt, blink::WebCryptoAlgorithmParams TypeAesCtrParams},
134 {blink::WebCryptoAlgorithmIdAesCtr, Decrypt, blink::WebCryptoAlgorithmParams TypeAesCtrParams},
135 {blink::WebCryptoAlgorithmIdAesCtr, GenerateKey, blink::WebCryptoAlgorithmPa ramsTypeAesKeyGenParams},
136 {blink::WebCryptoAlgorithmIdAesCtr, ImportKey, blink::WebCryptoAlgorithmPara msTypeNone},
137 {blink::WebCryptoAlgorithmIdAesCtr, WrapKey, blink::WebCryptoAlgorithmParams TypeAesCtrParams},
138 {blink::WebCryptoAlgorithmIdAesCtr, UnwrapKey, blink::WebCryptoAlgorithmPara msTypeAesCtrParams},
139
140 // AES-KW
141 {blink::WebCryptoAlgorithmIdAesKw, GenerateKey, blink::WebCryptoAlgorithmPar amsTypeAesKeyGenParams},
142 {blink::WebCryptoAlgorithmIdAesKw, ImportKey, blink::WebCryptoAlgorithmParam sTypeNone},
143 {blink::WebCryptoAlgorithmIdAesKw, WrapKey, blink::WebCryptoAlgorithmParamsT ypeNone},
144 {blink::WebCryptoAlgorithmIdAesKw, UnwrapKey, blink::WebCryptoAlgorithmParam sTypeNone},
145 }; 94 };
146 95
96 // A mapping from the algorithm ID to information about the algorithm.
97 const AlgorithmInfo algorithmIdToInfo[] = {
98 { // Index 0
99 "AES-CBC",
eroman 2014/04/23 23:26:23 The names were moved from algorithmIdToName().
100 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | blin k::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
101 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
102 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
103 Undefined, // Sign
104 Undefined, // Verify
105 Undefined, // Digest
106 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
107 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
108 Undefined, // DeriveKey
109 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
110 blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
111 }
112 }, { // Index 1
113 "HMAC",
114 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, {
115 Undefined, // Encrypt
116 Undefined, // Decrypt
117 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
118 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
119 Undefined, // Digest
120 blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
121 blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
122 Undefined, // DeriveKey
123 Undefined, // WrapKey
124 Undefined // UnwrapKey
125 }
126 }, { // Index 2
127 "RSASSA-PKCS1-v1_5",
128 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, {
129 Undefined, // Encrypt
130 Undefined, // Decrypt
131 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
132 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
133 Undefined, // Digest
134 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
135 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
136 Undefined, // DeriveKey
137 Undefined, // WrapKey
138 Undefined // UnwrapKey
139 }
140 }, { // Index 3
141 "RSAES-PKCS1-v1_5",
142 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | blin k::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
143 blink::WebCryptoAlgorithmParamsTypeNone, // Encrypt
144 blink::WebCryptoAlgorithmParamsTypeNone, // Decrypt
145 Undefined, // Sign
146 Undefined, // Verify
147 Undefined, // Digest
148 blink::WebCryptoAlgorithmParamsTypeRsaKeyGenParams, // GenerateKey
149 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
150 Undefined, // DeriveKey
151 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
152 blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
153 }
154 }, { // Index 4
155 "SHA-1",
156 0, {
157 Undefined, // Encrypt
158 Undefined, // Decrypt
159 Undefined, // Sign
160 Undefined, // Verify
161 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
162 Undefined, // GenerateKey
163 Undefined, // ImportKey
164 Undefined, // DeriveKey
165 Undefined, // WrapKey
166 Undefined // UnwrapKey
167 }
168 }, { // Index 5
169 "SHA-256",
170 0, {
171 Undefined, // Encrypt
172 Undefined, // Decrypt
173 Undefined, // Sign
174 Undefined, // Verify
175 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
176 Undefined, // GenerateKey
177 Undefined, // ImportKey
178 Undefined, // DeriveKey
179 Undefined, // WrapKey
180 Undefined // UnwrapKey
181 }
182 }, { // Index 6
183 "SHA-384",
184 0, {
185 Undefined, // Encrypt
186 Undefined, // Decrypt
187 Undefined, // Sign
188 Undefined, // Verify
189 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
190 Undefined, // GenerateKey
191 Undefined, // ImportKey
192 Undefined, // DeriveKey
193 Undefined, // WrapKey
194 Undefined // UnwrapKey
195 }
196 }, { // Index 7
197 "SHA-512",
198 0, {
199 Undefined, // Encrypt
200 Undefined, // Decrypt
201 Undefined, // Sign
202 Undefined, // Verify
203 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
204 Undefined, // GenerateKey
205 Undefined, // ImportKey
206 Undefined, // DeriveKey
207 Undefined, // WrapKey
208 Undefined // UnwrapKey
209 }
210 }, { // Index 8
211 "AES-GCM",
212 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | blin k::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
213 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
214 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
215 Undefined, // Sign
216 Undefined, // Verify
217 Undefined, // Digest
218 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
219 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
220 Undefined, // DeriveKey
221 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
222 blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
223 }
224 }, { // Index 9
225 "RSA-OAEP",
226 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | blin k::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
227 // FIXME:
228 Undefined, // Encrypt
229 Undefined, // Decrypt
230 Undefined, // Sign
231 Undefined, // Verify
232 Undefined, // Digest
233 Undefined, // GenerateKey
234 Undefined, // ImportKey
235 Undefined, // DeriveKey
236 Undefined, // WrapKey
237 Undefined // UnwrapKey
238 }
239 }, { // Index 10
240 "AES-CTR",
241 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | blin k::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
242 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
243 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
244 Undefined, // Sign
245 Undefined, // Verify
246 Undefined, // Digest
247 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
248 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
249 Undefined, // DeriveKey
250 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
251 blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
252 }
253 }, { // Index 11
254 "AES-KW",
255 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, {
256 Undefined, // Encrypt
257 Undefined, // Decrypt
258 Undefined, // Sign
259 Undefined, // Verify
260 Undefined, // Digest
261 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
262 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
263 Undefined, // DeriveKey
264 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
265 blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
266 }
267 },
268 };
269
270 // Initializing the algorithmIdToInfo table above depends on knowing the enum
271 // values for algorithm IDs. If those ever change, the table will need to be
272 // updated.
273 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch);
274 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch);
275 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_ idDoesntMatch);
276 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 == 3, RsaEsPkcs1v1_5_id DoesntMatch);
277 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha1 == 4, Sha1_idDoesntMatch);
278 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha256 == 5, Sha256_idDoesntMatch);
279 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha384 == 6, Sha384_idDoesntMatch);
280 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha512 == 7, Sha512_idDoesntMatch);
281 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesGcm == 8, AesGcm_idDoesntMatch);
282 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 9, RsaOaep_idDoesntMatch);
283 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 10, AesCtr_idDoesntMatch);
284 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 11, AesKw_idDoesntMatch);
285 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 11, Last_idDoesntMatch);
286 COMPILE_ASSERT(9 == LastAlgorithmOperation, UpdateParamsMapping);
287
147 #if ASSERT_ENABLED 288 #if ASSERT_ENABLED
148 289
149 // Essentially std::is_sorted() (however that function is new to C++11). 290 // Essentially std::is_sorted() (however that function is new to C++11).
150 template <typename Iterator> 291 template <typename Iterator>
151 bool isSorted(Iterator begin, Iterator end) 292 bool isSorted(Iterator begin, Iterator end)
152 { 293 {
153 if (begin == end) 294 if (begin == end)
154 return true; 295 return true;
155 296
156 Iterator prev = begin; 297 Iterator prev = begin;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 if (!str.containsOnlyASCII()) 337 if (!str.containsOnlyASCII())
197 return false; 338 return false;
198 if (str.upper() != str) 339 if (str.upper() != str)
199 return false; 340 return false;
200 } 341 }
201 342
202 return isSorted(begin, end); 343 return isSorted(begin, end);
203 } 344 }
204 #endif 345 #endif
205 346
206 bool OperationParamsMapping::operator<(const OperationParamsMapping& o) const
207 {
208 if (algorithmId < o.algorithmId)
209 return true;
210 if (algorithmId > o.algorithmId)
211 return false;
212 return operation < o.operation;
213 }
214
215 template <typename CharType> 347 template <typename CharType>
216 bool algorithmNameComparator(const AlgorithmNameMapping& a, StringImpl* b) 348 bool algorithmNameComparator(const AlgorithmNameMapping& a, StringImpl* b)
217 { 349 {
218 if (a.algorithmNameLength < b->length()) 350 if (a.algorithmNameLength < b->length())
219 return true; 351 return true;
220 if (a.algorithmNameLength > b->length()) 352 if (a.algorithmNameLength > b->length())
221 return false; 353 return false;
222 354
223 // Because the algorithm names contain many common prefixes, it is better 355 // Because the algorithm names contain many common prefixes, it is better
224 // to compare starting at the end of the string. 356 // to compare starting at the end of the string.
(...skipping 30 matching lines...) Expand all
255 if (it == end) 387 if (it == end)
256 return false; 388 return false;
257 389
258 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName)) 390 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName))
259 return false; 391 return false;
260 392
261 id = it->algorithmId; 393 id = it->algorithmId;
262 return true; 394 return true;
263 } 395 }
264 396
265 bool lookupAlgorithmParamsType(blink::WebCryptoAlgorithmId id, AlgorithmOperatio n op, blink::WebCryptoAlgorithmParamsType& paramsType) 397 const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id)
266 { 398 {
267 const OperationParamsMapping* begin = operationParamsMappings; 399 if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
268 const OperationParamsMapping* end = operationParamsMappings + WTF_ARRAY_LENG TH(operationParamsMappings); 400 return 0;
269 401 return &algorithmIdToInfo[id];
270 ASSERT(isSorted(begin, end));
271
272 OperationParamsMapping search = { id, op };
273 const OperationParamsMapping* it = std::lower_bound(begin, end, search);
274 if (it == end)
275 return false;
276 if (it->algorithmId != id || it->operation != op)
277 return false;
278 paramsType = it->params;
279 return true;
280 } 402 }
281 403
282 void completeWithSyntaxError(const String& message, CryptoResult* result) 404 void completeWithSyntaxError(const String& message, CryptoResult* result)
283 { 405 {
284 result->completeWithError(blink::WebCryptoErrorTypeSyntax, message); 406 result->completeWithError(blink::WebCryptoErrorTypeSyntax, message);
285 } 407 }
286 408
287 void completeWithNotSupportedError(const String& message, CryptoResult* result) 409 void completeWithNotSupportedError(const String& message, CryptoResult* result)
288 { 410 {
289 result->completeWithError(blink::WebCryptoErrorTypeNotSupported, message); 411 result->completeWithError(blink::WebCryptoErrorTypeNotSupported, message);
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { 910 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) {
789 // FIXME: The spec says to return a SyntaxError if the input contains 911 // FIXME: The spec says to return a SyntaxError if the input contains
790 // any non-ASCII characters. 912 // any non-ASCII characters.
791 completeWithNotSupportedError(context.toString("Unrecognized name"), res ult); 913 completeWithNotSupportedError(context.toString("Unrecognized name"), res ult);
792 return false; 914 return false;
793 } 915 }
794 916
795 // Remove the "Algorithm:" prefix for all subsequent errors. 917 // Remove the "Algorithm:" prefix for all subsequent errors.
796 context.removeLast(); 918 context.removeLast();
797 919
798 blink::WebCryptoAlgorithmParamsType paramsType; 920 const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId);
799 if (!lookupAlgorithmParamsType(algorithmId, op, paramsType)) { 921
800 context.add(algorithmIdToName(algorithmId)); 922 if (algorithmInfo->operationToParamsType[op] == Undefined) {
923 context.add(algorithmInfo->name);
801 completeWithNotSupportedError(context.toString("Unsupported operation", operationToString(op)), result); 924 completeWithNotSupportedError(context.toString("Unsupported operation", operationToString(op)), result);
802 return false; 925 return false;
803 } 926 }
804 927
928 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]);
929
805 OwnPtr<blink::WebCryptoAlgorithmParams> params; 930 OwnPtr<blink::WebCryptoAlgorithmParams> params;
806 if (!parseAlgorithmParams(raw, paramsType, params, context, result)) 931 if (!parseAlgorithmParams(raw, paramsType, params, context, result))
807 return false; 932 return false;
808 933
809 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release()); 934 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release());
810 return true; 935 return true;
811 } 936 }
812 937
813 } // namespace 938 } // namespace
814 939
815 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp toAlgorithm& algorithm, CryptoResult* result) 940 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp toAlgorithm& algorithm, CryptoResult* result)
816 { 941 {
817 return parseAlgorithm(raw, op, algorithm, ErrorContext(), result); 942 return parseAlgorithm(raw, op, algorithm, ErrorContext(), result);
818 } 943 }
819 944
820 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id) 945 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
821 { 946 {
822 switch (id) { 947 return lookupAlgorithmInfo(id)->name;
823 case blink::WebCryptoAlgorithmIdAesCbc: 948 }
824 return "AES-CBC"; 949
825 case blink::WebCryptoAlgorithmIdAesCtr: 950 bool verifyUsagesAreConsistentForAlgorithm(blink::WebCryptoAlgorithmId id, blink ::WebCryptoKeyUsageMask keyUsages, CryptoResult* result)
826 return "AES-CTR"; 951 {
827 case blink::WebCryptoAlgorithmIdAesGcm: 952 if ((keyUsages & lookupAlgorithmInfo(id)->allKeyUsages) != keyUsages) {
828 return "AES-GCM"; 953 completeWithDataError("keyUsages contains values not applicable to the a lgorithm", result);
829 case blink::WebCryptoAlgorithmIdHmac: 954 return false;
830 return "HMAC";
831 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
832 return "RSASSA-PKCS1-v1_5";
833 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
834 return "RSAES-PKCS1-v1_5";
835 case blink::WebCryptoAlgorithmIdSha1:
836 return "SHA-1";
837 case blink::WebCryptoAlgorithmIdSha256:
838 return "SHA-256";
839 case blink::WebCryptoAlgorithmIdSha384:
840 return "SHA-384";
841 case blink::WebCryptoAlgorithmIdSha512:
842 return "SHA-512";
843 case blink::WebCryptoAlgorithmIdAesKw:
844 return "AES-KW";
845 case blink::WebCryptoAlgorithmIdRsaOaep:
846 return "RSA-OAEP";
847 } 955 }
848 return 0; 956 return true;
849 } 957 }
850 958
851 } // namespace WebCore 959 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/NormalizeAlgorithm.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698