432c4e18 |
1 | /* -*-c-*- |
2 | * |
a69a3efd |
3 | * $Id$ |
432c4e18 |
4 | * |
5 | * Elliptic curve information management |
6 | * |
7 | * (c) 2004 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
432c4e18 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "ec.h" |
33 | #include "ectab.h" |
34 | #include "gf.h" |
35 | #include "pgen.h" |
36 | #include "mprand.h" |
389d8222 |
37 | #include "mpint.h" |
432c4e18 |
38 | #include "rabin.h" |
39 | |
40 | /*----- Main code ---------------------------------------------------------*/ |
41 | |
42 | /* --- @ec_curveparse@ --- * |
43 | * |
44 | * Arguments: @qd_parse *qd@ = parser context |
45 | * |
46 | * Returns: Elliptic curve pointer if OK, or null. |
47 | * |
48 | * Use: Parses an elliptic curve description, which has the form |
49 | * |
50 | * * a field description |
51 | * * an optional `/' |
52 | * * `prime', `primeproj', `bin', or `binproj' |
53 | * * an optional `:' |
54 | * * the %$a$% parameter |
55 | * * an optional `,' |
56 | * * the %$b$% parameter |
57 | */ |
58 | |
59 | ec_curve *ec_curveparse(qd_parse *qd) |
60 | { |
61 | mp *a = MP_NEW, *b = MP_NEW; |
62 | ec_curve *c; |
63 | field *f; |
64 | |
65 | if ((f = field_parse(qd)) == 0) goto fail; |
66 | qd_delim(qd, '/'); |
67 | switch (qd_enum(qd, "prime,primeproj,bin,binproj")) { |
68 | case 0: |
69 | if (F_TYPE(f) != FTY_PRIME) { |
70 | qd->e = "field not prime"; |
71 | goto fail; |
72 | } |
73 | qd_delim(qd, ':'); |
74 | if ((a = qd_getmp(qd)) == 0) goto fail; |
75 | qd_delim(qd, ','); |
76 | if ((b = qd_getmp(qd)) == 0) goto fail; |
77 | c = ec_prime(f, a, b); |
78 | break; |
79 | case 1: |
80 | if (F_TYPE(f) != FTY_PRIME) { |
81 | qd->e = "field not prime"; |
82 | goto fail; |
83 | } |
84 | qd_delim(qd, ':'); |
85 | if ((a = qd_getmp(qd)) == 0) goto fail; |
86 | qd_delim(qd, ','); |
87 | if ((b = qd_getmp(qd)) == 0) goto fail; |
88 | c = ec_primeproj(f, a, b); |
89 | break; |
90 | case 2: |
91 | if (F_TYPE(f) != FTY_BINARY) { |
92 | qd->e = "field not binary"; |
93 | goto fail; |
94 | } |
95 | qd_delim(qd, ':'); |
96 | if ((a = qd_getmp(qd)) == 0) goto fail; |
97 | qd_delim(qd, ','); |
98 | if ((b = qd_getmp(qd)) == 0) goto fail; |
99 | c = ec_bin(f, a, b); |
100 | break; |
101 | case 3: |
102 | if (F_TYPE(f) != FTY_BINARY) { |
103 | qd->e = "field not binary"; |
104 | goto fail; |
105 | } |
106 | qd_delim(qd, ':'); |
107 | if ((a = qd_getmp(qd)) == 0) goto fail; |
108 | qd_delim(qd, ','); |
109 | if ((b = qd_getmp(qd)) == 0) goto fail; |
110 | c = ec_binproj(f, a, b); |
111 | break; |
112 | default: |
113 | goto fail; |
114 | } |
02d7884d |
115 | if (!c) { |
116 | qd->e = "bad curve parameters"; |
117 | goto fail; |
118 | } |
432c4e18 |
119 | if (a) MP_DROP(a); |
120 | if (b) MP_DROP(b); |
121 | return (c); |
122 | |
123 | fail: |
124 | if (f) F_DESTROY(f); |
125 | if (a) MP_DROP(a); |
126 | if (b) MP_DROP(b); |
127 | return (0); |
128 | } |
129 | |
130 | /* --- @ec_ptparse@ --- * |
131 | * |
132 | * Arguments: @qd_parse *qd@ = parser context |
133 | * @ec *p@ = where to put the point |
134 | * |
135 | * Returns: The point address, or null. |
136 | * |
137 | * Use: Parses an elliptic curve point. This has the form |
138 | * |
139 | * * %$x$%-coordinate |
140 | * * optional `,' |
141 | * * %$y$%-coordinate |
142 | */ |
143 | |
144 | ec *ec_ptparse(qd_parse *qd, ec *p) |
145 | { |
146 | mp *x = MP_NEW, *y = MP_NEW; |
147 | |
148 | if (qd_enum(qd, "inf") >= 0) { |
149 | EC_SETINF(p); |
150 | return (p); |
151 | } |
152 | if ((x = qd_getmp(qd)) == 0) goto fail; |
153 | qd_delim(qd, ','); |
154 | if ((y = qd_getmp(qd)) == 0) goto fail; |
155 | EC_DESTROY(p); |
156 | p->x = x; |
157 | p->y = y; |
158 | p->z = 0; |
159 | return (p); |
160 | |
161 | fail: |
162 | if (x) MP_DROP(x); |
163 | if (y) MP_DROP(y); |
164 | return (0); |
165 | } |
166 | |
20ca05f0 |
167 | /* --- @ec_infofromdata@ --- * |
34e4f738 |
168 | * |
169 | * Arguments: @ec_info *ei@ = where to write the information |
170 | * @ecdata *ed@ = raw data |
171 | * |
172 | * Returns: --- |
173 | * |
174 | * Use: Loads elliptic curve information about one of the standard |
175 | * curves. |
176 | */ |
177 | |
20ca05f0 |
178 | void ec_infofromdata(ec_info *ei, ecdata *ed) |
34e4f738 |
179 | { |
180 | field *f; |
181 | |
182 | switch (ed->ftag) { |
183 | case FTAG_PRIME: |
184 | f = field_prime(&ed->p); |
185 | ei->c = ec_primeproj(f, &ed->a, &ed->b); |
186 | break; |
187 | case FTAG_NICEPRIME: |
188 | f = field_niceprime(&ed->p); |
189 | ei->c = ec_primeproj(f, &ed->a, &ed->b); |
190 | break; |
191 | case FTAG_BINPOLY: |
192 | f = field_binpoly(&ed->p); |
193 | ei->c = ec_binproj(f, &ed->a, &ed->b); |
194 | break; |
4edc47b8 |
195 | case FTAG_BINNORM: |
196 | f = field_binnorm(&ed->p, &ed->beta); |
197 | ei->c = ec_binproj(f, &ed->a, &ed->b); |
198 | break; |
34e4f738 |
199 | default: |
200 | abort(); |
201 | } |
202 | |
02d7884d |
203 | assert(f); assert(ei->c); |
34e4f738 |
204 | EC_CREATE(&ei->g); ei->g.x = &ed->gx; ei->g.y = &ed->gy; ei->g.z = 0; |
205 | ei->r = &ed->r; ei->h = &ed->h; |
206 | } |
207 | |
432c4e18 |
208 | /* --- @ec_infoparse@ --- * |
209 | * |
210 | * Arguments: @qd_parse *qd@ = parser context |
211 | * @ec_info *ei@ = curve information block, currently |
212 | * uninitialized |
213 | * |
214 | * Returns: Zero on success, nonzero on failure. |
215 | * |
216 | * Use: Parses an elliptic curve information string, and stores the |
34e4f738 |
217 | * information in @ei@. This is either the name of a standard |
218 | * curve, or it has the form |
432c4e18 |
219 | * |
220 | * * elliptic curve description |
221 | * * optional `/' |
222 | * * common point |
223 | * * optional `:' |
224 | * * group order |
225 | * * optional `*' |
226 | * * cofactor |
227 | */ |
228 | |
229 | int ec_infoparse(qd_parse *qd, ec_info *ei) |
230 | { |
231 | ec_curve *c = 0; |
232 | field *f; |
233 | ec g = EC_INIT; |
34e4f738 |
234 | const ecentry *ee; |
432c4e18 |
235 | mp *r = MP_NEW, *h = MP_NEW; |
236 | |
20ca05f0 |
237 | for (ee = ectab; ee->name; ee++) { |
238 | if (qd_enum(qd, ee->name) >= 0) { |
239 | ec_infofromdata(ei, ee->data); |
240 | goto found; |
241 | } |
242 | } |
02d7884d |
243 | |
432c4e18 |
244 | if ((c = ec_curveparse(qd)) == 0) goto fail; |
245 | qd_delim(qd, '/'); if (!ec_ptparse(qd, &g)) goto fail; |
246 | qd_delim(qd, ':'); if ((r = qd_getmp(qd)) == 0) goto fail; |
247 | qd_delim(qd, '*'); if ((h = qd_getmp(qd)) == 0) goto fail; |
432c4e18 |
248 | ei->c = c; ei->g = g; ei->r = r; ei->h = h; |
34e4f738 |
249 | |
250 | found: |
432c4e18 |
251 | return (0); |
252 | |
253 | fail: |
254 | EC_DESTROY(&g); |
255 | if (r) MP_DROP(r); |
256 | if (h) MP_DROP(h); |
257 | if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); } |
258 | return (-1); |
259 | } |
260 | |
432c4e18 |
261 | /* --- @ec_getinfo@ --- * |
262 | * |
263 | * Arguments: @ec_info *ei@ = where to write the information |
264 | * @const char *p@ = string describing a curve |
265 | * |
266 | * Returns: Null on success, or a pointer to an error message. |
267 | * |
268 | * Use: Parses out information about a curve. The string is either a |
269 | * standard curve name, or a curve info string. |
270 | */ |
271 | |
272 | const char *ec_getinfo(ec_info *ei, const char *p) |
273 | { |
274 | qd_parse qd; |
432c4e18 |
275 | |
276 | qd.p = p; |
277 | qd.e = 0; |
432c4e18 |
278 | if (ec_infoparse(&qd, ei)) |
279 | return (qd.e); |
432c4e18 |
280 | if (!qd_eofp(&qd)) { |
281 | ec_freeinfo(ei); |
282 | return ("junk found at end of string"); |
283 | } |
284 | return (0); |
285 | } |
286 | |
34e4f738 |
287 | /* --- @ec_sameinfop@ --- * |
288 | * |
289 | * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets |
290 | * |
291 | * Returns: Nonzero if the curves are identical (not just isomorphic). |
292 | * |
293 | * Use: Checks for sameness of curve parameters. |
294 | */ |
295 | |
296 | int ec_sameinfop(ec_info *ei, ec_info *ej) |
297 | { |
298 | return (ec_samep(ei->c, ej->c) && |
299 | MP_EQ(ei->r, ej->r) && MP_EQ(ei->h, ej->h) && |
300 | EC_EQ(&ei->g, &ej->g)); |
301 | } |
302 | |
432c4e18 |
303 | /* --- @ec_freeinfo@ --- * |
304 | * |
305 | * Arguments: @ec_info *ei@ = elliptic curve information block to free |
306 | * |
307 | * Returns: --- |
308 | * |
309 | * Use: Frees the information block. |
310 | */ |
311 | |
312 | void ec_freeinfo(ec_info *ei) |
313 | { |
314 | field *f; |
315 | |
316 | EC_DESTROY(&ei->g); |
317 | MP_DROP(ei->r); |
318 | MP_DROP(ei->h); |
319 | f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f); |
320 | } |
321 | |
322 | /* --- @ec_checkinfo@ --- * |
323 | * |
324 | * Arguments: @const ec_info *ei@ = elliptic curve information block |
325 | * |
326 | * Returns: Null if OK, or pointer to error message. |
327 | * |
328 | * Use: Checks an elliptic curve according to the rules in SEC1. |
329 | */ |
330 | |
432c4e18 |
331 | static int primeeltp(mp *x, field *f) |
332 | { |
a69a3efd |
333 | return (!MP_NEGP(x) && MP_CMP(x, <, f->m)); |
432c4e18 |
334 | } |
335 | |
336 | static const char *primecheck(const ec_info *ei, grand *gr) |
337 | { |
338 | ec_curve *c = ei->c; |
339 | field *f = c->f; |
340 | int i; |
341 | mp *x, *y; |
342 | ec p; |
343 | int rc; |
344 | |
345 | /* --- Check %$p$% is an odd prime --- */ |
346 | |
34e4f738 |
347 | if (!pgen_primep(f->m, gr)) return ("p not prime"); |
432c4e18 |
348 | |
349 | /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */ |
350 | |
351 | if (!primeeltp(c->a, f)) return ("a out of range"); |
352 | if (!primeeltp(c->b, f)) return ("b out of range"); |
353 | if (!primeeltp(ei->g.x, f)) return ("G_x out of range"); |
354 | if (!primeeltp(ei->g.x, f)) return ("G_y out of range"); |
355 | |
356 | /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */ |
357 | |
358 | x = F_SQR(f, MP_NEW, c->a); |
359 | x = F_MUL(f, x, x, c->a); |
360 | x = F_QDL(f, x, x); |
361 | y = F_SQR(f, MP_NEW, c->b); |
362 | y = F_TPL(f, y, y); |
363 | y = F_TPL(f, y, y); |
364 | y = F_TPL(f, y, y); |
365 | x = F_ADD(f, x, x, y); |
366 | rc = F_ZEROP(f, x); |
367 | MP_DROP(x); |
368 | MP_DROP(y); |
369 | if (rc) return ("not an elliptic curve"); |
370 | |
371 | /* --- Check %$G \in E$% --- */ |
372 | |
373 | if (EC_ATINF(&ei->g)) return ("generator at infinity"); |
374 | if (ec_check(c, &ei->g)) return ("generator not on curve"); |
375 | |
376 | /* --- Check %$r$% is prime --- */ |
377 | |
34e4f738 |
378 | if (!pgen_primep(ei->r, gr)) return ("generator order not prime"); |
432c4e18 |
379 | |
432c4e18 |
380 | /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- * |
381 | * |
382 | * This seems to work with the approximate-sqrt in the library, but might |
383 | * not be so good in some cases. Throw in some extra significate figures |
384 | * for good measure. |
385 | */ |
386 | |
387 | x = mp_lsl(MP_NEW, f->m, 128); |
388 | x = mp_sqrt(x, x); |
389 | y = mp_lsl(MP_NEW, MP_ONE, 64); |
390 | x = mp_add(x, x, y); |
391 | x = mp_sqr(x, x); |
392 | mp_div(&x, 0, x, ei->r); |
393 | x = mp_lsr(x, x, 128); |
394 | rc = MP_EQ(x, ei->h); |
395 | MP_DROP(x); |
396 | MP_DROP(y); |
397 | if (!rc) return ("incorrect cofactor"); |
398 | |
399 | /* --- Check %$n G = O$% --- */ |
400 | |
401 | EC_CREATE(&p); |
402 | ec_mul(c, &p, &ei->g, ei->r); |
403 | rc = EC_ATINF(&p); |
404 | EC_DESTROY(&p); |
405 | if (!rc) return ("incorrect group order"); |
406 | |
407 | /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- * |
408 | * |
409 | * The spec says %$q$%, not %$p$%, but I think that's a misprint. |
410 | */ |
411 | |
412 | x = MP_NEW; |
413 | mp_div(0, &x, f->m, ei->r); |
414 | i = 20; |
415 | while (i) { |
416 | if (MP_EQ(x, MP_ONE)) break; |
417 | x = mp_mul(x, x, f->m); |
418 | mp_div(0, &x, x, ei->r); |
419 | i--; |
420 | } |
421 | MP_DROP(x); |
422 | if (i) return ("curve is weak"); |
423 | |
5c3f75ec |
424 | /* --- Check %$0 < h \le 4$% --- */ |
425 | |
426 | if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR)) |
427 | return ("cofactor out of range"); |
428 | |
432c4e18 |
429 | /* --- Done --- */ |
430 | |
431 | return (0); |
432 | } |
433 | |
434 | static const char *bincheck(const ec_info *ei, grand *gr) |
435 | { |
436 | ec_curve *c = ei->c; |
437 | field *f = c->f; |
438 | int i; |
439 | mp *x, *y; |
440 | ec p; |
441 | int rc; |
442 | |
389d8222 |
443 | /* --- Check that %$m$% is prime --- */ |
444 | |
445 | x = mp_fromuint(MP_NEW, f->nbits); |
446 | rc = pfilt_smallfactor(x); |
447 | mp_drop(x); |
448 | if (rc != PGEN_DONE) return ("degree not prime"); |
449 | |
432c4e18 |
450 | /* --- Check that %$p$% is irreducible --- */ |
451 | |
452 | if (!gf_irreduciblep(f->m)) return ("p not irreducible"); |
453 | |
454 | /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */ |
455 | |
456 | if (mp_bits(c->a) > f->nbits) return ("a out of range"); |
457 | if (mp_bits(c->b) > f->nbits) return ("a out of range"); |
458 | if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range"); |
459 | if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range"); |
460 | |
461 | /* --- Check that %$b \ne 0$% --- */ |
462 | |
463 | if (F_ZEROP(f, c->b)) return ("b is zero"); |
464 | |
465 | /* --- Check that %$G \in E$% --- */ |
466 | |
467 | if (EC_ATINF(&ei->g)) return ("generator at infinity"); |
468 | if (ec_check(c, &ei->g)) return ("generator not on curve"); |
469 | |
470 | /* --- Check %$r$% is prime --- */ |
471 | |
34e4f738 |
472 | if (!pgen_primep(ei->r, gr)) return ("generator order not prime"); |
432c4e18 |
473 | |
432c4e18 |
474 | /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- * |
475 | * |
476 | * This seems to work with the approximate-sqrt in the library, but might |
477 | * not be so good in some cases. Throw in some extra significate figures |
478 | * for good measure. |
479 | */ |
480 | |
481 | x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128); |
482 | x = mp_sqrt(x, x); |
483 | y = mp_lsl(MP_NEW, MP_ONE, 64); |
484 | x = mp_add(x, x, y); |
485 | x = mp_sqr(x, x); |
486 | mp_div(&x, 0, x, ei->r); |
487 | x = mp_lsr(x, x, 128); |
488 | rc = MP_EQ(x, ei->h); |
489 | MP_DROP(x); |
490 | MP_DROP(y); |
491 | if (!rc) return ("incorrect cofactor"); |
492 | |
493 | /* --- Check %$n G = O$% --- */ |
494 | |
495 | EC_CREATE(&p); |
496 | ec_mul(c, &p, &ei->g, ei->r); |
497 | rc = EC_ATINF(&p); |
498 | EC_DESTROY(&p); |
499 | if (!rc) return ("incorrect group order"); |
500 | |
501 | /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */ |
502 | |
503 | x = mp_lsl(MP_NEW, MP_ONE, f->nbits); |
504 | mp_div(0, &x, x, ei->r); |
505 | i = 20; |
506 | while (i) { |
507 | if (MP_EQ(x, MP_ONE)) break; |
508 | x = mp_mul(x, x, f->m); |
509 | mp_div(0, &x, x, ei->r); |
510 | i--; |
511 | } |
512 | MP_DROP(x); |
513 | if (i) return ("curve is weak"); |
514 | |
5c3f75ec |
515 | /* --- Check %$0 < h \le 4$% --- */ |
516 | |
517 | if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR)) |
518 | return ("cofactor out of range"); |
519 | |
432c4e18 |
520 | /* --- Done --- */ |
521 | |
522 | return (0); |
523 | } |
524 | |
525 | const char *ec_checkinfo(const ec_info *ei, grand *gr) |
526 | { |
527 | switch (F_TYPE(ei->c->f)) { |
528 | case FTY_PRIME: return (primecheck(ei, gr)); break; |
529 | case FTY_BINARY: return (bincheck(ei, gr)); break; |
530 | } |
531 | return ("unknown curve type"); |
532 | } |
533 | |
534 | /*----- Test rig ----------------------------------------------------------*/ |
535 | |
536 | #ifdef TEST_RIG |
537 | |
538 | #include "fibrand.h" |
539 | |
53cbeae3 |
540 | int main(int argc, char *argv[]) |
432c4e18 |
541 | { |
542 | const ecentry *ee; |
543 | const char *e; |
544 | int ok = 1; |
53cbeae3 |
545 | int i; |
432c4e18 |
546 | grand *gr; |
547 | |
548 | gr = fibrand_create(0); |
53cbeae3 |
549 | if (argc > 1) { |
550 | for (i = 1; i < argc; i++) { |
551 | ec_info ei; |
552 | if ((e = ec_getinfo(&ei, argv[i])) != 0) |
553 | fprintf(stderr, "bad curve spec `%s': %s", argv[i], e); |
554 | else { |
555 | e = ec_checkinfo(&ei, gr); |
556 | ec_freeinfo(&ei); |
557 | if (!e) |
558 | printf("OK %s\n", argv[i]); |
559 | else { |
560 | printf("BAD %s: %s\n", argv[i], e); |
561 | ok = 0; |
562 | } |
563 | } |
564 | } |
565 | } else { |
20ca05f0 |
566 | fputs("checking standard curves:", stdout); |
567 | fflush(stdout); |
53cbeae3 |
568 | for (ee = ectab; ee->name; ee++) { |
569 | ec_info ei; |
20ca05f0 |
570 | ec_infofromdata(&ei, ee->data); |
53cbeae3 |
571 | e = ec_checkinfo(&ei, gr); |
572 | ec_freeinfo(&ei); |
573 | if (e) { |
20ca05f0 |
574 | printf(" [%s fails: %s]", ee->name, e); |
53cbeae3 |
575 | ok = 0; |
f94b972d |
576 | } else |
20ca05f0 |
577 | printf(" %s", ee->name); |
53cbeae3 |
578 | fflush(stdout); |
432c4e18 |
579 | } |
20ca05f0 |
580 | fputs(ok ? " ok\n" : " failed\n", stdout); |
432c4e18 |
581 | } |
582 | gr->ops->destroy(gr); |
432c4e18 |
583 | return (!ok); |
584 | } |
585 | |
586 | #endif |
587 | |
588 | /*----- That's all, folks -------------------------------------------------*/ |