5 * Elliptic curve information management
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @ec_curveparse@ --- *
44 * Arguments: @qd_parse *qd@ = parser context
46 * Returns: Elliptic curve pointer if OK, or null.
48 * Use: Parses an elliptic curve description, which has the form
50 * * a field description
52 * * `prime', `primeproj', `bin', or `binproj'
54 * * the %$a$% parameter
56 * * the %$b$% parameter
59 ec_curve *ec_curveparse(qd_parse *qd)
61 mp *a = MP_NEW, *b = MP_NEW;
65 if ((f = field_parse(qd)) == 0) goto fail;
67 switch (qd_enum(qd, "prime,primeproj,bin,binproj")) {
69 if (F_TYPE(f) != FTY_PRIME) {
70 qd->e = "field not prime";
74 if ((a = qd_getmp(qd)) == 0) goto fail;
76 if ((b = qd_getmp(qd)) == 0) goto fail;
77 c = ec_prime(f, a, b);
80 if (F_TYPE(f) != FTY_PRIME) {
81 qd->e = "field not prime";
85 if ((a = qd_getmp(qd)) == 0) goto fail;
87 if ((b = qd_getmp(qd)) == 0) goto fail;
88 c = ec_primeproj(f, a, b);
91 if (F_TYPE(f) != FTY_BINARY) {
92 qd->e = "field not binary";
96 if ((a = qd_getmp(qd)) == 0) goto fail;
98 if ((b = qd_getmp(qd)) == 0) goto fail;
102 if (F_TYPE(f) != FTY_BINARY) {
103 qd->e = "field not binary";
107 if ((a = qd_getmp(qd)) == 0) goto fail;
109 if ((b = qd_getmp(qd)) == 0) goto fail;
110 c = ec_binproj(f, a, b);
116 qd->e = "bad curve parameters";
130 /* --- @ec_ptparse@ --- *
132 * Arguments: @qd_parse *qd@ = parser context
133 * @ec *p@ = where to put the point
135 * Returns: The point address, or null.
137 * Use: Parses an elliptic curve point. This has the form
144 ec *ec_ptparse(qd_parse *qd, ec *p)
146 mp *x = MP_NEW, *y = MP_NEW;
148 if (qd_enum(qd, "inf") >= 0) {
152 if ((x = qd_getmp(qd)) == 0) goto fail;
154 if ((y = qd_getmp(qd)) == 0) goto fail;
167 /* --- @getinfo@ --- *
169 * Arguments: @ec_info *ei@ = where to write the information
170 * @ecdata *ed@ = raw data
174 * Use: Loads elliptic curve information about one of the standard
178 static void getinfo(ec_info *ei, ecdata *ed)
184 f = field_prime(&ed->p);
185 ei->c = ec_primeproj(f, &ed->a, &ed->b);
188 f = field_niceprime(&ed->p);
189 ei->c = ec_primeproj(f, &ed->a, &ed->b);
192 f = field_binpoly(&ed->p);
193 ei->c = ec_binproj(f, &ed->a, &ed->b);
196 f = field_binnorm(&ed->p, &ed->beta);
197 ei->c = ec_binproj(f, &ed->a, &ed->b);
203 assert(f); assert(ei->c);
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;
208 /* --- @ec_infoparse@ --- *
210 * Arguments: @qd_parse *qd@ = parser context
211 * @ec_info *ei@ = curve information block, currently
214 * Returns: Zero on success, nonzero on failure.
216 * Use: Parses an elliptic curve information string, and stores the
217 * information in @ei@. This is either the name of a standard
218 * curve, or it has the form
220 * * elliptic curve description
229 int ec_infoparse(qd_parse *qd, ec_info *ei)
235 mp *r = MP_NEW, *h = MP_NEW;
237 for (ee = ectab; ee->name; ee++)
238 if (qd_enum(qd, ee->name) >= 0) { getinfo(ei, ee->data); goto found; }
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;
244 ei->c = c; ei->g = g; ei->r = r; ei->h = h;
253 if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); }
257 /* --- @ec_getinfo@ --- *
259 * Arguments: @ec_info *ei@ = where to write the information
260 * @const char *p@ = string describing a curve
262 * Returns: Null on success, or a pointer to an error message.
264 * Use: Parses out information about a curve. The string is either a
265 * standard curve name, or a curve info string.
268 const char *ec_getinfo(ec_info *ei, const char *p)
274 if (ec_infoparse(&qd, ei))
278 return ("junk found at end of string");
283 /* --- @ec_sameinfop@ --- *
285 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
287 * Returns: Nonzero if the curves are identical (not just isomorphic).
289 * Use: Checks for sameness of curve parameters.
292 int ec_sameinfop(ec_info *ei, ec_info *ej)
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));
299 /* --- @ec_freeinfo@ --- *
301 * Arguments: @ec_info *ei@ = elliptic curve information block to free
305 * Use: Frees the information block.
308 void ec_freeinfo(ec_info *ei)
315 f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f);
318 /* --- @ec_checkinfo@ --- *
320 * Arguments: @const ec_info *ei@ = elliptic curve information block
322 * Returns: Null if OK, or pointer to error message.
324 * Use: Checks an elliptic curve according to the rules in SEC1.
327 static int primeeltp(mp *x, field *f)
329 return (!MP_NEGP(x) && MP_CMP(x, <, f->m));
332 static const char *primecheck(const ec_info *ei, grand *gr)
341 /* --- Check %$p$% is an odd prime --- */
343 if (!pgen_primep(f->m, gr)) return ("p not prime");
345 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
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");
352 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
354 x = F_SQR(f, MP_NEW, c->a);
355 x = F_MUL(f, x, x, c->a);
357 y = F_SQR(f, MP_NEW, c->b);
361 x = F_ADD(f, x, x, y);
365 if (rc) return ("not an elliptic curve");
367 /* --- Check %$G \in E$% --- */
369 if (EC_ATINF(&ei->g)) return ("generator at infinity");
370 if (ec_check(c, &ei->g)) return ("generator not on curve");
372 /* --- Check %$r$% is prime --- */
374 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
376 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
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
383 x = mp_lsl(MP_NEW, f->m, 128);
385 y = mp_lsl(MP_NEW, MP_ONE, 64);
388 mp_div(&x, 0, x, ei->r);
389 x = mp_lsr(x, x, 128);
390 rc = MP_EQ(x, ei->h);
393 if (!rc) return ("incorrect cofactor");
395 /* --- Check %$n G = O$% --- */
398 ec_mul(c, &p, &ei->g, ei->r);
401 if (!rc) return ("incorrect group order");
403 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
405 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
409 mp_div(0, &x, f->m, ei->r);
412 if (MP_EQ(x, MP_ONE)) break;
413 x = mp_mul(x, x, f->m);
414 mp_div(0, &x, x, ei->r);
418 if (i) return ("curve is weak");
420 /* --- Check %$0 < h \le 4$% --- */
422 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
423 return ("cofactor out of range");
430 static const char *bincheck(const ec_info *ei, grand *gr)
439 /* --- Check that %$m$% is prime --- */
441 x = mp_fromuint(MP_NEW, f->nbits);
442 rc = pfilt_smallfactor(x);
444 if (rc != PGEN_DONE) return ("degree not prime");
446 /* --- Check that %$p$% is irreducible --- */
448 if (!gf_irreduciblep(f->m)) return ("p not irreducible");
450 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
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");
457 /* --- Check that %$b \ne 0$% --- */
459 if (F_ZEROP(f, c->b)) return ("b is zero");
461 /* --- Check that %$G \in E$% --- */
463 if (EC_ATINF(&ei->g)) return ("generator at infinity");
464 if (ec_check(c, &ei->g)) return ("generator not on curve");
466 /* --- Check %$r$% is prime --- */
468 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
470 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
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
477 x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128);
479 y = mp_lsl(MP_NEW, MP_ONE, 64);
482 mp_div(&x, 0, x, ei->r);
483 x = mp_lsr(x, x, 128);
484 rc = MP_EQ(x, ei->h);
487 if (!rc) return ("incorrect cofactor");
489 /* --- Check %$n G = O$% --- */
492 ec_mul(c, &p, &ei->g, ei->r);
495 if (!rc) return ("incorrect group order");
497 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
499 x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
500 mp_div(0, &x, x, ei->r);
503 if (MP_EQ(x, MP_ONE)) break;
504 x = mp_mul(x, x, f->m);
505 mp_div(0, &x, x, ei->r);
509 if (i) return ("curve is weak");
511 /* --- Check %$0 < h \le 4$% --- */
513 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
514 return ("cofactor out of range");
521 const char *ec_checkinfo(const ec_info *ei, grand *gr)
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;
527 return ("unknown curve type");
530 /*----- Test rig ----------------------------------------------------------*/
536 int main(int argc, char *argv[])
544 gr = fibrand_create(0);
546 for (i = 1; i < argc; i++) {
548 if ((e = ec_getinfo(&ei, argv[i])) != 0)
549 fprintf(stderr, "bad curve spec `%s': %s", argv[i], e);
551 e = ec_checkinfo(&ei, gr);
554 printf("OK %s\n", argv[i]);
556 printf("BAD %s: %s\n", argv[i], e);
562 fputs("checking standard curves...\n", stdout);
563 for (ee = ectab; ee->name; ee++) {
565 printf(" %s: ", ee->name);
567 getinfo(&ei, ee->data);
568 e = ec_checkinfo(&ei, gr);
571 printf("fails: %s\n", e);
574 fputs("ok\n", stdout);
578 fputs("all ok\n", stdout);
580 gr->ops->destroy(gr);
586 /*----- That's all, folks -------------------------------------------------*/