source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/dh/dh_lib.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 5.7 KB
Line 
1/*
2 * Copyright 1995-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 <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/bn.h>
13#include "dh_locl.h"
14#include <openssl/engine.h>
15
16static const DH_METHOD *default_DH_method = NULL;
17
18void DH_set_default_method(const DH_METHOD *meth)
19{
20 default_DH_method = meth;
21}
22
23const DH_METHOD *DH_get_default_method(void)
24{
25 if (!default_DH_method)
26 default_DH_method = DH_OpenSSL();
27 return default_DH_method;
28}
29
30int DH_set_method(DH *dh, const DH_METHOD *meth)
31{
32 /*
33 * NB: The caller is specifically setting a method, so it's not up to us
34 * to deal with which ENGINE it comes from.
35 */
36 const DH_METHOD *mtmp;
37 mtmp = dh->meth;
38 if (mtmp->finish)
39 mtmp->finish(dh);
40#ifndef OPENSSL_NO_ENGINE
41 ENGINE_finish(dh->engine);
42 dh->engine = NULL;
43#endif
44 dh->meth = meth;
45 if (meth->init)
46 meth->init(dh);
47 return 1;
48}
49
50DH *DH_new(void)
51{
52 return DH_new_method(NULL);
53}
54
55DH *DH_new_method(ENGINE *engine)
56{
57 DH *ret = OPENSSL_zalloc(sizeof(*ret));
58
59 if (ret == NULL) {
60 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
61 return NULL;
62 }
63
64 ret->references = 1;
65 ret->lock = CRYPTO_THREAD_lock_new();
66 if (ret->lock == NULL) {
67 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
68 OPENSSL_free(ret);
69 return NULL;
70 }
71
72 ret->meth = DH_get_default_method();
73#ifndef OPENSSL_NO_ENGINE
74 ret->flags = ret->meth->flags; /* early default init */
75 if (engine) {
76 if (!ENGINE_init(engine)) {
77 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
78 goto err;
79 }
80 ret->engine = engine;
81 } else
82 ret->engine = ENGINE_get_default_DH();
83 if (ret->engine) {
84 ret->meth = ENGINE_get_DH(ret->engine);
85 if (ret->meth == NULL) {
86 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
87 goto err;
88 }
89 }
90#endif
91
92 ret->flags = ret->meth->flags;
93
94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
95 goto err;
96
97 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
98 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
99err:
100 DH_free(ret);
101 ret = NULL;
102 }
103
104 return ret;
105}
106
107void DH_free(DH *r)
108{
109 int i;
110
111 if (r == NULL)
112 return;
113
114 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
115 REF_PRINT_COUNT("DH", r);
116 if (i > 0)
117 return;
118 REF_ASSERT_ISNT(i < 0);
119
120 if (r->meth->finish)
121 r->meth->finish(r);
122#ifndef OPENSSL_NO_ENGINE
123 ENGINE_finish(r->engine);
124#endif
125
126 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
127
128 CRYPTO_THREAD_lock_free(r->lock);
129
130 BN_clear_free(r->p);
131 BN_clear_free(r->g);
132 BN_clear_free(r->q);
133 BN_clear_free(r->j);
134 OPENSSL_free(r->seed);
135 BN_clear_free(r->counter);
136 BN_clear_free(r->pub_key);
137 BN_clear_free(r->priv_key);
138 OPENSSL_free(r);
139}
140
141int DH_up_ref(DH *r)
142{
143 int i;
144
145 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
146 return 0;
147
148 REF_PRINT_COUNT("DH", r);
149 REF_ASSERT_ISNT(i < 2);
150 return ((i > 1) ? 1 : 0);
151}
152
153int DH_set_ex_data(DH *d, int idx, void *arg)
154{
155 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
156}
157
158void *DH_get_ex_data(DH *d, int idx)
159{
160 return (CRYPTO_get_ex_data(&d->ex_data, idx));
161}
162
163int DH_bits(const DH *dh)
164{
165 return BN_num_bits(dh->p);
166}
167
168int DH_size(const DH *dh)
169{
170 return (BN_num_bytes(dh->p));
171}
172
173int DH_security_bits(const DH *dh)
174{
175 int N;
176 if (dh->q)
177 N = BN_num_bits(dh->q);
178 else if (dh->length)
179 N = dh->length;
180 else
181 N = -1;
182 return BN_security_bits(BN_num_bits(dh->p), N);
183}
184
185
186void DH_get0_pqg(const DH *dh,
187 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
188{
189 if (p != NULL)
190 *p = dh->p;
191 if (q != NULL)
192 *q = dh->q;
193 if (g != NULL)
194 *g = dh->g;
195}
196
197int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
198{
199 /* If the fields p and g in d are NULL, the corresponding input
200 * parameters MUST be non-NULL. q may remain NULL.
201 */
202 if ((dh->p == NULL && p == NULL)
203 || (dh->g == NULL && g == NULL))
204 return 0;
205
206 if (p != NULL) {
207 BN_free(dh->p);
208 dh->p = p;
209 }
210 if (q != NULL) {
211 BN_free(dh->q);
212 dh->q = q;
213 }
214 if (g != NULL) {
215 BN_free(dh->g);
216 dh->g = g;
217 }
218
219 if (q != NULL) {
220 dh->length = BN_num_bits(q);
221 }
222
223 return 1;
224}
225
226long DH_get_length(const DH *dh)
227{
228 return dh->length;
229}
230
231int DH_set_length(DH *dh, long length)
232{
233 dh->length = length;
234 return 1;
235}
236
237void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
238{
239 if (pub_key != NULL)
240 *pub_key = dh->pub_key;
241 if (priv_key != NULL)
242 *priv_key = dh->priv_key;
243}
244
245int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
246{
247 /* If the field pub_key in dh is NULL, the corresponding input
248 * parameters MUST be non-NULL. The priv_key field may
249 * be left NULL.
250 */
251 if (dh->pub_key == NULL && pub_key == NULL)
252 return 0;
253
254 if (pub_key != NULL) {
255 BN_free(dh->pub_key);
256 dh->pub_key = pub_key;
257 }
258 if (priv_key != NULL) {
259 BN_free(dh->priv_key);
260 dh->priv_key = priv_key;
261 }
262
263 return 1;
264}
265
266void DH_clear_flags(DH *dh, int flags)
267{
268 dh->flags &= ~flags;
269}
270
271int DH_test_flags(const DH *dh, int flags)
272{
273 return dh->flags & flags;
274}
275
276void DH_set_flags(DH *dh, int flags)
277{
278 dh->flags |= flags;
279}
280
281ENGINE *DH_get0_engine(DH *dh)
282{
283 return dh->engine;
284}
Note: See TracBrowser for help on using the repository browser.