chiark / gitweb /
rand/rand.c (rand_gate): Evolve r->ibits in a more sensible manner.
[catacomb] / pub / oaep.c
1 /* -*-c-*-
2  *
3  * Optimal asymmetric encryption packing
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <string.h>
31
32 #include <mLib/alloc.h>
33 #include <mLib/bits.h>
34 #include <mLib/dstr.h>
35
36 #include "ct.h"
37 #include "gcipher.h"
38 #include "ghash.h"
39 #include "grand.h"
40 #include "rsa.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @oaep_encode@ --- *
45  *
46  * Arguments:   @mp *d@ = where to put the answer
47  *              @const void *m@ = pointer to message data
48  *              @size_t msz@ = size of message data
49  *              @octet *b@ = spare buffer
50  *              @size_t sz@ = size of the buffer (big enough)
51  *              @unsigned long nbits@ = length of bits of @n@
52  *              @void *p@ = pointer to OAEP parameter block
53  *
54  * Returns:     The encoded plaintext, or null on failure.
55  *
56  * Use:         Implements the operation @EME-OAEP-ENCODE@, as defined in
57  *              PKCS#1 v. 2.0 (RFC2437).
58  */
59
60 mp *oaep_encode(mp *d, const void *m, size_t msz, octet *b, size_t sz,
61                 unsigned long nbits, void *p)
62 {
63   oaep *o = p;
64   size_t hsz = o->ch->hashsz;
65   ghash *h;
66   octet *q, *mq;
67   octet *pp;
68   gcipher *c;
69   size_t n;
70
71   /* --- Ensure that everything is sensibly sized --- */
72
73   if (2 * hsz + 2 + msz > sz)
74     return (0);
75
76   /* --- Make the `seed' value --- */
77
78   q = b;
79   *q++ = 0; sz--;
80   mq = q + hsz;
81   GR_FILL(o->r, q, hsz);
82
83   /* --- Fill in the rest of the buffer --- */
84
85   h = GH_INIT(o->ch);
86   GH_HASH(h, o->ep, o->epsz);
87   GH_DONE(h, mq);
88   GH_DESTROY(h);
89   pp = mq + hsz;
90   n = sz - 2 * hsz - msz - 1;
91   memset(pp, 0, n);
92   pp += n;
93   *pp++ = 1;
94   memcpy(pp, m, msz);
95
96   /* --- Do the packing --- */
97
98   n = sz - hsz;
99   c = GC_INIT(o->cc, q, hsz);
100   GC_ENCRYPT(c, mq, mq, n);
101   GC_DESTROY(c);
102
103   c = GC_INIT(o->cc, mq, n);
104   GC_ENCRYPT(c, q, q, hsz);
105   GC_DESTROY(c);
106
107   /* --- Done --- */
108
109   return (mp_loadb(d, b, sz + 1));
110 }
111
112 /* --- @oaep_decode@ --- *
113  *
114  * Arguments:   @mp *m@ = the decrypted message
115  *              @octet *b@ = pointer to a buffer to work in
116  *              @size_t sz@ = the size of the buffer (big enough)
117  *              @unsigned long nbits@ = the number of bits in @n@
118  *              @void *p@ = pointer to OAEP parameter block
119  *
120  * Returns:     The length of the output string if successful, negative on
121  *              failure.
122  *
123  * Use:         Implements the operation @EME-OAEP-DECODE@, as defined in
124  *              PKCS#1 v. 2.0 (RFC2437).
125  */
126
127 int oaep_decode(mp *m, octet *b, size_t sz, unsigned long nbits, void *p)
128 {
129   oaep *o = p;
130   gcipher *c;
131   ghash *h;
132   octet *q, *mq, *qq;
133   octet *pp;
134   uint32 goodp = 1;
135   size_t n;
136   size_t hsz = o->ch->hashsz;
137
138   /* --- Ensure that the block is large enough --- */
139
140   if (sz < 2 * hsz)                     /* Doesn't depend on ciphertext */
141     return (-1);
142
143   /* --- Decrypt the message --- */
144
145   mp_storeb(m, b, sz);
146   q = b;
147   goodp &= ct_inteq(*q, 0);
148   q++; sz--;
149   mq = q + hsz;
150   qq = q + sz;
151   n = sz - hsz;
152   c = GC_INIT(o->cc, mq, n);
153   GC_DECRYPT(c, q, q, hsz);
154   GC_DESTROY(c);
155
156   c = GC_INIT(o->cc, q, hsz);
157   GC_DECRYPT(c, mq, mq, n);
158   GC_DESTROY(c);
159   q--;
160
161   /* --- Check the hash on the encoding parameters --- */
162
163   h = GH_INIT(o->ch);
164   GH_HASH(h, o->ep, o->epsz);
165   GH_DONE(h, q);
166   GH_DESTROY(h);
167   goodp &= ct_memeq(q, mq, hsz);
168
169   /* --- Now find the start of the actual message --- */
170
171   pp = mq + hsz;
172   while (*pp == 0 && pp < qq)
173     pp++;
174   goodp &= ~ct_intle(qq - b, pp - b);
175   goodp &= ct_inteq(*pp, 1);
176   pp++;
177   n = qq - pp;
178   memmove(q, pp, n);
179   return (goodp ? n : -1);
180 }
181
182 /*----- That's all, folks -------------------------------------------------*/