ad9b5831680d522d716d1873b217504b1ff39ae4
[u/mdw/catacomb] / ec-raw.c
1 /* -*-c-*-
2 *
3 * $Id: ec-raw.c,v 1.1 2004/04/04 19:04:11 mdw Exp $
4 *
5 * Raw formatting of elliptic curve points
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: ec-raw.c,v $
33 * Revision 1.1 2004/04/04 19:04:11 mdw
34 * Raw I/O of elliptic curve points and group elements.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include "ec.h"
41 #include "ec-raw.h"
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @ec_putraw@ --- *
46 *
47 * Arguments: @ec_curve *c@ = elliptic curve
48 * @buf *b@ = pointer to a buffer
49 * @const ec *p@ = an elliptic curve point
50 *
51 * Returns: Zero on success, nonzero on failure.
52 *
53 * Use: Puts an elliptic curve point to the given buffer using the
54 * standard uncompressed format described in P1383 and SEC1.
55 * This requires at most @1 + 2 * c->f->noctets@ space in the
56 * buffer. We don't do point compression.
57 */
58
59 int ec_putraw(ec_curve *c, buf *b, const ec *p)
60 {
61 octet *q;
62 size_t n;
63
64 if (EC_ATINF(p)) return (buf_putbyte(b, 0));
65 buf_putbyte(b, 4);
66 n = c->f->noctets;
67 if ((q = buf_get(b, n * 2)) == 0) return (-1);
68 mp_storeb(p->x, q, n);
69 mp_storeb(p->y, q + n, n);
70 return (0);
71 }
72
73 /* --- @ec_getraw@ --- *
74 *
75 * Arguments: @ec_curve *c@ = elliptic curve
76 * @buf *b@ = pointer to a buffer
77 * @ec *d@ = an elliptic curve point
78 *
79 * Returns: Zero on success, nonzero on failure.
80 *
81 * Use: Reads an elliptic curve point from the given buffer using the
82 * standard uncompressed format described in P1383 and SEC1.
83 * We don't do point compression.
84 */
85
86 int ec_getraw(ec_curve *c, buf *b, ec *d)
87 {
88 const octet *q;
89 size_t n;
90 int u;
91
92 if ((u = buf_getbyte(b)) < 0) return (-1);
93 if (!u) { EC_SETINF(d); return (0); }
94 if (!(u & 4)) return (-1);
95 n = c->f->noctets;
96 if ((q = buf_get(b, n * 2)) == 0) return (-1);
97 EC_DESTROY(d);
98 d->x = mp_loadb(MP_NEW, q, n);
99 d->y = mp_loadb(MP_NEW, q + n, n);
100 d->z = 0;
101 return (0);
102 }
103
104 /*----- That's all, folks -------------------------------------------------*/