7 * (c) 2001 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 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @buf_getmp@ --- *
42 * Arguments: @buf *b@ = pointer to a buffer block
44 * Returns: A multiprecision integer, or null if there wasn't one there.
46 * Use: Gets a multiprecision integer from a buffer.
54 if (buf_getu16(b, &sz) || buf_ensure(b, sz))
56 m = mp_loadb(MP_NEW, BCUR(b), sz);
58 if (n != sz && n != 0 && sz != 1) {
66 /* --- @buf_putmp@ --- *
68 * Arguments: @buf *b@ = pointer to a buffer block
69 * @mp *m@ = a multiprecision integer
71 * Returns: Zero if it worked, nonzero if there wasn't enough space.
73 * Use: Puts a multiprecision integer to a buffer.
76 int buf_putmp(buf *b, mp *m)
78 size_t sz = mp_octets(m);
81 if (buf_putu16(b, sz) || buf_ensure(b, sz))
83 mp_storeb(m, BCUR(b), sz);
88 /* --- @buf_getec@ --- *
90 * Arguments: @buf *b@ = pointer to a buffer block
91 * @ec *p@ = where to put the point
93 * Returns: Zero if it worked, nonzero if it failed.
95 * Use: Gets a multiprecision integer from a buffer. The point must
99 int buf_getec(buf *b, ec *p)
103 if (buf_ensure(b, 2)) return (-1);
104 n = LOAD16(BCUR(b)); if (!n) { BSTEP(b, 2); EC_SETINF(p); return (0); }
105 if ((x = buf_getmp(b)) == 0 || (y = buf_getmp(b)) == 0) {
106 mp_drop(x); mp_drop(y); return (-1);
108 EC_DESTROY(p); p->x = x; p->y = y; p->z = 0;
112 /* --- @buf_putec@ --- *
114 * Arguments: @buf *b@ = pointer to a buffer block
115 * @ec *p@ = an elliptic curve point
117 * Returns: Zero if it worked, nonzero if there wasn't enough space.
119 * Use: Puts an elliptic curve point to a buffer.
122 int buf_putec(buf *b, ec *p)
124 if (EC_ATINF(p)) return (buf_putu16(b, 0));
125 if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1);
129 /*----- That's all, folks -------------------------------------------------*/