ce6114c1335fd88381a93ddd44f05da5b97f5e38
[openssl.git] / crypto / dh / dh_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 "dh_locl.h"
11 #include <string.h>
12 #include <openssl/err.h>
13
14 DH_METHOD *DH_meth_new(const char *name, int flags)
15 {
16     DH_METHOD *dhm = OPENSSL_zalloc(sizeof(*dhm));
17
18     if (dhm != NULL) {
19         dhm->flags = flags;
20
21         dhm->name = OPENSSL_strdup(name);
22         if (dhm->name != NULL)
23             return dhm;
24
25         OPENSSL_free(dhm);
26     }
27
28     DHerr(DH_F_DH_METH_NEW, ERR_R_MALLOC_FAILURE);
29     return NULL;
30 }
31
32 void DH_meth_free(DH_METHOD *dhm)
33 {
34     if (dhm != NULL) {
35         OPENSSL_free(dhm->name);
36         OPENSSL_free(dhm);
37     }
38 }
39
40 DH_METHOD *DH_meth_dup(const DH_METHOD *dhm)
41 {
42     DH_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
43
44     if (ret != NULL) {
45         memcpy(ret, dhm, sizeof(*dhm));
46
47         ret->name = OPENSSL_strdup(dhm->name);
48         if (ret->name != NULL)
49             return ret;
50
51         OPENSSL_free(ret);
52     }
53
54     DHerr(DH_F_DH_METH_DUP, ERR_R_MALLOC_FAILURE);
55     return NULL;
56 }
57
58 const char *DH_meth_get0_name(const DH_METHOD *dhm)
59 {
60     return dhm->name;
61 }
62
63 int DH_meth_set1_name(DH_METHOD *dhm, const char *name)
64 {
65     char *tmpname = OPENSSL_strdup(name);
66
67     if (tmpname == NULL) {
68         DHerr(DH_F_DH_METH_SET1_NAME, ERR_R_MALLOC_FAILURE);
69         return 0;
70     }
71
72     OPENSSL_free(dhm->name);
73     dhm->name = tmpname;
74
75     return 1;
76 }
77
78 int DH_meth_get_flags(DH_METHOD *dhm)
79 {
80     return dhm->flags;
81 }
82
83 int DH_meth_set_flags(DH_METHOD *dhm, int flags)
84 {
85     dhm->flags = flags;
86     return 1;
87 }
88
89 void *DH_meth_get0_app_data(const DH_METHOD *dhm)
90 {
91     return dhm->app_data;
92 }
93
94 int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data)
95 {
96     dhm->app_data = app_data;
97     return 1;
98 }
99
100 int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *)
101 {
102     return dhm->generate_key;
103 }
104
105 int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *))
106 {
107     dhm->generate_key = generate_key;
108     return 1;
109 }
110
111 int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
112         (unsigned char *key, const BIGNUM *pub_key, DH *dh)
113 {
114     return dhm->compute_key;
115 }
116
117 int DH_meth_set_compute_key(DH_METHOD *dhm,
118         int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh))
119 {
120     dhm->compute_key = compute_key;
121     return 1;
122 }
123
124
125 int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
126     (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
127      BN_CTX *, BN_MONT_CTX *)
128 {
129     return dhm->bn_mod_exp;
130 }
131
132 int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
133     int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
134                        const BIGNUM *, BN_CTX *, BN_MONT_CTX *))
135 {
136     dhm->bn_mod_exp = bn_mod_exp;
137     return 1;
138 }
139
140 int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *)
141 {
142     return dhm->init;
143 }
144
145 int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *))
146 {
147     dhm->init = init;
148     return 1;
149 }
150
151 int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *)
152 {
153     return dhm->finish;
154 }
155
156 int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *))
157 {
158     dhm->finish = finish;
159     return 1;
160 }
161
162 int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
163         (DH *, int, int, BN_GENCB *)
164 {
165     return dhm->generate_params;
166 }
167
168 int DH_meth_set_generate_params(DH_METHOD *dhm,
169         int (*generate_params) (DH *, int, int, BN_GENCB *))
170 {
171     dhm->generate_params = generate_params;
172     return 1;
173 }