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

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: rebase against TOT 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 | « no previous file | public/platform/WebCrypto.h » ('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 {
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 // A map from the operation to the expected parameter type of the algorithm.
95 {blink::WebCryptoAlgorithmIdAesCbc, WrapKey, blink::WebCryptoAlgorithmParams TypeAesCbcParams}, 87 // If an operation is not applicable for the algorithm, set to Undefined.
96 {blink::WebCryptoAlgorithmIdAesCbc, UnwrapKey, blink::WebCryptoAlgorithmPara msTypeAesCbcParams}, 88 const ParamsTypeOrUndefined operationToParamsType[LastAlgorithmOperation + 1 ];
97
98 // HMAC
99 {blink::WebCryptoAlgorithmIdHmac, Sign, blink::WebCryptoAlgorithmParamsTypeN one},
100 {blink::WebCryptoAlgorithmIdHmac, Verify, blink::WebCryptoAlgorithmParamsTyp eNone},
101 {blink::WebCryptoAlgorithmIdHmac, GenerateKey, blink::WebCryptoAlgorithmPara msTypeHmacKeyGenParams},
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 }; 89 };
146 90
91 // A mapping from the algorithm ID to information about the algorithm.
92 const AlgorithmInfo algorithmIdToInfo[] = {
93 { // Index 0
94 "AES-CBC", {
95 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
96 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
97 Undefined, // Sign
98 Undefined, // Verify
99 Undefined, // Digest
100 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
101 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
102 Undefined, // DeriveKey
103 Undefined, // DeriveBits
104 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
105 blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
106 }
107 }, { // Index 1
108 "HMAC", {
109 Undefined, // Encrypt
110 Undefined, // Decrypt
111 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
112 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
113 Undefined, // Digest
114 blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
115 blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
116 Undefined, // DeriveKey
117 Undefined, // DeriveBits
118 Undefined, // WrapKey
119 Undefined // UnwrapKey
120 }
121 }, { // Index 2
122 "RSASSA-PKCS1-v1_5", {
123 Undefined, // Encrypt
124 Undefined, // Decrypt
125 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
126 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
127 Undefined, // Digest
128 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
129 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
130 Undefined, // DeriveKey
131 Undefined, // DeriveBits
132 Undefined, // WrapKey
133 Undefined // UnwrapKey
134 }
135 }, { // Index 3
136 "RSAES-PKCS1-v1_5", {
137 blink::WebCryptoAlgorithmParamsTypeNone, // Encrypt
138 blink::WebCryptoAlgorithmParamsTypeNone, // Decrypt
139 Undefined, // Sign
140 Undefined, // Verify
141 Undefined, // Digest
142 blink::WebCryptoAlgorithmParamsTypeRsaKeyGenParams, // GenerateKey
143 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
144 Undefined, // DeriveKey
145 Undefined, // DeriveBits
146 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
147 blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
148 }
149 }, { // Index 4
150 "SHA-1", {
151 Undefined, // Encrypt
152 Undefined, // Decrypt
153 Undefined, // Sign
154 Undefined, // Verify
155 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
156 Undefined, // GenerateKey
157 Undefined, // ImportKey
158 Undefined, // DeriveKey
159 Undefined, // DeriveBits
160 Undefined, // WrapKey
161 Undefined // UnwrapKey
162 }
163 }, { // Index 5
164 "SHA-256", {
165 Undefined, // Encrypt
166 Undefined, // Decrypt
167 Undefined, // Sign
168 Undefined, // Verify
169 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
170 Undefined, // GenerateKey
171 Undefined, // ImportKey
172 Undefined, // DeriveKey
173 Undefined, // DeriveBits
174 Undefined, // WrapKey
175 Undefined // UnwrapKey
176 }
177 }, { // Index 6
178 "SHA-384", {
179 Undefined, // Encrypt
180 Undefined, // Decrypt
181 Undefined, // Sign
182 Undefined, // Verify
183 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
184 Undefined, // GenerateKey
185 Undefined, // ImportKey
186 Undefined, // DeriveKey
187 Undefined, // DeriveBits
188 Undefined, // WrapKey
189 Undefined // UnwrapKey
190 }
191 }, { // Index 7
192 "SHA-512", {
193 Undefined, // Encrypt
194 Undefined, // Decrypt
195 Undefined, // Sign
196 Undefined, // Verify
197 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
198 Undefined, // GenerateKey
199 Undefined, // ImportKey
200 Undefined, // DeriveKey
201 Undefined, // DeriveBits
202 Undefined, // WrapKey
203 Undefined // UnwrapKey
204 }
205 }, { // Index 8
206 "AES-GCM", {
207 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
208 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
209 Undefined, // Sign
210 Undefined, // Verify
211 Undefined, // Digest
212 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
213 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
214 Undefined, // DeriveKey
215 Undefined, // DeriveBits
216 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
217 blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
218 }
219 }, { // Index 9
220 "RSA-OAEP", {
221 // FIXME:
222 Undefined, // Encrypt
223 Undefined, // Decrypt
224 Undefined, // Sign
225 Undefined, // Verify
226 Undefined, // Digest
227 Undefined, // GenerateKey
228 Undefined, // ImportKey
229 Undefined, // DeriveKey
230 Undefined, // DeriveBits
231 Undefined, // WrapKey
232 Undefined // UnwrapKey
233 }
234 }, { // Index 10
235 "AES-CTR", {
236 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
237 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
238 Undefined, // Sign
239 Undefined, // Verify
240 Undefined, // Digest
241 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
242 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
243 Undefined, // DeriveKey
244 Undefined, // DeriveBits
245 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
246 blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
247 }
248 }, { // Index 11
249 "AES-KW", {
250 Undefined, // Encrypt
251 Undefined, // Decrypt
252 Undefined, // Sign
253 Undefined, // Verify
254 Undefined, // Digest
255 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
256 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
257 Undefined, // DeriveKey
258 Undefined, // DeriveBits
259 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
260 blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
261 }
262 },
263 };
264
265 // Initializing the algorithmIdToInfo table above depends on knowing the enum
266 // values for algorithm IDs. If those ever change, the table will need to be
267 // updated.
268 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch);
269 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch);
270 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_ idDoesntMatch);
271 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 == 3, RsaEsPkcs1v1_5_id DoesntMatch);
272 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha1 == 4, Sha1_idDoesntMatch);
273 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha256 == 5, Sha256_idDoesntMatch);
274 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha384 == 6, Sha384_idDoesntMatch);
275 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha512 == 7, Sha512_idDoesntMatch);
276 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesGcm == 8, AesGcm_idDoesntMatch);
277 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 9, RsaOaep_idDoesntMatch);
278 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 10, AesCtr_idDoesntMatch);
279 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 11, AesKw_idDoesntMatch);
280 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 11, Last_idDoesntMatch);
281 COMPILE_ASSERT(10 == LastAlgorithmOperation, UpdateParamsMapping);
282
147 #if ASSERT_ENABLED 283 #if ASSERT_ENABLED
148 284
149 // Essentially std::is_sorted() (however that function is new to C++11). 285 // Essentially std::is_sorted() (however that function is new to C++11).
150 template <typename Iterator> 286 template <typename Iterator>
151 bool isSorted(Iterator begin, Iterator end) 287 bool isSorted(Iterator begin, Iterator end)
152 { 288 {
153 if (begin == end) 289 if (begin == end)
154 return true; 290 return true;
155 291
156 Iterator prev = begin; 292 Iterator prev = begin;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 if (!str.containsOnlyASCII()) 332 if (!str.containsOnlyASCII())
197 return false; 333 return false;
198 if (str.upper() != str) 334 if (str.upper() != str)
199 return false; 335 return false;
200 } 336 }
201 337
202 return isSorted(begin, end); 338 return isSorted(begin, end);
203 } 339 }
204 #endif 340 #endif
205 341
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> 342 template <typename CharType>
216 bool algorithmNameComparator(const AlgorithmNameMapping& a, StringImpl* b) 343 bool algorithmNameComparator(const AlgorithmNameMapping& a, StringImpl* b)
217 { 344 {
218 if (a.algorithmNameLength < b->length()) 345 if (a.algorithmNameLength < b->length())
219 return true; 346 return true;
220 if (a.algorithmNameLength > b->length()) 347 if (a.algorithmNameLength > b->length())
221 return false; 348 return false;
222 349
223 // Because the algorithm names contain many common prefixes, it is better 350 // Because the algorithm names contain many common prefixes, it is better
224 // to compare starting at the end of the string. 351 // to compare starting at the end of the string.
(...skipping 30 matching lines...) Expand all
255 if (it == end) 382 if (it == end)
256 return false; 383 return false;
257 384
258 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName)) 385 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName))
259 return false; 386 return false;
260 387
261 id = it->algorithmId; 388 id = it->algorithmId;
262 return true; 389 return true;
263 } 390 }
264 391
265 bool lookupAlgorithmParamsType(blink::WebCryptoAlgorithmId id, AlgorithmOperatio n op, blink::WebCryptoAlgorithmParamsType& paramsType) 392 const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id)
266 { 393 {
267 const OperationParamsMapping* begin = operationParamsMappings; 394 if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
268 const OperationParamsMapping* end = operationParamsMappings + WTF_ARRAY_LENG TH(operationParamsMappings); 395 return 0;
269 396 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 } 397 }
281 398
282 // ErrorContext holds a stack of string literals which describe what was 399 // ErrorContext holds a stack of string literals which describe what was
283 // happening at the time the error occurred. This is helpful because 400 // happening at the time the error occurred. This is helpful because
284 // parsing of the algorithm dictionary can be recursive and it is difficult to 401 // parsing of the algorithm dictionary can be recursive and it is difficult to
285 // tell what went wrong from a failure alone. 402 // tell what went wrong from a failure alone.
286 class ErrorContext { 403 class ErrorContext {
287 public: 404 public:
288 void add(const char* message) 405 void add(const char* message)
289 { 406 {
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 errorDetails = context.toString("name", "Missing or not a string"); 853 errorDetails = context.toString("name", "Missing or not a string");
737 return false; 854 return false;
738 } 855 }
739 856
740 blink::WebCryptoAlgorithmId algorithmId; 857 blink::WebCryptoAlgorithmId algorithmId;
741 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { 858 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) {
742 errorDetails = context.toString("Unrecognized algorithm name"); 859 errorDetails = context.toString("Unrecognized algorithm name");
743 return false; 860 return false;
744 } 861 }
745 862
746 context.add(algorithmIdToName(algorithmId)); 863 const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId);
864 context.add(algorithmInfo->name);
747 865
748 blink::WebCryptoAlgorithmParamsType paramsType; 866 if (algorithmInfo->operationToParamsType[op] == Undefined) {
749 if (!lookupAlgorithmParamsType(algorithmId, op, paramsType)) {
750 errorDetails = context.toString("Unsupported operation"); 867 errorDetails = context.toString("Unsupported operation");
751 return false; 868 return false;
752 } 869 }
753 870
871 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]);
872
754 OwnPtr<blink::WebCryptoAlgorithmParams> params; 873 OwnPtr<blink::WebCryptoAlgorithmParams> params;
755 if (!parseAlgorithmParams(raw, paramsType, params, context, errorDetails)) 874 if (!parseAlgorithmParams(raw, paramsType, params, context, errorDetails))
756 return false; 875 return false;
757 876
758 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release()); 877 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release());
759 return true; 878 return true;
760 } 879 }
761 880
762 } // namespace 881 } // namespace
763 882
764 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp toAlgorithm& algorithm, CryptoResult* result) 883 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp toAlgorithm& algorithm, CryptoResult* result)
765 { 884 {
766 String errorDetails; 885 String errorDetails;
767 if (!parseAlgorithm(raw, op, algorithm, ErrorContext(), errorDetails)) { 886 if (!parseAlgorithm(raw, op, algorithm, ErrorContext(), errorDetails)) {
768 result->completeWithError(errorDetails); 887 result->completeWithError(errorDetails);
769 return false; 888 return false;
770 } 889 }
771 return true; 890 return true;
772 } 891 }
773 892
774 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id) 893 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
775 { 894 {
776 switch (id) { 895 return lookupAlgorithmInfo(id)->name;
777 case blink::WebCryptoAlgorithmIdAesCbc:
778 return "AES-CBC";
779 case blink::WebCryptoAlgorithmIdAesCtr:
780 return "AES-CTR";
781 case blink::WebCryptoAlgorithmIdAesGcm:
782 return "AES-GCM";
783 case blink::WebCryptoAlgorithmIdHmac:
784 return "HMAC";
785 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
786 return "RSASSA-PKCS1-v1_5";
787 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
788 return "RSAES-PKCS1-v1_5";
789 case blink::WebCryptoAlgorithmIdSha1:
790 return "SHA-1";
791 case blink::WebCryptoAlgorithmIdSha256:
792 return "SHA-256";
793 case blink::WebCryptoAlgorithmIdSha384:
794 return "SHA-384";
795 case blink::WebCryptoAlgorithmIdSha512:
796 return "SHA-512";
797 case blink::WebCryptoAlgorithmIdAesKw:
798 return "AES-KW";
799 case blink::WebCryptoAlgorithmIdRsaOaep:
800 return "RSA-OAEP";
801 }
802 return 0;
803 } 896 }
804 897
805 } // namespace WebCore 898 } // namespace WebCore
OLDNEW
« no previous file with comments | « no previous file | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698