]>
Commit | Line | Data |
---|---|---|
aa6bb135 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
aa6bb135 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
0aeddcfa | 12 | #include "dh_locl.h" |
829ccf6a | 13 | #include "internal/bn_int.h" |
d02b48c6 | 14 | |
13066cee | 15 | static int generate_key(DH *dh); |
f971ccb2 RL |
16 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); |
17 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | |
0f113f3e MC |
18 | const BIGNUM *a, const BIGNUM *p, |
19 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | |
13066cee DSH |
20 | static int dh_init(DH *dh); |
21 | static int dh_finish(DH *dh); | |
22 | ||
6b691a5c | 23 | int DH_generate_key(DH *dh) |
0f113f3e MC |
24 | { |
25 | return dh->meth->generate_key(dh); | |
26 | } | |
13066cee | 27 | |
f971ccb2 | 28 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e MC |
29 | { |
30 | return dh->meth->compute_key(key, pub_key, dh); | |
31 | } | |
13066cee | 32 | |
bc91494e | 33 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e MC |
34 | { |
35 | int rv, pad; | |
36 | rv = dh->meth->compute_key(key, pub_key, dh); | |
37 | if (rv <= 0) | |
38 | return rv; | |
39 | pad = BN_num_bytes(dh->p) - rv; | |
40 | if (pad > 0) { | |
41 | memmove(key + pad, key, rv); | |
42 | memset(key, 0, pad); | |
43 | } | |
44 | return rv + pad; | |
45 | } | |
bc91494e | 46 | |
13066cee | 47 | static DH_METHOD dh_ossl = { |
0f113f3e MC |
48 | "OpenSSL DH Method", |
49 | generate_key, | |
50 | compute_key, | |
51 | dh_bn_mod_exp, | |
52 | dh_init, | |
53 | dh_finish, | |
54 | DH_FLAG_FIPS_METHOD, | |
55 | NULL, | |
56 | NULL | |
13066cee DSH |
57 | }; |
58 | ||
076fc555 RS |
59 | static const DH_METHOD *default_DH_method = &dh_ossl; |
60 | ||
f971ccb2 | 61 | const DH_METHOD *DH_OpenSSL(void) |
13066cee | 62 | { |
0f113f3e | 63 | return &dh_ossl; |
13066cee DSH |
64 | } |
65 | ||
076fc555 RS |
66 | void DH_set_default_method(const DH_METHOD *meth) |
67 | { | |
68 | default_DH_method = meth; | |
69 | } | |
70 | ||
71 | const DH_METHOD *DH_get_default_method(void) | |
72 | { | |
73 | return default_DH_method; | |
74 | } | |
75 | ||
13066cee | 76 | static int generate_key(DH *dh) |
0f113f3e MC |
77 | { |
78 | int ok = 0; | |
79 | int generate_new_key = 0; | |
80 | unsigned l; | |
81 | BN_CTX *ctx; | |
82 | BN_MONT_CTX *mont = NULL; | |
83 | BIGNUM *pub_key = NULL, *priv_key = NULL; | |
d02b48c6 | 84 | |
0f113f3e MC |
85 | ctx = BN_CTX_new(); |
86 | if (ctx == NULL) | |
87 | goto err; | |
d02b48c6 | 88 | |
0f113f3e | 89 | if (dh->priv_key == NULL) { |
74924dcb | 90 | priv_key = BN_secure_new(); |
0f113f3e MC |
91 | if (priv_key == NULL) |
92 | goto err; | |
93 | generate_new_key = 1; | |
94 | } else | |
95 | priv_key = dh->priv_key; | |
d02b48c6 | 96 | |
0f113f3e MC |
97 | if (dh->pub_key == NULL) { |
98 | pub_key = BN_new(); | |
99 | if (pub_key == NULL) | |
100 | goto err; | |
101 | } else | |
102 | pub_key = dh->pub_key; | |
d02b48c6 | 103 | |
0f113f3e MC |
104 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { |
105 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
d188a536 | 106 | dh->lock, dh->p, ctx); |
0f113f3e MC |
107 | if (!mont) |
108 | goto err; | |
109 | } | |
6ec8e63a | 110 | |
0f113f3e MC |
111 | if (generate_new_key) { |
112 | if (dh->q) { | |
113 | do { | |
ddc6a5c8 | 114 | if (!BN_priv_rand_range(priv_key, dh->q)) |
0f113f3e MC |
115 | goto err; |
116 | } | |
117 | while (BN_is_zero(priv_key) || BN_is_one(priv_key)); | |
118 | } else { | |
119 | /* secret exponent length */ | |
120 | l = dh->length ? dh->length : BN_num_bits(dh->p) - 1; | |
ddc6a5c8 | 121 | if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) |
0f113f3e MC |
122 | goto err; |
123 | } | |
124 | } | |
dfeab068 | 125 | |
0f113f3e | 126 | { |
5584f65a | 127 | BIGNUM *prk = BN_new(); |
46a64376 | 128 | |
5584f65a MC |
129 | if (prk == NULL) |
130 | goto err; | |
131 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | |
46a64376 | 132 | |
0f113f3e | 133 | if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) { |
5584f65a | 134 | BN_free(prk); |
0f113f3e MC |
135 | goto err; |
136 | } | |
5584f65a MC |
137 | /* We MUST free prk before any further use of priv_key */ |
138 | BN_free(prk); | |
0f113f3e | 139 | } |
46a64376 | 140 | |
0f113f3e MC |
141 | dh->pub_key = pub_key; |
142 | dh->priv_key = priv_key; | |
143 | ok = 1; | |
144 | err: | |
145 | if (ok != 1) | |
146 | DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB); | |
d02b48c6 | 147 | |
23a1d5e9 | 148 | if (pub_key != dh->pub_key) |
0f113f3e | 149 | BN_free(pub_key); |
23a1d5e9 | 150 | if (priv_key != dh->priv_key) |
0f113f3e MC |
151 | BN_free(priv_key); |
152 | BN_CTX_free(ctx); | |
153 | return (ok); | |
154 | } | |
d02b48c6 | 155 | |
f971ccb2 | 156 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e MC |
157 | { |
158 | BN_CTX *ctx = NULL; | |
159 | BN_MONT_CTX *mont = NULL; | |
160 | BIGNUM *tmp; | |
161 | int ret = -1; | |
162 | int check_result; | |
d02b48c6 | 163 | |
0f113f3e MC |
164 | if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
165 | DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE); | |
166 | goto err; | |
167 | } | |
5e3225cc | 168 | |
0f113f3e MC |
169 | ctx = BN_CTX_new(); |
170 | if (ctx == NULL) | |
171 | goto err; | |
172 | BN_CTX_start(ctx); | |
173 | tmp = BN_CTX_get(ctx); | |
7928ee4d BE |
174 | if (tmp == NULL) |
175 | goto err; | |
6ec8e63a | 176 | |
0f113f3e MC |
177 | if (dh->priv_key == NULL) { |
178 | DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE); | |
179 | goto err; | |
180 | } | |
dfeab068 | 181 | |
0f113f3e MC |
182 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { |
183 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
d188a536 | 184 | dh->lock, dh->p, ctx); |
5584f65a | 185 | BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); |
0f113f3e MC |
186 | if (!mont) |
187 | goto err; | |
188 | } | |
bf3d6c0c | 189 | |
0f113f3e MC |
190 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) { |
191 | DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY); | |
192 | goto err; | |
193 | } | |
d02b48c6 | 194 | |
0f113f3e MC |
195 | if (!dh-> |
196 | meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) { | |
197 | DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB); | |
198 | goto err; | |
199 | } | |
13066cee | 200 | |
0f113f3e MC |
201 | ret = BN_bn2bin(tmp, key); |
202 | err: | |
203 | if (ctx != NULL) { | |
204 | BN_CTX_end(ctx); | |
205 | BN_CTX_free(ctx); | |
206 | } | |
207 | return (ret); | |
208 | } | |
6dad7bd6 | 209 | |
0f113f3e MC |
210 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, |
211 | const BIGNUM *a, const BIGNUM *p, | |
212 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
213 | { | |
5584f65a | 214 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); |
0f113f3e | 215 | } |
13066cee DSH |
216 | |
217 | static int dh_init(DH *dh) | |
0f113f3e MC |
218 | { |
219 | dh->flags |= DH_FLAG_CACHE_MONT_P; | |
208fb891 | 220 | return 1; |
0f113f3e | 221 | } |
13066cee DSH |
222 | |
223 | static int dh_finish(DH *dh) | |
0f113f3e | 224 | { |
23a1d5e9 | 225 | BN_MONT_CTX_free(dh->method_mont_p); |
208fb891 | 226 | return 1; |
0f113f3e | 227 | } |