34e4f738 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: g-ec.c,v 1.1 2004/04/01 12:50:09 mdw Exp $ |
4 | * |
5 | * Abstraction for elliptic curve groups |
6 | * |
7 | * (c) 2004 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: g-ec.c,v $ |
33 | * Revision 1.1 2004/04/01 12:50:09 mdw |
34 | * Add cyclic group abstraction, with test code. Separate off exponentation |
35 | * functions for better static linking. Fix a buttload of bugs on the way. |
36 | * Generally ensure that negative exponents do inversion correctly. Add |
37 | * table of standard prime-field subgroups. (Binary field subgroups are |
38 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
39 | * |
40 | */ |
41 | |
42 | /*----- Header files ------------------------------------------------------*/ |
43 | |
44 | #include <ctype.h> |
45 | |
46 | #include <mLib/sub.h> |
47 | |
48 | #define ge ec |
49 | #include "group.h" |
50 | |
51 | /*----- Data structures ---------------------------------------------------*/ |
52 | |
53 | typedef struct gctx { |
54 | group g; |
55 | ec id, gen; |
56 | ec_info ei; |
57 | } gctx; |
58 | |
59 | /*----- Main code ---------------------------------------------------------*/ |
60 | |
61 | /* --- Group operations --- */ |
62 | |
63 | static void gdestroygroup(group *gg) { |
64 | gctx *g = (gctx *)gg; |
65 | EC_DESTROY(&g->gen); |
66 | ec_freeinfo(&g->ei); |
67 | DESTROY(g); |
68 | } |
69 | |
70 | static ec *gcreate(group *gg) |
71 | { ec *x = CREATE(ec); EC_CREATE(x); return (x); } |
72 | |
73 | static void gcopy(group *gg, ec *d, ec *x) { EC_COPY(d, x); } |
74 | |
75 | static void gburn(group *gg, ec *x) { if (x->x) (x->x)->f |= MP_BURN; } |
76 | |
77 | static void gdestroy(group *gg, ec *x) { EC_DESTROY(x); DESTROY(x); } |
78 | |
79 | static int gsamep(group *gg, group *hh) { |
80 | gctx *g = (gctx *)gg, *h = (gctx *)hh; |
81 | return (ec_sameinfop(&g->ei, &h->ei)); |
82 | } |
83 | |
84 | static int geq(group *gg, ec *x, ec *y) { |
85 | gctx *g = (gctx *)gg; EC_FIX(g->ei.c, x, x); EC_FIX(g->ei.c, y, y); |
86 | return (EC_EQ(x, y)); |
87 | } |
88 | |
89 | static int gidentp(group *gg, ec *x) { return (EC_ATINF(x)); } |
90 | |
91 | static const char *gcheck(group *gg, grand *gr) |
92 | { gctx *g = (gctx *)gg; return (ec_checkinfo(&g->ei, gr)); } |
93 | |
94 | static void gmul(group *gg, ec *d, ec *x, ec *y) |
95 | { gctx *g = (gctx *)gg; EC_ADD(g->ei.c, d, x, y); } |
96 | |
97 | static void gsqr(group *gg, ec *d, ec *x) |
98 | { gctx *g = (gctx *)gg; EC_DBL(g->ei.c, d, x); } |
99 | |
100 | static void ginv(group *gg, ec *d, ec *x) |
101 | { gctx *g = (gctx *)gg; EC_NEG(g->ei.c, d, x); } |
102 | |
103 | static void gdiv(group *gg, ec *d, ec *x, ec *y) |
104 | { gctx *g = (gctx *)gg; EC_SUB(g->ei.c, d, x, y); } |
105 | |
106 | static void gexp(group *gg, ec *d, ec *x, mp *n) |
107 | { gctx *g = (gctx *)gg; ec_imul(g->ei.c, d, x, n); } |
108 | |
109 | static void gmexp(group *gg, ec *d, const group_expfactor *f, size_t n) { |
110 | gctx *g = (gctx *)gg; size_t i; |
111 | ec_mulfactor *ff = xmalloc(n * sizeof(ec_mulfactor)); |
112 | for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; } |
113 | ec_immul(g->ei.c, d, ff, n); xfree(ff); |
114 | } |
115 | |
116 | static int gread(group *gg, ec *d, const mptext_ops *ops, void *p) { |
117 | gctx *g = (gctx *)gg; |
118 | ec t = EC_INIT; |
119 | int rc = -1; |
120 | int ch; |
121 | |
122 | ch = ops->get(p); |
123 | if (tolower(ch) == 'i') { |
124 | if (tolower(ops->get(p)) != 'n' || tolower(ops->get(p)) != 'f') |
125 | return (-1); |
126 | EC_SETINF(d); |
127 | return (0); |
128 | } |
129 | ops->unget(ch, p); |
130 | if ((t.x = mp_read(MP_NEW, 0, ops, p)) == 0) goto done; |
131 | do ch = ops->get(p); while (ch == ',' || isspace(ch)); ops->unget(ch, p); |
132 | if ((t.y = mp_read(MP_NEW, 0, ops, p)) == 0) goto done; |
133 | EC_IN(g->ei.c, &t, &t); |
134 | if (EC_CHECK(g->ei.c, &t)) goto done; |
135 | EC_COPY(d, &t); rc = 0; |
136 | EC_DESTROY(&t); |
137 | done: |
138 | return (rc); |
139 | } |
140 | |
141 | static int gwrite(group *gg, ec *x, const mptext_ops *ops, void *p) { |
142 | gctx *g = (gctx *)gg; int rc = -1; ec t = EC_INIT; EC_OUT(g->ei.c, &t, x); |
143 | if (EC_ATINF(&t)) rc = ops->put("inf", 3, p); |
144 | else if (!ops->put("0x", 2, p) && !mp_write(t.x, 16, ops, p) && |
145 | !ops->put(", 0x", 4, p) && !mp_write(t.y, 16, ops, p)) rc = 0; |
146 | EC_DESTROY(&t); return (rc); |
147 | } |
148 | |
149 | static mp *gtoint(group *gg, mp *d, ec *x) { |
150 | gctx *g = (gctx *)gg; ec t = EC_INIT; mp *i; if (EC_ATINF(x)) i = 0; |
151 | else { EC_OUT(g->ei.c, &t, x); i = MP_COPY(t.x); EC_DESTROY(&t); } |
152 | mp_drop(d); return (i); |
153 | } |
154 | |
155 | static int gfromint(group *gg, ec *d, mp *x) { |
156 | gctx *g = (gctx *)gg; ec t = EC_INIT; |
157 | if (!ec_find(g->ei.c, &t, x)) return (-1); |
158 | EC_IN(g->ei.c, d, &t); EC_DESTROY(&t); return (0); |
159 | } |
160 | |
161 | static int gtoec(group *gg, ec *d, ec *x) |
162 | { gctx *g = (gctx *)gg; EC_OUT(g->ei.c, d, x); return (0); } |
163 | |
164 | static int gfromec(group *gg, ec *d, ec *x) { |
165 | gctx *g = (gctx *)gg; ec t = EC_INIT; int rc; EC_IN(g->ei.c, &t, x); |
166 | rc = EC_CHECK(g->ei.c, &t); if (!rc) EC_COPY(d, &t); EC_DESTROY(&t); |
167 | return (rc); |
168 | } |
169 | |
170 | static int gtobuf(group *gg, buf *b, ec *x) { |
171 | gctx *g = (gctx *)gg; ec t = EC_INIT; int rc; |
172 | EC_OUT(g->ei.c, &t, x); rc = buf_putec(b, &t); EC_DESTROY(&t); return (rc); |
173 | } |
174 | |
175 | static int gfrombuf(group *gg, buf *b, ec *d) { |
176 | gctx *g = (gctx *)gg; ec t = EC_INIT; int rc; |
177 | if (buf_getec(b, &t)) return (-1); |
178 | EC_IN(g->ei.c, &t, &t); rc = EC_CHECK(g->ei.c, &t); |
179 | if (!rc) EC_COPY(d, &t); EC_DESTROY(&t); return (rc); |
180 | } |
181 | |
182 | /* --- @group_ec@ --- * |
183 | * |
184 | * Arguments: @const ec_info *ei@ = elliptic curve parameters |
185 | * |
186 | * Returns: A pointer to the group. |
187 | * |
188 | * Use: Constructs an abstract group interface for an elliptic curve |
189 | * group. Group elements are @ec@ structures. The contents of |
190 | * the @ec_info@ structure becomes the property of the @group@ |
191 | * object; you can (and should) free the structure itself, but |
192 | * calling @ec_freeinfo@ on it is not allowed. |
193 | */ |
194 | |
195 | static const group_ops gops = { |
196 | GTY_EC, |
197 | gdestroygroup, gcreate, gcopy, gburn, gdestroy, |
198 | gsamep, geq, gidentp, |
199 | gcheck, |
200 | gmul, gsqr, ginv, gdiv, gexp, gmexp, |
201 | gread, gwrite, |
202 | gtoint, gfromint, gtoec, gfromec, gtobuf, gfrombuf |
203 | }; |
204 | |
205 | group *group_ec(const ec_info *ei) |
206 | { |
207 | gctx *g = CREATE(gctx); |
208 | |
209 | g->g.ops = &gops; |
210 | g->g.nbits = ei->c->f->nbits * 2; |
211 | g->g.noctets = ei->c->f->noctets * 2; |
212 | g->ei = *ei; |
213 | EC_CREATE(&g->id); |
214 | g->g.i = &g->id; |
215 | EC_CREATE(&g->gen); |
216 | EC_IN(g->ei.c, &g->gen, &ei->g); |
217 | g->g.r = ei->r; |
218 | g->g.h = ei->h; |
219 | return (&g->g); |
220 | } |
221 | |
222 | /*----- That's all, folks -------------------------------------------------*/ |