Commit | Line | Data |
---|---|---|
cd6c3eeb | 1 | /* -*-c-*- |
2 | * | |
b817bfc6 | 3 | * $Id: pkcs1.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
cd6c3eeb | 4 | * |
5 | * PKCS#1 1.5 packing | |
6 | * | |
7 | * (c) 2000 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
cd6c3eeb | 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 | * |
cd6c3eeb | 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 | * |
cd6c3eeb | 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 | ||
cd6c3eeb | 30 | /*----- Header files ------------------------------------------------------*/ |
31 | ||
32 | #include <string.h> | |
33 | ||
34 | #include <mLib/bits.h> | |
35 | #include <mLib/dstr.h> | |
36 | ||
52f339e9 | 37 | #include "ct.h" |
cd6c3eeb | 38 | #include "grand.h" |
b817bfc6 | 39 | #include "rsa.h" |
cd6c3eeb | 40 | |
41 | /*----- Main code ---------------------------------------------------------*/ | |
42 | ||
43 | /* --- @pkcs1_cryptencode@ --- * | |
44 | * | |
b817bfc6 | 45 | * Arguments: @mp *d@ = where to put the answer |
46 | * @const void *m@ = pointer to message data | |
cd6c3eeb | 47 | * @size_t msz@ = size of message data |
b817bfc6 | 48 | * @octet *b@ = spare buffer |
49 | * @size_t sz@ = size of the buffer (big enough) | |
50 | * @unsigned long nbits@ = length of bits of @n@ | |
cd6c3eeb | 51 | * @void *p@ = pointer to PKCS1 parameter block |
52 | * | |
b817bfc6 | 53 | * Returns: The encoded result, or null. |
cd6c3eeb | 54 | * |
55 | * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined | |
56 | * in PKCS#1 v. 2.0 (RFC2437). | |
57 | */ | |
58 | ||
b817bfc6 | 59 | mp *pkcs1_cryptencode(mp *d, const void *m, size_t msz, octet *b, size_t sz, |
60 | unsigned long nbits, void *p) | |
cd6c3eeb | 61 | { |
62 | pkcs1 *pp = p; | |
63 | grand *r = pp->r; | |
b817bfc6 | 64 | octet *q; |
cd6c3eeb | 65 | size_t i, n; |
66 | ||
67 | /* --- Ensure that the buffer is sensibly sized --- */ | |
68 | ||
69 | if (pp->epsz + msz + 11 > sz) | |
b817bfc6 | 70 | return (0); |
cd6c3eeb | 71 | |
b817bfc6 | 72 | /* --- Allocate the buffer and fill it in --- */ |
cd6c3eeb | 73 | |
b817bfc6 | 74 | q = b; |
75 | *q++ = 0x00; | |
76 | *q++ = 0x02; | |
cd6c3eeb | 77 | n = sz - msz - pp->epsz - 3; |
b817bfc6 | 78 | GR_FILL(r, q, n); |
cd6c3eeb | 79 | for (i = 0; i < n; i++) { |
80 | if (*q == 0) | |
81 | *q = r->ops->range(r, 255) + 1; | |
82 | q++; | |
83 | } | |
84 | *q++ = 0; | |
b817bfc6 | 85 | if (pp->ep) { |
86 | memcpy(q, pp->ep, pp->epsz); | |
87 | q += pp->epsz; | |
88 | } | |
89 | memcpy(q, m, msz); | |
90 | q += msz; | |
91 | assert(q == b + sz); | |
92 | ||
93 | /* --- Collect the result --- */ | |
45c0fd36 | 94 | |
b817bfc6 | 95 | return (mp_loadb(d, b, sz)); |
cd6c3eeb | 96 | } |
97 | ||
98 | /* --- @pkcs1_cryptdecode@ --- * | |
99 | * | |
b817bfc6 | 100 | * Arguments: @mp *m@ = the decrypted message |
101 | * @octet *b@ = pointer to a buffer to work in | |
102 | * @size_t sz@ = the size of the buffer (big enough) | |
103 | * @unsigned long nbits@ = the number of bits in @n@ | |
cd6c3eeb | 104 | * @void *p@ = pointer to PKCS1 parameter block |
105 | * | |
106 | * Returns: The length of the output string if successful, negative on | |
107 | * failure. | |
108 | * | |
109 | * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined | |
110 | * in PKCS#1 v. 2.0 (RFC2437). | |
111 | */ | |
112 | ||
b817bfc6 | 113 | int pkcs1_cryptdecode(mp *m, octet *b, size_t sz, |
114 | unsigned long nbits, void *p) | |
cd6c3eeb | 115 | { |
116 | pkcs1 *pp = p; | |
117 | const octet *q, *qq; | |
118 | size_t n, i; | |
52f339e9 | 119 | uint32 goodp = 1; |
cd6c3eeb | 120 | |
121 | /* --- Check the size of the block looks sane --- */ | |
122 | ||
b817bfc6 | 123 | if (pp->epsz + 11 > sz) /* OK: independent of ciphertext */ |
cd6c3eeb | 124 | return (-1); |
b817bfc6 | 125 | mp_storeb(m, b, sz); |
126 | q = b; | |
7629bca8 | 127 | qq = q + sz; |
cd6c3eeb | 128 | |
129 | /* --- Ensure that the block looks OK --- */ | |
130 | ||
52f339e9 MW |
131 | goodp &= ct_inteq(*q++, 0); |
132 | goodp &= ct_inteq(*q++, 2); | |
cd6c3eeb | 133 | |
134 | /* --- Check the nonzero padding --- */ | |
135 | ||
136 | i = 0; | |
137 | while (*q != 0 && q < qq) | |
138 | i++, q++; | |
52f339e9 MW |
139 | goodp &= ct_intle(8, i); |
140 | goodp &= ~ct_intle(qq - q, pp->epsz + 1); | |
cd6c3eeb | 141 | q++; |
142 | ||
143 | /* --- Check the encoding parameters --- */ | |
144 | ||
52f339e9 MW |
145 | if (pp->ep) |
146 | goodp &= ct_memeq(b + ct_pick(goodp, 0, q - b), pp->ep, pp->epsz); | |
cd6c3eeb | 147 | q += pp->epsz; |
148 | ||
149 | /* --- Done --- */ | |
150 | ||
151 | n = qq - q; | |
52f339e9 MW |
152 | memmove(b, b + ct_pick(goodp, 1, q - b), n); |
153 | return (goodp ? n : -1); | |
cd6c3eeb | 154 | } |
155 | ||
156 | /* --- @pkcs1_sigencode@ --- * | |
157 | * | |
b817bfc6 | 158 | * Arguments: @mp *d@ = where to put the answer |
159 | * @const void *m@ = pointer to message data | |
cd6c3eeb | 160 | * @size_t msz@ = size of message data |
b817bfc6 | 161 | * @octet *b@ = spare buffer |
162 | * @size_t sz@ = size of the buffer (big enough) | |
163 | * @unsigned long nbits@ = length of bits of @n@ | |
cd6c3eeb | 164 | * @void *p@ = pointer to PKCS1 parameter block |
165 | * | |
b817bfc6 | 166 | * Returns: The encoded message representative, or null. |
cd6c3eeb | 167 | * |
168 | * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined | |
169 | * in PKCS#1 v. 2.0 (RFC2437). | |
170 | */ | |
171 | ||
b817bfc6 | 172 | mp *pkcs1_sigencode(mp *d, const void *m, size_t msz, octet *b, size_t sz, |
173 | unsigned long nbits, void *p) | |
cd6c3eeb | 174 | { |
175 | pkcs1 *pp = p; | |
b817bfc6 | 176 | octet *q; |
cd6c3eeb | 177 | size_t n; |
178 | ||
179 | /* --- Ensure that the buffer is sensibly sized --- */ | |
180 | ||
181 | if (pp->epsz + msz + 11 > sz) | |
b817bfc6 | 182 | return (0); |
cd6c3eeb | 183 | |
184 | /* --- Fill in the buffer --- */ | |
185 | ||
b817bfc6 | 186 | q = b; |
187 | *q++ = 0x00; | |
188 | *q++ = 0x01; | |
cd6c3eeb | 189 | n = sz - msz - pp->epsz - 3; |
190 | memset(q, 0xff, n); | |
191 | q += n; | |
192 | *q++ = 0; | |
b817bfc6 | 193 | if (pp->ep) { |
194 | memcpy(q, pp->ep, pp->epsz); | |
195 | q += pp->epsz; | |
196 | } | |
197 | memcpy(q, m, msz); | |
198 | q += msz; | |
199 | assert(q == b + sz); | |
200 | return (mp_loadb(d, b, sz)); | |
cd6c3eeb | 201 | } |
202 | ||
203 | /* --- @pkcs1_sigdecode@ --- * | |
204 | * | |
b817bfc6 | 205 | * Arguments: @mp *s@ = the message representative |
206 | * @const void *m@ = the original message, or null (ignored) | |
207 | * @size_t msz@ = the message size (ignored) | |
208 | * @octet *b@ = a scratch buffer | |
209 | * @size_t sz@ = size of the buffer (large enough) | |
210 | * @unsigned long nbits@ = number of bits in @n@ | |
211 | * @void *p@ = pointer to PKCS1 parameters | |
cd6c3eeb | 212 | * |
213 | * Returns: The length of the output string if successful, negative on | |
214 | * failure. | |
215 | * | |
216 | * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined | |
217 | * in PKCS#1 v. 2.0 (RFC2437). | |
218 | */ | |
219 | ||
b817bfc6 | 220 | int pkcs1_sigdecode(mp *s, const void *m, size_t msz, octet *b, size_t sz, |
221 | unsigned long nbits, void *p) | |
cd6c3eeb | 222 | { |
223 | pkcs1 *pp = p; | |
224 | const octet *q, *qq; | |
225 | size_t i, n; | |
226 | ||
227 | /* --- Check the size of the block looks sane --- */ | |
228 | ||
229 | if (pp->epsz + 10 > sz) | |
230 | return (-1); | |
b817bfc6 | 231 | mp_storeb(s, b, sz); |
232 | q = b; | |
7629bca8 | 233 | qq = q + sz; |
cd6c3eeb | 234 | |
235 | /* --- Ensure that the block looks OK --- */ | |
236 | ||
b817bfc6 | 237 | if (*q++ != 0x00 || *q++ != 0x01) |
cd6c3eeb | 238 | return (-1); |
239 | ||
240 | /* --- Check the padding --- */ | |
241 | ||
242 | i = 0; | |
243 | while (*q == 0xff && q < qq) | |
244 | i++, q++; | |
0586d2a1 | 245 | if (i < 8 || qq - q < pp->epsz + 1 || *q++ != 0) |
cd6c3eeb | 246 | return (-1); |
cd6c3eeb | 247 | |
248 | /* --- Check the encoding parameters --- */ | |
249 | ||
b817bfc6 | 250 | if (pp->ep && memcmp(q, pp->ep, pp->epsz) != 0) |
cd6c3eeb | 251 | return (-1); |
252 | q += pp->epsz; | |
253 | ||
254 | /* --- Done --- */ | |
255 | ||
256 | n = qq - q; | |
b817bfc6 | 257 | memmove(b, q, n); |
cd6c3eeb | 258 | return (n); |
259 | } | |
260 | ||
261 | /*----- That's all, folks -------------------------------------------------*/ |