3 * $Id: ec-info.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
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 ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @ec_curveparse@ --- *
43 * Arguments: @qd_parse *qd@ = parser context
45 * Returns: Elliptic curve pointer if OK, or null.
47 * Use: Parses an elliptic curve description, which has the form
49 * * a field description
51 * * `prime', `primeproj', `bin', or `binproj'
53 * * the %$a$% parameter
55 * * the %$b$% parameter
58 ec_curve
*ec_curveparse(qd_parse
*qd
)
60 mp
*a
= MP_NEW
, *b
= MP_NEW
;
64 if ((f
= field_parse(qd
)) == 0) goto fail
;
66 switch (qd_enum(qd
, "prime,primeproj,bin,binproj")) {
68 if (F_TYPE(f
) != FTY_PRIME
) {
69 qd
->e
= "field not prime";
73 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
75 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
76 c
= ec_prime(f
, a
, b
);
79 if (F_TYPE(f
) != FTY_PRIME
) {
80 qd
->e
= "field not prime";
84 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
86 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
87 c
= ec_primeproj(f
, a
, b
);
90 if (F_TYPE(f
) != FTY_BINARY
) {
91 qd
->e
= "field not binary";
95 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
97 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
101 if (F_TYPE(f
) != FTY_BINARY
) {
102 qd
->e
= "field not binary";
106 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
108 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
109 c
= ec_binproj(f
, a
, b
);
115 qd
->e
= "bad curve parameters";
129 /* --- @ec_ptparse@ --- *
131 * Arguments: @qd_parse *qd@ = parser context
132 * @ec *p@ = where to put the point
134 * Returns: The point address, or null.
136 * Use: Parses an elliptic curve point. This has the form
143 ec
*ec_ptparse(qd_parse
*qd
, ec
*p
)
145 mp
*x
= MP_NEW
, *y
= MP_NEW
;
147 if (qd_enum(qd
, "inf") >= 0) {
151 if ((x
= qd_getmp(qd
)) == 0) goto fail
;
153 if ((y
= qd_getmp(qd
)) == 0) goto fail
;
166 /* --- @getinfo@ --- *
168 * Arguments: @ec_info *ei@ = where to write the information
169 * @ecdata *ed@ = raw data
173 * Use: Loads elliptic curve information about one of the standard
177 static void getinfo(ec_info
*ei
, ecdata
*ed
)
183 f
= field_prime(&ed
->p
);
184 ei
->c
= ec_primeproj(f
, &ed
->a
, &ed
->b
);
187 f
= field_niceprime(&ed
->p
);
188 ei
->c
= ec_primeproj(f
, &ed
->a
, &ed
->b
);
191 f
= field_binpoly(&ed
->p
);
192 ei
->c
= ec_binproj(f
, &ed
->a
, &ed
->b
);
195 f
= field_binnorm(&ed
->p
, &ed
->beta
);
196 ei
->c
= ec_binproj(f
, &ed
->a
, &ed
->b
);
202 assert(f
); assert(ei
->c
);
203 EC_CREATE(&ei
->g
); ei
->g
.x
= &ed
->gx
; ei
->g
.y
= &ed
->gy
; ei
->g
.z
= 0;
204 ei
->r
= &ed
->r
; ei
->h
= &ed
->h
;
207 /* --- @ec_infoparse@ --- *
209 * Arguments: @qd_parse *qd@ = parser context
210 * @ec_info *ei@ = curve information block, currently
213 * Returns: Zero on success, nonzero on failure.
215 * Use: Parses an elliptic curve information string, and stores the
216 * information in @ei@. This is either the name of a standard
217 * curve, or it has the form
219 * * elliptic curve description
228 int ec_infoparse(qd_parse
*qd
, ec_info
*ei
)
234 mp
*r
= MP_NEW
, *h
= MP_NEW
;
236 for (ee
= ectab
; ee
->name
; ee
++)
237 if (qd_enum(qd
, ee
->name
) >= 0) { getinfo(ei
, ee
->data
); goto found
; }
239 if ((c
= ec_curveparse(qd
)) == 0) goto fail
;
240 qd_delim(qd
, '/'); if (!ec_ptparse(qd
, &g
)) goto fail
;
241 qd_delim(qd
, ':'); if ((r
= qd_getmp(qd
)) == 0) goto fail
;
242 qd_delim(qd
, '*'); if ((h
= qd_getmp(qd
)) == 0) goto fail
;
243 ei
->c
= c
; ei
->g
= g
; ei
->r
= r
; ei
->h
= h
;
252 if (c
) { f
= c
->f
; ec_destroycurve(c
); F_DESTROY(f
); }
256 /* --- @ec_getinfo@ --- *
258 * Arguments: @ec_info *ei@ = where to write the information
259 * @const char *p@ = string describing a curve
261 * Returns: Null on success, or a pointer to an error message.
263 * Use: Parses out information about a curve. The string is either a
264 * standard curve name, or a curve info string.
267 const char *ec_getinfo(ec_info
*ei
, const char *p
)
273 if (ec_infoparse(&qd
, ei
))
277 return ("junk found at end of string");
282 /* --- @ec_sameinfop@ --- *
284 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
286 * Returns: Nonzero if the curves are identical (not just isomorphic).
288 * Use: Checks for sameness of curve parameters.
291 int ec_sameinfop(ec_info
*ei
, ec_info
*ej
)
293 return (ec_samep(ei
->c
, ej
->c
) &&
294 MP_EQ(ei
->r
, ej
->r
) && MP_EQ(ei
->h
, ej
->h
) &&
295 EC_EQ(&ei
->g
, &ej
->g
));
298 /* --- @ec_freeinfo@ --- *
300 * Arguments: @ec_info *ei@ = elliptic curve information block to free
304 * Use: Frees the information block.
307 void ec_freeinfo(ec_info
*ei
)
314 f
= ei
->c
->f
; ec_destroycurve(ei
->c
); F_DESTROY(f
);
317 /* --- @ec_checkinfo@ --- *
319 * Arguments: @const ec_info *ei@ = elliptic curve information block
321 * Returns: Null if OK, or pointer to error message.
323 * Use: Checks an elliptic curve according to the rules in SEC1.
326 static int primeeltp(mp
*x
, field
*f
)
328 return (!MP_ISNEG(x
) && MP_CMP(x
, <, f
->m
));
331 static const char *primecheck(const ec_info
*ei
, grand
*gr
)
340 /* --- Check %$p$% is an odd prime --- */
342 if (!pgen_primep(f
->m
, gr
)) return ("p not prime");
344 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
346 if (!primeeltp(c
->a
, f
)) return ("a out of range");
347 if (!primeeltp(c
->b
, f
)) return ("b out of range");
348 if (!primeeltp(ei
->g
.x
, f
)) return ("G_x out of range");
349 if (!primeeltp(ei
->g
.x
, f
)) return ("G_y out of range");
351 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
353 x
= F_SQR(f
, MP_NEW
, c
->a
);
354 x
= F_MUL(f
, x
, x
, c
->a
);
356 y
= F_SQR(f
, MP_NEW
, c
->b
);
360 x
= F_ADD(f
, x
, x
, y
);
364 if (rc
) return ("not an elliptic curve");
366 /* --- Check %$G \in E$% --- */
368 if (EC_ATINF(&ei
->g
)) return ("generator at infinity");
369 if (ec_check(c
, &ei
->g
)) return ("generator not on curve");
371 /* --- Check %$r$% is prime --- */
373 if (!pgen_primep(ei
->r
, gr
)) return ("generator order not prime");
375 /* --- Check %$0 < h \le 4$% --- */
377 if (MP_CMP(ei
->h
, <, MP_ONE
) || MP_CMP(ei
->h
, >, MP_FOUR
))
378 return ("cofactor out of range");
380 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
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
387 x
= mp_lsl(MP_NEW
, f
->m
, 128);
389 y
= mp_lsl(MP_NEW
, MP_ONE
, 64);
392 mp_div(&x
, 0, x
, ei
->r
);
393 x
= mp_lsr(x
, x
, 128);
394 rc
= MP_EQ(x
, ei
->h
);
397 if (!rc
) return ("incorrect cofactor");
399 /* --- Check %$n G = O$% --- */
402 ec_mul(c
, &p
, &ei
->g
, ei
->r
);
405 if (!rc
) return ("incorrect group order");
407 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
409 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
413 mp_div(0, &x
, f
->m
, ei
->r
);
416 if (MP_EQ(x
, MP_ONE
)) break;
417 x
= mp_mul(x
, x
, f
->m
);
418 mp_div(0, &x
, x
, ei
->r
);
422 if (i
) return ("curve is weak");
429 static const char *bincheck(const ec_info
*ei
, grand
*gr
)
438 /* --- Check that %$p$% is irreducible --- */
440 if (!gf_irreduciblep(f
->m
)) return ("p not irreducible");
442 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
444 if (mp_bits(c
->a
) > f
->nbits
) return ("a out of range");
445 if (mp_bits(c
->b
) > f
->nbits
) return ("a out of range");
446 if (mp_bits(ei
->g
.x
) > f
->nbits
) return ("G_x out of range");
447 if (mp_bits(ei
->g
.y
) > f
->nbits
) return ("G_y out of range");
449 /* --- Check that %$b \ne 0$% --- */
451 if (F_ZEROP(f
, c
->b
)) return ("b is zero");
453 /* --- Check that %$G \in E$% --- */
455 if (EC_ATINF(&ei
->g
)) return ("generator at infinity");
456 if (ec_check(c
, &ei
->g
)) return ("generator not on curve");
458 /* --- Check %$r$% is prime --- */
460 if (!pgen_primep(ei
->r
, gr
)) return ("generator order not prime");
462 /* --- Check %$0 < h \le 4$% --- */
464 if (MP_CMP(ei
->h
, <, MP_ONE
) || MP_CMP(ei
->h
, >, MP_FOUR
))
465 return ("cofactor out of range");
467 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
469 * This seems to work with the approximate-sqrt in the library, but might
470 * not be so good in some cases. Throw in some extra significate figures
474 x
= mp_lsl(MP_NEW
, MP_ONE
, f
->nbits
+ 128);
476 y
= mp_lsl(MP_NEW
, MP_ONE
, 64);
479 mp_div(&x
, 0, x
, ei
->r
);
480 x
= mp_lsr(x
, x
, 128);
481 rc
= MP_EQ(x
, ei
->h
);
484 if (!rc
) return ("incorrect cofactor");
486 /* --- Check %$n G = O$% --- */
489 ec_mul(c
, &p
, &ei
->g
, ei
->r
);
492 if (!rc
) return ("incorrect group order");
494 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
496 x
= mp_lsl(MP_NEW
, MP_ONE
, f
->nbits
);
497 mp_div(0, &x
, x
, ei
->r
);
500 if (MP_EQ(x
, MP_ONE
)) break;
501 x
= mp_mul(x
, x
, f
->m
);
502 mp_div(0, &x
, x
, ei
->r
);
506 if (i
) return ("curve is weak");
513 const char *ec_checkinfo(const ec_info
*ei
, grand
*gr
)
515 switch (F_TYPE(ei
->c
->f
)) {
516 case FTY_PRIME
: return (primecheck(ei
, gr
)); break;
517 case FTY_BINARY
: return (bincheck(ei
, gr
)); break;
519 return ("unknown curve type");
522 /*----- Test rig ----------------------------------------------------------*/
535 gr
= fibrand_create(0);
536 fputs("checking standard curves: ", stdout
);
537 for (ee
= ectab
; ee
->name
; ee
++) {
539 getinfo(&ei
, ee
->data
);
540 e
= ec_checkinfo(&ei
, gr
);
543 fprintf(stderr
, "\n*** curve %s fails: %s\n", ee
->name
, e
);
549 gr
->ops
->destroy(gr
);
550 fputs(ok ?
" ok\n" : " failed\n", stdout
);
556 /*----- That's all, folks -------------------------------------------------*/