Add an internal-representation no-op function.
[u/mdw/catacomb] / rsa-pub.c
1 /* -*-c-*-
2 *
3 * $Id: rsa-pub.c,v 1.2 2000/10/08 16:00:32 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 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: rsa-pub.c,v $
32 * Revision 1.2 2000/10/08 16:00:32 mdw
33 * Fix compiler warning.
34 *
35 * Revision 1.1 2000/07/01 11:23:52 mdw
36 * Public-key operations, for symmetry with `rsa-priv.c'. Functions for
37 * doing padded RSA encryption and signature verification.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/alloc.h>
44 #include <mLib/bits.h>
45 #include <mLib/dstr.h>
46
47 #include "mp.h"
48 #include "mpmont.h"
49 #include "rsa.h"
50
51 /*----- Public key operations ---------------------------------------------*/
52
53 /* --- @rsa_pubcreate@ --- *
54 *
55 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
56 * @rsa_pub *rp@ = pointer to RSA public key
57 *
58 * Returns: ---
59 *
60 * Use: Initializes an RSA public-key context.
61 */
62
63 void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp)
64 {
65 rd->rp = rp;
66 mpmont_create(&rd->mm, rp->n);
67 }
68
69 /* --- @rsa_pubdestroy@ --- *
70 *
71 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
72 *
73 * Returns: ---
74 *
75 * Use: Destroys an RSA public-key context.
76 */
77
78 void rsa_pubdestroy(rsa_pubctx *rd)
79 {
80 mpmont_destroy(&rd->mm);
81 }
82
83 /* --- @rsa_pubop@ --- *
84 *
85 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
86 * @mp *d@ = destination
87 * @mp *p@ = input message
88 *
89 * Returns: The transformed output message.
90 *
91 * Use: Performs an RSA public key operation.
92 */
93
94 mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p)
95 {
96 return (mpmont_exp(&rd->mm, d, p, rd->rp->e));
97 }
98
99 /* --- @rsa_qpubop@ --- *
100 *
101 * Arguments: @rsa_pub *rp@ = pointer to RSA parameters
102 * @mp *d@ = destination
103 * @mp *p@ = input message
104 *
105 * Returns: Correctly transformed output message.
106 *
107 * Use: Performs an RSA public key operation.
108 */
109
110 mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c)
111 {
112 rsa_pubctx rd;
113 rsa_pubcreate(&rd, rp);
114 d = rsa_pubop(&rd, d, c);
115 rsa_pubdestroy(&rd);
116 return (d);
117 }
118
119 /*----- Operations with padding -------------------------------------------*/
120
121 /* --- @rsa_encrypt@ --- *
122 *
123 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
124 * @const void *m@ = pointer to input message
125 * @size_t sz@ = size of input message
126 * @dstr *d@ = pointer to output string
127 * @rsa_encodeproc e@ = encoding procedure
128 * @void *earg@ = argument pointer for encoding procedure
129 *
130 * Returns: The length of the output string if successful, negative on
131 * failure.
132 *
133 * Use: Does RSA encryption.
134 */
135
136 int rsa_encrypt(rsa_pubctx *rp, const void *m, size_t sz,
137 dstr *d, rsa_encodeproc e, void *earg)
138 {
139 mp *x;
140 size_t n = mp_octets(rp->rp->n);
141 octet *p;
142 int rc;
143
144 /* --- Sort out some space --- */
145
146 dstr_ensure(d, n);
147 p = (octet *)d->buf + d->len;
148 p[0] = 0;
149
150 /* --- Do the packing --- */
151
152 if ((rc = e(m, sz, p, n, earg)) < 0)
153 return (rc);
154
155 /* --- Do the encryption --- */
156
157 x = mp_loadb(MP_NEWSEC, p, n);
158 x = rsa_pubop(rp, x, x);
159 mp_storeb(x, p, n);
160 d->len += n;
161 mp_drop(x);
162 return (n);
163 }
164
165 /* --- @rsa_verify@ --- *
166 *
167 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
168 * @const void *m@ = pointer to input message
169 * @size_t sz@ = size of input message
170 * @dstr *d@ = pointer to output string
171 * @rsa_decodeproc e@ = decoding procedure
172 * @void *earg@ = argument pointer for decoding procedure
173 *
174 * Returns: The length of the output string if successful, negative on
175 * failure.
176 *
177 * Use: Does RSA signature verification.
178 */
179
180 int rsa_verify(rsa_pubctx *rp, const void *m, size_t sz,
181 dstr *d, rsa_decodeproc e, void *earg)
182 {
183 mp *x;
184 size_t n = mp_octets(rp->rp->n);
185 octet *p;
186 int rc;
187
188 /* --- Do the exponentiation --- */
189
190 p = x_alloc(d->a, n);
191 x = mp_loadb(MP_NEW, m, sz);
192 x = rsa_pubop(rp, x, x);
193 mp_storeb(x, p, n);
194 mp_drop(x);
195
196 /* --- Do the decoding --- */
197
198 rc = e(p, n, d, earg);
199 x_free(d->a, p);
200 return (rc);
201 }
202
203 /*----- That's all, folks -------------------------------------------------*/