]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
a38c878c | 2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
e38873f5 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | ||
0f113f3e MC |
10 | /* |
11 | * NB: These functions have been upgraded - the previous prototypes are in | |
12 | * dh_depr.c as wrappers to these ones. - Geoff | |
e9224c71 GT |
13 | */ |
14 | ||
d02b48c6 | 15 | #include <stdio.h> |
b39fc560 | 16 | #include "internal/cryptlib.h" |
ec577822 | 17 | #include <openssl/bn.h> |
0aeddcfa | 18 | #include "dh_locl.h" |
71fa4513 | 19 | |
0f113f3e MC |
20 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, |
21 | BN_GENCB *cb); | |
0e4aa0d2 | 22 | |
0f113f3e MC |
23 | int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, |
24 | BN_GENCB *cb) | |
25 | { | |
26 | if (ret->meth->generate_params) | |
27 | return ret->meth->generate_params(ret, prime_len, generator, cb); | |
28 | return dh_builtin_genparams(ret, prime_len, generator, cb); | |
29 | } | |
0e4aa0d2 | 30 | |
1d97c843 TH |
31 | /*- |
32 | * We generate DH parameters as follows | |
a38c878c BE |
33 | * find a prime p which is prime_len bits long, |
34 | * where q=(p-1)/2 is also prime. | |
35 | * In the following we assume that g is not 0, 1 or p-1, since it | |
36 | * would generate only trivial subgroups. | |
37 | * For this case, g is a generator of the order-q subgroup if | |
38 | * g^q mod p == 1. | |
39 | * Or in terms of the Legendre symbol: (g/p) == 1. | |
d02b48c6 RE |
40 | * |
41 | * Having said all that, | |
42 | * there is another special case method for the generators 2, 3 and 5. | |
a38c878c BE |
43 | * Using the quadratic reciprocity law it is possible to solve |
44 | * (g/p) == 1 for the special values 2, 3, 5: | |
45 | * (2/p) == 1 if p mod 8 == 1 or 7. | |
46 | * (3/p) == 1 if p mod 12 == 1 or 11. | |
47 | * (5/p) == 1 if p mod 5 == 1 or 4. | |
48 | * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol | |
d02b48c6 | 49 | * |
a38c878c BE |
50 | * Since all safe primes > 7 must satisfy p mod 12 == 11 |
51 | * and all safe primes > 11 must satisfy p mod 5 != 1 | |
52 | * we can further improve the condition for g = 2, 3 and 5: | |
53 | * for 2, p mod 24 == 23 | |
54 | * for 3, p mod 12 == 11 | |
55 | * for 5, p mod 60 == 59 | |
82652aaf | 56 | */ |
0f113f3e MC |
57 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, |
58 | BN_GENCB *cb) | |
59 | { | |
60 | BIGNUM *t1, *t2; | |
61 | int g, ok = -1; | |
62 | BN_CTX *ctx = NULL; | |
d02b48c6 | 63 | |
6de1fe90 BE |
64 | if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) { |
65 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | if (prime_len < DH_MIN_MODULUS_BITS) { | |
70 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL); | |
71 | return 0; | |
72 | } | |
73 | ||
0f113f3e MC |
74 | ctx = BN_CTX_new(); |
75 | if (ctx == NULL) | |
76 | goto err; | |
77 | BN_CTX_start(ctx); | |
78 | t1 = BN_CTX_get(ctx); | |
79 | t2 = BN_CTX_get(ctx); | |
edea42c6 | 80 | if (t2 == NULL) |
0f113f3e | 81 | goto err; |
e9224c71 | 82 | |
0f113f3e MC |
83 | /* Make sure 'ret' has the necessary elements */ |
84 | if (!ret->p && ((ret->p = BN_new()) == NULL)) | |
85 | goto err; | |
86 | if (!ret->g && ((ret->g = BN_new()) == NULL)) | |
87 | goto err; | |
88 | ||
89 | if (generator <= 1) { | |
90 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR); | |
91 | goto err; | |
92 | } | |
93 | if (generator == DH_GENERATOR_2) { | |
94 | if (!BN_set_word(t1, 24)) | |
95 | goto err; | |
a38c878c | 96 | if (!BN_set_word(t2, 23)) |
0f113f3e MC |
97 | goto err; |
98 | g = 2; | |
dfb56425 | 99 | } else if (generator == DH_GENERATOR_5) { |
a38c878c | 100 | if (!BN_set_word(t1, 60)) |
0f113f3e | 101 | goto err; |
a38c878c | 102 | if (!BN_set_word(t2, 59)) |
0f113f3e | 103 | goto err; |
0f113f3e MC |
104 | g = 5; |
105 | } else { | |
106 | /* | |
107 | * in the general case, don't worry if 'generator' is a generator or | |
108 | * not: since we are using safe primes, it will generate either an | |
109 | * order-q or an order-2q group, which both is OK | |
110 | */ | |
a38c878c | 111 | if (!BN_set_word(t1, 12)) |
0f113f3e | 112 | goto err; |
a38c878c | 113 | if (!BN_set_word(t2, 11)) |
0f113f3e MC |
114 | goto err; |
115 | g = generator; | |
116 | } | |
117 | ||
118 | if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb)) | |
119 | goto err; | |
120 | if (!BN_GENCB_call(cb, 3, 0)) | |
121 | goto err; | |
122 | if (!BN_set_word(ret->g, g)) | |
123 | goto err; | |
8b84b075 | 124 | ret->dirty_cnt++; |
0f113f3e MC |
125 | ok = 1; |
126 | err: | |
127 | if (ok == -1) { | |
128 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB); | |
129 | ok = 0; | |
130 | } | |
d02b48c6 | 131 | |
ce1415ed SL |
132 | BN_CTX_end(ctx); |
133 | BN_CTX_free(ctx); | |
0f113f3e MC |
134 | return ok; |
135 | } |