c05adeea9a6fbd5e505248a6d59ebe5efebdda76
3 * $Id: key-binary.c,v 1.4 2004/03/28 01:58:47 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.4 2004/03/28 01:58:47 mdw
34 * Generate, store and retreive elliptic curve keys.
36 * Revision 1.3 2001/02/03 11:57:00 mdw
37 * Track mLib change: symbols no longer need to include a terminating
40 * Revision 1.2 2000/06/17 11:25:20 mdw
41 * Use secure memory interface from MP library.
43 * Revision 1.1 2000/02/12 18:21:02 mdw
44 * Overhaul of key management (again).
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
54 #include <mLib/dstr.h>
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @key_decode@ --- *
66 * Arguments: @const void *p@ = pointer to buffer to read
67 * @size_t sz@ = size of the buffer
68 * @key_data *k@ = pointer to key data block to write to
70 * Returns: Zero if everything worked, nonzero otherwise.
72 * Use: Decodes a binary representation of a key.
75 int key_decode(const void *p
, size_t sz
, key_data
*k
)
81 /* --- Parse the header information --- *
83 * Make sure the size matches external reality. Security holes have been
84 * known to creep in without this sort of check. (No, this isn't an after-
94 /* --- Now decide what to do --- */
96 switch (e
& KF_ENCMASK
) {
98 /* --- Plain binary data --- */
102 k
->u
.k
.k
= sub_alloc(psz
);
103 memcpy(k
->u
.k
.k
, q
+ 4, psz
);
107 /* --- Multiprecision integer data --- */
110 k
->u
.m
= mp_loadb(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, q
+ 4, psz
);
113 /* --- String data --- */
116 k
->u
.p
= xmalloc(sz
+ 1);
117 memcpy(k
->u
.p
, q
+ 4, sz
);
121 /* --- Elliptic curve point data --- */
125 if (sz
< 2) return (-1);
127 if (sz
< xsz
+ 4) return (-1);
128 ysz
= LOAD16(q
+ 6 + xsz
);
129 if (sz
< xsz
+ ysz
+ 4) return (-1);
131 k
->u
.e
.x
= mp_loadb(MP_NEW
, q
+ 6, xsz
);
132 k
->u
.e
.y
= mp_loadb(MP_NEW
, q
+ 6 + xsz
, ysz
);
135 /* --- Structured key data --- */
142 if ((k
->e
& ~KF_ENCMASK
) || (psz
& 3))
149 /* --- Read the tag string --- */
155 DPUTM(&d
, q
+ 1, sz
);
160 /* --- Read the encoding and size --- */
163 sz
= (LOAD16(q
+ 2) + 7) & ~3;
167 /* --- Create a table node and fill it in --- */
169 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
172 if (key_decode(q
, sz
, &ks
->k
)) {
173 sym_remove(&k
->u
.s
, ks
);
182 /* --- Tidy up after a failure --- */
190 /* --- Everything else --- */
196 /* --- OK, that was good --- */
201 /* --- @key_encode@ --- *
203 * Arguments: @key_data *k@ = pointer to key data block
204 * @dstr *d@ = pointer to destination string
205 * @const key_filter *kf@ = pointer to key selection block
207 * Returns: Nonzero if an item was actually written.
209 * Use: Encodes a key block as binary data.
212 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
215 if (!KEY_MATCH(k
, kf
))
217 switch (k
->e
& KF_ENCMASK
) {
222 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
225 STORE16(p
+ 2, k
->u
.k
.sz
);
227 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
233 size_t sz
= mp_octets(k
->u
.m
);
235 DENSURE(d
, (sz
+ 7) & ~3);
239 mp_storeb(k
->u
.m
, p
+ 4, sz
);
246 size_t sz
= strlen(k
->u
.p
);
248 DENSURE(d
, (sz
+ 7) & ~3);
252 memcpy(p
+ 4, k
->u
.p
, sz
);
259 size_t xsz
= mp_octets(k
->u
.e
.x
), ysz
= mp_octets(k
->u
.e
.y
);
260 size_t sz
= xsz
+ ysz
+ 4;
262 DENSURE(d
, (sz
+ 7) & ~3);
267 mp_storeb(k
->u
.e
.x
, p
+ 6, xsz
);
268 STORE16(p
+ 6 + xsz
, ysz
);
269 mp_storeb(k
->u
.e
.y
, p
+ 8 + xsz
, ysz
);
283 STORE16(p
, k
->e
& KF_ENCMASK
);
285 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
288 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
289 DPUTS(d
, SYM_NAME(ks
));
292 if (key_encode(&ks
->k
, d
, kf
))
311 /*----- That's all, folks -------------------------------------------------*/