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