Use @MP_EQ@ instead of @MP_CMP@.
[u/mdw/catacomb] / oaep.c
1 /* -*-c-*-
2 *
3 * $Id: oaep.c,v 1.2 2000/07/15 10:01:48 mdw Exp $
4 *
5 * Optimal asymmetric encryption 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: oaep.c,v $
33 * Revision 1.2 2000/07/15 10:01:48 mdw
34 * Test rig added, based on RIPEMD160-MGF1 test vectors.
35 *
36 * Revision 1.1 2000/07/01 11:18:30 mdw
37 * Support for Optimal Asymmetric Encryption Padding.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <string.h>
44
45 #include <mLib/alloc.h>
46 #include <mLib/bits.h>
47 #include <mLib/dstr.h>
48
49 #include "gcipher.h"
50 #include "ghash.h"
51 #include "grand.h"
52 #include "oaep.h"
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @oaep_encode@ --- *
57 *
58 * Arguments: @const void *msg@ = pointer to message data
59 * @size_t msz@ = size of message data
60 * @void *buf@ = pointer to output buffer
61 * @size_t sz@ = size of the output buffer
62 * @void *p@ = pointer to OAEP parameter block
63 *
64 * Returns: Zero if all went well, negative on failure.
65 *
66 * Use: Implements the operation @EME-OAEP-ENCODE@, as defined in
67 * PKCS#1 v. 2.0 (RFC2437).
68 */
69
70 int oaep_encode(const void *msg, size_t msz, void *buf, size_t sz, void *p)
71 {
72 oaep *o = p;
73 size_t hsz = o->ch->hashsz;
74 ghash *h = o->ch->init();
75 octet *q, *mq, *qq;
76 octet *pp;
77 gcipher *c;
78 size_t n;
79
80 /* --- Ensure that everything is sensibly sized --- */
81
82 if (2 * hsz + 2 + msz > sz)
83 return (-1);
84
85 /* --- Make the `seed' value --- */
86
87 q = buf;
88 *q++ = 0; sz--;
89 mq = q + hsz;
90 qq = q + sz;
91 o->r->ops->fill(o->r, q, hsz);
92
93 /* --- Fill in the rest of the buffer --- */
94
95 h->ops->hash(h, o->ep, o->epsz);
96 h->ops->done(h, mq);
97 h->ops->destroy(h);
98 pp = mq + hsz;
99 n = sz - 2 * hsz - msz - 1;
100 memset(pp, 0, n);
101 pp += n;
102 *pp++ = 1;
103 memcpy(pp, msg, msz);
104
105 /* --- Do the packing --- */
106
107 n = sz - hsz;
108 c = o->cc->init(q, hsz);
109 c->ops->encrypt(c, mq, mq, n);
110 c->ops->destroy(c);
111
112 c = o->cc->init(mq, n);
113 c->ops->encrypt(c, q, q, hsz);
114 c->ops->destroy(c);
115
116 /* --- Done --- */
117
118 return (0);
119 }
120
121 /* --- @oaep_decode@ --- *
122 *
123 * Arguments: @const void *buf@ = pointer to encoded buffer
124 * @size_t sz@ = size of the encoded buffer
125 * @dstr *d@ = pointer to destination string
126 * @void *p@ = pointer to OAEP parameter block
127 *
128 * Returns: The length of the output string if successful, negative on
129 * failure.
130 *
131 * Use: Implements the operation @EME-OAEP-DECODE@, as defined in
132 * PKCS#1 v. 2.0 (RFC2437).
133 */
134
135 int oaep_decode(const void *buf, size_t sz, dstr *d, void *p)
136 {
137 oaep *o = p;
138 gcipher *c;
139 ghash *h;
140 octet *q, *mq, *qq;
141 octet *pp;
142 size_t n;
143 size_t hsz = o->ch->hashsz;
144 int rc = -1;
145
146 /* --- Ensure that the block is large enough --- */
147
148 if (sz < 2 * hsz)
149 return (-1);
150
151 q = x_alloc(d->a, sz);
152 memcpy(q, buf, sz);
153
154 /* --- Decrypt the message --- */
155
156 if (*q != 0)
157 goto fail;
158 q++; sz--;
159 mq = q + hsz;
160 qq = q + sz;
161 n = sz - hsz;
162 c = o->cc->init(mq, n);
163 c->ops->decrypt(c, q, q, hsz);
164 c->ops->destroy(c);
165
166 c = o->cc->init(q, hsz);
167 c->ops->decrypt(c, mq, mq, n);
168 c->ops->destroy(c);
169 q--;
170
171 /* --- Check the hash on the encoding parameters --- */
172
173 h = o->ch->init();
174 h->ops->hash(h, o->ep, o->epsz);
175 h->ops->done(h, q);
176 if (memcmp(q, mq, hsz) != 0)
177 goto fail;
178
179 /* --- Now find the start of the actual message --- */
180
181 pp = mq + hsz;
182 while (*pp == 0 && pp < qq)
183 pp++;
184 if (pp >= qq || *pp++ != 1)
185 return (-1);
186 n = qq - pp;
187 dstr_putm(d, pp, n);
188 rc = n;
189
190 fail:
191 x_free(d->a, q);
192 return (rc);
193 }
194
195 /*----- Test rig ----------------------------------------------------------*/
196
197 #ifdef TEST_RIG
198
199 #include <mLib/testrig.h>
200
201 #include "rmd160.h"
202 #include "rmd160-mgf.h"
203
204 typedef struct gctx {
205 grand r;
206 octet *buf;
207 } gctx;
208
209 static void rfill(grand *r, void *buf, size_t sz)
210 {
211 gctx *g = (gctx *)r;
212 memcpy(buf, g->buf, sz);
213 }
214
215 static const grand_ops gops = {
216 "const", 0, 0,
217 0, 0,
218 0, 0, 0, 0, rfill
219 };
220
221 static int verify(dstr *v)
222 {
223 gctx gr;
224 dstr d = DSTR_INIT;
225 oaep o;
226 int ok = 1;
227
228 dstr_ensure(&d, v[3].len);
229 d.len = v[3].len;
230 gr.r.ops = &gops;
231 gr.buf = v[2].buf;
232
233 o.cc = &rmd160_mgf;
234 o.ch = &rmd160;
235 o.r = &gr.r;
236 o.ep = v[1].buf;
237 o.epsz = v[1].len;
238
239 if (oaep_encode(v[0].buf, v[0].len, d.buf, d.len, &o) ||
240 memcmp(d.buf, v[3].buf, d.len) != 0) {
241 ok = 0;
242 fputs("\nfailure in oaep_encode", stderr);
243 fputs("\n message = ", stderr); type_hex.dump(&v[0], stderr);
244 fputs("\n params = ", stderr); type_hex.dump(&v[1], stderr);
245 fputs("\n salt = ", stderr); type_hex.dump(&v[2], stderr);
246 fputs("\nexpected = ", stderr); type_hex.dump(&v[3], stderr);
247 fputs("\n output = ", stderr); type_hex.dump(&d, stderr);
248 fputc('\n', stderr);
249 }
250
251 DRESET(&d);
252 if (oaep_decode(v[3].buf, v[3].len, &d, &o) < 0 ||
253 d.len != v[0].len || memcmp(d.buf, v[0].buf, d.len) != 0) {
254 ok = 0;
255 fputs("\nfailure in oaep_decode", stderr);
256 fputs("\n goop = ", stderr); type_hex.dump(&v[3], stderr);
257 fputs("\n params = ", stderr); type_hex.dump(&v[1], stderr);
258 fputs("\n salt = ", stderr); type_hex.dump(&v[2], stderr);
259 fputs("\nexpected = ", stderr); type_hex.dump(&v[0], stderr);
260 fputs("\n output = ", stderr); type_hex.dump(&d, stderr);
261 fputc('\n', stderr);
262 }
263
264 dstr_destroy(&d);
265 return (ok);
266 }
267
268 static test_chunk tests[] = {
269 { "oaep", verify, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } },
270 { 0, 0, { 0 } }
271 };
272
273 int main(int argc, char *argv[])
274 {
275 test_run(argc, argv, tests, SRCDIR "/tests/oaep");
276 return (0);
277 }
278
279 #endif
280
281 /*----- That's all, folks -------------------------------------------------*/