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

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: Fixed constant definitions. 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 78 typedef char ParamsTypeOrUndefined;
79 const ParamsTypeOrUndefined Undefined = -1; 79 const ParamsTypeOrUndefined Undefined = -1;
80 80
81 struct AlgorithmInfo { 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. 82 // 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. 83 // If an operation is not applicable for the algorithm, set to Undefined.
87 const ParamsTypeOrUndefined operationToParamsType[blink::WebCryptoOperationL ast + 1]; 84 const ParamsTypeOrUndefined operationToParamsType[blink::WebCryptoOperationL ast + 1];
88 }; 85 };
89 86
90 // A mapping from the algorithm ID to information about the algorithm. 87 // A mapping from the algorithm ID to information about the algorithm.
91 const AlgorithmInfo algorithmIdToInfo[] = { 88 const AlgorithmInfo algorithmIdToInfo[] = {
92 { // Index 0 89 { // Index 0
93 "AES-CBC", { 90 {
94 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt 91 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
95 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt 92 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
96 Undefined, // Sign 93 Undefined, // Sign
97 Undefined, // Verify 94 Undefined, // Verify
98 Undefined, // Digest 95 Undefined, // Digest
99 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey 96 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
100 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey 97 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
101 Undefined, // DeriveKey 98 Undefined, // DeriveKey
102 Undefined, // DeriveBits 99 Undefined, // DeriveBits
103 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey 100 blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
104 blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey 101 blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
105 } 102 }
106 }, { // Index 1 103 }, { // Index 1
107 "HMAC", { 104 {
108 Undefined, // Encrypt 105 Undefined, // Encrypt
109 Undefined, // Decrypt 106 Undefined, // Decrypt
110 blink::WebCryptoAlgorithmParamsTypeNone, // Sign 107 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
111 blink::WebCryptoAlgorithmParamsTypeNone, // Verify 108 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
112 Undefined, // Digest 109 Undefined, // Digest
113 blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey 110 blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
114 blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey 111 blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
115 Undefined, // DeriveKey 112 Undefined, // DeriveKey
116 Undefined, // DeriveBits 113 Undefined, // DeriveBits
117 Undefined, // WrapKey 114 Undefined, // WrapKey
118 Undefined // UnwrapKey 115 Undefined // UnwrapKey
119 } 116 }
120 }, { // Index 2 117 }, { // Index 2
121 "RSASSA-PKCS1-v1_5", { 118 {
122 Undefined, // Encrypt 119 Undefined, // Encrypt
123 Undefined, // Decrypt 120 Undefined, // Decrypt
124 blink::WebCryptoAlgorithmParamsTypeNone, // Sign 121 blink::WebCryptoAlgorithmParamsTypeNone, // Sign
125 blink::WebCryptoAlgorithmParamsTypeNone, // Verify 122 blink::WebCryptoAlgorithmParamsTypeNone, // Verify
126 Undefined, // Digest 123 Undefined, // Digest
127 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey 124 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
128 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey 125 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
129 Undefined, // DeriveKey 126 Undefined, // DeriveKey
130 Undefined, // DeriveBits 127 Undefined, // DeriveBits
131 Undefined, // WrapKey 128 Undefined, // WrapKey
132 Undefined // UnwrapKey 129 Undefined // UnwrapKey
133 } 130 }
134 }, { // Index 3 131 }, { // Index 3
135 "SHA-1", { 132 {
136 Undefined, // Encrypt 133 Undefined, // Encrypt
137 Undefined, // Decrypt 134 Undefined, // Decrypt
138 Undefined, // Sign 135 Undefined, // Sign
139 Undefined, // Verify 136 Undefined, // Verify
140 blink::WebCryptoAlgorithmParamsTypeNone, // Digest 137 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
141 Undefined, // GenerateKey 138 Undefined, // GenerateKey
142 Undefined, // ImportKey 139 Undefined, // ImportKey
143 Undefined, // DeriveKey 140 Undefined, // DeriveKey
144 Undefined, // DeriveBits 141 Undefined, // DeriveBits
145 Undefined, // WrapKey 142 Undefined, // WrapKey
146 Undefined // UnwrapKey 143 Undefined // UnwrapKey
147 } 144 }
148 }, { // Index 4 145 }, { // Index 4
149 "SHA-256", { 146 {
150 Undefined, // Encrypt 147 Undefined, // Encrypt
151 Undefined, // Decrypt 148 Undefined, // Decrypt
152 Undefined, // Sign 149 Undefined, // Sign
153 Undefined, // Verify 150 Undefined, // Verify
154 blink::WebCryptoAlgorithmParamsTypeNone, // Digest 151 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
155 Undefined, // GenerateKey 152 Undefined, // GenerateKey
156 Undefined, // ImportKey 153 Undefined, // ImportKey
157 Undefined, // DeriveKey 154 Undefined, // DeriveKey
158 Undefined, // DeriveBits 155 Undefined, // DeriveBits
159 Undefined, // WrapKey 156 Undefined, // WrapKey
160 Undefined // UnwrapKey 157 Undefined // UnwrapKey
161 } 158 }
162 }, { // Index 5 159 }, { // Index 5
163 "SHA-384", { 160 {
164 Undefined, // Encrypt 161 Undefined, // Encrypt
165 Undefined, // Decrypt 162 Undefined, // Decrypt
166 Undefined, // Sign 163 Undefined, // Sign
167 Undefined, // Verify 164 Undefined, // Verify
168 blink::WebCryptoAlgorithmParamsTypeNone, // Digest 165 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
169 Undefined, // GenerateKey 166 Undefined, // GenerateKey
170 Undefined, // ImportKey 167 Undefined, // ImportKey
171 Undefined, // DeriveKey 168 Undefined, // DeriveKey
172 Undefined, // DeriveBits 169 Undefined, // DeriveBits
173 Undefined, // WrapKey 170 Undefined, // WrapKey
174 Undefined // UnwrapKey 171 Undefined // UnwrapKey
175 } 172 }
176 }, { // Index 6 173 }, { // Index 6
177 "SHA-512", { 174 {
178 Undefined, // Encrypt 175 Undefined, // Encrypt
179 Undefined, // Decrypt 176 Undefined, // Decrypt
180 Undefined, // Sign 177 Undefined, // Sign
181 Undefined, // Verify 178 Undefined, // Verify
182 blink::WebCryptoAlgorithmParamsTypeNone, // Digest 179 blink::WebCryptoAlgorithmParamsTypeNone, // Digest
183 Undefined, // GenerateKey 180 Undefined, // GenerateKey
184 Undefined, // ImportKey 181 Undefined, // ImportKey
185 Undefined, // DeriveKey 182 Undefined, // DeriveKey
186 Undefined, // DeriveBits 183 Undefined, // DeriveBits
187 Undefined, // WrapKey 184 Undefined, // WrapKey
188 Undefined // UnwrapKey 185 Undefined // UnwrapKey
189 } 186 }
190 }, { // Index 7 187 }, { // Index 7
191 "AES-GCM", { 188 {
192 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt 189 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
193 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt 190 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
194 Undefined, // Sign 191 Undefined, // Sign
195 Undefined, // Verify 192 Undefined, // Verify
196 Undefined, // Digest 193 Undefined, // Digest
197 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey 194 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
198 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey 195 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
199 Undefined, // DeriveKey 196 Undefined, // DeriveKey
200 Undefined, // DeriveBits 197 Undefined, // DeriveBits
201 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey 198 blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
202 blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey 199 blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
203 } 200 }
204 }, { // Index 8 201 }, { // Index 8
205 "RSA-OAEP", { 202 {
206 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Encrypt 203 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Encrypt
207 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Decrypt 204 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Decrypt
208 Undefined, // Sign 205 Undefined, // Sign
209 Undefined, // Verify 206 Undefined, // Verify
210 Undefined, // Digest 207 Undefined, // Digest
211 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey 208 blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // Generat eKey
212 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey 209 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportK ey
213 Undefined, // DeriveKey 210 Undefined, // DeriveKey
214 Undefined, // DeriveBits 211 Undefined, // DeriveBits
215 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // WrapKey 212 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // WrapKey
216 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams // UnwrapKey 213 blink::WebCryptoAlgorithmParamsTypeRsaOaepParams // UnwrapKey
217 } 214 }
218 }, { // Index 9 215 }, { // Index 9
219 "AES-CTR", { 216 {
220 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt 217 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
221 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt 218 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
222 Undefined, // Sign 219 Undefined, // Sign
223 Undefined, // Verify 220 Undefined, // Verify
224 Undefined, // Digest 221 Undefined, // Digest
225 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey 222 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
226 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey 223 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
227 Undefined, // DeriveKey 224 Undefined, // DeriveKey
228 Undefined, // DeriveBits 225 Undefined, // DeriveBits
229 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey 226 blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
230 blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey 227 blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
231 } 228 }
232 }, { // Index 10 229 }, { // Index 10
233 "AES-KW", { 230 {
234 Undefined, // Encrypt 231 Undefined, // Encrypt
235 Undefined, // Decrypt 232 Undefined, // Decrypt
236 Undefined, // Sign 233 Undefined, // Sign
237 Undefined, // Verify 234 Undefined, // Verify
238 Undefined, // Digest 235 Undefined, // Digest
239 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey 236 blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
240 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey 237 blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
241 Undefined, // DeriveKey 238 Undefined, // DeriveKey
242 Undefined, // DeriveBits 239 Undefined, // DeriveBits
243 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey 240 blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 setNotSupportedError(context.toString("Unrecognized name"), error); 889 setNotSupportedError(context.toString("Unrecognized name"), error);
893 return false; 890 return false;
894 } 891 }
895 892
896 // Remove the "Algorithm:" prefix for all subsequent errors. 893 // Remove the "Algorithm:" prefix for all subsequent errors.
897 context.removeLast(); 894 context.removeLast();
898 895
899 const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId); 896 const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId);
900 897
901 if (algorithmInfo->operationToParamsType[op] == Undefined) { 898 if (algorithmInfo->operationToParamsType[op] == Undefined) {
902 context.add(algorithmIdToName(algorithmId)); 899 context.add(blink::WebCryptoAlgorithm::idToName(algorithmId));
903 setNotSupportedError(context.toString("Unsupported operation", operation ToString(op)), error); 900 setNotSupportedError(context.toString("Unsupported operation", operation ToString(op)), error);
904 return false; 901 return false;
905 } 902 }
906 903
907 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]); 904 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt oAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]);
908 905
909 OwnPtr<blink::WebCryptoAlgorithmParams> params; 906 OwnPtr<blink::WebCryptoAlgorithmParams> params;
910 if (!parseAlgorithmParams(raw, paramsType, params, context, error)) 907 if (!parseAlgorithmParams(raw, paramsType, params, context, error))
911 return false; 908 return false;
912 909
913 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release()); 910 algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release());
914 return true; 911 return true;
915 } 912 }
916 913
917 } // namespace 914 } // namespace
918 915
919 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error) 916 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error)
920 { 917 {
921 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); 918 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error);
922 } 919 }
923 920
924 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
925 {
926 return lookupAlgorithmInfo(id)->name;
927 }
928
929 } // namespace WebCore 921 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698