Debian Codegraph @

lib_pypy/_cffi_ssl/_cffi_src/openssl/dh.py

raw symbols SCIP
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4
5 from __future__ import absolute_import, division, print_function
6
7 INCLUDES = """
8 #include <openssl/dh.h>
9 """
10
11 TYPES = """
12 typedef ... DH;
13
14 const long DH_NOT_SUITABLE_GENERATOR;
15 """
16
17 FUNCTIONS = """
18 DH *DH_new(void);
19 void DH_free(DH *);
20 int DH_size(const DH *);
21 int DH_generate_key(DH *);
22 int DH_compute_key(unsigned char *, const BIGNUM *, DH *);
23 DH *DHparams_dup(DH *);
24
25 /* added in 1.1.0 when the DH struct was opaqued */
26 void DH_get0_pqg(const DH *, const BIGNUM **, const BIGNUM **,
27 const BIGNUM **);
28 int DH_set0_pqg(DH *, BIGNUM *, BIGNUM *, BIGNUM *);
29 void DH_get0_key(const DH *, const BIGNUM **, const BIGNUM **);
30 int DH_set0_key(DH *, BIGNUM *, BIGNUM *);
31
32 int Cryptography_DH_check(const DH *, int *);
33 int DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
34 DH *d2i_DHparams_bio(BIO *, DH **);
35 int i2d_DHparams_bio(BIO *, DH *);
36 DH *Cryptography_d2i_DHxparams_bio(BIO *bp, DH **x);
37 int Cryptography_i2d_DHxparams_bio(BIO *bp, DH *x);
38 """
39
40 CUSTOMIZATIONS = """
41 /* These functions were added in OpenSSL 1.1.0 */
42 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 && !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER
43 void DH_get0_pqg(const DH *dh,
44 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
45 {
46 if (p != NULL)
47 *p = dh->p;
48 if (q != NULL)
49 *q = dh->q;
50 if (g != NULL)
51 *g = dh->g;
52 }
53
54 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
55 {
56 /* If the fields p and g in d are NULL, the corresponding input
57 * parameters MUST be non-NULL. q may remain NULL.
58 */
59 if ((dh->p == NULL && p == NULL)
60 || (dh->g == NULL && g == NULL))
61 return 0;
62
63 if (p != NULL) {
64 BN_free(dh->p);
65 dh->p = p;
66 }
67 if (q != NULL) {
68 BN_free(dh->q);
69 dh->q = q;
70 }
71 if (g != NULL) {
72 BN_free(dh->g);
73 dh->g = g;
74 }
75
76 if (q != NULL) {
77 dh->length = BN_num_bits(q);
78 }
79
80 return 1;
81 }
82
83 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
84 {
85 if (pub_key != NULL)
86 *pub_key = dh->pub_key;
87 if (priv_key != NULL)
88 *priv_key = dh->priv_key;
89 }
90
91 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
92 {
93 /* If the field pub_key in dh is NULL, the corresponding input
94 * parameters MUST be non-NULL. The priv_key field may
95 * be left NULL.
96 */
97 if (dh->pub_key == NULL && pub_key == NULL)
98 return 0;
99
100 if (pub_key != NULL) {
101 BN_free(dh->pub_key);
102 dh->pub_key = pub_key;
103 }
104 if (priv_key != NULL) {
105 BN_free(dh->priv_key);
106 dh->priv_key = priv_key;
107 }
108
109 return 1;
110 }
111 #endif
112
113 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110
114 #ifndef DH_CHECK_Q_NOT_PRIME
115 #define DH_CHECK_Q_NOT_PRIME 0x10
116 #endif
117
118 #ifndef DH_CHECK_INVALID_Q_VALUE
119 #define DH_CHECK_INVALID_Q_VALUE 0x20
120 #endif
121
122 #ifndef DH_CHECK_INVALID_J_VALUE
123 #define DH_CHECK_INVALID_J_VALUE 0x40
124 #endif
125
126 /* DH_check implementation taken from OpenSSL 1.1.0pre6 */
127
128 /*-
129 * Check that p is a safe prime and
130 * if g is 2, 3 or 5, check that it is a suitable generator
131 * where
132 * for 2, p mod 24 == 11
133 * for 3, p mod 12 == 5
134 * for 5, p mod 10 == 3 or 7
135 * should hold.
136 */
137
138 int Cryptography_DH_check(const DH *dh, int *ret)
139 {
140 int ok = 0, r;
141 BN_CTX *ctx = NULL;
142 BN_ULONG l;
143 BIGNUM *t1 = NULL, *t2 = NULL;
144
145 *ret = 0;
146 ctx = BN_CTX_new();
147 if (ctx == NULL)
148 goto err;
149 BN_CTX_start(ctx);
150 t1 = BN_CTX_get(ctx);
151 if (t1 == NULL)
152 goto err;
153 t2 = BN_CTX_get(ctx);
154 if (t2 == NULL)
155 goto err;
156
157 if (dh->q) {
158 if (BN_cmp(dh->g, BN_value_one()) <= 0)
159 *ret |= DH_NOT_SUITABLE_GENERATOR;
160 else if (BN_cmp(dh->g, dh->p) >= 0)
161 *ret |= DH_NOT_SUITABLE_GENERATOR;
162 else {
163 /* Check g^q == 1 mod p */
164 if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
165 goto err;
166 if (!BN_is_one(t1))
167 *ret |= DH_NOT_SUITABLE_GENERATOR;
168 }
169 r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);
170 if (r < 0)
171 goto err;
172 if (!r)
173 *ret |= DH_CHECK_Q_NOT_PRIME;
174 /* Check p == 1 mod q i.e. q divides p - 1 */
175 if (!BN_div(t1, t2, dh->p, dh->q, ctx))
176 goto err;
177 if (!BN_is_one(t2))
178 *ret |= DH_CHECK_INVALID_Q_VALUE;
179 if (dh->j && BN_cmp(dh->j, t1))
180 *ret |= DH_CHECK_INVALID_J_VALUE;
181
182 } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
183 l = BN_mod_word(dh->p, 24);
184 if (l == (BN_ULONG)-1)
185 goto err;
186 if (l != 11)
187 *ret |= DH_NOT_SUITABLE_GENERATOR;
188 } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
189 l = BN_mod_word(dh->p, 10);
190 if (l == (BN_ULONG)-1)
191 goto err;
192 if ((l != 3) && (l != 7))
193 *ret |= DH_NOT_SUITABLE_GENERATOR;
194 } else
195 *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
196
197 r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);
198 if (r < 0)
199 goto err;
200 if (!r)
201 *ret |= DH_CHECK_P_NOT_PRIME;
202 else if (!dh->q) {
203 if (!BN_rshift1(t1, dh->p))
204 goto err;
205 r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);
206 if (r < 0)
207 goto err;
208 if (!r)
209 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
210 }
211 ok = 1;
212 err:
213 if (ctx != NULL) {
214 BN_CTX_end(ctx);
215 BN_CTX_free(ctx);
216 }
217 return (ok);
218 }
219 #else
220 int Cryptography_DH_check(const DH *dh, int *ret) {
221 return DH_check(dh, ret);
222 }
223 #endif
224
225 /* These functions were added in OpenSSL 1.1.0f commit d0c50e80a8 */
226 /* Define our own to simplify support across all versions. */
227 #if defined(EVP_PKEY_DHX) && EVP_PKEY_DHX != -1
228 DH *Cryptography_d2i_DHxparams_bio(BIO *bp, DH **x) {
229 return ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, bp, x);
230 }
231 int Cryptography_i2d_DHxparams_bio(BIO *bp, DH *x) {
232 return ASN1_i2d_bio_of_const(DH, i2d_DHxparams, bp, x);
233 }
234 #else
235 DH *(*Cryptography_d2i_DHxparams_bio)(BIO *bp, DH **x) = NULL;
236 int (*Cryptography_i2d_DHxparams_bio)(BIO *bp, DH *x) = NULL;
237 #endif
238 """