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