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