| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * [RSA encryption with padding * |
| 4 | * (c) 2000 Straylight/Edgeware |
| 5 | */ |
| 6 | |
| 7 | /*----- Licensing notice --------------------------------------------------* |
| 8 | * |
| 9 | * This file is part of Catacomb. |
| 10 | * |
| 11 | * Catacomb is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Library General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License, or (at your option) any later version. |
| 15 | * |
| 16 | * Catacomb is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Library General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Library General Public |
| 22 | * License along with Catacomb; if not, write to the Free |
| 23 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 24 | * MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include <mLib/alloc.h> |
| 30 | #include <mLib/bits.h> |
| 31 | #include <mLib/dstr.h> |
| 32 | |
| 33 | #include "mp.h" |
| 34 | #include "mpmont.h" |
| 35 | #include "rsa.h" |
| 36 | |
| 37 | /*----- Public key operations ---------------------------------------------*/ |
| 38 | |
| 39 | /* --- @rsa_pubcreate@ --- * |
| 40 | * |
| 41 | * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context |
| 42 | * @rsa_pub *rp@ = pointer to RSA public key |
| 43 | * |
| 44 | * Returns: --- |
| 45 | * |
| 46 | * Use: Initializes an RSA public-key context. |
| 47 | */ |
| 48 | |
| 49 | void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp) |
| 50 | { |
| 51 | rd->rp = rp; |
| 52 | mpmont_create(&rd->mm, rp->n); |
| 53 | } |
| 54 | |
| 55 | /* --- @rsa_pubdestroy@ --- * |
| 56 | * |
| 57 | * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context |
| 58 | * |
| 59 | * Returns: --- |
| 60 | * |
| 61 | * Use: Destroys an RSA public-key context. |
| 62 | */ |
| 63 | |
| 64 | void rsa_pubdestroy(rsa_pubctx *rd) |
| 65 | { |
| 66 | mpmont_destroy(&rd->mm); |
| 67 | } |
| 68 | |
| 69 | /* --- @rsa_pubop@ --- * |
| 70 | * |
| 71 | * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context |
| 72 | * @mp *d@ = destination |
| 73 | * @mp *p@ = input message |
| 74 | * |
| 75 | * Returns: The transformed output message. |
| 76 | * |
| 77 | * Use: Performs an RSA public key operation. |
| 78 | */ |
| 79 | |
| 80 | mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p) |
| 81 | { |
| 82 | return (mpmont_exp(&rd->mm, d, p, rd->rp->e)); |
| 83 | } |
| 84 | |
| 85 | /* --- @rsa_qpubop@ --- * |
| 86 | * |
| 87 | * Arguments: @rsa_pub *rp@ = pointer to RSA parameters |
| 88 | * @mp *d@ = destination |
| 89 | * @mp *p@ = input message |
| 90 | * |
| 91 | * Returns: Correctly transformed output message. |
| 92 | * |
| 93 | * Use: Performs an RSA public key operation. |
| 94 | */ |
| 95 | |
| 96 | mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c) |
| 97 | { |
| 98 | rsa_pubctx rd; |
| 99 | rsa_pubcreate(&rd, rp); |
| 100 | d = rsa_pubop(&rd, d, c); |
| 101 | rsa_pubdestroy(&rd); |
| 102 | return (d); |
| 103 | } |
| 104 | |
| 105 | /*----- Operations with padding -------------------------------------------*/ |
| 106 | |
| 107 | /* --- @rsa_encrypt@ --- * |
| 108 | * |
| 109 | * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context |
| 110 | * @mp *d@ = proposed destination integer |
| 111 | * @const void *m@ = pointer to input message |
| 112 | * @size_t msz@ = size of input message |
| 113 | * @rsa_pad *e@ = encoding procedure |
| 114 | * @void *earg@ = argument pointer for encoding procedure |
| 115 | * |
| 116 | * Returns: The encrypted message, as a multiprecision integer, or null |
| 117 | * on failure. |
| 118 | * |
| 119 | * Use: Does RSA encryption. |
| 120 | */ |
| 121 | |
| 122 | mp *rsa_encrypt(rsa_pubctx *rp, mp *d, const void *m, size_t msz, |
| 123 | rsa_pad *e, void *earg) |
| 124 | { |
| 125 | octet *p; |
| 126 | unsigned long nb = mp_bits(rp->rp->n); |
| 127 | size_t n = (nb + 7)/8; |
| 128 | arena *a = d && d->a ? d->a->a : arena_global; |
| 129 | |
| 130 | p = x_alloc(a, n); |
| 131 | d = e(d, m, msz, p, n, nb, earg); |
| 132 | x_free(a, p); |
| 133 | return (d ? rsa_pubop(rp, d, d) : 0); |
| 134 | } |
| 135 | |
| 136 | /* --- @rsa_verify@ --- * |
| 137 | * |
| 138 | * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key contxt |
| 139 | * @mp *s@ = the signature, as a multiprecision integer |
| 140 | * @const void *m@ = pointer to message to verify, or null |
| 141 | * @size_t msz@ = size of input message |
| 142 | * @dstr *d@ = pointer to output string, or null |
| 143 | * @rsa_vfrunpad *e@ = decoding procedure |
| 144 | * @void *earg@ = argument pointer for decoding procedure |
| 145 | * |
| 146 | * Returns: The length of the output string if successful (0 if no output |
| 147 | * was wanted); negative on failure. |
| 148 | * |
| 149 | * Use: Does RSA signature verification. To use a signature scheme |
| 150 | * with recovery, pass in @m == 0@ and @d != 0@: the recovered |
| 151 | * message should appear in @d@. To use a signature scheme with |
| 152 | * appendix, provide @m != 0@ and @d == 0@; the result should be |
| 153 | * zero for success. |
| 154 | */ |
| 155 | |
| 156 | int rsa_verify(rsa_pubctx *rp, mp *s, const void *m, size_t msz, |
| 157 | dstr *d, rsa_vrfunpad *e, void *earg) |
| 158 | { |
| 159 | mp *p = rsa_pubop(rp, MP_NEW, s); |
| 160 | unsigned long nb = mp_bits(rp->rp->n); |
| 161 | size_t n = (nb + 7)/8; |
| 162 | dstr dd = DSTR_INIT; |
| 163 | int rc; |
| 164 | |
| 165 | /* --- Decoder protocol --- * |
| 166 | * |
| 167 | * We deal with two kinds of decoders: ones with message recovery and ones |
| 168 | * with appendix. A decoder with recovery will leave a message in the |
| 169 | * buffer and exit nonzero: we'll check that against @m@ if provided and |
| 170 | * just leave it otherwise. A decoder with appendix will inspect @m@ and |
| 171 | * return zero or @-1@ itself. |
| 172 | */ |
| 173 | |
| 174 | if (!d) d = ⅆ |
| 175 | dstr_ensure(d, n); |
| 176 | rc = e(p, m, msz, (octet *)d->buf + d->len, n, nb, earg); |
| 177 | if (rc > 0 && m) { |
| 178 | if (rc != msz || memcmp(d->buf + d->len, m, msz) != 0) |
| 179 | rc = -1; |
| 180 | else |
| 181 | rc = 0; |
| 182 | } |
| 183 | if (rc > 0) |
| 184 | d->len += rc; |
| 185 | mp_drop(p); |
| 186 | dstr_destroy(&dd); |
| 187 | return (rc); |
| 188 | } |
| 189 | |
| 190 | /*----- That's all, folks -------------------------------------------------*/ |