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