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

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

Issue 312393004: Expose WebCrypto's lookupAlgorithmInfo. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@share_normalize
Patch Set: Moving lookupAlgorithmInfo as well. Created 6 years, 6 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
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 {"SHA-512", 7, blink::WebCryptoAlgorithmIdSha512}, 68 {"SHA-512", 7, blink::WebCryptoAlgorithmIdSha512},
69 {"SHA-384", 7, blink::WebCryptoAlgorithmIdSha384}, 69 {"SHA-384", 7, blink::WebCryptoAlgorithmIdSha384},
70 {"SHA-256", 7, blink::WebCryptoAlgorithmIdSha256}, 70 {"SHA-256", 7, blink::WebCryptoAlgorithmIdSha256},
71 {"AES-CBC", 7, blink::WebCryptoAlgorithmIdAesCbc}, 71 {"AES-CBC", 7, blink::WebCryptoAlgorithmIdAesCbc},
72 {"AES-GCM", 7, blink::WebCryptoAlgorithmIdAesGcm}, 72 {"AES-GCM", 7, blink::WebCryptoAlgorithmIdAesGcm},
73 {"AES-CTR", 7, blink::WebCryptoAlgorithmIdAesCtr}, 73 {"AES-CTR", 7, blink::WebCryptoAlgorithmIdAesCtr},
74 {"RSA-OAEP", 8, blink::WebCryptoAlgorithmIdRsaOaep}, 74 {"RSA-OAEP", 8, blink::WebCryptoAlgorithmIdRsaOaep},
75 {"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, 75 {"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5},
76 }; 76 };
77 77
78 typedef char ParamsTypeOrUndefined;
79 const ParamsTypeOrUndefined Undefined = -1;
80
81 struct AlgorithmInfo {
82 // The canonical (case-sensitive) name for the algorithm.
83 const char* name;
84
85 // A map from the operation to the expected parameter type of the algorithm.
86 // If an operation is not applicable for the algorithm, set to Undefined.
87 const ParamsTypeOrUndefined operationToParamsType[blink::WebCryptoOperationL ast + 1];
88 };
89
90 // A mapping from the algorithm ID to information about the algorithm.
91 const AlgorithmInfo algorithmIdToInfo[] = {
92 { // Index 0
93 "AES-CBC", {
94 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
95 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
96 Undefined, // Sign
97 Undefined, // Verify
98 Undefined, // Digest
99 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
100 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
101 Undefined, // DeriveKey
102 Undefined, // DeriveBits
103 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
104 blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
105 }
106 }, { // Index 1
107 "HMAC", {
108 Undefined, // Encrypt
109 Undefined, // Decrypt
110 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
111 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
112 Undefined, // Digest
113 blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
114 blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
115 Undefined, // DeriveKey
116 Undefined, // DeriveBits
117 Undefined, // WrapKey
118 Undefined // UnwrapKey
119 }
120 }, { // Index 2
121 "RSASSA-PKCS1-v1_5", {
122 Undefined, // Encrypt
123 Undefined, // Decrypt
124 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
125 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
126 Undefined, // Digest
127 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
128 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
129 Undefined, // DeriveKey
130 Undefined, // DeriveBits
131 Undefined, // WrapKey
132 Undefined // UnwrapKey
133 }
134 }, { // Index 3
135 "SHA-1", {
136 Undefined, // Encrypt
137 Undefined, // Decrypt
138 Undefined, // Sign
139 Undefined, // Verify
140 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
141 Undefined, // GenerateKey
142 Undefined, // ImportKey
143 Undefined, // DeriveKey
144 Undefined, // DeriveBits
145 Undefined, // WrapKey
146 Undefined // UnwrapKey
147 }
148 }, { // Index 4
149 "SHA-256", {
150 Undefined, // Encrypt
151 Undefined, // Decrypt
152 Undefined, // Sign
153 Undefined, // Verify
154 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
155 Undefined, // GenerateKey
156 Undefined, // ImportKey
157 Undefined, // DeriveKey
158 Undefined, // DeriveBits
159 Undefined, // WrapKey
160 Undefined // UnwrapKey
161 }
162 }, { // Index 5
163 "SHA-384", {
164 Undefined, // Encrypt
165 Undefined, // Decrypt
166 Undefined, // Sign
167 Undefined, // Verify
168 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
169 Undefined, // GenerateKey
170 Undefined, // ImportKey
171 Undefined, // DeriveKey
172 Undefined, // DeriveBits
173 Undefined, // WrapKey
174 Undefined // UnwrapKey
175 }
176 }, { // Index 6
177 "SHA-512", {
178 Undefined, // Encrypt
179 Undefined, // Decrypt
180 Undefined, // Sign
181 Undefined, // Verify
182 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
183 Undefined, // GenerateKey
184 Undefined, // ImportKey
185 Undefined, // DeriveKey
186 Undefined, // DeriveBits
187 Undefined, // WrapKey
188 Undefined // UnwrapKey
189 }
190 }, { // Index 7
191 "AES-GCM", {
192 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
193 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
194 Undefined, // Sign
195 Undefined, // Verify
196 Undefined, // Digest
197 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
198 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
199 Undefined, // DeriveKey
200 Undefined, // DeriveBits
201 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
202 blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
203 }
204 }, { // Index 8
205 "RSA-OAEP", {
206 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Encrypt
207 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Decrypt
208 Undefined, // Sign
209 Undefined, // Verify
210 Undefined, // Digest
211 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
212 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
213 Undefined, // DeriveKey
214 Undefined, // DeriveBits
215 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // WrapKey
216 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams // UnwrapKey
217 }
218 }, { // Index 9
219 "AES-CTR", {
220 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
221 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
222 Undefined, // Sign
223 Undefined, // Verify
224 Undefined, // Digest
225 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
226 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
227 Undefined, // DeriveKey
228 Undefined, // DeriveBits
229 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
230 blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
231 }
232 }, { // Index 10
233 "AES-KW", {
234 Undefined, // Encrypt
235 Undefined, // Decrypt
236 Undefined, // Sign
237 Undefined, // Verify
238 Undefined, // Digest
239 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
240 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
241 Undefined, // DeriveKey
242 Undefined, // DeriveBits
243 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
244 blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
245 }
246 },
247 };
248
249 // Initializing the algorithmIdToInfo table above depends on knowing the enum
250 // values for algorithm IDs. If those ever change, the table will need to be
251 // updated.
252 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch);
253 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch);
254 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_ idDoesntMatch);
255 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha1 == 3, Sha1_idDoesntMatch);
256 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha256 == 4, Sha256_idDoesntMatch);
257 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha384 == 5, Sha384_idDoesntMatch);
258 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha512 == 6, Sha512_idDoesntMatch);
259 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesGcm == 7, AesGcm_idDoesntMatch);
260 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch);
261 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch);
262 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch);
263 COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 10, Last_idDoesntMatch);
264 COMPILE_ASSERT(10 == blink::WebCryptoOperationLast, UpdateParamsMapping);
265
266 #if ASSERT_ENABLED 78 #if ASSERT_ENABLED
267 79
268 // Essentially std::is_sorted() (however that function is new to C++11). 80 // Essentially std::is_sorted() (however that function is new to C++11).
269 template <typename Iterator> 81 template <typename Iterator>
270 bool isSorted(Iterator begin, Iterator end) 82 bool isSorted(Iterator begin, Iterator end)
271 { 83 {
272 if (begin == end) 84 if (begin == end)
273 return true; 85 return true;
274 86
275 Iterator prev = begin; 87 Iterator prev = begin;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 if (it == end) 177 if (it == end)
366 return false; 178 return false;
367 179
368 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName)) 180 if (it->algorithmNameLength != algorithmName.length() || !equalIgnoringCase( algorithmName, it->algorithmName))
369 return false; 181 return false;
370 182
371 id = it->algorithmId; 183 id = it->algorithmId;
372 return true; 184 return true;
373 } 185 }
374 186
375 const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id)
376 {
377 if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
378 return 0;
379 return &algorithmIdToInfo[id];
380 }
381
382 void setSyntaxError(const String& message, AlgorithmError* error) 187 void setSyntaxError(const String& message, AlgorithmError* error)
383 { 188 {
384 error->errorType = blink::WebCryptoErrorTypeSyntax; 189 error->errorType = blink::WebCryptoErrorTypeSyntax;
385 error->errorDetails = message; 190 error->errorDetails = message;
386 } 191 }
387 192
388 void setNotSupportedError(const String& message, AlgorithmError* error) 193 void setNotSupportedError(const String& message, AlgorithmError* error)
389 { 194 {
390 error->errorType = blink::WebCryptoErrorTypeNotSupported; 195 error->errorType = blink::WebCryptoErrorTypeNotSupported;
391 error->errorDetails = message; 196 error->errorDetails = message;
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { 694 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) {
890 // FIXME: The spec says to return a SyntaxError if the input contains 695 // FIXME: The spec says to return a SyntaxError if the input contains
891 // any non-ASCII characters. 696 // any non-ASCII characters.
892 setNotSupportedError(context.toString("Unrecognized name"), error); 697 setNotSupportedError(context.toString("Unrecognized name"), error);
893 return false; 698 return false;
894 } 699 }
895 700
896 // Remove the "Algorithm:" prefix for all subsequent errors. 701 // Remove the "Algorithm:" prefix for all subsequent errors.
897 context.removeLast(); 702 context.removeLast();
898 703
899 const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId); 704 const blink::WebCryptoAlgorithmInfo* algorithmInfo = blink::WebCryptoAlgorit hm::lookupAlgorithmInfo(algorithmId);
900 705
901 if (algorithmInfo->operationToParamsType[op] == Undefined) { 706 if (algorithmInfo->operationToParamsType[op] == blink::WebCryptoAlgorithmInf o::Undefined) {
902 context.add(algorithmIdToName(algorithmId)); 707 context.add(blink::WebCryptoAlgorithm::idToName(algorithmId));
eroman 2014/06/13 22:57:55 This could be algorithmInfo->name instead
pneubeck (no reviews) 2014/06/16 07:42:12 Done.
903 setNotSupportedError(context.toString("Unsupported operation", operation ToString(op)), error); 708 setNotSupportedError(context.toString("Unsupported operation", operation ToString(op)), error);
904 return false; 709 return false;
905 } 710 }
906 711
907 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]); 712 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]);
908 713
909 OwnPtr<blink::WebCryptoAlgorithmParams> params; 714 OwnPtr<blink::WebCryptoAlgorithmParams> params;
910 if (!parseAlgorithmParams(raw, paramsType, params, context, error)) 715 if (!parseAlgorithmParams(raw, paramsType, params, context, error))
911 return false; 716 return false;
912 717
913 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release()); 718 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release());
914 return true; 719 return true;
915 } 720 }
916 721
917 } // namespace 722 } // namespace
918 723
919 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error) 724 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error)
920 { 725 {
921 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); 726 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error);
922 } 727 }
923 728
924 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
925 {
926 return lookupAlgorithmInfo(id)->name;
927 }
928
929 } // namespace WebCore 729 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/NormalizeAlgorithm.h ('k') | Source/platform/exported/WebCryptoAlgorithm.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698