3 * $Id: key-binary.c,v 1.5 2004/04/01 12:50:09 mdw Exp $
7 * (c) 1999 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 --------------------------------------------------*
32 * $Log: key-binary.c,v $
33 * Revision 1.5 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.)
40 * Revision 1.4 2004/03/28 01:58:47 mdw
41 * Generate, store and retreive elliptic curve keys.
43 * Revision 1.3 2001/02/03 11:57:00 mdw
44 * Track mLib change: symbols no longer need to include a terminating
47 * Revision 1.2 2000/06/17 11:25:20 mdw
48 * Use secure memory interface from MP library.
50 * Revision 1.1 2000/02/12 18:21:02 mdw
51 * Overhaul of key management (again).
55 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/bits.h>
61 #include <mLib/dstr.h>
69 /*----- Main code ---------------------------------------------------------*/
71 /* --- @key_decode@ --- *
73 * Arguments: @const void *p@ = pointer to buffer to read
74 * @size_t sz@ = size of the buffer
75 * @key_data *k@ = pointer to key data block to write to
77 * Returns: Zero if everything worked, nonzero otherwise.
79 * Use: Decodes a binary representation of a key.
82 int key_decode(const void *p
, size_t sz
, key_data
*k
)
88 /* --- Parse the header information --- *
90 * Make sure the size matches external reality. Security holes have been
91 * known to creep in without this sort of check. (No, this isn't an after-
101 /* --- Now decide what to do --- */
103 switch (e
& KF_ENCMASK
) {
105 /* --- Plain binary data --- */
109 k
->u
.k
.k
= sub_alloc(psz
);
110 memcpy(k
->u
.k
.k
, q
+ 4, psz
);
114 /* --- Multiprecision integer data --- */
117 k
->u
.m
= mp_loadb(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, q
+ 4, psz
);
120 /* --- String data --- */
123 k
->u
.p
= xmalloc(sz
+ 1);
124 memcpy(k
->u
.p
, q
+ 4, sz
);
128 /* --- Elliptic curve point data --- */
134 if (sz
< 2) return (-1);
136 if (sz
< xsz
+ 4) return (-1);
137 ysz
= LOAD16(q
+ 6 + xsz
);
138 if (sz
< xsz
+ ysz
+ 4) return (-1);
139 k
->u
.e
.x
= mp_loadb(MP_NEW
, q
+ 6, xsz
);
140 k
->u
.e
.y
= mp_loadb(MP_NEW
, q
+ 6 + xsz
, ysz
);
143 /* --- Structured key data --- */
150 if ((k
->e
& ~KF_ENCMASK
) || (psz
& 3))
157 /* --- Read the tag string --- */
163 DPUTM(&d
, q
+ 1, sz
);
168 /* --- Read the encoding and size --- */
171 sz
= (LOAD16(q
+ 2) + 7) & ~3;
175 /* --- Create a table node and fill it in --- */
177 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
180 if (key_decode(q
, sz
, &ks
->k
)) {
181 sym_remove(&k
->u
.s
, ks
);
190 /* --- Tidy up after a failure --- */
198 /* --- Everything else --- */
204 /* --- OK, that was good --- */
209 /* --- @key_encode@ --- *
211 * Arguments: @key_data *k@ = pointer to key data block
212 * @dstr *d@ = pointer to destination string
213 * @const key_filter *kf@ = pointer to key selection block
215 * Returns: Nonzero if an item was actually written.
217 * Use: Encodes a key block as binary data.
220 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
223 if (!KEY_MATCH(k
, kf
))
225 switch (k
->e
& KF_ENCMASK
) {
230 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
233 STORE16(p
+ 2, k
->u
.k
.sz
);
235 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
241 size_t sz
= mp_octets(k
->u
.m
);
243 DENSURE(d
, (sz
+ 7) & ~3);
247 mp_storeb(k
->u
.m
, p
+ 4, sz
);
254 size_t sz
= strlen(k
->u
.p
);
256 DENSURE(d
, (sz
+ 7) & ~3);
260 memcpy(p
+ 4, k
->u
.p
, sz
);
270 if (EC_ATINF(&k
->u
.e
))
273 xsz
= mp_octets(k
->u
.e
.x
);
274 ysz
= mp_octets(k
->u
.e
.y
);
277 DENSURE(d
, (sz
+ 7) & ~3);
281 if (!EC_ATINF(&k
->u
.e
)) {
283 mp_storeb(k
->u
.e
.x
, p
+ 6, xsz
);
284 STORE16(p
+ 6 + xsz
, ysz
);
285 mp_storeb(k
->u
.e
.y
, p
+ 8 + xsz
, ysz
);
300 STORE16(p
, k
->e
& KF_ENCMASK
);
302 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
305 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
306 DPUTS(d
, SYM_NAME(ks
));
309 if (key_encode(&ks
->k
, d
, kf
))
328 /*----- That's all, folks -------------------------------------------------*/