| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: buf.c,v 1.3 2004/04/01 12:50:09 mdw Exp $ |
| 4 | * |
| 5 | * Buffer handling |
| 6 | * |
| 7 | * (c) 2001 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: buf.c,v $ |
| 33 | * Revision 1.3 2004/04/01 12:50:09 mdw |
| 34 | * Add cyclic group abstraction, with test code. Separate off exponentation |
| 35 | * functions for better static linking. Fix a buttload of bugs on the way. |
| 36 | * Generally ensure that negative exponents do inversion correctly. Add |
| 37 | * table of standard prime-field subgroups. (Binary field subgroups are |
| 38 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
| 39 | * |
| 40 | * Revision 1.2 2003/11/10 22:18:30 mdw |
| 41 | * Build fixes. |
| 42 | * |
| 43 | * Revision 1.1 2003/10/11 21:02:33 mdw |
| 44 | * Import buf stuff from tripe. |
| 45 | * |
| 46 | * Revision 1.4 2001/06/19 22:09:54 mdw |
| 47 | * Expose interface, for use in the proxy. |
| 48 | * |
| 49 | * Revision 1.3 2001/03/03 12:06:48 mdw |
| 50 | * Use 16-bit lengths on MPs, since there's a packet limit of 64K anyway. |
| 51 | * |
| 52 | * Revision 1.2 2001/02/16 21:23:20 mdw |
| 53 | * Various minor changes. Check that MPs are in canonical form when |
| 54 | * loading. |
| 55 | * |
| 56 | * Revision 1.1 2001/02/03 20:26:37 mdw |
| 57 | * Initial checkin. |
| 58 | * |
| 59 | */ |
| 60 | |
| 61 | /*----- Header files ------------------------------------------------------*/ |
| 62 | |
| 63 | #include <string.h> |
| 64 | |
| 65 | #include "mp.h" |
| 66 | #include "ec.h" |
| 67 | #include "buf.h" |
| 68 | |
| 69 | /*----- Main code ---------------------------------------------------------*/ |
| 70 | |
| 71 | /* --- @buf_init@ --- * |
| 72 | * |
| 73 | * Arguments: @buf *b@ = pointer to a buffer block |
| 74 | * @void *p@ = pointer to a buffer |
| 75 | * @size_t sz@ = size of the buffer |
| 76 | * |
| 77 | * Returns: --- |
| 78 | * |
| 79 | * Use: Initializes the buffer block appropriately. |
| 80 | */ |
| 81 | |
| 82 | void buf_init(buf *b, void *p, size_t sz) |
| 83 | { |
| 84 | b->base = b->p = p; |
| 85 | b->limit = b->p + sz; |
| 86 | b->f = 0; |
| 87 | } |
| 88 | |
| 89 | /* --- @buf_break@ --- * |
| 90 | * |
| 91 | * Arguments: @buf *b@ = pointer to a buffer block |
| 92 | * |
| 93 | * Returns: Some negative value. |
| 94 | * |
| 95 | * Use: Marks a buffer as broken. |
| 96 | */ |
| 97 | |
| 98 | int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); } |
| 99 | |
| 100 | /* --- @buf_flip@ --- * |
| 101 | * |
| 102 | * Arguments: @buf *b@ = pointer to a buffer block |
| 103 | * |
| 104 | * Returns: --- |
| 105 | * |
| 106 | * Use: Flips a buffer so that if you've just been writing to it, |
| 107 | * you can now read from the bit you've written. |
| 108 | */ |
| 109 | |
| 110 | void buf_flip(buf *b) |
| 111 | { |
| 112 | b->limit = b->p; |
| 113 | b->p = b->base; |
| 114 | } |
| 115 | |
| 116 | /* --- @buf_ensure@ --- * |
| 117 | * |
| 118 | * Arguments: @buf *b@ = pointer to a buffer block |
| 119 | * @size_t sz@ = size of data wanted |
| 120 | * |
| 121 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 122 | * |
| 123 | * Use: Ensures that there are @sz@ bytes still in the buffer. |
| 124 | */ |
| 125 | |
| 126 | int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); } |
| 127 | |
| 128 | /* --- @buf_get@ --- * |
| 129 | * |
| 130 | * Arguments: @buf *b@ = pointer to a buffer block |
| 131 | * @size_t sz@ = size of the buffer |
| 132 | * |
| 133 | * Returns: Pointer to the place in the buffer. |
| 134 | * |
| 135 | * Use: Reserves a space in the buffer of the requested size, and |
| 136 | * returns its start address. |
| 137 | */ |
| 138 | |
| 139 | void *buf_get(buf *b, size_t sz) |
| 140 | { |
| 141 | void *p; |
| 142 | if (BENSURE(b, sz)) |
| 143 | return (0); |
| 144 | p = BCUR(b); |
| 145 | BSTEP(b, sz); |
| 146 | return (p); |
| 147 | } |
| 148 | |
| 149 | /* --- @buf_put@ --- * |
| 150 | * |
| 151 | * Arguments: @buf *b@ = pointer to a buffer block |
| 152 | * @const void *p@ = pointer to a buffer |
| 153 | * @size_t sz@ = size of the buffer |
| 154 | * |
| 155 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 156 | * |
| 157 | * Use: Fetches data from some place and puts it in the buffer |
| 158 | */ |
| 159 | |
| 160 | int buf_put(buf *b, const void *p, size_t sz) |
| 161 | { |
| 162 | if (BENSURE(b, sz)) |
| 163 | return (-1); |
| 164 | memcpy(BCUR(b), p, sz); |
| 165 | BSTEP(b, sz); |
| 166 | return (0); |
| 167 | } |
| 168 | |
| 169 | /* --- @buf_getbyte@ --- * |
| 170 | * |
| 171 | * Arguments: @buf *b@ = pointer to a buffer block |
| 172 | * |
| 173 | * Returns: A byte, or less than zero if there wasn't a byte there. |
| 174 | * |
| 175 | * Use: Gets a single byte from a buffer. |
| 176 | */ |
| 177 | |
| 178 | int buf_getbyte(buf *b) |
| 179 | { |
| 180 | if (BENSURE(b, 1)) |
| 181 | return (-1); |
| 182 | return (*b->p++); |
| 183 | } |
| 184 | |
| 185 | /* --- @buf_putbyte@ --- * |
| 186 | * |
| 187 | * Arguments: @buf *b@ = pointer to a buffer block |
| 188 | * @int ch@ = byte to write |
| 189 | * |
| 190 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 191 | * |
| 192 | * Use: Puts a single byte in a buffer. |
| 193 | */ |
| 194 | |
| 195 | int buf_putbyte(buf *b, int ch) |
| 196 | { |
| 197 | if (BENSURE(b, 1)) |
| 198 | return (-1); |
| 199 | *b->p++ = ch; |
| 200 | return (0); |
| 201 | } |
| 202 | |
| 203 | /* --- @buf_getu16@ --- * |
| 204 | * |
| 205 | * Arguments: @buf *b@ = pointer to a buffer block |
| 206 | * @uint16 *w@ = where to put the word |
| 207 | * |
| 208 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 209 | * |
| 210 | * Use: Gets a 16-bit word from a buffer. |
| 211 | */ |
| 212 | |
| 213 | int buf_getu16(buf *b, uint16 *w) |
| 214 | { |
| 215 | if (BENSURE(b, 2)) |
| 216 | return (-1); |
| 217 | *w = LOAD16(b->p); |
| 218 | BSTEP(b, 2); |
| 219 | return (0); |
| 220 | } |
| 221 | |
| 222 | /* --- @buf_putu16@ --- * |
| 223 | * |
| 224 | * Arguments: @buf *b@ = pointer to a buffer block |
| 225 | * @uint16 w@ = word to write |
| 226 | * |
| 227 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 228 | * |
| 229 | * Use: Puts a 16-but word in a buffer. |
| 230 | */ |
| 231 | |
| 232 | int buf_putu16(buf *b, uint16 w) |
| 233 | { |
| 234 | if (BENSURE(b, 2)) |
| 235 | return (-1); |
| 236 | STORE16(b->p, w); |
| 237 | BSTEP(b, 2); |
| 238 | return (0); |
| 239 | } |
| 240 | |
| 241 | /* --- @buf_getu32@ --- * |
| 242 | * |
| 243 | * Arguments: @buf *b@ = pointer to a buffer block |
| 244 | * @uint32 *w@ = where to put the word |
| 245 | * |
| 246 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 247 | * |
| 248 | * Use: Gets a 32-bit word from a buffer. |
| 249 | */ |
| 250 | |
| 251 | int buf_getu32(buf *b, uint32 *w) |
| 252 | { |
| 253 | if (BENSURE(b, 4)) |
| 254 | return (-1); |
| 255 | *w = LOAD32(b->p); |
| 256 | BSTEP(b, 4); |
| 257 | return (0); |
| 258 | } |
| 259 | |
| 260 | /* --- @buf_putu32@ --- * |
| 261 | * |
| 262 | * Arguments: @buf *b@ = pointer to a buffer block |
| 263 | * @uint32 w@ = word to write |
| 264 | * |
| 265 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 266 | * |
| 267 | * Use: Puts a 32-but word in a buffer. |
| 268 | */ |
| 269 | |
| 270 | int buf_putu32(buf *b, uint32 w) |
| 271 | { |
| 272 | if (BENSURE(b, 4)) |
| 273 | return (-1); |
| 274 | STORE32(b->p, w); |
| 275 | BSTEP(b, 4); |
| 276 | return (0); |
| 277 | } |
| 278 | |
| 279 | /* --- @buf_getmp@ --- * |
| 280 | * |
| 281 | * Arguments: @buf *b@ = pointer to a buffer block |
| 282 | * |
| 283 | * Returns: A multiprecision integer, or null if there wasn't one there. |
| 284 | * |
| 285 | * Use: Gets a multiprecision integer from a buffer. |
| 286 | */ |
| 287 | |
| 288 | mp *buf_getmp(buf *b) |
| 289 | { |
| 290 | uint16 sz; |
| 291 | size_t n; |
| 292 | mp *m; |
| 293 | if (buf_getu16(b, &sz) || buf_ensure(b, sz)) |
| 294 | return (0); |
| 295 | m = mp_loadb(MP_NEW, BCUR(b), sz); |
| 296 | n = mp_octets(m); |
| 297 | if (n != sz && n != 0 && sz != 1) { |
| 298 | mp_drop(m); |
| 299 | return (0); |
| 300 | } |
| 301 | BSTEP(b, sz); |
| 302 | return (m); |
| 303 | } |
| 304 | |
| 305 | /* --- @buf_putmp@ --- * |
| 306 | * |
| 307 | * Arguments: @buf *b@ = pointer to a buffer block |
| 308 | * @mp *m@ = a multiprecision integer |
| 309 | * |
| 310 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 311 | * |
| 312 | * Use: Puts a multiprecision integer to a buffer. |
| 313 | */ |
| 314 | |
| 315 | int buf_putmp(buf *b, mp *m) |
| 316 | { |
| 317 | size_t sz = mp_octets(m); |
| 318 | assert(sz < MASK16); |
| 319 | if (!sz) sz = 1; |
| 320 | if (buf_putu16(b, sz) || buf_ensure(b, sz)) |
| 321 | return (-1); |
| 322 | mp_storeb(m, BCUR(b), sz); |
| 323 | BSTEP(b, sz); |
| 324 | return (0); |
| 325 | } |
| 326 | |
| 327 | /* --- @buf_getec@ --- * |
| 328 | * |
| 329 | * Arguments: @buf *b@ = pointer to a buffer block |
| 330 | * @ec *p@ = where to put the point |
| 331 | * |
| 332 | * Returns: Zero if it worked, nonzero if it failed. |
| 333 | * |
| 334 | * Use: Gets a multiprecision integer from a buffer. The point must |
| 335 | * be initialized. |
| 336 | */ |
| 337 | |
| 338 | int buf_getec(buf *b, ec *p) |
| 339 | { |
| 340 | mp *x = 0, *y = 0; |
| 341 | uint16 n; |
| 342 | if (buf_ensure(b, 2)) return (-1); |
| 343 | n = LOAD16(BCUR(b)); if (!n) { BSTEP(b, 2); EC_SETINF(p); return (0); } |
| 344 | if ((x = buf_getmp(b)) == 0 || (y = buf_getmp(b)) == 0) { |
| 345 | mp_drop(x); mp_drop(y); return (-1); |
| 346 | } |
| 347 | EC_DESTROY(p); p->x = x; p->y = y; p->z = 0; |
| 348 | return (0); |
| 349 | } |
| 350 | |
| 351 | /* --- @buf_putec@ --- * |
| 352 | * |
| 353 | * Arguments: @buf *b@ = pointer to a buffer block |
| 354 | * @ec *p@ = an elliptic curve point |
| 355 | * |
| 356 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 357 | * |
| 358 | * Use: Puts an elliptic curve point to a buffer. |
| 359 | */ |
| 360 | |
| 361 | int buf_putec(buf *b, ec *p) |
| 362 | { |
| 363 | if (EC_ATINF(p)) return (buf_putu16(b, 0)); |
| 364 | if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1); |
| 365 | return (0); |
| 366 | } |
| 367 | |
| 368 | /*----- That's all, folks -------------------------------------------------*/ |