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