3 * [RSA encryption with padding *
4 * (c) 2000 Straylight/Edgeware
7 /*----- Licensing notice --------------------------------------------------*
9 * This file is part of Catacomb.
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.
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.
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,
27 /*----- Header files ------------------------------------------------------*/
29 #include <mLib/alloc.h>
30 #include <mLib/bits.h>
31 #include <mLib/dstr.h>
37 /*----- Public key operations ---------------------------------------------*/
39 /* --- @rsa_pubcreate@ --- *
41 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
42 * @rsa_pub *rp@ = pointer to RSA public key
46 * Use: Initializes an RSA public-key context.
49 void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp)
52 mpmont_create(&rd->mm, rp->n);
55 /* --- @rsa_pubdestroy@ --- *
57 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
61 * Use: Destroys an RSA public-key context.
64 void rsa_pubdestroy(rsa_pubctx *rd)
66 mpmont_destroy(&rd->mm);
69 /* --- @rsa_pubop@ --- *
71 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
72 * @mp *d@ = destination
73 * @mp *p@ = input message
75 * Returns: The transformed output message.
77 * Use: Performs an RSA public key operation.
80 mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p)
82 return (mpmont_exp(&rd->mm, d, p, rd->rp->e));
85 /* --- @rsa_qpubop@ --- *
87 * Arguments: @rsa_pub *rp@ = pointer to RSA parameters
88 * @mp *d@ = destination
89 * @mp *p@ = input message
91 * Returns: Correctly transformed output message.
93 * Use: Performs an RSA public key operation.
96 mp *rsa_qpubop(rsa_pub *rp, mp *d, mp *c)
99 rsa_pubcreate(&rd, rp);
100 d = rsa_pubop(&rd, d, c);
105 /*----- Operations with padding -------------------------------------------*/
107 /* --- @rsa_encrypt@ --- *
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
116 * Returns: The encrypted message, as a multiprecision integer, or null
119 * Use: Does RSA encryption.
122 mp *rsa_encrypt(rsa_pubctx *rp, mp *d, const void *m, size_t msz,
123 rsa_pad *e, void *earg)
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;
131 d = e(d, m, msz, p, n, nb, earg);
133 return (d ? rsa_pubop(rp, d, d) : 0);
136 /* --- @rsa_verify@ --- *
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
146 * Returns: The length of the output string if successful (0 if no output
147 * was wanted); negative on failure.
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
156 int rsa_verify(rsa_pubctx *rp, mp *s, const void *m, size_t msz,
157 dstr *d, rsa_vrfunpad *e, void *earg)
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;
165 /* --- Decoder protocol --- *
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.
176 rc = e(p, m, msz, (octet *)d->buf + d->len, n, nb, earg);
178 if (rc != msz || memcmp(d->buf + d->len, m, msz) != 0)
190 /*----- That's all, folks -------------------------------------------------*/