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