3 * $Id: rsa-pub.c,v 1.2 2000/10/08 16:00:32 mdw Exp $
5 * [RSA encryption with padding *
6 * (c) 2000 Straylight/Edgeware
9 /*----- Licensing notice --------------------------------------------------*
11 * This file is part of Catacomb.
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.
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.
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,
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 2000/10/08 16:00:32 mdw
33 * Fix compiler warning.
35 * Revision 1.1 2000/07/01 11:23:52 mdw
36 * Public-key operations, for symmetry with `rsa-priv.c'. Functions for
37 * doing padded RSA encryption and signature verification.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/alloc.h>
44 #include <mLib/bits.h>
45 #include <mLib/dstr.h>
51 /*----- Public key operations ---------------------------------------------*/
53 /* --- @rsa_pubcreate@ --- *
55 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
56 * @rsa_pub *rp@ = pointer to RSA public key
60 * Use: Initializes an RSA public-key context.
63 void rsa_pubcreate(rsa_pubctx
*rd
, rsa_pub
*rp
)
66 mpmont_create(&rd
->mm
, rp
->n
);
69 /* --- @rsa_pubdestroy@ --- *
71 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
75 * Use: Destroys an RSA public-key context.
78 void rsa_pubdestroy(rsa_pubctx
*rd
)
80 mpmont_destroy(&rd
->mm
);
83 /* --- @rsa_pubop@ --- *
85 * Arguments: @rsa_pubctx *rd@ = pointer to an RSA public key context
86 * @mp *d@ = destination
87 * @mp *p@ = input message
89 * Returns: The transformed output message.
91 * Use: Performs an RSA public key operation.
94 mp
*rsa_pubop(rsa_pubctx
*rd
, mp
*d
, mp
*p
)
96 return (mpmont_exp(&rd
->mm
, d
, p
, rd
->rp
->e
));
99 /* --- @rsa_qpubop@ --- *
101 * Arguments: @rsa_pub *rp@ = pointer to RSA parameters
102 * @mp *d@ = destination
103 * @mp *p@ = input message
105 * Returns: Correctly transformed output message.
107 * Use: Performs an RSA public key operation.
110 mp
*rsa_qpubop(rsa_pub
*rp
, mp
*d
, mp
*c
)
113 rsa_pubcreate(&rd
, rp
);
114 d
= rsa_pubop(&rd
, d
, c
);
119 /*----- Operations with padding -------------------------------------------*/
121 /* --- @rsa_encrypt@ --- *
123 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
124 * @const void *m@ = pointer to input message
125 * @size_t sz@ = size of input message
126 * @dstr *d@ = pointer to output string
127 * @rsa_encodeproc e@ = encoding procedure
128 * @void *earg@ = argument pointer for encoding procedure
130 * Returns: The length of the output string if successful, negative on
133 * Use: Does RSA encryption.
136 int rsa_encrypt(rsa_pubctx
*rp
, const void *m
, size_t sz
,
137 dstr
*d
, rsa_encodeproc e
, void *earg
)
140 size_t n
= mp_octets(rp
->rp
->n
);
144 /* --- Sort out some space --- */
147 p
= (octet
*)d
->buf
+ d
->len
;
150 /* --- Do the packing --- */
152 if ((rc
= e(m
, sz
, p
, n
, earg
)) < 0)
155 /* --- Do the encryption --- */
157 x
= mp_loadb(MP_NEWSEC
, p
, n
);
158 x
= rsa_pubop(rp
, x
, x
);
165 /* --- @rsa_verify@ --- *
167 * Arguments: @rsa_pubctx *rp@ = pointer to an RSA public key context
168 * @const void *m@ = pointer to input message
169 * @size_t sz@ = size of input message
170 * @dstr *d@ = pointer to output string
171 * @rsa_decodeproc e@ = decoding procedure
172 * @void *earg@ = argument pointer for decoding procedure
174 * Returns: The length of the output string if successful, negative on
177 * Use: Does RSA signature verification.
180 int rsa_verify(rsa_pubctx
*rp
, const void *m
, size_t sz
,
181 dstr
*d
, rsa_decodeproc e
, void *earg
)
184 size_t n
= mp_octets(rp
->rp
->n
);
188 /* --- Do the exponentiation --- */
190 p
= x_alloc(d
->a
, n
);
191 x
= mp_loadb(MP_NEW
, m
, sz
);
192 x
= rsa_pubop(rp
, x
, x
);
196 /* --- Do the decoding --- */
198 rc
= e(p
, n
, d
, earg
);
203 /*----- That's all, folks -------------------------------------------------*/