3 * $Id: ec-info.c,v 1.1 2004/03/27 17:54:11 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
38 /*----- Header files ------------------------------------------------------*/
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @ec_curveparse@ --- *
51 * Arguments: @qd_parse *qd@ = parser context
53 * Returns: Elliptic curve pointer if OK, or null.
55 * Use: Parses an elliptic curve description, which has the form
57 * * a field description
59 * * `prime', `primeproj', `bin', or `binproj'
61 * * the %$a$% parameter
63 * * the %$b$% parameter
66 ec_curve
*ec_curveparse(qd_parse
*qd
)
68 mp
*a
= MP_NEW
, *b
= MP_NEW
;
72 if ((f
= field_parse(qd
)) == 0) goto fail
;
74 switch (qd_enum(qd
, "prime,primeproj,bin,binproj")) {
76 if (F_TYPE(f
) != FTY_PRIME
) {
77 qd
->e
= "field not prime";
81 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
83 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
84 c
= ec_prime(f
, a
, b
);
87 if (F_TYPE(f
) != FTY_PRIME
) {
88 qd
->e
= "field not prime";
92 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
94 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
95 c
= ec_primeproj(f
, a
, b
);
98 if (F_TYPE(f
) != FTY_BINARY
) {
99 qd
->e
= "field not binary";
103 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
105 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
109 if (F_TYPE(f
) != FTY_BINARY
) {
110 qd
->e
= "field not binary";
114 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
116 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
117 c
= ec_binproj(f
, a
, b
);
133 /* --- @ec_ptparse@ --- *
135 * Arguments: @qd_parse *qd@ = parser context
136 * @ec *p@ = where to put the point
138 * Returns: The point address, or null.
140 * Use: Parses an elliptic curve point. This has the form
147 ec
*ec_ptparse(qd_parse
*qd
, ec
*p
)
149 mp
*x
= MP_NEW
, *y
= MP_NEW
;
151 if (qd_enum(qd
, "inf") >= 0) {
155 if ((x
= qd_getmp(qd
)) == 0) goto fail
;
157 if ((y
= qd_getmp(qd
)) == 0) goto fail
;
170 /* --- @ec_infoparse@ --- *
172 * Arguments: @qd_parse *qd@ = parser context
173 * @ec_info *ei@ = curve information block, currently
176 * Returns: Zero on success, nonzero on failure.
178 * Use: Parses an elliptic curve information string, and stores the
179 * information in @ei@. This has the form
181 * * elliptic curve description
190 int ec_infoparse(qd_parse
*qd
, ec_info
*ei
)
195 mp
*r
= MP_NEW
, *h
= MP_NEW
;
197 if ((c
= ec_curveparse(qd
)) == 0) goto fail
;
198 qd_delim(qd
, '/'); if (!ec_ptparse(qd
, &g
)) goto fail
;
199 qd_delim(qd
, ':'); if ((r
= qd_getmp(qd
)) == 0) goto fail
;
200 qd_delim(qd
, '*'); if ((h
= qd_getmp(qd
)) == 0) goto fail
;
202 ei
->c
= c
; ei
->g
= g
; ei
->r
= r
; ei
->h
= h
;
209 if (c
) { f
= c
->f
; ec_destroycurve(c
); F_DESTROY(f
); }
213 /* --- @getinfo@ --- *
215 * Arguments: @ec_info *ei@ = where to write the information
216 * @const ecdata *ed@ = raw data
220 * Use: Loads elliptic curve information about one of the standard
224 static mp
*getmp(const mpw
*v
, size_t n
)
226 mp
*x
= mp_new(n
, 0);
227 memcpy(x
->v
, v
, MPWS(n
));
231 static void getinfo(ec_info
*ei
, const ecdata
*ed
)
234 mp
*p
= 0, *a
= 0, *b
= 0;
238 p
= getmp(ed
->p
, ed
->psz
);
240 a
= getmp(ed
->a
, ed
->asz
); b
= getmp(ed
->b
, ed
->bsz
);
241 ei
->c
= ec_primeproj(f
, a
, b
);
244 p
= getmp(ed
->p
, ed
->psz
);
245 f
= field_niceprime(p
);
246 a
= getmp(ed
->a
, ed
->asz
); b
= getmp(ed
->b
, ed
->bsz
);
247 ei
->c
= ec_primeproj(f
, a
, b
);
250 p
= getmp(ed
->p
, ed
->psz
);
251 f
= field_binpoly(p
);
252 a
= getmp(ed
->a
, ed
->asz
); b
= getmp(ed
->b
, ed
->bsz
);
253 ei
->c
= ec_binproj(f
, a
, b
);
260 ei
->g
.x
= getmp(ed
->gx
, ed
->gxsz
);
261 ei
->g
.y
= getmp(ed
->gy
, ed
->gysz
);
263 ei
->r
= getmp(ed
->r
, ed
->rsz
);
264 ei
->h
= getmp(ed
->h
, ed
->hsz
);
271 /* --- @ec_getinfo@ --- *
273 * Arguments: @ec_info *ei@ = where to write the information
274 * @const char *p@ = string describing a curve
276 * Returns: Null on success, or a pointer to an error message.
278 * Use: Parses out information about a curve. The string is either a
279 * standard curve name, or a curve info string.
282 const char *ec_getinfo(ec_info
*ei
, const char *p
)
289 for (ee
= ectab
; ee
->name
; ee
++) {
290 if (qd_enum(&qd
, ee
->name
) >= 0) {
291 getinfo(ei
, ee
->data
);
295 if (ec_infoparse(&qd
, ei
))
300 return ("junk found at end of string");
305 /* --- @ec_freeinfo@ --- *
307 * Arguments: @ec_info *ei@ = elliptic curve information block to free
311 * Use: Frees the information block.
314 void ec_freeinfo(ec_info
*ei
)
321 f
= ei
->c
->f
; ec_destroycurve(ei
->c
); F_DESTROY(f
);
324 /* --- @ec_checkinfo@ --- *
326 * Arguments: @const ec_info *ei@ = elliptic curve information block
328 * Returns: Null if OK, or pointer to error message.
330 * Use: Checks an elliptic curve according to the rules in SEC1.
333 static int primep(mp
*p
, grand
*gr
)
335 int i
= rabin_iters(mp_bits(p
));
339 switch (pfilt_smallfactor(p
)) {
340 case PGEN_DONE
: return (1);
341 case PGEN_FAIL
: return (0);
345 x
= mprand_range(x
, p
, gr
, 0);
346 if (rabin_rtest(&r
, x
) == PGEN_FAIL
)
355 static int primeeltp(mp
*x
, field
*f
)
357 return (!MP_ISNEG(x
) && MP_CMP(x
, <, f
->m
));
360 static const char *primecheck(const ec_info
*ei
, grand
*gr
)
369 /* --- Check %$p$% is an odd prime --- */
371 if (!primep(f
->m
, gr
)) return ("p not prime");
373 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
375 if (!primeeltp(c
->a
, f
)) return ("a out of range");
376 if (!primeeltp(c
->b
, f
)) return ("b out of range");
377 if (!primeeltp(ei
->g
.x
, f
)) return ("G_x out of range");
378 if (!primeeltp(ei
->g
.x
, f
)) return ("G_y out of range");
380 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
382 x
= F_SQR(f
, MP_NEW
, c
->a
);
383 x
= F_MUL(f
, x
, x
, c
->a
);
385 y
= F_SQR(f
, MP_NEW
, c
->b
);
389 x
= F_ADD(f
, x
, x
, y
);
393 if (rc
) return ("not an elliptic curve");
395 /* --- Check %$G \in E$% --- */
397 if (EC_ATINF(&ei
->g
)) return ("generator at infinity");
398 if (ec_check(c
, &ei
->g
)) return ("generator not on curve");
400 /* --- Check %$r$% is prime --- */
402 if (!primep(ei
->r
, gr
)) return ("generator order not prime");
404 /* --- Check %$0 < h \le 4$% --- */
406 if (MP_CMP(ei
->h
, <, MP_ONE
) || MP_CMP(ei
->h
, >, MP_FOUR
))
407 return ("cofactor out of range");
409 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
411 * This seems to work with the approximate-sqrt in the library, but might
412 * not be so good in some cases. Throw in some extra significate figures
416 x
= mp_lsl(MP_NEW
, f
->m
, 128);
418 y
= mp_lsl(MP_NEW
, MP_ONE
, 64);
421 mp_div(&x
, 0, x
, ei
->r
);
422 x
= mp_lsr(x
, x
, 128);
423 rc
= MP_EQ(x
, ei
->h
);
426 if (!rc
) return ("incorrect cofactor");
428 /* --- Check %$n G = O$% --- */
431 ec_mul(c
, &p
, &ei
->g
, ei
->r
);
434 if (!rc
) return ("incorrect group order");
436 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
438 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
442 mp_div(0, &x
, f
->m
, ei
->r
);
445 if (MP_EQ(x
, MP_ONE
)) break;
446 x
= mp_mul(x
, x
, f
->m
);
447 mp_div(0, &x
, x
, ei
->r
);
451 if (i
) return ("curve is weak");
458 static const char *bincheck(const ec_info
*ei
, grand
*gr
)
467 /* --- Check that %$p$% is irreducible --- */
469 if (!gf_irreduciblep(f
->m
)) return ("p not irreducible");
471 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
473 if (mp_bits(c
->a
) > f
->nbits
) return ("a out of range");
474 if (mp_bits(c
->b
) > f
->nbits
) return ("a out of range");
475 if (mp_bits(ei
->g
.x
) > f
->nbits
) return ("G_x out of range");
476 if (mp_bits(ei
->g
.y
) > f
->nbits
) return ("G_y out of range");
478 /* --- Check that %$b \ne 0$% --- */
480 if (F_ZEROP(f
, c
->b
)) return ("b is zero");
482 /* --- Check that %$G \in E$% --- */
484 if (EC_ATINF(&ei
->g
)) return ("generator at infinity");
485 if (ec_check(c
, &ei
->g
)) return ("generator not on curve");
487 /* --- Check %$r$% is prime --- */
489 if (!primep(ei
->r
, gr
)) return ("generator order not prime");
491 /* --- Check %$0 < h \le 4$% --- */
493 if (MP_CMP(ei
->h
, <, MP_ONE
) || MP_CMP(ei
->h
, >, MP_FOUR
))
494 return ("cofactor out of range");
496 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
498 * This seems to work with the approximate-sqrt in the library, but might
499 * not be so good in some cases. Throw in some extra significate figures
503 x
= mp_lsl(MP_NEW
, MP_ONE
, f
->nbits
+ 128);
505 y
= mp_lsl(MP_NEW
, MP_ONE
, 64);
508 mp_div(&x
, 0, x
, ei
->r
);
509 x
= mp_lsr(x
, x
, 128);
510 rc
= MP_EQ(x
, ei
->h
);
513 if (!rc
) return ("incorrect cofactor");
515 /* --- Check %$n G = O$% --- */
518 ec_mul(c
, &p
, &ei
->g
, ei
->r
);
521 if (!rc
) return ("incorrect group order");
523 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
525 x
= mp_lsl(MP_NEW
, MP_ONE
, f
->nbits
);
526 mp_div(0, &x
, x
, ei
->r
);
529 if (MP_EQ(x
, MP_ONE
)) break;
530 x
= mp_mul(x
, x
, f
->m
);
531 mp_div(0, &x
, x
, ei
->r
);
535 if (i
) return ("curve is weak");
542 const char *ec_checkinfo(const ec_info
*ei
, grand
*gr
)
544 switch (F_TYPE(ei
->c
->f
)) {
545 case FTY_PRIME
: return (primecheck(ei
, gr
)); break;
546 case FTY_BINARY
: return (bincheck(ei
, gr
)); break;
548 return ("unknown curve type");
551 /*----- Test rig ----------------------------------------------------------*/
564 gr
= fibrand_create(0);
565 fputs("checking standard curves: ", stdout
);
566 for (ee
= ectab
; ee
->name
; ee
++) {
568 getinfo(&ei
, ee
->data
);
569 e
= ec_checkinfo(&ei
, gr
);
572 fprintf(stderr
, "\n*** curve %s fails: %s\n", ee
->name
, e
);
578 gr
->ops
->destroy(gr
);
579 fputs(ok ?
" ok\n" : " failed\n", stdout
);
585 /*----- That's all, folks -------------------------------------------------*/