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