cd6c3eeb |
1 | /* -*-c-*- |
2 | * |
7629bca8 |
3 | * $Id: pkcs1.c,v 1.3 2000/10/08 12:07:04 mdw Exp $ |
cd6c3eeb |
4 | * |
5 | * PKCS#1 1.5 packing |
6 | * |
7 | * (c) 2000 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: pkcs1.c,v $ |
7629bca8 |
33 | * Revision 1.3 2000/10/08 12:07:04 mdw |
34 | * Don't do arithmetic on @void *@ pointers. |
35 | * |
0586d2a1 |
36 | * Revision 1.2 2000/07/05 17:49:48 mdw |
37 | * Fix decoding functions, so that they don't run off the end of the |
38 | * buffer. |
39 | * |
cd6c3eeb |
40 | * Revision 1.1 2000/07/01 11:17:38 mdw |
41 | * New support for PKCS#1 message encoding. |
42 | * |
43 | */ |
44 | |
45 | /*----- Header files ------------------------------------------------------*/ |
46 | |
47 | #include <string.h> |
48 | |
49 | #include <mLib/bits.h> |
50 | #include <mLib/dstr.h> |
51 | |
52 | #include "grand.h" |
53 | #include "pkcs1.h" |
54 | |
55 | /*----- Main code ---------------------------------------------------------*/ |
56 | |
57 | /* --- @pkcs1_cryptencode@ --- * |
58 | * |
59 | * Arguments: @const void *msg@ = pointer to message data |
60 | * @size_t msz@ = size of message data |
61 | * @void *buf@ = pointer to output buffer |
62 | * @size_t sz@ = size of the output buffer |
63 | * @void *p@ = pointer to PKCS1 parameter block |
64 | * |
65 | * Returns: Zero if all went well, negative on failure. |
66 | * |
67 | * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined |
68 | * in PKCS#1 v. 2.0 (RFC2437). |
69 | */ |
70 | |
71 | int pkcs1_cryptencode(const void *msg, size_t msz, void *buf, size_t sz, |
72 | void *p) |
73 | { |
74 | pkcs1 *pp = p; |
75 | grand *r = pp->r; |
76 | octet *q, *qq; |
77 | size_t i, n; |
78 | |
79 | /* --- Ensure that the buffer is sensibly sized --- */ |
80 | |
81 | if (pp->epsz + msz + 11 > sz) |
82 | return (-1); |
83 | |
84 | /* --- Fill in the buffer --- */ |
85 | |
86 | q = buf; |
87 | qq = q + sz; |
88 | *q++ = 0; |
89 | *q++ = 2; |
90 | n = sz - msz - pp->epsz - 3; |
91 | r->ops->fill(r, q, n); |
92 | for (i = 0; i < n; i++) { |
93 | if (*q == 0) |
94 | *q = r->ops->range(r, 255) + 1; |
95 | q++; |
96 | } |
97 | *q++ = 0; |
98 | memcpy(q, pp->ep, pp->epsz); |
99 | q += pp->epsz; |
100 | memcpy(q, msg, msz); |
101 | return (0); |
102 | } |
103 | |
104 | /* --- @pkcs1_cryptdecode@ --- * |
105 | * |
106 | * Arguments: @const void *buf@ = pointer to encoded buffer |
107 | * @size_t sz@ = size of the encoded buffer |
108 | * @dstr *d@ = pointer to destination string |
109 | * @void *p@ = pointer to PKCS1 parameter block |
110 | * |
111 | * Returns: The length of the output string if successful, negative on |
112 | * failure. |
113 | * |
114 | * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined |
115 | * in PKCS#1 v. 2.0 (RFC2437). |
116 | */ |
117 | |
118 | int pkcs1_cryptdecode(const void *buf, size_t sz, dstr *d, void *p) |
119 | { |
120 | pkcs1 *pp = p; |
121 | const octet *q, *qq; |
122 | size_t n, i; |
123 | |
124 | /* --- Check the size of the block looks sane --- */ |
125 | |
126 | if (pp->epsz + 11 > sz) |
127 | return (-1); |
128 | q = buf; |
7629bca8 |
129 | qq = q + sz; |
cd6c3eeb |
130 | |
131 | /* --- Ensure that the block looks OK --- */ |
132 | |
133 | if (*q++ != 0 || *q++ != 2) |
134 | return (-1); |
135 | |
136 | /* --- Check the nonzero padding --- */ |
137 | |
138 | i = 0; |
139 | while (*q != 0 && q < qq) |
140 | i++, q++; |
0586d2a1 |
141 | if (i < 8 || qq - q < pp->epsz + 1) |
cd6c3eeb |
142 | return (-1); |
143 | q++; |
144 | |
145 | /* --- Check the encoding parameters --- */ |
146 | |
147 | if (memcmp(q, pp->ep, pp->epsz) != 0) |
148 | return (-1); |
149 | q += pp->epsz; |
150 | |
151 | /* --- Done --- */ |
152 | |
153 | n = qq - q; |
154 | dstr_putm(d, q, n); |
155 | return (n); |
156 | } |
157 | |
158 | /* --- @pkcs1_sigencode@ --- * |
159 | * |
160 | * Arguments: @const void *msg@ = pointer to message data |
161 | * @size_t msz@ = size of message data |
162 | * @void *buf@ = pointer to output buffer |
163 | * @size_t sz@ = size of the output buffer |
164 | * @void *p@ = pointer to PKCS1 parameter block |
165 | * |
166 | * Returns: Zero if all went well, negative on failure. |
167 | * |
168 | * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined |
169 | * in PKCS#1 v. 2.0 (RFC2437). |
170 | */ |
171 | |
172 | int pkcs1_sigencode(const void *msg, size_t msz, void *buf, size_t sz, |
173 | void *p) |
174 | { |
175 | pkcs1 *pp = p; |
176 | octet *q, *qq; |
177 | size_t n; |
178 | |
179 | /* --- Ensure that the buffer is sensibly sized --- */ |
180 | |
181 | if (pp->epsz + msz + 11 > sz) |
182 | return (-1); |
183 | |
184 | /* --- Fill in the buffer --- */ |
185 | |
186 | q = buf; |
187 | qq = q + sz; |
188 | *q++ = 0; |
189 | *q++ = 1; |
190 | n = sz - msz - pp->epsz - 3; |
191 | memset(q, 0xff, n); |
192 | q += n; |
193 | *q++ = 0; |
194 | memcpy(q, pp->ep, pp->epsz); |
195 | q += pp->epsz; |
196 | memcpy(q, msg, msz); |
197 | return (0); |
198 | } |
199 | |
200 | /* --- @pkcs1_sigdecode@ --- * |
201 | * |
202 | * Arguments: @const void *buf@ = pointer to encoded buffer |
203 | * @size_t sz@ = size of the encoded buffer |
204 | * @dstr *d@ = pointer to destination string |
205 | * @void *p@ = pointer to PKCS1 parameter block |
206 | * |
207 | * Returns: The length of the output string if successful, negative on |
208 | * failure. |
209 | * |
210 | * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined |
211 | * in PKCS#1 v. 2.0 (RFC2437). |
212 | */ |
213 | |
214 | int pkcs1_sigdecode(const void *buf, size_t sz, dstr *d, void *p) |
215 | { |
216 | pkcs1 *pp = p; |
217 | const octet *q, *qq; |
218 | size_t i, n; |
219 | |
220 | /* --- Check the size of the block looks sane --- */ |
221 | |
222 | if (pp->epsz + 10 > sz) |
223 | return (-1); |
224 | q = buf; |
7629bca8 |
225 | qq = q + sz; |
cd6c3eeb |
226 | |
227 | /* --- Ensure that the block looks OK --- */ |
228 | |
229 | if (*q++ != 0 || *q++ != 1) |
230 | return (-1); |
231 | |
232 | /* --- Check the padding --- */ |
233 | |
234 | i = 0; |
235 | while (*q == 0xff && q < qq) |
236 | i++, q++; |
0586d2a1 |
237 | if (i < 8 || qq - q < pp->epsz + 1 || *q++ != 0) |
cd6c3eeb |
238 | return (-1); |
cd6c3eeb |
239 | |
240 | /* --- Check the encoding parameters --- */ |
241 | |
242 | if (memcmp(q, pp->ep, pp->epsz) != 0) |
243 | return (-1); |
244 | q += pp->epsz; |
245 | |
246 | /* --- Done --- */ |
247 | |
248 | n = qq - q; |
249 | dstr_putm(d, q, n); |
250 | return (n); |
251 | } |
252 | |
253 | /*----- That's all, folks -------------------------------------------------*/ |