1 /* $OpenBSD: rsa_meth.c,v 1.7 2023/07/08 12:26:45 beck Exp $ */
2 /*
3 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <openssl/err.h>
22 #include <openssl/rsa.h>
23
24 #include "rsa_local.h"
25
26 RSA_METHOD *
RSA_meth_new(const char * name,int flags)27 RSA_meth_new(const char *name, int flags)
28 {
29 RSA_METHOD *meth;
30
31 if ((meth = calloc(1, sizeof(*meth))) == NULL)
32 return NULL;
33 if ((meth->name = strdup(name)) == NULL) {
34 free(meth);
35 return NULL;
36 }
37 meth->flags = flags;
38
39 return meth;
40 }
41 LCRYPTO_ALIAS(RSA_meth_new);
42
43 void
RSA_meth_free(RSA_METHOD * meth)44 RSA_meth_free(RSA_METHOD *meth)
45 {
46 if (meth == NULL)
47 return;
48
49 free(meth->name);
50 free(meth);
51 }
52 LCRYPTO_ALIAS(RSA_meth_free);
53
54 RSA_METHOD *
RSA_meth_dup(const RSA_METHOD * meth)55 RSA_meth_dup(const RSA_METHOD *meth)
56 {
57 RSA_METHOD *copy;
58
59 if ((copy = calloc(1, sizeof(*copy))) == NULL)
60 return NULL;
61 memcpy(copy, meth, sizeof(*copy));
62 if ((copy->name = strdup(meth->name)) == NULL) {
63 free(copy);
64 return NULL;
65 }
66
67 return copy;
68 }
69 LCRYPTO_ALIAS(RSA_meth_dup);
70
71 int
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)72 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
73 {
74 char *new_name;
75
76 if ((new_name = strdup(name)) == NULL)
77 return 0;
78 free(meth->name);
79 meth->name = new_name;
80 return 1;
81 }
82 LCRYPTO_ALIAS(RSA_meth_set1_name);
83
84 int
RSA_meth_get_finish(const RSA_METHOD * meth)85 (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
86 {
87 return meth->finish;
88 }
89 LCRYPTO_ALIAS(RSA_meth_get_finish);
90
91 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))92 RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
93 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
94 {
95 meth->rsa_priv_enc = priv_enc;
96 return 1;
97 }
98 LCRYPTO_ALIAS(RSA_meth_set_priv_enc);
99
100 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))101 RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
102 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
103 {
104 meth->rsa_priv_dec = priv_dec;
105 return 1;
106 }
107 LCRYPTO_ALIAS(RSA_meth_set_priv_dec);
108
109 int
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))110 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
111 {
112 meth->finish = finish;
113 return 1;
114 }
115 LCRYPTO_ALIAS(RSA_meth_set_finish);
116
117 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))118 RSA_meth_set_pub_enc(RSA_METHOD *meth, int (*pub_enc)(int flen,
119 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
120 {
121 meth->rsa_pub_enc = pub_enc;
122 return 1;
123 }
124 LCRYPTO_ALIAS(RSA_meth_set_pub_enc);
125
126 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))127 RSA_meth_set_pub_dec(RSA_METHOD *meth, int (*pub_dec)(int flen,
128 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
129 {
130 meth->rsa_pub_dec = pub_dec;
131 return 1;
132 }
133 LCRYPTO_ALIAS(RSA_meth_set_pub_dec);
134
135 int
RSA_meth_set_mod_exp(RSA_METHOD * meth,int (* mod_exp)(BIGNUM * r0,const BIGNUM * i,RSA * rsa,BN_CTX * ctx))136 RSA_meth_set_mod_exp(RSA_METHOD *meth, int (*mod_exp)(BIGNUM *r0,
137 const BIGNUM *i, RSA *rsa, BN_CTX *ctx))
138 {
139 meth->rsa_mod_exp = mod_exp;
140 return 1;
141 }
142 LCRYPTO_ALIAS(RSA_meth_set_mod_exp);
143
144 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))145 RSA_meth_set_bn_mod_exp(RSA_METHOD *meth, int (*bn_mod_exp)(BIGNUM *r,
146 const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
147 BN_MONT_CTX *m_ctx))
148 {
149 meth->bn_mod_exp = bn_mod_exp;
150 return 1;
151 }
152 LCRYPTO_ALIAS(RSA_meth_set_bn_mod_exp);
153
154 int
RSA_meth_set_init(RSA_METHOD * meth,int (* init)(RSA * rsa))155 RSA_meth_set_init(RSA_METHOD *meth, int (*init)(RSA *rsa))
156 {
157 meth->init = init;
158 return 1;
159 }
160 LCRYPTO_ALIAS(RSA_meth_set_init);
161
162 int
RSA_meth_set_keygen(RSA_METHOD * meth,int (* keygen)(RSA * rsa,int bits,BIGNUM * e,BN_GENCB * cb))163 RSA_meth_set_keygen(RSA_METHOD *meth, int (*keygen)(RSA *rsa, int bits,
164 BIGNUM *e, BN_GENCB *cb))
165 {
166 meth->rsa_keygen = keygen;
167 return 1;
168 }
169 LCRYPTO_ALIAS(RSA_meth_set_keygen);
170
171 int
RSA_meth_set_flags(RSA_METHOD * meth,int flags)172 RSA_meth_set_flags(RSA_METHOD *meth, int flags)
173 {
174 meth->flags = flags;
175 return 1;
176 }
177 LCRYPTO_ALIAS(RSA_meth_set_flags);
178
179 int
RSA_meth_set0_app_data(RSA_METHOD * meth,void * app_data)180 RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data)
181 {
182 meth->app_data = app_data;
183 return 1;
184 }
185 LCRYPTO_ALIAS(RSA_meth_set0_app_data);
186
187 const char *
RSA_meth_get0_name(const RSA_METHOD * meth)188 RSA_meth_get0_name(const RSA_METHOD *meth)
189 {
190 return meth->name;
191 }
192 LCRYPTO_ALIAS(RSA_meth_get0_name);
193
194 int
RSA_meth_get_pub_enc(const RSA_METHOD * meth)195 (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))(int flen,
196 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
197 {
198 return meth->rsa_pub_enc;
199 }
200 LCRYPTO_ALIAS(RSA_meth_get_pub_enc);
201
202 int
RSA_meth_get_pub_dec(const RSA_METHOD * meth)203 (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))(int flen,
204 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
205 {
206 return meth->rsa_pub_dec;
207 }
208 LCRYPTO_ALIAS(RSA_meth_get_pub_dec);
209
210 int
RSA_meth_get_priv_enc(const RSA_METHOD * meth)211 (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))(int flen,
212 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
213 {
214 return meth->rsa_priv_enc;
215 }
216 LCRYPTO_ALIAS(RSA_meth_get_priv_enc);
217
218 int
RSA_meth_get_priv_dec(const RSA_METHOD * meth)219 (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))(int flen,
220 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
221 {
222 return meth->rsa_priv_dec;
223 }
224 LCRYPTO_ALIAS(RSA_meth_get_priv_dec);
225
226 int
RSA_meth_get_mod_exp(const RSA_METHOD * meth)227 (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))(BIGNUM *r0, const BIGNUM *i,
228 RSA *rsa, BN_CTX *ctx)
229 {
230 return meth->rsa_mod_exp;
231 }
232 LCRYPTO_ALIAS(RSA_meth_get_mod_exp);
233
234 int
RSA_meth_get_bn_mod_exp(const RSA_METHOD * meth)235 (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))(BIGNUM *r,
236 const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
237 BN_MONT_CTX *m_ctx)
238 {
239 return meth->bn_mod_exp;
240 }
241 LCRYPTO_ALIAS(RSA_meth_get_bn_mod_exp);
242
243 int
RSA_meth_get_init(const RSA_METHOD * meth)244 (*RSA_meth_get_init(const RSA_METHOD *meth))(RSA *rsa)
245 {
246 return meth->init;
247 }
248 LCRYPTO_ALIAS(RSA_meth_get_init);
249
250 int
RSA_meth_get_keygen(const RSA_METHOD * meth)251 (*RSA_meth_get_keygen(const RSA_METHOD *meth))(RSA *rsa, int bits, BIGNUM *e,
252 BN_GENCB *cb)
253 {
254 return meth->rsa_keygen;
255 }
256 LCRYPTO_ALIAS(RSA_meth_get_keygen);
257
258 int
RSA_meth_get_flags(const RSA_METHOD * meth)259 RSA_meth_get_flags(const RSA_METHOD *meth)
260 {
261 return meth->flags;
262 }
263 LCRYPTO_ALIAS(RSA_meth_get_flags);
264
265 void *
RSA_meth_get0_app_data(const RSA_METHOD * meth)266 RSA_meth_get0_app_data(const RSA_METHOD *meth)
267 {
268 return meth->app_data;
269 }
270 LCRYPTO_ALIAS(RSA_meth_get0_app_data);
271
272 int
RSA_meth_get_sign(const RSA_METHOD * meth)273 (*RSA_meth_get_sign(const RSA_METHOD *meth))(int type,
274 const unsigned char *m, unsigned int m_length,
275 unsigned char *sigret, unsigned int *siglen,
276 const RSA *rsa)
277 {
278 return meth->rsa_sign;
279 }
280 LCRYPTO_ALIAS(RSA_meth_get_sign);
281
282 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))283 RSA_meth_set_sign(RSA_METHOD *meth, int (*sign)(int type,
284 const unsigned char *m, unsigned int m_length, unsigned char *sigret,
285 unsigned int *siglen, const RSA *rsa))
286 {
287 meth->rsa_sign = sign;
288 return 1;
289 }
290 LCRYPTO_ALIAS(RSA_meth_set_sign);
291
292 int
RSA_meth_get_verify(const RSA_METHOD * meth)293 (*RSA_meth_get_verify(const RSA_METHOD *meth))(int dtype,
294 const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf,
295 unsigned int siglen, const RSA *rsa)
296 {
297 return meth->rsa_verify;
298 }
299 LCRYPTO_ALIAS(RSA_meth_get_verify);
300
301 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))302 RSA_meth_set_verify(RSA_METHOD *meth, int (*verify)(int dtype,
303 const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf,
304 unsigned int siglen, const RSA *rsa))
305 {
306 meth->rsa_verify = verify;
307 return 1;
308 }
309 LCRYPTO_ALIAS(RSA_meth_set_verify);
310