1*de0e0e4dSAntonio Huete Jimenez /*	$OpenBSD: rsa_meth.c,v 1.5 2022/07/04 12:23:30 tb Exp $	*/
272c33676SMaxim Ag /*
372c33676SMaxim Ag  * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
472c33676SMaxim Ag  *
572c33676SMaxim Ag  * Permission to use, copy, modify, and distribute this software for any
672c33676SMaxim Ag  * purpose with or without fee is hereby granted, provided that the above
772c33676SMaxim Ag  * copyright notice and this permission notice appear in all copies.
872c33676SMaxim Ag  *
972c33676SMaxim Ag  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1072c33676SMaxim Ag  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1172c33676SMaxim Ag  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1272c33676SMaxim Ag  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1372c33676SMaxim Ag  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1472c33676SMaxim Ag  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1572c33676SMaxim Ag  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1672c33676SMaxim Ag  */
1772c33676SMaxim Ag 
1872c33676SMaxim Ag #include <stdlib.h>
1972c33676SMaxim Ag #include <string.h>
2072c33676SMaxim Ag 
2172c33676SMaxim Ag #include <openssl/err.h>
2272c33676SMaxim Ag #include <openssl/rsa.h>
2372c33676SMaxim Ag 
24*de0e0e4dSAntonio Huete Jimenez #include "rsa_locl.h"
25*de0e0e4dSAntonio Huete Jimenez 
2672c33676SMaxim Ag RSA_METHOD *
RSA_meth_new(const char * name,int flags)2772c33676SMaxim Ag RSA_meth_new(const char *name, int flags)
2872c33676SMaxim Ag {
2972c33676SMaxim Ag 	RSA_METHOD *meth;
3072c33676SMaxim Ag 
3172c33676SMaxim Ag 	if ((meth = calloc(1, sizeof(*meth))) == NULL)
3272c33676SMaxim Ag 		return NULL;
3372c33676SMaxim Ag 	if ((meth->name = strdup(name)) == NULL) {
3472c33676SMaxim Ag 		free(meth);
3572c33676SMaxim Ag 		return NULL;
3672c33676SMaxim Ag 	}
3772c33676SMaxim Ag 	meth->flags = flags;
3872c33676SMaxim Ag 
3972c33676SMaxim Ag 	return meth;
4072c33676SMaxim Ag }
4172c33676SMaxim Ag 
4272c33676SMaxim Ag void
RSA_meth_free(RSA_METHOD * meth)4372c33676SMaxim Ag RSA_meth_free(RSA_METHOD *meth)
4472c33676SMaxim Ag {
45*de0e0e4dSAntonio Huete Jimenez 	if (meth == NULL)
46*de0e0e4dSAntonio Huete Jimenez 		return;
47*de0e0e4dSAntonio Huete Jimenez 
48*de0e0e4dSAntonio Huete Jimenez 	free(meth->name);
4972c33676SMaxim Ag 	free(meth);
5072c33676SMaxim Ag }
5172c33676SMaxim Ag 
5272c33676SMaxim Ag RSA_METHOD *
RSA_meth_dup(const RSA_METHOD * meth)5372c33676SMaxim Ag RSA_meth_dup(const RSA_METHOD *meth)
5472c33676SMaxim Ag {
5572c33676SMaxim Ag 	RSA_METHOD *copy;
5672c33676SMaxim Ag 
5772c33676SMaxim Ag 	if ((copy = calloc(1, sizeof(*copy))) == NULL)
5872c33676SMaxim Ag 		return NULL;
5972c33676SMaxim Ag 	memcpy(copy, meth, sizeof(*copy));
6072c33676SMaxim Ag 	if ((copy->name = strdup(meth->name)) == NULL) {
6172c33676SMaxim Ag 		free(copy);
6272c33676SMaxim Ag 		return NULL;
6372c33676SMaxim Ag 	}
6472c33676SMaxim Ag 
6572c33676SMaxim Ag 	return copy;
6672c33676SMaxim Ag }
6772c33676SMaxim Ag 
6872c33676SMaxim Ag int
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)6972c33676SMaxim Ag RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
7072c33676SMaxim Ag {
71*de0e0e4dSAntonio Huete Jimenez 	char *new_name;
7272c33676SMaxim Ag 
73*de0e0e4dSAntonio Huete Jimenez 	if ((new_name = strdup(name)) == NULL)
7472c33676SMaxim Ag 		return 0;
75*de0e0e4dSAntonio Huete Jimenez 	free(meth->name);
76*de0e0e4dSAntonio Huete Jimenez 	meth->name = new_name;
7772c33676SMaxim Ag 	return 1;
7872c33676SMaxim Ag }
7972c33676SMaxim Ag 
8072c33676SMaxim Ag int
RSA_meth_get_finish(const RSA_METHOD * meth)8172c33676SMaxim Ag (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
8272c33676SMaxim Ag {
8372c33676SMaxim Ag 	return meth->finish;
8472c33676SMaxim Ag }
8572c33676SMaxim Ag 
8672c33676SMaxim Ag int
RSA_meth_set_priv_enc(RSA_METHOD * meth,int (* priv_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))8772c33676SMaxim Ag RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
8872c33676SMaxim Ag     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
8972c33676SMaxim Ag {
9072c33676SMaxim Ag 	meth->rsa_priv_enc = priv_enc;
9172c33676SMaxim Ag 	return 1;
9272c33676SMaxim Ag }
9372c33676SMaxim Ag 
9472c33676SMaxim Ag int
RSA_meth_set_priv_dec(RSA_METHOD * meth,int (* priv_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))9572c33676SMaxim Ag RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
9672c33676SMaxim Ag     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
9772c33676SMaxim Ag {
9872c33676SMaxim Ag 	meth->rsa_priv_dec = priv_dec;
9972c33676SMaxim Ag 	return 1;
10072c33676SMaxim Ag }
10172c33676SMaxim Ag 
10272c33676SMaxim Ag int
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))10372c33676SMaxim Ag RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
10472c33676SMaxim Ag {
10572c33676SMaxim Ag 	meth->finish = finish;
10672c33676SMaxim Ag 	return 1;
10772c33676SMaxim Ag }
108cca6fc52SDaniel Fojt 
109cca6fc52SDaniel Fojt int
RSA_meth_set_pub_enc(RSA_METHOD * meth,int (* pub_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))110cca6fc52SDaniel Fojt RSA_meth_set_pub_enc(RSA_METHOD *meth, int (*pub_enc)(int flen,
111cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
112cca6fc52SDaniel Fojt {
113cca6fc52SDaniel Fojt 	meth->rsa_pub_enc = pub_enc;
114cca6fc52SDaniel Fojt 	return 1;
115cca6fc52SDaniel Fojt }
116cca6fc52SDaniel Fojt 
117cca6fc52SDaniel Fojt int
RSA_meth_set_pub_dec(RSA_METHOD * meth,int (* pub_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))118cca6fc52SDaniel Fojt RSA_meth_set_pub_dec(RSA_METHOD *meth, int (*pub_dec)(int flen,
119cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
120cca6fc52SDaniel Fojt {
121cca6fc52SDaniel Fojt 	meth->rsa_pub_dec = pub_dec;
122cca6fc52SDaniel Fojt 	return 1;
123cca6fc52SDaniel Fojt }
124cca6fc52SDaniel Fojt 
125cca6fc52SDaniel Fojt int
RSA_meth_set_mod_exp(RSA_METHOD * meth,int (* mod_exp)(BIGNUM * r0,const BIGNUM * i,RSA * rsa,BN_CTX * ctx))126cca6fc52SDaniel Fojt RSA_meth_set_mod_exp(RSA_METHOD *meth, int (*mod_exp)(BIGNUM *r0,
127cca6fc52SDaniel Fojt     const BIGNUM *i, RSA *rsa, BN_CTX *ctx))
128cca6fc52SDaniel Fojt {
129cca6fc52SDaniel Fojt 	meth->rsa_mod_exp = mod_exp;
130cca6fc52SDaniel Fojt 	return 1;
131cca6fc52SDaniel Fojt }
132cca6fc52SDaniel Fojt 
133cca6fc52SDaniel Fojt int
RSA_meth_set_bn_mod_exp(RSA_METHOD * meth,int (* bn_mod_exp)(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx))134cca6fc52SDaniel Fojt RSA_meth_set_bn_mod_exp(RSA_METHOD *meth, int (*bn_mod_exp)(BIGNUM *r,
135cca6fc52SDaniel Fojt     const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
136cca6fc52SDaniel Fojt 	BN_MONT_CTX *m_ctx))
137cca6fc52SDaniel Fojt {
138cca6fc52SDaniel Fojt 	meth->bn_mod_exp = bn_mod_exp;
139cca6fc52SDaniel Fojt 	return 1;
140cca6fc52SDaniel Fojt }
141cca6fc52SDaniel Fojt 
142cca6fc52SDaniel Fojt int
RSA_meth_set_init(RSA_METHOD * meth,int (* init)(RSA * rsa))143cca6fc52SDaniel Fojt RSA_meth_set_init(RSA_METHOD *meth, int (*init)(RSA *rsa))
144cca6fc52SDaniel Fojt {
145cca6fc52SDaniel Fojt 	meth->init = init;
146cca6fc52SDaniel Fojt 	return 1;
147cca6fc52SDaniel Fojt }
148cca6fc52SDaniel Fojt 
149cca6fc52SDaniel Fojt int
RSA_meth_set_keygen(RSA_METHOD * meth,int (* keygen)(RSA * rsa,int bits,BIGNUM * e,BN_GENCB * cb))150cca6fc52SDaniel Fojt RSA_meth_set_keygen(RSA_METHOD *meth, int (*keygen)(RSA *rsa, int bits,
151cca6fc52SDaniel Fojt     BIGNUM *e, BN_GENCB *cb))
152cca6fc52SDaniel Fojt {
153cca6fc52SDaniel Fojt 	meth->rsa_keygen = keygen;
154cca6fc52SDaniel Fojt 	return 1;
155cca6fc52SDaniel Fojt }
156cca6fc52SDaniel Fojt 
157cca6fc52SDaniel Fojt int
RSA_meth_set_flags(RSA_METHOD * meth,int flags)158cca6fc52SDaniel Fojt RSA_meth_set_flags(RSA_METHOD *meth, int flags)
159cca6fc52SDaniel Fojt {
160cca6fc52SDaniel Fojt 	meth->flags = flags;
161cca6fc52SDaniel Fojt 	return 1;
162cca6fc52SDaniel Fojt }
163cca6fc52SDaniel Fojt 
164cca6fc52SDaniel Fojt int
RSA_meth_set0_app_data(RSA_METHOD * meth,void * app_data)165cca6fc52SDaniel Fojt RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data)
166cca6fc52SDaniel Fojt {
167cca6fc52SDaniel Fojt 	meth->app_data = app_data;
168cca6fc52SDaniel Fojt 	return 1;
169cca6fc52SDaniel Fojt }
170cca6fc52SDaniel Fojt 
171cca6fc52SDaniel Fojt const char *
RSA_meth_get0_name(const RSA_METHOD * meth)172cca6fc52SDaniel Fojt RSA_meth_get0_name(const RSA_METHOD *meth)
173cca6fc52SDaniel Fojt {
174cca6fc52SDaniel Fojt 	return meth->name;
175cca6fc52SDaniel Fojt }
176cca6fc52SDaniel Fojt 
177cca6fc52SDaniel Fojt int
RSA_meth_get_pub_enc(const RSA_METHOD * meth)178cca6fc52SDaniel Fojt (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))(int flen,
179cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
180cca6fc52SDaniel Fojt {
181cca6fc52SDaniel Fojt 	return meth->rsa_pub_enc;
182cca6fc52SDaniel Fojt }
183cca6fc52SDaniel Fojt 
184cca6fc52SDaniel Fojt int
RSA_meth_get_pub_dec(const RSA_METHOD * meth)185cca6fc52SDaniel Fojt (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))(int flen,
186cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
187cca6fc52SDaniel Fojt {
188cca6fc52SDaniel Fojt 	return meth->rsa_pub_dec;
189cca6fc52SDaniel Fojt }
190cca6fc52SDaniel Fojt 
191cca6fc52SDaniel Fojt int
RSA_meth_get_priv_enc(const RSA_METHOD * meth)192cca6fc52SDaniel Fojt (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))(int flen,
193cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
194cca6fc52SDaniel Fojt {
195cca6fc52SDaniel Fojt 	return meth->rsa_priv_enc;
196cca6fc52SDaniel Fojt }
197cca6fc52SDaniel Fojt 
198cca6fc52SDaniel Fojt int
RSA_meth_get_priv_dec(const RSA_METHOD * meth)199cca6fc52SDaniel Fojt (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))(int flen,
200cca6fc52SDaniel Fojt     const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
201cca6fc52SDaniel Fojt {
202cca6fc52SDaniel Fojt 	return meth->rsa_priv_dec;
203cca6fc52SDaniel Fojt }
204cca6fc52SDaniel Fojt 
205cca6fc52SDaniel Fojt int
RSA_meth_get_mod_exp(const RSA_METHOD * meth)206cca6fc52SDaniel Fojt (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))(BIGNUM *r0, const BIGNUM *i,
207cca6fc52SDaniel Fojt     RSA *rsa, BN_CTX *ctx)
208cca6fc52SDaniel Fojt {
209cca6fc52SDaniel Fojt 	return meth->rsa_mod_exp;
210cca6fc52SDaniel Fojt }
211cca6fc52SDaniel Fojt 
212cca6fc52SDaniel Fojt int
RSA_meth_get_bn_mod_exp(const RSA_METHOD * meth)213cca6fc52SDaniel Fojt (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))(BIGNUM *r,
214cca6fc52SDaniel Fojt     const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
215cca6fc52SDaniel Fojt     BN_MONT_CTX *m_ctx)
216cca6fc52SDaniel Fojt {
217cca6fc52SDaniel Fojt 	return meth->bn_mod_exp;
218cca6fc52SDaniel Fojt }
219cca6fc52SDaniel Fojt 
220cca6fc52SDaniel Fojt int
RSA_meth_get_init(const RSA_METHOD * meth)221cca6fc52SDaniel Fojt (*RSA_meth_get_init(const RSA_METHOD *meth))(RSA *rsa)
222cca6fc52SDaniel Fojt {
223cca6fc52SDaniel Fojt 	return meth->init;
224cca6fc52SDaniel Fojt }
225cca6fc52SDaniel Fojt 
226cca6fc52SDaniel Fojt int
RSA_meth_get_keygen(const RSA_METHOD * meth)227cca6fc52SDaniel Fojt (*RSA_meth_get_keygen(const RSA_METHOD *meth))(RSA *rsa, int bits, BIGNUM *e,
228cca6fc52SDaniel Fojt     BN_GENCB *cb)
229cca6fc52SDaniel Fojt {
230cca6fc52SDaniel Fojt 	return meth->rsa_keygen;
231cca6fc52SDaniel Fojt }
232cca6fc52SDaniel Fojt 
233cca6fc52SDaniel Fojt int
RSA_meth_get_flags(const RSA_METHOD * meth)234cca6fc52SDaniel Fojt RSA_meth_get_flags(const RSA_METHOD *meth)
235cca6fc52SDaniel Fojt {
236cca6fc52SDaniel Fojt 	return meth->flags;
237cca6fc52SDaniel Fojt }
238cca6fc52SDaniel Fojt 
239cca6fc52SDaniel Fojt void *
RSA_meth_get0_app_data(const RSA_METHOD * meth)240cca6fc52SDaniel Fojt RSA_meth_get0_app_data(const RSA_METHOD *meth)
241cca6fc52SDaniel Fojt {
242cca6fc52SDaniel Fojt 	return meth->app_data;
243cca6fc52SDaniel Fojt }
244cca6fc52SDaniel Fojt 
245cca6fc52SDaniel Fojt int
RSA_meth_get_sign(const RSA_METHOD * meth)246cca6fc52SDaniel Fojt (*RSA_meth_get_sign(const RSA_METHOD *meth))(int type,
247cca6fc52SDaniel Fojt     const unsigned char *m, unsigned int m_length,
248cca6fc52SDaniel Fojt     unsigned char *sigret, unsigned int *siglen,
249cca6fc52SDaniel Fojt     const RSA *rsa)
250cca6fc52SDaniel Fojt {
251cca6fc52SDaniel Fojt 	return meth->rsa_sign;
252cca6fc52SDaniel Fojt }
253cca6fc52SDaniel Fojt 
254cca6fc52SDaniel Fojt int
RSA_meth_set_sign(RSA_METHOD * meth,int (* sign)(int type,const unsigned char * m,unsigned int m_length,unsigned char * sigret,unsigned int * siglen,const RSA * rsa))255cca6fc52SDaniel Fojt RSA_meth_set_sign(RSA_METHOD *meth, int (*sign)(int type,
256cca6fc52SDaniel Fojt     const unsigned char *m, unsigned int m_length, unsigned char *sigret,
257cca6fc52SDaniel Fojt     unsigned int *siglen, const RSA *rsa))
258cca6fc52SDaniel Fojt {
259cca6fc52SDaniel Fojt 	meth->rsa_sign = sign;
260cca6fc52SDaniel Fojt 	return 1;
261cca6fc52SDaniel Fojt }
262cca6fc52SDaniel Fojt 
263cca6fc52SDaniel Fojt int
RSA_meth_get_verify(const RSA_METHOD * meth)264cca6fc52SDaniel Fojt (*RSA_meth_get_verify(const RSA_METHOD *meth))(int dtype,
265cca6fc52SDaniel Fojt     const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf,
266cca6fc52SDaniel Fojt     unsigned int siglen, const RSA *rsa)
267cca6fc52SDaniel Fojt {
268cca6fc52SDaniel Fojt 	return meth->rsa_verify;
269cca6fc52SDaniel Fojt }
270cca6fc52SDaniel Fojt 
271cca6fc52SDaniel Fojt int
RSA_meth_set_verify(RSA_METHOD * meth,int (* verify)(int dtype,const unsigned char * m,unsigned int m_length,const unsigned char * sigbuf,unsigned int siglen,const RSA * rsa))272cca6fc52SDaniel Fojt RSA_meth_set_verify(RSA_METHOD *meth, int (*verify)(int dtype,
273cca6fc52SDaniel Fojt     const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf,
274cca6fc52SDaniel Fojt     unsigned int siglen, const RSA *rsa))
275cca6fc52SDaniel Fojt {
276cca6fc52SDaniel Fojt 	meth->rsa_verify = verify;
277cca6fc52SDaniel Fojt 	return 1;
278cca6fc52SDaniel Fojt }
279