3 * Elliptic curve information management
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/darray.h>
36 #include "mpbarrett.h"
38 #include "primeiter.h"
43 /*----- Embedding degree checking -----------------------------------------*
45 * Let %$q = p^m$% be a prime power, and let %$E$% be an elliptic curve over
46 * %$\gf{q}$% with %$n = \#E(\gf{q}) = r h$% where %$r$% is prime. Then the
47 * Weil and Tate pairings can be used to map %$r$%-torsion points on
48 * %$E(\gf{q})$% onto the %$r$%-th roots of unity (i.e., the order-%$r$%
49 * subgroup) in an extension field %$\gf{p^k}$% of %$\gf{p}$% (%%\emph{not}%%
50 * of %$\gf{q}$% -- see [Hitt]). We call the smallest such %$k$% the
51 * %%\emph{embedding degree}%% of the curve %$E$%. The
52 * Menezes-Okamoto-Vanstone (MOV) attack solves the discrete log problem in
53 * %$E(\gf{q})$% by using the pairing and then applying index calculus to
54 * extract a discrete log in %$\gf{p^k}$%; obviously this only works if %$k$%
57 * The usual check, suggested in, e.g., [P1363] or [SEC1], only covers
58 * extension fields %$\gf{q^\ell}$% of %$\gf{q}$%, which is fine when %$q$%
59 * is prime, but when we're dealing with binary fields it works less well.
60 * Indeed, as [Hitt] demonstrates, the embedding field can actually be
61 * %%\emph{smaller}%% than %$\gf{q}$%, and choosing %$m$% prime doesn't help
62 * (even though I previously thought it did).
64 * Define the %%\emph{embedding degree bound}%% %$B$% to be the smallest
65 * %$i$% such that discrete logs in %$\gf{p^i}$% are about as hard as in
68 * The embedding group is a subgroup of the multiplicative group
69 * %$\gf{p^k}^*$% which contains %$p^k - 1$% elements; therefore we must have
70 * %$r \mid p^k - 1$%, or, equivalently, %$p^k \equiv 1 \pmod{r}$%.
72 * The recommended checking procedure, e.g., in [P1363], is just to check
73 * %$q^i \not\equiv 1 \pmod{r}$% for each %$0 < i < B$%. This is fast when
74 * you only consider extension fields of %$\gf{q}$%, since %$B$% is at most
75 * about 27. However, as noted above, this is inadequate when %$q$% is a
76 * prime power, and we must check all the extension fields of %$p$%. Now
77 * %$B$% can be about 15000, which is rather scarier -- we need a better
80 * As noted, we must have %$p^k \equiv 1 \pmod{r}$%; but by minimality of
81 * %$k$%, we must have %$p^i \not\equiv 1 \pmod{r}$% for %$0 < i < k$%.
82 * Therefore %$p$% generates an order-%$k$% subgroup in %$\gf{r}^*$%, so we
83 * must have %$k \mid r - 1$%.
85 * Of course, factoring %$r - 1$% is a mug's game; but we're not interested
86 * in the complete factorization -- just the %$B$%-smooth portion. An
87 * algorithm suggests itself:
89 * 1. Extract the factors of %$r - 1$% which are less than %$B$%.
91 * 2. For each divisor %$d$% of %$r - 1$% less than %$B$% (which we can
92 * construct using this factorization), make sure that
93 * %$p^d \not\equiv 1 \pmod{r}$%.
95 * This takes a little while but not ever-so long.
97 * This is enough for cryptosystems based on the computational Diffie-
98 * Hellman problem to be secure. However, it's %%\emph{not}%% enough for the
99 * %%\emph{decisional}%% Diffie-Hellman problem to be hard; it appears we
100 * also need to hope that there aren't any suitable distortion maps with
101 * which one can solve the DDH problem. I don't know how to check for those
104 * We'll take the subgroup order as indicative of the security level actually
105 * wanted. Then, to ensure security against the MOV attack, we must ensure
106 * that the embedding degree is sufficiently large that discrete logs in
107 * %$\gf{q^m}$% are at least as hard as discrete logs over the curve.
109 * We actually allow a small amount of slop in the conversions, in order to
110 * let people pick nice round numbers for their key lengths.
114 * [Hitt] L. Hitt, On an improved definition of embedding degree;
115 * http://eprint.iacr.org/2006/415
117 * [P1363] IEEE 1363-2000: Standard Specifications for Public Key
118 * Cryptography; http://grouper.ieee.org/groups/1363/P1363/index.html
120 * [SEC1] SEC 1: Elliptic Curve Cryptography;
121 * http://www.secg.org/download/aid-385/sec1_final.pdf
124 /* --- @movcheck@ --- *
126 * Arguments: @mp *r@ = curve subgroup order
127 * @mp *p@ = field characteristic
128 * @unsigned long B@ = embedding degree bound
130 * Returns: Zero if OK, nonzero if an embedding was found.
132 * Use: Checks a curve for embeddings with degree less than the
133 * stated bound %$B$%. See above for explanation and a
134 * description of the algorithm.
137 static int movcheck(mp
*r
, mp
*p
, unsigned long B
)
140 mp
*r1
, *pp
= MP_NEW
, *t
= MP_NEW
, *u
= MP_NEW
, *v
= MP_NEW
, *tt
;
145 DA_DECL(factor_v
, struct factor
);
146 factor_v fv
= DA_INIT
;
155 /* --- Special case --- *
157 * If %$r = 2$% then (a) Montgomery reduction won't work, and (b) we have
158 * no security worth checking anyway. Otherwise we're guaranteed that
159 * %$r$% is a prime, so it must be odd.
162 if (MP_EQ(r
, MP_TWO
))
165 /* --- First factor the %$B%-smooth portion of %$r - 1$% --- *
167 * We can generate prime numbers up to %$B$% efficiently, so trial division
171 BB
= mp_fromulong(MP_NEW
, B
);
172 r1
= mp_sub(MP_NEW
, r
, MP_ONE
);
173 primeiter_create(&pi
, 0);
175 pp
= primeiter_next(&pi
, pp
);
176 if (MP_CMP(pp
, >, BB
))
178 mp_div(&u
, &v
, r1
, pp
);
183 tt
= r1
; r1
= u
; u
= tt
; i
++;
184 mp_div(&u
, &v
, r1
, pp
);
185 } while (MP_ZEROP(v
));
187 DA_UNSAFE_EXTEND(&fv
, 1);
188 DA_LAST(&fv
).f
= mp_toulong(pp
);
192 MP_DROP(BB
); MP_DROP(pp
); primeiter_destroy(&pi
);
193 nf
= DA_LEN(&fv
); ff
= DA(&fv
);
195 /* --- Now generate divisors of %$r - 1$% less than %$B$% --- *
197 * For each divisor %$d$%, check whether %$p^d \equiv 1 \pmod{r}$%.
200 mpmont_create(&mm
, r
);
201 u
= mpmont_mul(&mm
, u
, p
, mm
.r2
);
204 /* --- Construct the divisor --- */
207 for (i
= 0; i
< nf
; i
++) {
208 f
= ff
[i
].f
; j
= ff
[i
].c
; if (!j
) continue;
210 if (f
>= (B
+ d
- 1)/d
) goto toobig
;
212 j
>>= 1; if (!j
) break;
216 v
= mp_fromulong(v
, d
);
218 /* --- Compute %$p^k \bmod r$% and check --- */
220 t
= mpmont_expr(&mm
, t
, u
, v
);
221 if (MP_EQ(t
, mm
.r
)) {
226 /* --- Step the divisors along --- */
229 for (i
= 0; i
< nf
; i
++) {
230 if (ff
[i
].c
< ff
[i
].e
) {
240 /* --- Clear away the debris --- */
243 MP_DROP(t
); MP_DROP(u
); MP_DROP(v
); MP_DROP(r1
);
248 /*----- Main code ---------------------------------------------------------*/
250 /* --- @ec_curveparse@ --- *
252 * Arguments: @qd_parse *qd@ = parser context
254 * Returns: Elliptic curve pointer if OK, or null.
256 * Use: Parses an elliptic curve description, which has the form
258 * * a field description
260 * * `prime', `primeproj', `bin', or `binproj'
262 * * the %$a$% parameter
264 * * the %$b$% parameter
267 ec_curve
*ec_curveparse(qd_parse
*qd
)
269 mp
*a
= MP_NEW
, *b
= MP_NEW
;
273 if ((f
= field_parse(qd
)) == 0) goto fail
;
275 switch (qd_enum(qd
, "prime,primeproj,bin,binproj")) {
277 if (F_TYPE(f
) != FTY_PRIME
) {
278 qd
->e
= "field not prime";
282 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
284 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
285 c
= ec_prime(f
, a
, b
);
288 if (F_TYPE(f
) != FTY_PRIME
) {
289 qd
->e
= "field not prime";
293 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
295 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
296 c
= ec_primeproj(f
, a
, b
);
299 if (F_TYPE(f
) != FTY_BINARY
) {
300 qd
->e
= "field not binary";
304 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
306 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
310 if (F_TYPE(f
) != FTY_BINARY
) {
311 qd
->e
= "field not binary";
315 if ((a
= qd_getmp(qd
)) == 0) goto fail
;
317 if ((b
= qd_getmp(qd
)) == 0) goto fail
;
318 c
= ec_binproj(f
, a
, b
);
324 qd
->e
= "bad curve parameters";
338 /* --- @ec_ptparse@ --- *
340 * Arguments: @qd_parse *qd@ = parser context
341 * @ec *p@ = where to put the point
343 * Returns: The point address, or null.
345 * Use: Parses an elliptic curve point. This has the form
352 ec
*ec_ptparse(qd_parse
*qd
, ec
*p
)
354 mp
*x
= MP_NEW
, *y
= MP_NEW
;
356 if (qd_enum(qd
, "inf") >= 0) {
360 if ((x
= qd_getmp(qd
)) == 0) goto fail
;
362 if ((y
= qd_getmp(qd
)) == 0) goto fail
;
375 /* --- @ec_infofromdata@ --- *
377 * Arguments: @ec_info *ei@ = where to write the information
378 * @ecdata *ed@ = raw data
382 * Use: Loads elliptic curve information about one of the standard
386 void ec_infofromdata(ec_info
*ei
, ecdata
*ed
)
392 f
= field_prime(&ed
->p
);
393 ei
->c
= ec_primeproj(f
, &ed
->a
, &ed
->b
);
396 f
= field_niceprime(&ed
->p
);
397 ei
->c
= ec_primeproj(f
, &ed
->a
, &ed
->b
);
400 f
= field_binpoly(&ed
->p
);
401 ei
->c
= ec_binproj(f
, &ed
->a
, &ed
->b
);
404 f
= field_binnorm(&ed
->p
, &ed
->beta
);
405 ei
->c
= ec_binproj(f
, &ed
->a
, &ed
->b
);
411 assert(f
); assert(ei
->c
);
412 EC_CREATE(&ei
->g
); ei
->g
.x
= &ed
->gx
; ei
->g
.y
= &ed
->gy
; ei
->g
.z
= 0;
413 ei
->r
= &ed
->r
; ei
->h
= &ed
->h
;
416 /* --- @ec_infoparse@ --- *
418 * Arguments: @qd_parse *qd@ = parser context
419 * @ec_info *ei@ = curve information block, currently
422 * Returns: Zero on success, nonzero on failure.
424 * Use: Parses an elliptic curve information string, and stores the
425 * information in @ei@. This is either the name of a standard
426 * curve, or it has the form
428 * * elliptic curve description
437 int ec_infoparse(qd_parse
*qd
, ec_info
*ei
)
443 mp
*r
= MP_NEW
, *h
= MP_NEW
;
445 for (ee
= ectab
; ee
->name
; ee
++) {
446 if (qd_enum(qd
, ee
->name
) >= 0) {
447 ec_infofromdata(ei
, ee
->data
);
452 if ((c
= ec_curveparse(qd
)) == 0) goto fail
;
453 qd_delim(qd
, ';'); if (!ec_ptparse(qd
, &g
)) goto fail
;
454 qd_delim(qd
, ':'); if ((r
= qd_getmp(qd
)) == 0) goto fail
;
455 qd_delim(qd
, '*'); if ((h
= qd_getmp(qd
)) == 0) goto fail
;
456 ei
->c
= c
; ei
->g
= g
; ei
->r
= r
; ei
->h
= h
;
465 if (c
) { f
= c
->f
; ec_destroycurve(c
); F_DESTROY(f
); }
469 /* --- @ec_getinfo@ --- *
471 * Arguments: @ec_info *ei@ = where to write the information
472 * @const char *p@ = string describing a curve
474 * Returns: Null on success, or a pointer to an error message.
476 * Use: Parses out information about a curve. The string is either a
477 * standard curve name, or a curve info string.
480 const char *ec_getinfo(ec_info
*ei
, const char *p
)
486 if (ec_infoparse(&qd
, ei
))
490 return ("junk found at end of string");
495 /* --- @ec_sameinfop@ --- *
497 * Arguments: @const ec_info *ei, *ej@ = two elliptic curve parameter sets
499 * Returns: Nonzero if the curves are identical (not just isomorphic).
501 * Use: Checks for sameness of curve parameters.
504 int ec_sameinfop(const ec_info
*ei
, const ec_info
*ej
)
506 return (ec_samep(ei
->c
, ej
->c
) &&
507 MP_EQ(ei
->r
, ej
->r
) && MP_EQ(ei
->h
, ej
->h
) &&
508 EC_EQ(&ei
->g
, &ej
->g
));
511 /* --- @ec_freeinfo@ --- *
513 * Arguments: @ec_info *ei@ = elliptic curve information block to free
517 * Use: Frees the information block.
520 void ec_freeinfo(ec_info
*ei
)
527 f
= ei
->c
->f
; ec_destroycurve(ei
->c
); F_DESTROY(f
);
530 /* --- @ec_checkinfo@ --- *
532 * Arguments: @const ec_info *ei@ = elliptic curve information block
534 * Returns: Null if OK, or pointer to error message.
536 * Use: Checks an elliptic curve according to the rules in SEC1.
539 static const char *gencheck(const ec_info
*ei
, grand
*gr
, mp
*q
, mp
*ch
)
542 unsigned long qmbits
, rbits
, cbits
, B
;
549 /* --- Check curve isn't anomalous --- */
551 if (MP_EQ(ei
->r
, q
)) return ("curve is anomalous");
553 /* --- Check %$G \in E \setminus \{ 0 \}$% --- */
555 if (EC_ATINF(&ei
->g
)) return ("generator at infinity");
556 if (ec_check(c
, &ei
->g
)) return ("generator not on curve");
558 /* --- Check %$r$% is prime --- */
560 if (!pgen_primep(ei
->r
, gr
)) return ("generator order not prime");
562 /* --- Check that the cofactor is correct --- *
564 * Let %$q$% be the size of the field, and let %$n = h r = \#E(\gf{q})$% be
565 * the number of %$\gf{q}$%-rational points on our curve. Hasse's theorem
568 * %$|q + 1 - n| \le 2\sqrt{q}$%
570 * or, if we square both sides,
572 * %$(q + 1 - n)^2 \le 4 q$%.
574 * We'd like the cofactor to be uniquely determined by this equation, which
575 * is possible as long as it's not too big. (If it is, we have to mess
576 * about with Weil pairings, which is no fun.) For this, we need the
577 * following inequalities:
579 * * %$A = (q + 1 - n)^2 \le 4 q$% (both lower and upper bounds from
582 * * %$B = (q + 1 - n - r)^2 > 4 q$% (check %$h - 1$% isn't possible);
585 * * %$C = (q + 1 - n + r)^2 > 4 q$% (check %$h + 1$% isn't possible).
589 qq
= mp_add(MP_NEW
, q
, MP_ONE
);
590 nn
= mp_mul(MP_NEW
, ei
->r
, ei
->h
);
591 nn
= mp_sub(nn
, qq
, nn
);
592 qq
= mp_lsl(qq
, q
, 2);
594 y
= mp_sqr(MP_NEW
, nn
);
595 if (MP_CMP(y
, >, qq
)) rc
= 0;
597 x
= mp_sub(MP_NEW
, nn
, ei
->r
);
599 if (MP_CMP(y
, <=, qq
)) rc
= 0;
601 x
= mp_add(x
, nn
, ei
->r
);
603 if (MP_CMP(y
, <=, qq
)) rc
= 0;
609 if (!rc
) return ("incorrect or ambiguous cofactor");
611 /* --- Check %$n G = 0$% --- */
614 ec_mul(c
, &p
, &ei
->g
, ei
->r
);
617 if (!rc
) return ("incorrect group order");
619 /* --- Check the embedding degree --- */
621 rbits
= mp_bits(ei
->r
);
623 qmbits
= keysz_todl(keysz_fromec(rbits
* 7/8));
624 B
= (qmbits
+ cbits
- 1)/cbits
;
625 if (movcheck(ei
->r
, ch
, B
))
626 return("curve embedding degree too low");
633 static int primeeltp(mp
*x
, field
*f
)
634 { return (!MP_NEGP(x
) && MP_CMP(x
, <, f
->m
)); }
636 static const char *primecheck(const ec_info
*ei
, grand
*gr
)
644 /* --- Check %$p$% is an odd prime --- */
646 if (!pgen_primep(f
->m
, gr
)) return ("p not prime");
648 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
650 if (!primeeltp(c
->a
, f
)) return ("a out of range");
651 if (!primeeltp(c
->b
, f
)) return ("b out of range");
652 if (!primeeltp(ei
->g
.x
, f
)) return ("G_x out of range");
653 if (!primeeltp(ei
->g
.x
, f
)) return ("G_y out of range");
655 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
657 x
= F_SQR(f
, MP_NEW
, c
->a
);
658 x
= F_MUL(f
, x
, x
, c
->a
);
660 y
= F_SQR(f
, MP_NEW
, c
->b
);
664 x
= F_ADD(f
, x
, x
, y
);
668 if (rc
) return ("not an elliptic curve");
670 /* --- Now do the general checks --- */
672 err
= gencheck(ei
, gr
, f
->m
, f
->m
);
676 static const char *bincheck(const ec_info
*ei
, grand
*gr
)
684 /* --- Check that %$m$% is prime --- */
686 x
= mp_fromuint(MP_NEW
, f
->nbits
);
687 rc
= pfilt_smallfactor(x
);
689 if (rc
!= PGEN_DONE
) return ("degree not prime");
691 /* --- Check that %$p$% is irreducible --- */
693 if (!gf_irreduciblep(f
->m
)) return ("p not irreducible");
695 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
697 if (mp_bits(c
->a
) > f
->nbits
) return ("a out of range");
698 if (mp_bits(c
->b
) > f
->nbits
) return ("a out of range");
699 if (mp_bits(ei
->g
.x
) > f
->nbits
) return ("G_x out of range");
700 if (mp_bits(ei
->g
.y
) > f
->nbits
) return ("G_y out of range");
702 /* --- Check that %$b \ne 0$% --- */
704 if (F_ZEROP(f
, c
->b
)) return ("b is zero");
706 /* --- Now do the general checks --- */
708 x
= mp_lsl(MP_NEW
, MP_ONE
, f
->nbits
);
709 err
= gencheck(ei
, gr
, x
, MP_TWO
);
714 const char *ec_checkinfo(const ec_info
*ei
, grand
*gr
)
716 switch (F_TYPE(ei
->c
->f
)) {
717 case FTY_PRIME
: return (primecheck(ei
, gr
)); break;
718 case FTY_BINARY
: return (bincheck(ei
, gr
)); break;
720 return ("unknown curve type");
723 /*----- Test rig ----------------------------------------------------------*/
729 int main(int argc
, char *argv
[])
737 gr
= fibrand_create(0);
739 for (i
= 1; i
< argc
; i
++) {
741 if ((e
= ec_getinfo(&ei
, argv
[i
])) != 0)
742 fprintf(stderr
, "bad curve spec `%s': %s\n", argv
[i
], e
);
744 e
= ec_checkinfo(&ei
, gr
);
747 printf("OK %s\n", argv
[i
]);
749 printf("BAD %s: %s\n", argv
[i
], e
);
753 assert(mparena_count(MPARENA_GLOBAL
) == 0);
756 fputs("checking standard curves:", stdout
);
758 for (ee
= ectab
; ee
->name
; ee
++) {
760 ec_infofromdata(&ei
, ee
->data
);
761 e
= ec_checkinfo(&ei
, gr
);
764 printf(" [%s fails: %s]", ee
->name
, e
);
767 printf(" %s", ee
->name
);
769 assert(mparena_count(MPARENA_GLOBAL
) == 0);
771 fputs(ok ?
" ok\n" : " failed\n", stdout
);
773 gr
->ops
->destroy(gr
);
779 /*----- That's all, folks -------------------------------------------------*/