5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @buf_getmp@ --- *
40 * Arguments: @buf *b@ = pointer to a buffer block
42 * Returns: A multiprecision integer, or null if there wasn't one there.
44 * Use: Gets a multiprecision integer from a buffer.
52 if (buf_getu16(b, &sz) || buf_ensure(b, sz))
54 m = mp_loadb(MP_NEW, BCUR(b), sz);
56 if (n != sz && n != 0 && sz != 1) {
64 /* --- @buf_putmp@ --- *
66 * Arguments: @buf *b@ = pointer to a buffer block
67 * @mp *m@ = a multiprecision integer
69 * Returns: Zero if it worked, nonzero if there wasn't enough space.
71 * Use: Puts a multiprecision integer to a buffer.
74 int buf_putmp(buf *b, mp *m)
76 size_t sz = mp_octets(m);
79 if (buf_putu16(b, sz) || buf_ensure(b, sz))
81 mp_storeb(m, BCUR(b), sz);
86 /* --- @buf_getec@ --- *
88 * Arguments: @buf *b@ = pointer to a buffer block
89 * @ec *p@ = where to put the point
91 * Returns: Zero if it worked, nonzero if it failed.
93 * Use: Gets a multiprecision integer from a buffer. The point must
97 int buf_getec(buf *b, ec *p)
101 if (buf_ensure(b, 2)) return (-1);
102 n = LOAD16(BCUR(b)); if (!n) { BSTEP(b, 2); EC_SETINF(p); return (0); }
103 if ((x = buf_getmp(b)) == 0 || (y = buf_getmp(b)) == 0) {
104 mp_drop(x); mp_drop(y); return (-1);
106 EC_DESTROY(p); p->x = x; p->y = y; p->z = 0;
110 /* --- @buf_putec@ --- *
112 * Arguments: @buf *b@ = pointer to a buffer block
113 * @ec *p@ = an elliptic curve point
115 * Returns: Zero if it worked, nonzero if there wasn't enough space.
117 * Use: Puts an elliptic curve point to a buffer.
120 int buf_putec(buf *b, ec *p)
122 if (EC_ATINF(p)) return (buf_putu16(b, 0));
123 if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1);
127 /*----- That's all, folks -------------------------------------------------*/