Generate, store and retreive elliptic curve keys.
[u/mdw/catacomb] / ec.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
432c4e18 3 * $Id: ec.h,v 1.8 2004/03/27 17:54:11 mdw Exp $
b0ab12e6 4 *
5 * Elliptic curve definitions
6 *
7 * (c) 2001 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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: ec.h,v $
432c4e18 33 * Revision 1.8 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
35 *
bc985cef 36 * Revision 1.7 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
38 *
391faf42 39 * Revision 1.6 2004/03/22 02:19:10 mdw
40 * Rationalise the sliding-window threshold. Drop guarantee that right
41 * arguments to EC @add@ are canonical, and fix up projective implementations
42 * to cope.
43 *
c3caa2fa 44 * Revision 1.5 2004/03/21 22:52:06 mdw
45 * Merge and close elliptic curve branch.
46 *
ceb3f0c0 47 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
48 * Elliptic curves on binary fields work.
49 *
8823192f 50 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
51 * Projective coordinates for prime curves
52 *
dbfee00a 53 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
54 * Simple (non-projective) curves over prime fields now seem to work.
55 *
41cb1beb 56 * Revision 1.4 2003/05/15 23:25:59 mdw
57 * Make elliptic curve stuff build.
58 *
b085fd91 59 * Revision 1.3 2002/01/13 13:48:44 mdw
60 * Further progress.
61 *
41a324a7 62 * Revision 1.2 2001/05/07 17:29:44 mdw
63 * Treat projective coordinates as an internal representation. Various
64 * minor interface changes.
65 *
b0ab12e6 66 * Revision 1.1 2001/04/29 18:12:33 mdw
67 * Prototype version.
68 *
69 */
70
71#ifndef CATACOMB_EC_H
72#define CATACOMB_EC_H
73
74#ifdef __cplusplus
75 extern "C" {
76#endif
77
78/*----- Header files ------------------------------------------------------*/
79
432c4e18 80#ifndef CATACOMB_FIELD_H
81# include "field.h"
82#endif
83
84#ifndef CATACOMB_MP_H
85# include "mp.h"
86#endif
87
88#ifndef CATACOMB_QDPARSE_H
89# include "qdparse.h"
90#endif
b0ab12e6 91
92/*----- Data structures ---------------------------------------------------*/
93
b085fd91 94/* --- An elliptic curve representation --- */
95
b0ab12e6 96typedef struct ec_curve {
97 const struct ec_ops *ops; /* Curve operations */
98 field *f; /* Underlying field structure */
432c4e18 99 mp *a, *b; /* Standard params (internal form) */
b0ab12e6 100} ec_curve;
101
b085fd91 102/* --- An elliptic curve point --- */
103
b0ab12e6 104typedef struct ec {
105 mp *x, *y; /* Point coordinates */
106 mp *z; /* Common denominator (or null) */
107} ec;
108
b085fd91 109/* --- A factor for simultaneous multiplication --- */
110
111typedef struct ec_mulfactor {
112 ec base; /* The point */
41cb1beb 113 mp *exp; /* The exponent */
b085fd91 114} ec_mulfactor;
115
8823192f 116/* --- Elliptic curve operations --- *
117 *
118 * All operations (apart from @destroy@ and @in@) are guaranteed to be
391faf42 119 * performed on internal representations of points.
120 *
121 * (Historical note. We used to guarantee that the second to @add@ and @mul@
122 * was the output of @in@ or @fix@, but this canonification turned out to
123 * make the precomputation in @ec_exp@ too slow. Projective implementations
124 * must therefore cope with a pair of arbitrary points.)
8823192f 125 */
b085fd91 126
b0ab12e6 127typedef struct ec_ops {
128 void (*destroy)(ec_curve */*c*/);
41a324a7 129 ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
130 ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 131 ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 132 ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
b085fd91 133 ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 134 ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
b085fd91 135 ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
41a324a7 136 ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 137 int (*check)(ec_curve */*c*/, const ec */*p*/);
b0ab12e6 138} ec_ops;
139
41a324a7 140#define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
dbfee00a 141#define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
8823192f 142#define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
41a324a7 143
b0ab12e6 144#define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
b085fd91 145#define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
b0ab12e6 146#define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
b085fd91 147#define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
b0ab12e6 148#define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
8823192f 149#define EC_CHECK(c, p) (c)->ops->check((c), (p))
b0ab12e6 150
432c4e18 151/* --- Elliptic curve parameters --- */
152
153typedef struct ec_info {
154 ec_curve *c; /* The actual curve */
155 ec g; /* The common point */
156 mp *r; /* Order of %$g$% */
157 mp *h; /* Cofactor %$h = \#E/r$% */
158} ec_info;
159
b0ab12e6 160/*----- Simple memory management things -----------------------------------*/
161
162/* --- @ec_create@ --- *
163 *
164 * Arguments: @ec *p@ = pointer to an elliptic-curve point
165 *
41a324a7 166 * Returns: The argument @p@.
b0ab12e6 167 *
168 * Use: Initializes a new point. The initial value is the additive
169 * identity (which is universal for all curves).
170 */
171
172#define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
173
174#define EC_CREATE(p) do { \
175 ec *_p = (p); \
176 _p->x = _p->y = _p->z = MP_NEW; \
177} while (0)
178
41a324a7 179extern ec *ec_create(ec */*p*/);
b0ab12e6 180
181/* --- @ec_destroy@ --- *
182 *
183 * Arguments: @ec *p@ = pointer to an elliptic-curve point
184 *
185 * Returns: ---
186 *
187 * Use: Destroys a point, making it invalid.
188 */
189
190#define EC_DESTROY(p) do { \
191 ec *_p = (p); \
192 if (!EC_ATINF(_p)) { \
193 MP_DROP(_p->x); \
194 MP_DROP(_p->y); \
195 if (_p->z) MP_DROP(_p->z); \
196 } \
197} while (0)
198
199extern void ec_destroy(ec */*p*/);
200
201/* --- @ec_atinf@ --- *
202 *
203 * Arguments: @const ec *p@ = pointer to a point
204 *
205 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
206 * otherwise.
207 */
208
209#define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
210
211extern int ec_atinf(const ec */*p*/);
212
213/* --- @ec_setinf@ --- *
214 *
215 * Arguments: @ec *p@ = pointer to a point
216 *
41a324a7 217 * Returns: The argument @p@.
b0ab12e6 218 *
219 * Use: Sets the given point to be the point %$O$% at infinity.
220 */
221
222#define EC_SETINF(p) do { \
223 ec *_p = (p); \
224 if (!EC_ATINF(_p)) { \
225 MP_DROP(_p->x); \
226 MP_DROP(_p->y); \
227 if (_p->z) MP_DROP(_p->z); \
228 _p->x = _p->y = _p->z = MP_NEW; \
229 _p->y = MP_NEW; \
230 _p->z = MP_NEW; \
231 } \
232} while (0)
233
41a324a7 234extern ec *ec_setinf(ec */*p*/);
b0ab12e6 235
236/* --- @ec_copy@ --- *
237 *
238 * Arguments: @ec *d@ = pointer to destination point
239 * @const ec *p@ = pointer to source point
240 *
41a324a7 241 * Returns: The destination @d@.
b0ab12e6 242 *
243 * Use: Creates a copy of an elliptic curve point.
244 */
245
246#define EC_COPY(d, p) do { \
247 ec *_d = (d); \
248 const ec *_p = (p); \
249 if (d != p) { \
250 EC_DESTROY(d); \
251 if (EC_ATINF(p)) \
252 _d->x = _d->y = _d->z = MP_NEW; \
253 else { \
b085fd91 254 _d->x = MP_COPY(_p->x); \
255 _d->y = MP_COPY(_p->y); \
256 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
b0ab12e6 257 } \
258 } \
259} while (0)
260
41a324a7 261extern ec *ec_copy(ec */*d*/, const ec */*p*/);
b0ab12e6 262
bc985cef 263/* --- @ec_eq@ --- *
264 *
265 * Arguments: @const ec *p, *q@ = two points
266 *
267 * Returns: Nonzero if the points are equal. Compares external-format
268 * points.
269 */
270
271#define EC_EQ(p, q) \
272 ((EC_ATINF(p) && EC_ATINF(q)) || \
273 (!EC_ATINF(p) && !EC_ATINF(q) && \
274 MP_EQ((p)->x, (q)->x) && \
275 MP_EQ((p)->y, (q)->y)))
276
277extern int ec_eq(const ec *p, const ec *q);
278
b0ab12e6 279/*----- Interesting arithmetic --------------------------------------------*/
280
b0ab12e6 281/* --- @ec_find@ --- *
282 *
283 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
284 * @ec *d@ = pointer to the destination point
285 * @mp *x@ = a possible x-coordinate
286 *
b085fd91 287 * Returns: The destination if OK, or null if no point was found.
b0ab12e6 288 *
b085fd91 289 * Use: Finds a point on an elliptic curve with a given
290 * x-coordinate. If there is no point with the given
291 * %$x$%-coordinate, a null pointer is returned and the
292 * destination is left invalid.
b0ab12e6 293 */
294
b085fd91 295extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
296
bc985cef 297/* --- @ec_rand@ --- *
298 *
299 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
300 * @ec *d@ = pointer to the destination point
301 * @grand *r@ = random number source
302 *
303 * Returns: The destination @d@.
304 *
305 * Use: Finds a random point on the given curve.
306 */
307
308extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
309
b085fd91 310/* --- @ec_neg@ --- *
311 *
312 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
313 * @ec *d@ = pointer to the destination point
314 * @const ec *p@ = pointer to the operand point
315 *
316 * Returns: The destination point.
317 *
318 * Use: Computes the negation of the given point.
319 */
320
321extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 322
323/* --- @ec_add@ --- *
324 *
325 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
326 * @ec *d@ = pointer to the destination point
327 * @const ec *p, *q@ = pointers to the operand points
328 *
41a324a7 329 * Returns: The destination @d@.
b0ab12e6 330 *
331 * Use: Adds two points on an elliptic curve.
332 */
333
41a324a7 334extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
335 const ec */*p*/, const ec */*q*/);
b0ab12e6 336
b085fd91 337/* --- @ec_sub@ --- *
338 *
339 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
340 * @ec *d@ = pointer to the destination point
341 * @const ec *p, *q@ = pointers to the operand points
342 *
343 * Returns: The destination @d@.
344 *
345 * Use: Subtracts one point from another on an elliptic curve.
346 */
347
348extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
349 const ec */*p*/, const ec */*q*/);
350
b0ab12e6 351/* --- @ec_dbl@ --- *
352 *
353 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
354 * @ec *d@ = pointer to the destination point
355 * @const ec *p@ = pointer to the operand point
356 *
41a324a7 357 * Returns: The destination @d@.
b0ab12e6 358 *
359 * Use: Doubles a point on an elliptic curve.
360 */
361
41a324a7 362extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 363
8823192f 364/* --- @ec_check@ --- *
365 *
366 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
367 * @const ec *p@ = pointer to the point
368 *
369 * Returns: Zero if OK, nonzero if this is an invalid point.
370 *
371 * Use: Checks that a point is actually on an elliptic curve.
372 */
373
374extern int ec_check(ec_curve */*c*/, const ec */*p*/);
375
b085fd91 376/* --- @ec_mul@, @ec_imul@ --- *
b0ab12e6 377 *
378 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
379 * @ec *d@ = pointer to the destination point
380 * @const ec *p@ = pointer to the generator point
381 * @mp *n@ = integer multiplier
382 *
41a324a7 383 * Returns: The destination @d@.
b0ab12e6 384 *
b085fd91 385 * Use: Multiplies a point by a scalar, returning %$n p$%. The
386 * @imul@ variant uses internal representations for argument
387 * and result.
b0ab12e6 388 */
389
41a324a7 390extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
b085fd91 391extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
392
393/* --- @ec_mmul@, @ec_immul@ --- *
394 *
395 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
396 * @ec *d@ = pointer to the destination point
397 * @const ec_mulfactor *f@ = pointer to vector of factors
398 * @size_t n@ = number of factors
399 *
400 * Returns: The destination @d@.
401 *
402 * Use: Does simultaneous point multiplication. The @immul@ variant
403 * uses internal representations for arguments and result.
404 */
405
406extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/,
407 const ec_mulfactor */*f*/, size_t /*n*/);
408extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
409 const ec_mulfactor */*f*/, size_t /*n*/);
41a324a7 410
411/*----- Standard curve operations -----------------------------------------*/
412
8823192f 413/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
41a324a7 414 *
415 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
416 * @ec *d@ = pointer to the destination
417 * @const ec *p@ = pointer to a source point
418 *
419 * Returns: The destination @d@.
420 *
421 * Use: An identity operation if your curve has no internal
422 * representation. (The field internal representation is still
423 * used.)
424 */
425
426extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
427extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 428extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 429
8823192f 430/* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
41a324a7 431 *
432 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
433 * @ec *d@ = pointer to the destination
434 * @const ec *p@ = pointer to a source point
435 *
436 * Returns: The destination @d@.
437 *
438 * Use: Conversion functions if your curve operations use a
439 * projective representation.
440 */
441
442extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
443extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 444extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 445
b085fd91 446/* --- @ec_stdsub@ --- *
447 *
448 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
449 * @ec *d@ = pointer to the destination
41cb1beb 450 * @const ec *p, *q@ = the operand points
b085fd91 451 *
452 * Returns: The destination @d@.
453 *
454 * Use: Standard point subtraction operation, in terms of negation
455 * and addition. This isn't as efficient as a ready-made
456 * subtraction operator.
457 */
458
41cb1beb 459extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
460 const ec */*p*/, const ec */*q*/);
b085fd91 461
b0ab12e6 462/*----- Creating curves ---------------------------------------------------*/
463
b085fd91 464/* --- @ec_destroycurve@ --- *
465 *
466 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
467 *
468 * Returns: ---
469 *
470 * Use: Destroys a description of an elliptic curve.
471 */
472
473extern void ec_destroycurve(ec_curve */*c*/);
474
475/* --- @ec_prime@, @ec_primeproj@ --- *
b0ab12e6 476 *
dbfee00a 477 * Arguments: @field *f@ = the underlying field for this elliptic curve
b0ab12e6 478 * @mp *a, *b@ = the coefficients for this curve
479 *
480 * Returns: A pointer to the curve.
481 *
482 * Use: Creates a curve structure for an elliptic curve defined over
b085fd91 483 * a prime field. The @primeproj@ variant uses projective
484 * coordinates, which can be a win.
b0ab12e6 485 */
486
487extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
b085fd91 488extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 489
ceb3f0c0 490/* --- @ec_bin@, @ec_binproj@ --- *
b0ab12e6 491 *
492 * Arguments: @field *f@ = the underlying field for this elliptic curve
493 * @mp *a, *b@ = the coefficients for this curve
494 *
495 * Returns: A pointer to the curve.
496 *
ceb3f0c0 497 * Use: Creates a curve structure for an elliptic curve defined over
498 * a binary field. The @binproj@ variant uses projective
499 * coordinates, which can be a win.
b0ab12e6 500 */
501
502extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
ceb3f0c0 503extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 504
432c4e18 505/*----- Curve parameter sets ----------------------------------------------*/
506
507/* --- @ec_curveparse@ --- *
508 *
509 * Arguments: @qd_parse *qd@ = parser context
510 *
511 * Returns: Elliptic curve pointer if OK, or null.
512 *
513 * Use: Parses an elliptic curve description, which has the form
514 *
515 * * a field description
516 * * an optional `/'
517 * * `prime', `primeproj', `bin', or `binproj'
518 * * an optional `:'
519 * * the %$a$% parameter
520 * * an optional `,'
521 * * the %$b$% parameter
522 */
523
524extern ec_curve *ec_curveparse(qd_parse */*qd*/);
525
526/* --- @ec_ptparse@ --- *
527 *
528 * Arguments: @qd_parse *qd@ = parser context
529 * @ec *p@ = where to put the point
530 *
531 * Returns: The point address, or null.
532 *
533 * Use: Parses an elliptic curve point. This has the form
534 *
535 * * %$x$%-coordinate
536 * * optional `,'
537 * * %$y$%-coordinate
538 */
539
540extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
541
542/* --- @ec_infoparse@ --- *
543 *
544 * Arguments: @qd_parse *qd@ = parser context
545 * @ec_info *ei@ = curve information block, currently
546 * uninitialized
547 *
548 * Returns: Zero on success, nonzero on failure.
549 *
550 * Use: Parses an elliptic curve information string, and stores the
551 * information in @ei@. This has the form
552 *
553 * * elliptic curve description
554 * * optional `/'
555 * * common point
556 * * optional `:'
557 * * group order
558 * * optional `*'
559 * * cofactor
560 */
561
562extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
563
564/* --- @ec_getinfo@ --- *
565 *
566 * Arguments: @ec_info *ei@ = where to write the information
567 * @const char *p@ = string describing a curve
568 *
569 * Returns: Null on success, or a pointer to an error message.
570 *
571 * Use: Parses out information about a curve. The string is either a
572 * standard curve name, or a curve info string.
573 */
574
575extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
576
577/* --- @ec_freeinfo@ --- *
578 *
579 * Arguments: @ec_info *ei@ = elliptic curve information block to free
580 *
581 * Returns: ---
582 *
583 * Use: Frees the information block.
584 */
585
586extern void ec_freeinfo(ec_info */*ei*/);
587
588/* --- @ec_checkinfo@ --- *
589 *
590 * Arguments: @const ec_info *ei@ = elliptic curve information block
591 *
592 * Returns: Null if OK, or pointer to error message.
593 *
594 * Use: Checks an elliptic curve according to the rules in SEC1.
595 */
596
597extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
598
b0ab12e6 599/*----- That's all, folks -------------------------------------------------*/
600
601#ifdef __cplusplus
602 }
603#endif
604
605#endif