/* -*-c-*-
*
- * $Id: rsa-priv.c,v 1.3 2001/06/16 12:56:38 mdw Exp $
+ * $Id: rsa-priv.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
*
* RSA private-key operations
*
* (c) 1999 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: rsa-priv.c,v $
- * Revision 1.3 2001/06/16 12:56:38 mdw
- * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
- *
- * Revision 1.2 2000/10/08 12:11:22 mdw
- * Use @MP_EQ@ instead of @MP_CMP@.
- *
- * Revision 1.1 2000/07/01 11:23:20 mdw
- * Renamed from `rsa-decrypt', since the name was no longer appropriate.
- * Add functions for doing padded RSA decryption and signing.
- *
- * --- Previous lives as rsa-decrypt.c ---
- *
- * Revision 1.2 2000/06/17 11:57:56 mdw
- * Improve bulk performance by making better use of Montgomery
- * multiplication and separating out initialization and finalization from
- * the main code.
- *
- * Revision 1.1 1999/12/22 15:50:45 mdw
- * Initial RSA support.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <mLib/alloc.h>
/* --- @rsa_sign@ --- *
*
* Arguments: @rsa_privctx *rp@ = pointer to an RSA private key context
+ * @mp *d@ = where to put the result
* @const void *m@ = pointer to input message
- * @size_t sz@ = size of input message
- * @dstr *d@ = pointer to output string
- * @rsa_encodeproc e@ = encoding procedure
+ * @size_t msz@ = size of input message
+ * @rsa_pad *e@ = encoding procedure
* @void *earg@ = argument pointer for encoding procedure
*
- * Returns: The length of the output string if successful, negative on
+ * Returns: The signature, as a multiprecision integer, or null on
* failure.
*
* Use: Computes an RSA digital signature.
*/
-int rsa_sign(rsa_privctx *rp, const void *m, size_t sz,
- dstr *d, rsa_encodeproc e, void *earg)
+mp *rsa_sign(rsa_privctx *rp, mp *d, const void *m, size_t msz,
+ rsa_pad *e, void *earg)
{
- mp *x;
- size_t n = mp_octets(rp->rp->n);
octet *p;
- int rc;
-
- /* --- Sort out some space --- */
-
- dstr_ensure(d, n);
- p = (octet *)d->buf + d->len;
- p[0] = 0;
-
- /* --- Do the packing --- */
-
- if ((rc = e(m, sz, p, n, earg)) < 0)
- return (rc);
-
- /* --- Do the encryption --- */
-
- x = mp_loadb(MP_NEWSEC, p, n);
- x = rsa_privop(rp, x, x);
- mp_storeb(x, p, n);
- d->len += n;
- mp_drop(x);
- return (n);
+ unsigned long nb = mp_bits(rp->rp->n);
+ size_t n = (nb + 7)/8;
+ arena *a = d && d->a ? d->a->a : arena_global;
+
+ p = x_alloc(a, n);
+ d = e(d, m, msz, p, n, nb, earg);
+ x_free(a, p);
+ return (d ? rsa_privop(rp, d, d) : 0);
}
/* --- @rsa_decrypt@ --- *
*
* Arguments: @rsa_privctx *rp@ = pointer to an RSA private key context
- * @const void *m@ = pointer to input message
- * @size_t sz@ = size of input message
+ * @mp *m@ = encrypted message, as a multiprecision integer
* @dstr *d@ = pointer to output string
- * @rsa_decodeproc e@ = decoding procedure
+ * @rsa_decunpad *e@ = decoding procedure
* @void *earg@ = argument pointer for decoding procedure
*
* Returns: The length of the output string if successful, negative on
* failure.
*
- * Use: Does RSA signature verification.
+ * Use: Does RSA decryption.
*/
-int rsa_decrypt(rsa_privctx *rp, const void *m, size_t sz,
- dstr *d, rsa_decodeproc e, void *earg)
+int rsa_decrypt(rsa_privctx *rp, mp *m, dstr *d,
+ rsa_decunpad *e, void *earg)
{
- mp *x;
- size_t n = mp_octets(rp->rp->n);
- octet *p;
+ mp *p = rsa_privop(rp, MP_NEW, m);
+ unsigned long nb = mp_bits(rp->rp->n);
+ size_t n = (nb + 7)/8;
int rc;
- /* --- Do the exponentiation --- */
-
- p = x_alloc(d->a, n);
- x = mp_loadb(MP_NEW, m, sz);
- x = rsa_privop(rp, x, x);
- mp_storeb(x, p, n);
- mp_drop(x);
-
- /* --- Do the decoding --- */
-
- rc = e(p, n, d, earg);
- x_free(d->a, p);
+ dstr_ensure(d, n);
+ rc = e(p, (octet *)d->buf + d->len, n, nb, earg);
+ if (rc >= 0)
+ d->len += rc;
+ mp_drop(p);
return (rc);
}