3 * $Id: rsa-pub.c,v 1.1 2000/07/01 11:23:52 mdw Exp $
5 * [RSA encryption with padding *
6 * (c) 2000 Straylight/Edgeware
9 /*----- Licensing notice --------------------------------------------------*
11 * This file is part of Catacomb.
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.
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.
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,
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2000/07/01 11:23:52 mdw
33 * Public-key operations, for symmetry with `rsa-priv.c'. Functions for
34 * doing padded RSA encryption and signature verification.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/alloc.h>
41 #include <mLib/bits.h>
42 #include <mLib/dstr.h>
48 /*----- Public key operations ---------------------------------------------*/
50 /* --- @rsa_pubcreate@ --- *
52 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
53 * @rsa_pub *rp@ = pointer to RSA public key
57 * Use: Initializes an RSA public-key context.
60 void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp)
63 mpmont_create(&rd->mm, rp->n);
66 /* --- @rsa_pubdestroy@ --- *
68 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
72 * Use: Destroys an RSA public-key context.
75 void rsa_pubdestroy(rsa_pubctx *rd)
77 mpmont_destroy(&rd->mm);
80 /* --- @rsa_pubop@ --- *
82 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
83 * @mp *d@ = destination
84 * @mp *p@ = input message
86 * Returns: The transformed output message.
88 * Use: Performs an RSA public key operation.
91 mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p)
93 return (mpmont_exp(&rd->mm, d, p, rd->rp->e));
96 /* --- @rsa_qpubop@ --- *
98 * Arguments: @rsa_pub *rp@ = pointer to RSA parameters
99 * @mp *d@ = destination
100 * @mp *p@ = input message
102 * Returns: Correctly transformed output message.
104 * Use: Performs an RSA public key operation.
107 mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c)
110 rsa_pubcreate(&rd, rp);
111 d = rsa_pubop(&rd, d, c);
116 /*----- Operations with padding -------------------------------------------*/
118 /* --- @rsa_encrypt@ --- *
120 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
121 * @const void *m@ = pointer to input message
122 * @size_t sz@ = size of input message
123 * @dstr *d@ = pointer to output string
124 * @rsa_encodeproc e@ = encoding procedure
125 * @void *earg@ = argument pointer for encoding procedure
127 * Returns: The length of the output string if successful, negative on
130 * Use: Does RSA encryption.
133 int rsa_encrypt(rsa_pubctx *rp, const void *m, size_t sz,
134 dstr *d, rsa_encodeproc e, void *earg)
137 size_t n = mp_octets(rp->rp->n);
141 /* --- Sort out some space --- */
147 /* --- Do the packing --- */
149 if ((rc = e(m, sz, p, n, earg)) < 0)
152 /* --- Do the encryption --- */
154 x = mp_loadb(MP_NEWSEC, p, n);
155 x = rsa_pubop(rp, x, x);
162 /* --- @rsa_verify@ --- *
164 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
165 * @const void *m@ = pointer to input message
166 * @size_t sz@ = size of input message
167 * @dstr *d@ = pointer to output string
168 * @rsa_decodeproc e@ = decoding procedure
169 * @void *earg@ = argument pointer for decoding procedure
171 * Returns: The length of the output string if successful, negative on
174 * Use: Does RSA signature verification.
177 int rsa_verify(rsa_pubctx *rp, const void *m, size_t sz,
178 dstr *d, rsa_decodeproc e, void *earg)
181 size_t n = mp_octets(rp->rp->n);
185 /* --- Do the exponentiation --- */
187 p = x_alloc(d->a, n);
188 x = mp_loadb(MP_NEW, m, sz);
189 x = rsa_pubop(rp, x, x);
193 /* --- Do the decoding --- */
195 rc = e(p, n, d, earg);
200 /*----- That's all, folks -------------------------------------------------*/