19 #if defined( USE_ECDH ) || defined( USE_ECDSA )
30 static
int enableSidechannelProtection(
INOUT PKC_INFO *pkcInfo,
33 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
50 ( void ) calculateBignumChecksum( pkcInfo, cryptAlgo );
75 const int curveSizeBits;
76 const BYTE *
p, *a, *b, *gx, *gy, *
n, *h;
79 static const ECC_DOMAIN_PARAMS domainParamTbl[] = {
82 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
83 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
84 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ),
85 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
86 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
87 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC" ),
88 MKDATA(
"\x64\x21\x05\x19\xE5\x9C\x80\xE7" \
89 "\x0F\xA7\xE9\xAB\x72\x24\x30\x49" \
90 "\xFE\xB8\xDE\xEC\xC1\x46\xB9\xB1" ),
91 MKDATA(
"\x18\x8D\xA8\x0E\xB0\x30\x90\xF6" \
92 "\x7C\xBF\x20\xEB\x43\xA1\x88\x00" \
93 "\xF4\xFF\x0A\xFD\x82\xFF\x10\x12" ),
94 MKDATA(
"\x07\x19\x2B\x95\xFF\xC8\xDA\x78" \
95 "\x63\x10\x11\xED\x6B\x24\xCD\xD5" \
96 "\x73\xF9\x77\xA1\x1E\x79\x48\x11" ),
97 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
98 "\xFF\xFF\xFF\xFF\x99\xDE\xF8\x36" \
99 "\x14\x6B\xC9\xB1\xB4\xD2\x28\x31" ),
103 { CRYPT_ECCCURVE_P192v2, 192,
104 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
105 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
106 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ),
107 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
108 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
109 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC" ),
110 MKDATA(
"\xCC\x22\xD6\xDF\xB9\x5C\x6B\x25" \
111 "\xE4\x9C\x0D\x63\x64\xA4\xE5\x98" \
112 "\x0C\x39\x3A\xA2\x16\x68\xD9\x53" ),
113 MKDATA(
"\xEE\xA2\xBA\xE7\xE1\x49\x78\x42" \
114 "\xF2\xDE\x77\x69\xCF\xE9\xC9\x89" \
115 "\xC0\x72\xAD\x69\x6F\x48\x03\x4A" ),
116 MKDATA(
"\x65\x74\xD1\x1D\x69\xB6\xEC\x7A" \
117 "\x67\x2B\xB8\x2A\x08\x3D\xF2\xF2" \
118 "\xB0\x84\x7D\xE9\x70\xB2\xDE\x15" ),
119 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
120 "\xFF\xFF\xFF\xFE\x5F\xB1\xA7\x24" \
121 "\xDC\x80\x41\x86\x48\xD8\xDD\x31" ) },
123 { CRYPT_ECCCURVE_P192v3, 192,
124 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
125 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
126 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ),
127 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
128 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
129 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC" ),
130 MKDATA(
"\x22\x12\x3D\xC2\x39\x5A\x05\xCA" \
131 "\xA7\x42\x3D\xAE\xCC\xC9\x47\x60" \
132 "\xA7\xD4\x62\x25\x6B\xD5\x69\x16" ),
133 MKDATA(
"\x7D\x29\x77\x81\x00\xC6\x5A\x1D" \
134 "\xA1\x78\x37\x16\x58\x8D\xCE\x2B" \
135 "\x8B\x4A\xEE\x8E\x22\x8F\x18\x96" ),
136 MKDATA(
"\x38\xA9\x0F\x22\x63\x73\x37\x33" \
137 "\x4B\x49\xDC\xB6\x6A\x6D\xC8\xF9" \
138 "\x97\x8A\xCA\x76\x48\xA9\x43\xB0" ),
139 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
140 "\xFF\xFF\xFF\xFF\x7A\x62\xD0\x31" \
141 "\xC8\x3F\x42\x94\xF6\x40\xEC\x13" ) },
146 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
147 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
148 "\x00\x00\x00\x00\x00\x00\x00\x00" \
149 "\x00\x00\x00\x01" ),
150 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
151 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
152 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
153 "\xFF\xFF\xFF\xFE" ),
154 MKDATA(
"\xB4\x05\x0A\x85\x0C\x04\xB3\xAB" \
155 "\xF5\x41\x32\x56\x50\x44\xB0\xB7" \
156 "\xD7\xBF\xD8\xBA\x27\x0B\x39\x43" \
157 "\x23\x55\xFF\xB4" ),
158 MKDATA(
"\xB7\x0E\x0C\xBD\x6B\xB4\xBF\x7F" \
159 "\x32\x13\x90\xB9\x4A\x03\xC1\xD3" \
160 "\x56\xC2\x11\x22\x34\x32\x80\xD6" \
161 "\x11\x5C\x1D\x21" ),
162 MKDATA(
"\xBD\x37\x63\x88\xB5\xF7\x23\xFB" \
163 "\x4C\x22\xDF\xE6\xCD\x43\x75\xA0" \
164 "\x5A\x07\x47\x64\x44\xD5\x81\x99" \
165 "\x85\x00\x7E\x34" ),
166 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
167 "\xFF\xFF\xFF\xFF\xFF\xFF\x16\xA2" \
168 "\xE0\xB8\xF0\x3E\x13\xDD\x29\x45" \
169 "\x5C\x5C\x2A\x3D" ),
173 { CRYPT_ECCCURVE_P239, 239,
174 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
175 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
176 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
177 "\x7F\xFF\xFF\xFF\xFF\xFF" ),
178 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
179 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
180 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
181 "\x7F\xFF\xFF\xFF\xFF\xFC" ),
182 MKDATA(
"\x6B\x01\x6C\x3B\xDC\xF1\x89\x41" \
183 "\xD0\xD6\x54\x92\x14\x75\xCA\x71" \
184 "\xA9\xDB\x2F\xB2\x7D\x1D\x37\x79" \
185 "\x61\x85\xC2\x94\x2C\x0A" ),
186 MKDATA(
"\x0F\xFA\x96\x3C\xDC\xA8\x81\x6C" \
187 "\xCC\x33\xB8\x64\x2B\xED\xF9\x05" \
188 "\xC3\xD3\x58\x57\x3D\x3F\x27\xFB" \
189 "\xBD\x3B\x3C\xB9\xAA\xAF" ),
190 MKDATA(
"\x7D\xEB\xE8\xE4\xE9\x0A\x5D\xAE" \
191 "\x6E\x40\x54\xCA\x53\x0B\xA0\x46" \
192 "\x54\xB3\x68\x18\xCE\x22\x6B\x39" \
193 "\xFC\xCB\x7B\x02\xF1\xAE" ),
194 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
195 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\x9E" \
196 "\x5E\x9A\x9F\x5D\x90\x71\xFB\xD1" \
197 "\x52\x26\x88\x90\x9D\x0B" ) },
199 { CRYPT_ECCCURVE_P239v2, 239,
200 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
201 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
202 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
203 "\x7F\xFF\xFF\xFF\xFF\xFF" ),
204 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
205 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
206 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
207 "\x7F\xFF\xFF\xFF\xFF\xFC" ),
208 MKDATA(
"\x61\x7F\xAB\x68\x32\x57\x6C\xBB" \
209 "\xFE\xD5\x0D\x99\xF0\x24\x9C\x3F" \
210 "\xEE\x58\xB9\x4B\xA0\x03\x8C\x7A" \
211 "\xE8\x4C\x8C\x83\x2F\x2C" ),
212 MKDATA(
"\x38\xAF\x09\xD9\x87\x27\x70\x51" \
213 "\x20\xC9\x21\xBB\x5E\x9E\x26\x29" \
214 "\x6A\x3C\xDC\xF2\xF3\x57\x57\xA0" \
215 "\xEA\xFD\x87\xB8\x30\xE7" ),
216 MKDATA(
"\x5B\x01\x25\xE4\xDB\xEA\x0E\xC7" \
217 "\x20\x6D\xA0\xFC\x01\xD9\xB0\x81" \
218 "\x32\x9F\xB5\x55\xDE\x6E\xF4\x60" \
219 "\x23\x7D\xFF\x8B\xE4\xBA" ),
220 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
221 "\xFF\xFF\xFF\xFF\x80\x00\x00\xCF" \
222 "\xA7\xE8\x59\x43\x77\xD4\x14\xC0" \
223 "\x38\x21\xBC\x58\x20\x63" ) },
225 { CRYPT_ECCCURVE_P239v3, 239,
226 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
227 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
228 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
229 "\x7F\xFF\xFF\xFF\xFF\xFF" ),
230 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
231 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF" \
232 "\xFF\xFF\x80\x00\x00\x00\x00\x00" \
233 "\x7F\xFF\xFF\xFF\xFF\xFC" ),
234 MKDATA(
"\x25\x57\x05\xFA\x2A\x30\x66\x54" \
235 "\xB1\xF4\xCB\x03\xD6\xA7\x50\xA3" \
236 "\x0C\x25\x01\x02\xD4\x98\x87\x17" \
237 "\xD9\xBA\x15\xAB\x6D\x3E" ),
238 MKDATA(
"\x67\x68\xAE\x8E\x18\xBB\x92\xCF" \
239 "\xCF\x00\x5C\x94\x9A\xA2\xC6\xD9" \
240 "\x48\x53\xD0\xE6\x60\xBB\xF8\x54" \
241 "\xB1\xC9\x50\x5F\xE9\x5A" ),
242 MKDATA(
"\x16\x07\xE6\x89\x8F\x39\x0C\x06" \
243 "\xBC\x1D\x55\x2B\xAD\x22\x6F\x3B" \
244 "\x6F\xCF\xE4\x8B\x6E\x81\x84\x99" \
245 "\xAF\x18\xE3\xED\x6C\xF3" ),
246 MKDATA(
"\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
247 "\xFF\xFF\xFF\xFF\x7F\xFF\xFF\x97" \
248 "\x5D\xEB\x41\xB3\xA6\x05\x7C\x3C" \
249 "\x43\x21\x46\x52\x65\x51" ) },
254 MKDATA(
"\xFF\xFF\xFF\xFF\x00\x00\x00\x01" \
255 "\x00\x00\x00\x00\x00\x00\x00\x00" \
256 "\x00\x00\x00\x00\xFF\xFF\xFF\xFF" \
257 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ),
258 MKDATA(
"\xFF\xFF\xFF\xFF\x00\x00\x00\x01" \
259 "\x00\x00\x00\x00\x00\x00\x00\x00" \
260 "\x00\x00\x00\x00\xFF\xFF\xFF\xFF" \
261 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC" ),
262 MKDATA(
"\x5A\xC6\x35\xD8\xAA\x3A\x93\xE7" \
263 "\xB3\xEB\xBD\x55\x76\x98\x86\xBC" \
264 "\x65\x1D\x06\xB0\xCC\x53\xB0\xF6" \
265 "\x3B\xCE\x3C\x3E\x27\xD2\x60\x4B" ),
266 MKDATA(
"\x6B\x17\xD1\xF2\xE1\x2C\x42\x47" \
267 "\xF8\xBC\xE6\xE5\x63\xA4\x40\xF2" \
268 "\x77\x03\x7D\x81\x2D\xEB\x33\xA0" \
269 "\xF4\xA1\x39\x45\xD8\x98\xC2\x96" ),
270 MKDATA(
"\x4F\xE3\x42\xE2\xFE\x1A\x7F\x9B" \
271 "\x8E\xE7\xEB\x4A\x7C\x0F\x9E\x16" \
272 "\x2B\xCE\x33\x57\x6B\x31\x5E\xCE" \
273 "\xCB\xB6\x40\x68\x37\xBF\x51\xF5" ),
274 MKDATA(
"\xFF\xFF\xFF\xFF\x00\x00\x00\x00" \
275 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
276 "\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84" \
277 "\xF3\xB9\xCA\xC2\xFC\x63\x25\x51" ),
282 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
283 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
284 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
285 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
286 "\xFF\xFF\xFF\xFF\x00\x00\x00\x00" \
287 "\x00\x00\x00\x00\xFF\xFF\xFF\xFF" ),
288 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
289 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
290 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
291 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" \
292 "\xFF\xFF\xFF\xFF\x00\x00\x00\x00" \
293 "\x00\x00\x00\x00\xFF\xFF\xFF\xFC" ),
294 MKDATA(
"\xB3\x31\x2F\xA7\xE2\x3E\xE7\xE4" \
295 "\x98\x8E\x05\x6B\xE3\xF8\x2D\x19" \
296 "\x18\x1D\x9C\x6E\xFE\x81\x41\x12" \
297 "\x03\x14\x08\x8F\x50\x13\x87\x5A" \
298 "\xC6\x56\x39\x8D\x8A\x2E\xD1\x9D" \
299 "\x2A\x85\xC8\xED\xD3\xEC\x2A\xEF" ),
300 MKDATA(
"\xAA\x87\xCA\x22\xBE\x8B\x05\x37" \
301 "\x8E\xB1\xC7\x1E\xF3\x20\xAD\x74" \
302 "\x6E\x1D\x3B\x62\x8B\xA7\x9B\x98" \
303 "\x59\xF7\x41\xE0\x82\x54\x2A\x38" \
304 "\x55\x02\xF2\x5D\xBF\x55\x29\x6C" \
305 "\x3A\x54\x5E\x38\x72\x76\x0A\xB7" ),
306 MKDATA(
"\x36\x17\xDE\x4A\x96\x26\x2C\x6F" \
307 "\x5D\x9E\x98\xBF\x92\x92\xDC\x29" \
308 "\xF8\xF4\x1D\xBD\x28\x9A\x14\x7C" \
309 "\xE9\xDA\x31\x13\xB5\xF0\xB8\xC0" \
310 "\x0A\x60\xB1\xCE\x1D\x7E\x81\x9D" \
311 "\x7A\x43\x1D\x7C\x90\xEA\x0E\x5F" ),
312 MKDATA(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
313 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
314 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
315 "\xC7\x63\x4D\x81\xF4\x37\x2D\xDF" \
316 "\x58\x1A\x0D\xB2\x48\xB0\xA7\x7A" \
317 "\xEC\xEC\x19\x6A\xCC\xC5\x29\x73" ),
322 MKDATA(
"\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
323 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
324 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
325 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
326 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
327 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
328 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
329 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
331 MKDATA(
"\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
332 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
333 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
334 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
335 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
336 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
337 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
338 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
340 MKDATA(
"\x00\x51\x95\x3E\xB9\x61\x8E\x1C" \
341 "\x9A\x1F\x92\x9A\x21\xA0\xB6\x85" \
342 "\x40\xEE\xA2\xDA\x72\x5B\x99\xB3" \
343 "\x15\xF3\xB8\xB4\x89\x91\x8E\xF1" \
344 "\x09\xE1\x56\x19\x39\x51\xEC\x7E" \
345 "\x93\x7B\x16\x52\xC0\xBD\x3B\xB1" \
346 "\xBF\x07\x35\x73\xDF\x88\x3D\x2C" \
347 "\x34\xF1\xEF\x45\x1F\xD4\x6B\x50" \
349 MKDATA(
"\x00\xC6\x85\x8E\x06\xB7\x04\x04" \
350 "\xE9\xCD\x9E\x3E\xCB\x66\x23\x95" \
351 "\xB4\x42\x9C\x64\x81\x39\x05\x3F" \
352 "\xB5\x21\xF8\x28\xAF\x60\x6B\x4D" \
353 "\x3D\xBA\xA1\x4B\x5E\x77\xEF\xE7" \
354 "\x59\x28\xFE\x1D\xC1\x27\xA2\xFF" \
355 "\xA8\xDE\x33\x48\xB3\xC1\x85\x6A" \
356 "\x42\x9B\xF9\x7E\x7E\x31\xC2\xE5" \
358 MKDATA(
"\x01\x18\x39\x29\x6A\x78\x9A\x3B" \
359 "\xC0\x04\x5C\x8A\x5F\xB4\x2C\x7D" \
360 "\x1B\xD9\x98\xF5\x44\x49\x57\x9B" \
361 "\x44\x68\x17\xAF\xBD\x17\x27\x3E" \
362 "\x66\x2C\x97\xEE\x72\x99\x5E\xF4" \
363 "\x26\x40\xC5\x50\xB9\x01\x3F\xAD" \
364 "\x07\x61\x35\x3C\x70\x86\xA2\x72" \
365 "\xC2\x40\x88\xBE\x94\x76\x9F\xD1" \
367 MKDATA(
"\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
368 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
369 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
370 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
371 "\xFF\xFA\x51\x86\x87\x83\xBF\x2F" \
372 "\x96\x6B\x7F\xCC\x01\x48\xF7\x09" \
373 "\xA5\xD0\x3B\xB5\xC9\xB8\x89\x9C" \
374 "\x47\xAE\xBB\x6F\xB7\x1E\x91\x38" \
391 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
392 const ECC_DOMAIN_PARAMS *eccParams = NULL;
402 if( domainParamTbl[ i ].paramType == pkcInfo->curveType )
404 eccParams = &domainParamTbl[ i ];
415 pkcInfo->keySizeBits = eccParams->curveSizeBits;
416 curveSize =
bitsToBytes( eccParams->curveSizeBits );
419 CKPTR(
BN_bin2bn( eccParams->p, curveSize, &pkcInfo->eccParam_p ) );
420 CKPTR(
BN_bin2bn( eccParams->a, curveSize, &pkcInfo->eccParam_a ) );
421 CKPTR(
BN_bin2bn( eccParams->b, curveSize, &pkcInfo->eccParam_b ) );
422 CKPTR(
BN_bin2bn( eccParams->gx, curveSize, &pkcInfo->eccParam_gx ) );
423 CKPTR(
BN_bin2bn( eccParams->gy, curveSize, &pkcInfo->eccParam_gy ) );
424 CKPTR(
BN_bin2bn( eccParams->n, curveSize, &pkcInfo->eccParam_n ) );
425 CKPTR(
BN_bin2bn( eccParams->h, curveSize, &pkcInfo->eccParam_h ) );
439 int getECCFieldSize(
IN_ENUM( CRYPT_ECCCURVE ) \
445 assert(
isWritePtr( fieldSize,
sizeof(
int ) ) );
459 if( domainParamTbl[ i ].paramType == fieldID )
461 *fieldSize =
bitsToBytes( domainParamTbl[ i ].curveSizeBits );
493 if(
bitsToBytes( domainParamTbl[ i ].curveSizeBits ) >= fieldSize )
495 *fieldID = domainParamTbl[ i ].paramType;
523 static
int generateECCPrivateValue(
INOUT PKC_INFO *pkcInfo,
527 BIGNUM *
p = &pkcInfo->eccParam_p, *d = &pkcInfo->eccParam_d;
530 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
538 status = generateBignum( d, keyBits, 0xC0, 0 );
546 CK(
BN_mod( d, d, p, pkcInfo->bnCTX ) );
553 status = generateBignum( d, keyBits - 1, 0xC0, 0 );
561 static
int generateECCPublicValue(
INOUT PKC_INFO *pkcInfo )
563 BIGNUM *d = &pkcInfo->eccParam_d;
564 BIGNUM *qx = &pkcInfo->eccParam_qx, *qy = &pkcInfo->eccParam_qy;
569 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
609 static
BOOLEAN checkComponentLength(
const BIGNUM *component,
624 if(
BN_cmp( component, p ) >= 0 )
635 INOUT PKC_INFO *pkcInfo )
637 BIGNUM *p = &pkcInfo->eccParam_p;
638 BIGNUM *tmp1 = &pkcInfo->tmp1, *tmp2 = &pkcInfo->tmp2;
639 BN_CTX *ctx = pkcInfo->bnCTX;
647 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
656 if(
BN_cmp( tmp1, tmp2 ) != 0 )
669 static
int checkECCDomainParameters(
INOUT PKC_INFO *pkcInfo,
670 const BOOLEAN isFullyInitialised )
673 BIGNUM *p = &pkcInfo->eccParam_p;
674 BIGNUM *a = &pkcInfo->eccParam_a, *b = &pkcInfo->eccParam_b;
675 BIGNUM *
n = &pkcInfo->eccParam_n, *h = &pkcInfo->eccParam_h;
676 BIGNUM *gx = &pkcInfo->eccParam_gx, *gy = &pkcInfo->eccParam_gy;
677 BIGNUM *tmp1 = &pkcInfo->tmp1, *tmp2 = &pkcInfo->tmp2;
678 BIGNUM *tmp3_h = &pkcInfo->tmp3;
680 const BOOLEAN isStandardCurve = \
684 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
696 if( !checkComponentLength( a, p,
TRUE ) || \
697 !checkComponentLength( b, p,
TRUE ) )
699 if( !checkComponentLength( gx, p,
TRUE ) || \
700 !checkComponentLength( gy, p,
TRUE ) )
703 if( length < MIN_PKCSIZE_ECC || length > CRYPT_MAX_PKCSIZE_ECC )
708 if( length < 1 || length > CRYPT_MAX_PKCSIZE_ECC )
725 if( isStandardCurve )
729 if( !primeSieve( &pkcInfo->eccParam_p ) )
736 CK(
BN_sqr( tmp1, a, pkcInfo->bnCTX ) );
737 CK(
BN_mul( tmp1, tmp1, a, pkcInfo->bnCTX ) );
739 CK(
BN_sqr( tmp2, b, pkcInfo->bnCTX ) );
742 CK(
BN_mod( tmp1, tmp1, p, pkcInfo->bnCTX ) );
749 if( !isPointOnCurve( gy, gx, a, b, pkcInfo ) )
763 if( !primeSieve( &pkcInfo->eccParam_n ) )
772 if( isFullyInitialised )
842 CK(
BN_div( tmp3_h, NULL, tmp1, n, pkcInfo->bnCTX ) );
847 if( isStandardCurve )
896 CK(
BN_mod( tmp2, p, n, pkcInfo->bnCTX ) );
899 for( i = 0; i < 100; i++ )
901 CK(
BN_mod_mul( tmp1, tmp1, tmp2, n, pkcInfo->bnCTX ) );
916 CK(
BN_mul( tmp1, n, h, pkcInfo->bnCTX ) );
919 if(
BN_cmp( tmp1, p ) == 0 )
926 static
int checkECCPublicKey(
INOUT PKC_INFO *pkcInfo )
928 BIGNUM *p = &pkcInfo->eccParam_p, *n = &pkcInfo->eccParam_n;
929 BIGNUM *qx = &pkcInfo->eccParam_qx, *qy = &pkcInfo->eccParam_qy;
934 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
939 if( !checkComponentLength( qx, p,
FALSE ) || \
940 !checkComponentLength( qy, p,
FALSE ) )
956 if( !isPointOnCurve( qy, qx, &pkcInfo->eccParam_a, &pkcInfo->eccParam_b,
973 static
int checkECCPrivateKey(
INOUT PKC_INFO *pkcInfo )
975 BIGNUM *p = &pkcInfo->eccParam_p, *d = &pkcInfo->eccParam_d;
976 BIGNUM *tmp1 = &pkcInfo->tmp1, *tmp2 = &pkcInfo->tmp2;
981 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
986 if( !checkComponentLength( d, p,
FALSE ) )
995 if(
BN_cmp( tmp1, &pkcInfo->eccParam_qx ) != 0 || \
996 BN_cmp( tmp2, &pkcInfo->eccParam_qy ) != 0 )
1015 static
int initECCVariables(
INOUT PKC_INFO *pkcInfo )
1018 EC_POINT *ecPoint = pkcInfo->ecPoint;
1021 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
1024 &pkcInfo->eccParam_a, &pkcInfo->eccParam_b,
1029 &pkcInfo->eccParam_gx,
1030 &pkcInfo->eccParam_gy,
1035 &pkcInfo->eccParam_h ) );
1049 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
1056 keyBits <=
bytesToBits( CRYPT_MAX_PKCSIZE_ECC ) );
1066 status = getECCFieldID(
bitsToBytes( keyBits ), &fieldID );
1070 status = loadECCparams( contextInfoPtr );
1073 keySizeBits = pkcInfo->keySizeBits;
1076 status = initECCVariables( pkcInfo );
1081 status = generateECCPrivateValue( pkcInfo, keySizeBits );
1086 status = generateECCPublicValue( pkcInfo );
1091 status = checkECCDomainParameters( pkcInfo,
TRUE );
1093 status = checkECCPublicKey( pkcInfo );
1095 status = checkECCPrivateKey( pkcInfo );
1102 return( enableSidechannelProtection( pkcInfo,
1103 contextInfoPtr->capabilityInfo->cryptAlgo ) );
1114 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
1116 BIGNUM *p = &pkcInfo->eccParam_p;
1117 const BOOLEAN isPrivateKey = \
1130 status = loadECCparams( contextInfoPtr );
1148 if( isPrivateKey &&
BN_is_zero( &pkcInfo->eccParam_d ) )
1153 status = checkECCDomainParameters( pkcInfo,
FALSE );
1158 status = initECCVariables( pkcInfo );
1167 CK(
EC_POINT_mul( ecCTX, pkcInfo->tmpPoint, &pkcInfo->eccParam_n,
1168 NULL, NULL, pkcInfo->bnCTX ) );
1178 if( isECDH &&
BN_is_zero( &pkcInfo->eccParam_d ) )
1180 status = generateECCPrivateValue( pkcInfo, pkcInfo->keySizeBits );
1205 if(
BN_is_zero( &pkcInfo->eccParam_qx ) || generatedD )
1207 status = generateECCPublicValue( pkcInfo );
1213 status = checkECCPublicKey( pkcInfo );
1218 if( isPrivateKey || generatedD )
1220 status = checkECCPrivateKey( pkcInfo );
1229 if( pkcInfo->keySizeBits <= 0 )
1235 return( enableSidechannelProtection( pkcInfo,
1236 contextInfoPtr->capabilityInfo->cryptAlgo ) );