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