From: mdw Date: Sat, 1 Jul 2000 11:23:52 +0000 (+0000) Subject: Public-key operations, for symmetry with `rsa-priv.c'. Functions for X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/395de3d134246a7a57f19994e60d4091c43172d0 Public-key operations, for symmetry with `rsa-priv.c'. Functions for doing padded RSA encryption and signature verification. --- diff --git a/rsa-pub.c b/rsa-pub.c new file mode 100644 index 0000000..f4b1125 --- /dev/null +++ b/rsa-pub.c @@ -0,0 +1,200 @@ +/* -*-c-*- + * + * $Id: rsa-pub.c,v 1.1 2000/07/01 11:23:52 mdw Exp $ + * + * [RSA encryption with padding * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * 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-pub.c,v $ + * Revision 1.1 2000/07/01 11:23:52 mdw + * Public-key operations, for symmetry with `rsa-priv.c'. Functions for + * doing padded RSA encryption and signature verification. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include + +#include "mp.h" +#include "mpmont.h" +#include "rsa.h" + +/*----- Public key operations ---------------------------------------------*/ + +/* --- @rsa_pubcreate@ --- * + * + * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context + * @rsa_pub *rp@ = pointer to RSA public key + * + * Returns: --- + * + * Use: Initializes an RSA public-key context. + */ + +void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp) +{ + rd->rp = rp; + mpmont_create(&rd->mm, rp->n); +} + +/* --- @rsa_pubdestroy@ --- * + * + * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context + * + * Returns: --- + * + * Use: Destroys an RSA public-key context. + */ + +void rsa_pubdestroy(rsa_pubctx *rd) +{ + mpmont_destroy(&rd->mm); +} + +/* --- @rsa_pubop@ --- * + * + * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context + * @mp *d@ = destination + * @mp *p@ = input message + * + * Returns: The transformed output message. + * + * Use: Performs an RSA public key operation. + */ + +mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p) +{ + return (mpmont_exp(&rd->mm, d, p, rd->rp->e)); +} + +/* --- @rsa_qpubop@ --- * + * + * Arguments: @rsa_pub *rp@ = pointer to RSA parameters + * @mp *d@ = destination + * @mp *p@ = input message + * + * Returns: Correctly transformed output message. + * + * Use: Performs an RSA public key operation. + */ + +mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c) +{ + rsa_pubctx rd; + rsa_pubcreate(&rd, rp); + d = rsa_pubop(&rd, d, c); + rsa_pubdestroy(&rd); + return (d); +} + +/*----- Operations with padding -------------------------------------------*/ + +/* --- @rsa_encrypt@ --- * + * + * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context + * @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 + * @void *earg@ = argument pointer for encoding procedure + * + * Returns: The length of the output string if successful, negative on + * failure. + * + * Use: Does RSA encryption. + */ + +int rsa_encrypt(rsa_pubctx *rp, const void *m, size_t sz, + dstr *d, rsa_encodeproc 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 = 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_pubop(rp, x, x); + mp_storeb(x, p, n); + d->len += n; + mp_drop(x); + return (n); +} + +/* --- @rsa_verify@ --- * + * + * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context + * @const void *m@ = pointer to input message + * @size_t sz@ = size of input message + * @dstr *d@ = pointer to output string + * @rsa_decodeproc 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. + */ + +int rsa_verify(rsa_pubctx *rp, const void *m, size_t sz, + dstr *d, rsa_decodeproc e, void *earg) +{ + mp *x; + size_t n = mp_octets(rp->rp->n); + octet *p; + int rc; + + /* --- Do the exponentiation --- */ + + p = x_alloc(d->a, n); + x = mp_loadb(MP_NEW, m, sz); + x = rsa_pubop(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); + return (rc); +} + +/*----- That's all, folks -------------------------------------------------*/