6b80b6c4 |
1 | /* -*-c-*- |
2 | * |
fe9de6c9 |
3 | * $Id$ |
6b80b6c4 |
4 | * |
5 | * Buffer handling |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
6b80b6c4 |
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. |
45c0fd36 |
18 | * |
6b80b6c4 |
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. |
45c0fd36 |
23 | * |
6b80b6c4 |
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 | |
6b80b6c4 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <string.h> |
33 | |
5ff5e658 |
34 | #include "mp.h" |
34e4f738 |
35 | #include "ec.h" |
6b80b6c4 |
36 | #include "buf.h" |
37 | |
38 | /*----- Main code ---------------------------------------------------------*/ |
39 | |
6b80b6c4 |
40 | /* --- @buf_getmp@ --- * |
41 | * |
42 | * Arguments: @buf *b@ = pointer to a buffer block |
43 | * |
44 | * Returns: A multiprecision integer, or null if there wasn't one there. |
45 | * |
46 | * Use: Gets a multiprecision integer from a buffer. |
47 | */ |
48 | |
49 | mp *buf_getmp(buf *b) |
50 | { |
51 | uint16 sz; |
34e4f738 |
52 | size_t n; |
6b80b6c4 |
53 | mp *m; |
54 | if (buf_getu16(b, &sz) || buf_ensure(b, sz)) |
55 | return (0); |
56 | m = mp_loadb(MP_NEW, BCUR(b), sz); |
34e4f738 |
57 | n = mp_octets(m); |
58 | if (n != sz && n != 0 && sz != 1) { |
6b80b6c4 |
59 | mp_drop(m); |
60 | return (0); |
61 | } |
62 | BSTEP(b, sz); |
63 | return (m); |
64 | } |
65 | |
66 | /* --- @buf_putmp@ --- * |
67 | * |
68 | * Arguments: @buf *b@ = pointer to a buffer block |
69 | * @mp *m@ = a multiprecision integer |
70 | * |
71 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
72 | * |
73 | * Use: Puts a multiprecision integer to a buffer. |
74 | */ |
75 | |
76 | int buf_putmp(buf *b, mp *m) |
77 | { |
78 | size_t sz = mp_octets(m); |
79 | assert(sz < MASK16); |
34e4f738 |
80 | if (!sz) sz = 1; |
6b80b6c4 |
81 | if (buf_putu16(b, sz) || buf_ensure(b, sz)) |
82 | return (-1); |
83 | mp_storeb(m, BCUR(b), sz); |
84 | BSTEP(b, sz); |
85 | return (0); |
86 | } |
87 | |
34e4f738 |
88 | /* --- @buf_getec@ --- * |
89 | * |
90 | * Arguments: @buf *b@ = pointer to a buffer block |
91 | * @ec *p@ = where to put the point |
92 | * |
93 | * Returns: Zero if it worked, nonzero if it failed. |
94 | * |
95 | * Use: Gets a multiprecision integer from a buffer. The point must |
96 | * be initialized. |
97 | */ |
98 | |
99 | int buf_getec(buf *b, ec *p) |
100 | { |
101 | mp *x = 0, *y = 0; |
102 | uint16 n; |
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); |
107 | } |
108 | EC_DESTROY(p); p->x = x; p->y = y; p->z = 0; |
109 | return (0); |
110 | } |
111 | |
112 | /* --- @buf_putec@ --- * |
113 | * |
114 | * Arguments: @buf *b@ = pointer to a buffer block |
115 | * @ec *p@ = an elliptic curve point |
116 | * |
117 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
118 | * |
119 | * Use: Puts an elliptic curve point to a buffer. |
120 | */ |
121 | |
122 | int buf_putec(buf *b, ec *p) |
123 | { |
124 | if (EC_ATINF(p)) return (buf_putu16(b, 0)); |
125 | if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1); |
126 | return (0); |
127 | } |
128 | |
6b80b6c4 |
129 | /*----- That's all, folks -------------------------------------------------*/ |