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