xref: /openssl/crypto/rsa/rsa_meth.c (revision e077455e)
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <string.h>
17 #include "rsa_local.h"
18 #include <openssl/err.h>
19 
RSA_meth_new(const char * name,int flags)20 RSA_METHOD *RSA_meth_new(const char *name, int flags)
21 {
22     RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth));
23 
24     if (meth != NULL) {
25         meth->flags = flags;
26 
27         meth->name = OPENSSL_strdup(name);
28         if (meth->name != NULL)
29             return meth;
30 
31         OPENSSL_free(meth);
32     }
33 
34     return NULL;
35 }
36 
RSA_meth_free(RSA_METHOD * meth)37 void RSA_meth_free(RSA_METHOD *meth)
38 {
39     if (meth != NULL) {
40         OPENSSL_free(meth->name);
41         OPENSSL_free(meth);
42     }
43 }
44 
RSA_meth_dup(const RSA_METHOD * meth)45 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
46 {
47     RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
48 
49     if (ret != NULL) {
50         memcpy(ret, meth, sizeof(*meth));
51 
52         ret->name = OPENSSL_strdup(meth->name);
53         if (ret->name != NULL)
54             return ret;
55 
56         OPENSSL_free(ret);
57     }
58 
59     return NULL;
60 }
61 
RSA_meth_get0_name(const RSA_METHOD * meth)62 const char *RSA_meth_get0_name(const RSA_METHOD *meth)
63 {
64     return meth->name;
65 }
66 
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)67 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
68 {
69     char *tmpname = OPENSSL_strdup(name);
70 
71     if (tmpname == NULL)
72         return 0;
73 
74     OPENSSL_free(meth->name);
75     meth->name = tmpname;
76 
77     return 1;
78 }
79 
RSA_meth_get_flags(const RSA_METHOD * meth)80 int RSA_meth_get_flags(const RSA_METHOD *meth)
81 {
82     return meth->flags;
83 }
84 
RSA_meth_set_flags(RSA_METHOD * meth,int flags)85 int RSA_meth_set_flags(RSA_METHOD *meth, int flags)
86 {
87     meth->flags = flags;
88     return 1;
89 }
90 
RSA_meth_get0_app_data(const RSA_METHOD * meth)91 void *RSA_meth_get0_app_data(const RSA_METHOD *meth)
92 {
93     return meth->app_data;
94 }
95 
RSA_meth_set0_app_data(RSA_METHOD * meth,void * app_data)96 int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data)
97 {
98     meth->app_data = app_data;
99     return 1;
100 }
101 
RSA_meth_get_pub_enc(const RSA_METHOD * meth)102 int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))
103     (int flen, const unsigned char *from,
104      unsigned char *to, RSA *rsa, int padding)
105 {
106     return meth->rsa_pub_enc;
107 }
108 
RSA_meth_set_pub_enc(RSA_METHOD * meth,int (* pub_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))109 int RSA_meth_set_pub_enc(RSA_METHOD *meth,
110                          int (*pub_enc) (int flen, const unsigned char *from,
111                                          unsigned char *to, RSA *rsa,
112                                          int padding))
113 {
114     meth->rsa_pub_enc = pub_enc;
115     return 1;
116 }
117 
RSA_meth_get_pub_dec(const RSA_METHOD * meth)118 int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))
119     (int flen, const unsigned char *from,
120      unsigned char *to, RSA *rsa, int padding)
121 {
122     return meth->rsa_pub_dec;
123 }
124 
RSA_meth_set_pub_dec(RSA_METHOD * meth,int (* pub_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))125 int RSA_meth_set_pub_dec(RSA_METHOD *meth,
126                          int (*pub_dec) (int flen, const unsigned char *from,
127                                          unsigned char *to, RSA *rsa,
128                                          int padding))
129 {
130     meth->rsa_pub_dec = pub_dec;
131     return 1;
132 }
133 
RSA_meth_get_priv_enc(const RSA_METHOD * meth)134 int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))
135     (int flen, const unsigned char *from,
136      unsigned char *to, RSA *rsa, int padding)
137 {
138     return meth->rsa_priv_enc;
139 }
140 
RSA_meth_set_priv_enc(RSA_METHOD * meth,int (* priv_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))141 int RSA_meth_set_priv_enc(RSA_METHOD *meth,
142                           int (*priv_enc) (int flen, const unsigned char *from,
143                                            unsigned char *to, RSA *rsa,
144                                            int padding))
145 {
146     meth->rsa_priv_enc = priv_enc;
147     return 1;
148 }
149 
RSA_meth_get_priv_dec(const RSA_METHOD * meth)150 int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))
151     (int flen, const unsigned char *from,
152      unsigned char *to, RSA *rsa, int padding)
153 {
154     return meth->rsa_priv_dec;
155 }
156 
RSA_meth_set_priv_dec(RSA_METHOD * meth,int (* priv_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))157 int RSA_meth_set_priv_dec(RSA_METHOD *meth,
158                           int (*priv_dec) (int flen, const unsigned char *from,
159                                            unsigned char *to, RSA *rsa,
160                                            int padding))
161 {
162     meth->rsa_priv_dec = priv_dec;
163     return 1;
164 }
165 
166     /* Can be null */
RSA_meth_get_mod_exp(const RSA_METHOD * meth)167 int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))
168     (BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx)
169 {
170     return meth->rsa_mod_exp;
171 }
172 
RSA_meth_set_mod_exp(RSA_METHOD * meth,int (* mod_exp)(BIGNUM * r0,const BIGNUM * i,RSA * rsa,BN_CTX * ctx))173 int RSA_meth_set_mod_exp(RSA_METHOD *meth,
174                          int (*mod_exp) (BIGNUM *r0, const BIGNUM *i, RSA *rsa,
175                                          BN_CTX *ctx))
176 {
177     meth->rsa_mod_exp = mod_exp;
178     return 1;
179 }
180 
181     /* Can be null */
RSA_meth_get_bn_mod_exp(const RSA_METHOD * meth)182 int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))
183     (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
184      const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
185 {
186     return meth->bn_mod_exp;
187 }
188 
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))189 int RSA_meth_set_bn_mod_exp(RSA_METHOD *meth,
190                             int (*bn_mod_exp) (BIGNUM *r,
191                                                const BIGNUM *a,
192                                                const BIGNUM *p,
193                                                const BIGNUM *m,
194                                                BN_CTX *ctx,
195                                                BN_MONT_CTX *m_ctx))
196 {
197     meth->bn_mod_exp = bn_mod_exp;
198     return 1;
199 }
200 
201     /* called at new */
RSA_meth_get_init(const RSA_METHOD * meth)202 int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa)
203 {
204     return meth->init;
205 }
206 
RSA_meth_set_init(RSA_METHOD * meth,int (* init)(RSA * rsa))207 int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa))
208 {
209     meth->init = init;
210     return 1;
211 }
212 
213     /* called at free */
RSA_meth_get_finish(const RSA_METHOD * meth)214 int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa)
215 {
216     return meth->finish;
217 }
218 
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))219 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
220 {
221     meth->finish = finish;
222     return 1;
223 }
224 
RSA_meth_get_sign(const RSA_METHOD * meth)225 int (*RSA_meth_get_sign(const RSA_METHOD *meth))
226     (int type,
227      const unsigned char *m, unsigned int m_length,
228      unsigned char *sigret, unsigned int *siglen,
229      const RSA *rsa)
230 {
231     return meth->rsa_sign;
232 }
233 
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))234 int RSA_meth_set_sign(RSA_METHOD *meth,
235                       int (*sign) (int type, const unsigned char *m,
236                                    unsigned int m_length,
237                                    unsigned char *sigret, unsigned int *siglen,
238                                    const RSA *rsa))
239 {
240     meth->rsa_sign = sign;
241     return 1;
242 }
243 
RSA_meth_get_verify(const RSA_METHOD * meth)244 int (*RSA_meth_get_verify(const RSA_METHOD *meth))
245     (int dtype, const unsigned char *m,
246      unsigned int m_length, const unsigned char *sigbuf,
247      unsigned int siglen, const RSA *rsa)
248 {
249     return meth->rsa_verify;
250 }
251 
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))252 int RSA_meth_set_verify(RSA_METHOD *meth,
253                         int (*verify) (int dtype, const unsigned char *m,
254                                        unsigned int m_length,
255                                        const unsigned char *sigbuf,
256                                        unsigned int siglen, const RSA *rsa))
257 {
258     meth->rsa_verify = verify;
259     return 1;
260 }
261 
RSA_meth_get_keygen(const RSA_METHOD * meth)262 int (*RSA_meth_get_keygen(const RSA_METHOD *meth))
263     (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
264 {
265     return meth->rsa_keygen;
266 }
267 
RSA_meth_set_keygen(RSA_METHOD * meth,int (* keygen)(RSA * rsa,int bits,BIGNUM * e,BN_GENCB * cb))268 int RSA_meth_set_keygen(RSA_METHOD *meth,
269                         int (*keygen) (RSA *rsa, int bits, BIGNUM *e,
270                                        BN_GENCB *cb))
271 {
272     meth->rsa_keygen = keygen;
273     return 1;
274 }
275 
RSA_meth_get_multi_prime_keygen(const RSA_METHOD * meth)276 int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth))
277     (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb)
278 {
279     return meth->rsa_multi_prime_keygen;
280 }
281 
RSA_meth_set_multi_prime_keygen(RSA_METHOD * meth,int (* keygen)(RSA * rsa,int bits,int primes,BIGNUM * e,BN_GENCB * cb))282 int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth,
283                                     int (*keygen) (RSA *rsa, int bits,
284                                                    int primes, BIGNUM *e,
285                                                    BN_GENCB *cb))
286 {
287     meth->rsa_multi_prime_keygen = keygen;
288     return 1;
289 }
290