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 | |
34e4f738 |
167 | /* --- @getinfo@ --- * |
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 | |
178 | static void getinfo(ec_info *ei, ecdata *ed) |
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 | |
02d7884d |
237 | for (ee = ectab; ee->name; ee++) |
238 | if (qd_enum(qd, ee->name) >= 0) { getinfo(ei, ee->data); goto found; } |
239 | |
432c4e18 |
240 | if ((c = ec_curveparse(qd)) == 0) goto fail; |
241 | qd_delim(qd, '/'); if (!ec_ptparse(qd, &g)) goto fail; |
242 | qd_delim(qd, ':'); if ((r = qd_getmp(qd)) == 0) goto fail; |
243 | qd_delim(qd, '*'); if ((h = qd_getmp(qd)) == 0) goto fail; |
432c4e18 |
244 | ei->c = c; ei->g = g; ei->r = r; ei->h = h; |
34e4f738 |
245 | |
246 | found: |
432c4e18 |
247 | return (0); |
248 | |
249 | fail: |
250 | EC_DESTROY(&g); |
251 | if (r) MP_DROP(r); |
252 | if (h) MP_DROP(h); |
253 | if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); } |
254 | return (-1); |
255 | } |
256 | |
432c4e18 |
257 | /* --- @ec_getinfo@ --- * |
258 | * |
259 | * Arguments: @ec_info *ei@ = where to write the information |
260 | * @const char *p@ = string describing a curve |
261 | * |
262 | * Returns: Null on success, or a pointer to an error message. |
263 | * |
264 | * Use: Parses out information about a curve. The string is either a |
265 | * standard curve name, or a curve info string. |
266 | */ |
267 | |
268 | const char *ec_getinfo(ec_info *ei, const char *p) |
269 | { |
270 | qd_parse qd; |
432c4e18 |
271 | |
272 | qd.p = p; |
273 | qd.e = 0; |
432c4e18 |
274 | if (ec_infoparse(&qd, ei)) |
275 | return (qd.e); |
432c4e18 |
276 | if (!qd_eofp(&qd)) { |
277 | ec_freeinfo(ei); |
278 | return ("junk found at end of string"); |
279 | } |
280 | return (0); |
281 | } |
282 | |
34e4f738 |
283 | /* --- @ec_sameinfop@ --- * |
284 | * |
285 | * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets |
286 | * |
287 | * Returns: Nonzero if the curves are identical (not just isomorphic). |
288 | * |
289 | * Use: Checks for sameness of curve parameters. |
290 | */ |
291 | |
292 | int ec_sameinfop(ec_info *ei, ec_info *ej) |
293 | { |
294 | return (ec_samep(ei->c, ej->c) && |
295 | MP_EQ(ei->r, ej->r) && MP_EQ(ei->h, ej->h) && |
296 | EC_EQ(&ei->g, &ej->g)); |
297 | } |
298 | |
432c4e18 |
299 | /* --- @ec_freeinfo@ --- * |
300 | * |
301 | * Arguments: @ec_info *ei@ = elliptic curve information block to free |
302 | * |
303 | * Returns: --- |
304 | * |
305 | * Use: Frees the information block. |
306 | */ |
307 | |
308 | void ec_freeinfo(ec_info *ei) |
309 | { |
310 | field *f; |
311 | |
312 | EC_DESTROY(&ei->g); |
313 | MP_DROP(ei->r); |
314 | MP_DROP(ei->h); |
315 | f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f); |
316 | } |
317 | |
318 | /* --- @ec_checkinfo@ --- * |
319 | * |
320 | * Arguments: @const ec_info *ei@ = elliptic curve information block |
321 | * |
322 | * Returns: Null if OK, or pointer to error message. |
323 | * |
324 | * Use: Checks an elliptic curve according to the rules in SEC1. |
325 | */ |
326 | |
432c4e18 |
327 | static int primeeltp(mp *x, field *f) |
328 | { |
a69a3efd |
329 | return (!MP_NEGP(x) && MP_CMP(x, <, f->m)); |
432c4e18 |
330 | } |
331 | |
332 | static const char *primecheck(const ec_info *ei, grand *gr) |
333 | { |
334 | ec_curve *c = ei->c; |
335 | field *f = c->f; |
336 | int i; |
337 | mp *x, *y; |
338 | ec p; |
339 | int rc; |
340 | |
341 | /* --- Check %$p$% is an odd prime --- */ |
342 | |
34e4f738 |
343 | if (!pgen_primep(f->m, gr)) return ("p not prime"); |
432c4e18 |
344 | |
345 | /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */ |
346 | |
347 | if (!primeeltp(c->a, f)) return ("a out of range"); |
348 | if (!primeeltp(c->b, f)) return ("b out of range"); |
349 | if (!primeeltp(ei->g.x, f)) return ("G_x out of range"); |
350 | if (!primeeltp(ei->g.x, f)) return ("G_y out of range"); |
351 | |
352 | /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */ |
353 | |
354 | x = F_SQR(f, MP_NEW, c->a); |
355 | x = F_MUL(f, x, x, c->a); |
356 | x = F_QDL(f, x, x); |
357 | y = F_SQR(f, MP_NEW, c->b); |
358 | y = F_TPL(f, y, y); |
359 | y = F_TPL(f, y, y); |
360 | y = F_TPL(f, y, y); |
361 | x = F_ADD(f, x, x, y); |
362 | rc = F_ZEROP(f, x); |
363 | MP_DROP(x); |
364 | MP_DROP(y); |
365 | if (rc) return ("not an elliptic curve"); |
366 | |
367 | /* --- Check %$G \in E$% --- */ |
368 | |
369 | if (EC_ATINF(&ei->g)) return ("generator at infinity"); |
370 | if (ec_check(c, &ei->g)) return ("generator not on curve"); |
371 | |
372 | /* --- Check %$r$% is prime --- */ |
373 | |
34e4f738 |
374 | if (!pgen_primep(ei->r, gr)) return ("generator order not prime"); |
432c4e18 |
375 | |
432c4e18 |
376 | /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- * |
377 | * |
378 | * This seems to work with the approximate-sqrt in the library, but might |
379 | * not be so good in some cases. Throw in some extra significate figures |
380 | * for good measure. |
381 | */ |
382 | |
383 | x = mp_lsl(MP_NEW, f->m, 128); |
384 | x = mp_sqrt(x, x); |
385 | y = mp_lsl(MP_NEW, MP_ONE, 64); |
386 | x = mp_add(x, x, y); |
387 | x = mp_sqr(x, x); |
388 | mp_div(&x, 0, x, ei->r); |
389 | x = mp_lsr(x, x, 128); |
390 | rc = MP_EQ(x, ei->h); |
391 | MP_DROP(x); |
392 | MP_DROP(y); |
393 | if (!rc) return ("incorrect cofactor"); |
394 | |
395 | /* --- Check %$n G = O$% --- */ |
396 | |
397 | EC_CREATE(&p); |
398 | ec_mul(c, &p, &ei->g, ei->r); |
399 | rc = EC_ATINF(&p); |
400 | EC_DESTROY(&p); |
401 | if (!rc) return ("incorrect group order"); |
402 | |
403 | /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- * |
404 | * |
405 | * The spec says %$q$%, not %$p$%, but I think that's a misprint. |
406 | */ |
407 | |
408 | x = MP_NEW; |
409 | mp_div(0, &x, f->m, ei->r); |
410 | i = 20; |
411 | while (i) { |
412 | if (MP_EQ(x, MP_ONE)) break; |
413 | x = mp_mul(x, x, f->m); |
414 | mp_div(0, &x, x, ei->r); |
415 | i--; |
416 | } |
417 | MP_DROP(x); |
418 | if (i) return ("curve is weak"); |
419 | |
5c3f75ec |
420 | /* --- Check %$0 < h \le 4$% --- */ |
421 | |
422 | if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR)) |
423 | return ("cofactor out of range"); |
424 | |
432c4e18 |
425 | /* --- Done --- */ |
426 | |
427 | return (0); |
428 | } |
429 | |
430 | static const char *bincheck(const ec_info *ei, grand *gr) |
431 | { |
432 | ec_curve *c = ei->c; |
433 | field *f = c->f; |
434 | int i; |
435 | mp *x, *y; |
436 | ec p; |
437 | int rc; |
438 | |
389d8222 |
439 | /* --- Check that %$m$% is prime --- */ |
440 | |
441 | x = mp_fromuint(MP_NEW, f->nbits); |
442 | rc = pfilt_smallfactor(x); |
443 | mp_drop(x); |
444 | if (rc != PGEN_DONE) return ("degree not prime"); |
445 | |
432c4e18 |
446 | /* --- Check that %$p$% is irreducible --- */ |
447 | |
448 | if (!gf_irreduciblep(f->m)) return ("p not irreducible"); |
449 | |
450 | /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */ |
451 | |
452 | if (mp_bits(c->a) > f->nbits) return ("a out of range"); |
453 | if (mp_bits(c->b) > f->nbits) return ("a out of range"); |
454 | if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range"); |
455 | if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range"); |
456 | |
457 | /* --- Check that %$b \ne 0$% --- */ |
458 | |
459 | if (F_ZEROP(f, c->b)) return ("b is zero"); |
460 | |
461 | /* --- Check that %$G \in E$% --- */ |
462 | |
463 | if (EC_ATINF(&ei->g)) return ("generator at infinity"); |
464 | if (ec_check(c, &ei->g)) return ("generator not on curve"); |
465 | |
466 | /* --- Check %$r$% is prime --- */ |
467 | |
34e4f738 |
468 | if (!pgen_primep(ei->r, gr)) return ("generator order not prime"); |
432c4e18 |
469 | |
432c4e18 |
470 | /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- * |
471 | * |
472 | * This seems to work with the approximate-sqrt in the library, but might |
473 | * not be so good in some cases. Throw in some extra significate figures |
474 | * for good measure. |
475 | */ |
476 | |
477 | x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128); |
478 | x = mp_sqrt(x, x); |
479 | y = mp_lsl(MP_NEW, MP_ONE, 64); |
480 | x = mp_add(x, x, y); |
481 | x = mp_sqr(x, x); |
482 | mp_div(&x, 0, x, ei->r); |
483 | x = mp_lsr(x, x, 128); |
484 | rc = MP_EQ(x, ei->h); |
485 | MP_DROP(x); |
486 | MP_DROP(y); |
487 | if (!rc) return ("incorrect cofactor"); |
488 | |
489 | /* --- Check %$n G = O$% --- */ |
490 | |
491 | EC_CREATE(&p); |
492 | ec_mul(c, &p, &ei->g, ei->r); |
493 | rc = EC_ATINF(&p); |
494 | EC_DESTROY(&p); |
495 | if (!rc) return ("incorrect group order"); |
496 | |
497 | /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */ |
498 | |
499 | x = mp_lsl(MP_NEW, MP_ONE, f->nbits); |
500 | mp_div(0, &x, x, ei->r); |
501 | i = 20; |
502 | while (i) { |
503 | if (MP_EQ(x, MP_ONE)) break; |
504 | x = mp_mul(x, x, f->m); |
505 | mp_div(0, &x, x, ei->r); |
506 | i--; |
507 | } |
508 | MP_DROP(x); |
509 | if (i) return ("curve is weak"); |
510 | |
5c3f75ec |
511 | /* --- Check %$0 < h \le 4$% --- */ |
512 | |
513 | if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR)) |
514 | return ("cofactor out of range"); |
515 | |
432c4e18 |
516 | /* --- Done --- */ |
517 | |
518 | return (0); |
519 | } |
520 | |
521 | const char *ec_checkinfo(const ec_info *ei, grand *gr) |
522 | { |
523 | switch (F_TYPE(ei->c->f)) { |
524 | case FTY_PRIME: return (primecheck(ei, gr)); break; |
525 | case FTY_BINARY: return (bincheck(ei, gr)); break; |
526 | } |
527 | return ("unknown curve type"); |
528 | } |
529 | |
530 | /*----- Test rig ----------------------------------------------------------*/ |
531 | |
532 | #ifdef TEST_RIG |
533 | |
534 | #include "fibrand.h" |
535 | |
53cbeae3 |
536 | int main(int argc, char *argv[]) |
432c4e18 |
537 | { |
538 | const ecentry *ee; |
539 | const char *e; |
540 | int ok = 1; |
53cbeae3 |
541 | int i; |
432c4e18 |
542 | grand *gr; |
543 | |
544 | gr = fibrand_create(0); |
53cbeae3 |
545 | if (argc > 1) { |
546 | for (i = 1; i < argc; i++) { |
547 | ec_info ei; |
548 | if ((e = ec_getinfo(&ei, argv[i])) != 0) |
549 | fprintf(stderr, "bad curve spec `%s': %s", argv[i], e); |
550 | else { |
551 | e = ec_checkinfo(&ei, gr); |
552 | ec_freeinfo(&ei); |
553 | if (!e) |
554 | printf("OK %s\n", argv[i]); |
555 | else { |
556 | printf("BAD %s: %s\n", argv[i], e); |
557 | ok = 0; |
558 | } |
559 | } |
560 | } |
561 | } else { |
f94b972d |
562 | fputs("checking standard curves...\n", stdout); |
53cbeae3 |
563 | for (ee = ectab; ee->name; ee++) { |
564 | ec_info ei; |
f94b972d |
565 | printf(" %s: ", ee->name); |
566 | fflush(stdout); |
53cbeae3 |
567 | getinfo(&ei, ee->data); |
568 | e = ec_checkinfo(&ei, gr); |
569 | ec_freeinfo(&ei); |
570 | if (e) { |
f94b972d |
571 | printf("fails: %s\n", e); |
53cbeae3 |
572 | ok = 0; |
f94b972d |
573 | } else |
574 | fputs("ok\n", stdout); |
53cbeae3 |
575 | fflush(stdout); |
432c4e18 |
576 | } |
f94b972d |
577 | if (ok) |
578 | fputs("all ok\n", stdout); |
432c4e18 |
579 | } |
580 | gr->ops->destroy(gr); |
432c4e18 |
581 | return (!ok); |
582 | } |
583 | |
584 | #endif |
585 | |
586 | /*----- That's all, folks -------------------------------------------------*/ |