3 * $Id: oaep.c,v 1.2 2000/07/15 10:01:48 mdw Exp $
5 * Optimal asymmetric encryption packing
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/07/15 10:01:48 mdw
34 * Test rig added, based on RIPEMD160-MGF1 test vectors.
36 * Revision 1.1 2000/07/01 11:18:30 mdw
37 * Support for Optimal Asymmetric Encryption Padding.
41 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/alloc.h>
46 #include <mLib/bits.h>
47 #include <mLib/dstr.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @oaep_encode@ --- *
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
64 * Returns: Zero if all went well, negative on failure.
66 * Use: Implements the operation @EME-OAEP-ENCODE@, as defined in
67 * PKCS#1 v. 2.0 (RFC2437).
70 int oaep_encode(const void *msg
, size_t msz
, void *buf
, size_t sz
, void *p
)
73 size_t hsz
= o
->ch
->hashsz
;
74 ghash
*h
= o
->ch
->init();
80 /* --- Ensure that everything is sensibly sized --- */
82 if (2 * hsz
+ 2 + msz
> sz
)
85 /* --- Make the `seed' value --- */
91 o
->r
->ops
->fill(o
->r
, q
, hsz
);
93 /* --- Fill in the rest of the buffer --- */
95 h
->ops
->hash(h
, o
->ep
, o
->epsz
);
99 n
= sz
- 2 * hsz
- msz
- 1;
103 memcpy(pp
, msg
, msz
);
105 /* --- Do the packing --- */
108 c
= o
->cc
->init(q
, hsz
);
109 c
->ops
->encrypt(c
, mq
, mq
, n
);
112 c
= o
->cc
->init(mq
, n
);
113 c
->ops
->encrypt(c
, q
, q
, hsz
);
121 /* --- @oaep_decode@ --- *
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
128 * Returns: The length of the output string if successful, negative on
131 * Use: Implements the operation @EME-OAEP-DECODE@, as defined in
132 * PKCS#1 v. 2.0 (RFC2437).
135 int oaep_decode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
143 size_t hsz
= o
->ch
->hashsz
;
146 /* --- Ensure that the block is large enough --- */
151 q
= x_alloc(d
->a
, sz
);
154 /* --- Decrypt the message --- */
162 c
= o
->cc
->init(mq
, n
);
163 c
->ops
->decrypt(c
, q
, q
, hsz
);
166 c
= o
->cc
->init(q
, hsz
);
167 c
->ops
->decrypt(c
, mq
, mq
, n
);
171 /* --- Check the hash on the encoding parameters --- */
174 h
->ops
->hash(h
, o
->ep
, o
->epsz
);
176 if (memcmp(q
, mq
, hsz
) != 0)
179 /* --- Now find the start of the actual message --- */
182 while (*pp
== 0 && pp
< qq
)
184 if (pp
>= qq
|| *pp
++ != 1)
195 /*----- Test rig ----------------------------------------------------------*/
199 #include <mLib/testrig.h>
202 #include "rmd160-mgf.h"
204 typedef struct gctx
{
209 static void rfill(grand
*r
, void *buf
, size_t sz
)
212 memcpy(buf
, g
->buf
, sz
);
215 static const grand_ops gops
= {
221 static int verify(dstr
*v
)
228 dstr_ensure(&d
, v
[3].len
);
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) {
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
);
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) {
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
);
268 static test_chunk tests
[] = {
269 { "oaep", verify
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
273 int main(int argc
, char *argv
[])
275 test_run(argc
, argv
, tests
, SRCDIR
"/tests/oaep");
281 /*----- That's all, folks -------------------------------------------------*/