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