Merge branch '2.5.x'
[catacomb] / pub / rsa-pub.c
CommitLineData
395de3d1 1/* -*-c-*-
2 *
395de3d1 3 * [RSA encryption with padding *
4 * (c) 2000 Straylight/Edgeware
5 */
6
45c0fd36 7/*----- Licensing notice --------------------------------------------------*
395de3d1 8 *
9 * This file is part of Catacomb.
10 *
11 * Catacomb is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
45c0fd36 15 *
395de3d1 16 * Catacomb is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
45c0fd36 20 *
395de3d1 21 * You should have received a copy of the GNU Library General Public
22 * License along with Catacomb; if not, write to the Free
23 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * MA 02111-1307, USA.
25 */
26
395de3d1 27/*----- Header files ------------------------------------------------------*/
28
29#include <mLib/alloc.h>
30#include <mLib/bits.h>
31#include <mLib/dstr.h>
32
33#include "mp.h"
34#include "mpmont.h"
35#include "rsa.h"
36
37/*----- Public key operations ---------------------------------------------*/
38
39/* --- @rsa_pubcreate@ --- *
40 *
41 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
42 * @rsa_pub *rp@ = pointer to RSA public key
43 *
44 * Returns: ---
45 *
46 * Use: Initializes an RSA public-key context.
47 */
48
49void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp)
50{
791af0ba 51 rd->rp = rp; mp_shrink(rp->e);
395de3d1 52 mpmont_create(&rd->mm, rp->n);
53}
54
55/* --- @rsa_pubdestroy@ --- *
56 *
57 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
58 *
59 * Returns: ---
60 *
61 * Use: Destroys an RSA public-key context.
62 */
63
64void rsa_pubdestroy(rsa_pubctx *rd)
65{
66 mpmont_destroy(&rd->mm);
67}
68
69/* --- @rsa_pubop@ --- *
70 *
71 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
72 * @mp *d@ = destination
73 * @mp *p@ = input message
74 *
75 * Returns: The transformed output message.
76 *
77 * Use: Performs an RSA public key operation.
78 */
79
80mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p)
81{
791af0ba
MW
82 mp *e = rd->rp->e;
83 unsigned i;
84
85 if (MP_EQ(e, MP_THREE)) {
86 MP_COPY(p);
87 d = mpmont_mul(&rd->mm, d, p, rd->mm.r2);
88 d = mp_sqr(d, d); d = mpmont_reduce(&rd->mm, d, d);
89 d = mpmont_mul(&rd->mm, d, d, p);
90 MP_DROP(p);
91 return (d);
92 }
93#if MPW_BITS > 16
94 if (MP_LEN(e) == 1 && e->v[0] == 65537)
95#else
96 if (0 && MP_LEN(e) == 2 && e->v[0] == 1 && e->v[1] == (1 << (16 - MPW_BITS)))
97#endif
98 {
99 MP_COPY(p);
100 d = mpmont_mul(&rd->mm, d, p, rd->mm.r2);
101 for (i = 0; i < 16; i++)
102 { d = mp_sqr(d, d); d = mpmont_reduce(&rd->mm, d, d); }
103 d = mpmont_mul(&rd->mm, d, d, p);
104 MP_DROP(p);
105 return (d);
106 }
395de3d1 107 return (mpmont_exp(&rd->mm, d, p, rd->rp->e));
108}
109
110/* --- @rsa_qpubop@ --- *
111 *
112 * Arguments: @rsa_pub *rp@ = pointer to RSA parameters
113 * @mp *d@ = destination
114 * @mp *p@ = input message
115 *
116 * Returns: Correctly transformed output message.
117 *
118 * Use: Performs an RSA public key operation.
119 */
120
121mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c)
122{
123 rsa_pubctx rd;
124 rsa_pubcreate(&rd, rp);
125 d = rsa_pubop(&rd, d, c);
126 rsa_pubdestroy(&rd);
127 return (d);
128}
129
130/*----- Operations with padding -------------------------------------------*/
131
132/* --- @rsa_encrypt@ --- *
133 *
134 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
b817bfc6 135 * @mp *d@ = proposed destination integer
395de3d1 136 * @const void *m@ = pointer to input message
b817bfc6 137 * @size_t msz@ = size of input message
138 * @rsa_pad *e@ = encoding procedure
395de3d1 139 * @void *earg@ = argument pointer for encoding procedure
140 *
b817bfc6 141 * Returns: The encrypted message, as a multiprecision integer, or null
142 * on failure.
395de3d1 143 *
144 * Use: Does RSA encryption.
145 */
146
b817bfc6 147mp *rsa_encrypt(rsa_pubctx *rp, mp *d, const void *m, size_t msz,
148 rsa_pad *e, void *earg)
395de3d1 149{
395de3d1 150 octet *p;
b817bfc6 151 unsigned long nb = mp_bits(rp->rp->n);
152 size_t n = (nb + 7)/8;
153 arena *a = d && d->a ? d->a->a : arena_global;
154
155 p = x_alloc(a, n);
156 d = e(d, m, msz, p, n, nb, earg);
157 x_free(a, p);
158 return (d ? rsa_pubop(rp, d, d) : 0);
395de3d1 159}
160
161/* --- @rsa_verify@ --- *
162 *
b817bfc6 163 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key contxt
164 * @mp *s@ = the signature, as a multiprecision integer
165 * @const void *m@ = pointer to message to verify, or null
166 * @size_t msz@ = size of input message
167 * @dstr *d@ = pointer to output string, or null
168 * @rsa_vfrunpad *e@ = decoding procedure
395de3d1 169 * @void *earg@ = argument pointer for decoding procedure
170 *
b817bfc6 171 * Returns: The length of the output string if successful (0 if no output
172 * was wanted); negative on failure.
395de3d1 173 *
b817bfc6 174 * Use: Does RSA signature verification. To use a signature scheme
175 * with recovery, pass in @m == 0@ and @d != 0@: the recovered
176 * message should appear in @d@. To use a signature scheme with
177 * appendix, provide @m != 0@ and @d == 0@; the result should be
178 * zero for success.
395de3d1 179 */
180
b817bfc6 181int rsa_verify(rsa_pubctx *rp, mp *s, const void *m, size_t msz,
182 dstr *d, rsa_vrfunpad *e, void *earg)
395de3d1 183{
b817bfc6 184 mp *p = rsa_pubop(rp, MP_NEW, s);
185 unsigned long nb = mp_bits(rp->rp->n);
186 size_t n = (nb + 7)/8;
187 dstr dd = DSTR_INIT;
395de3d1 188 int rc;
189
b817bfc6 190 /* --- Decoder protocol --- *
191 *
192 * We deal with two kinds of decoders: ones with message recovery and ones
193 * with appendix. A decoder with recovery will leave a message in the
194 * buffer and exit nonzero: we'll check that against @m@ if provided and
195 * just leave it otherwise. A decoder with appendix will inspect @m@ and
196 * return zero or @-1@ itself.
197 */
395de3d1 198
b817bfc6 199 if (!d) d = &dd;
200 dstr_ensure(d, n);
201 rc = e(p, m, msz, (octet *)d->buf + d->len, n, nb, earg);
202 if (rc > 0 && m) {
203 if (rc != msz || memcmp(d->buf + d->len, m, msz) != 0)
204 rc = -1;
205 else
206 rc = 0;
207 }
208 if (rc > 0)
209 d->len += rc;
210 mp_drop(p);
211 dstr_destroy(&dd);
395de3d1 212 return (rc);
213}
214
215/*----- That's all, folks -------------------------------------------------*/