/src/openssl111/crypto/dh/dh_gen.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 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 | | /* |
11 | | * NB: These functions have been upgraded - the previous prototypes are in |
12 | | * dh_depr.c as wrappers to these ones. - Geoff |
13 | | */ |
14 | | |
15 | | #include <stdio.h> |
16 | | #include "internal/cryptlib.h" |
17 | | #include <openssl/bn.h> |
18 | | #include "dh_local.h" |
19 | | |
20 | | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, |
21 | | BN_GENCB *cb); |
22 | | |
23 | | int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, |
24 | | BN_GENCB *cb) |
25 | 0 | { |
26 | 0 | if (ret->meth->generate_params) |
27 | 0 | return ret->meth->generate_params(ret, prime_len, generator, cb); |
28 | 0 | return dh_builtin_genparams(ret, prime_len, generator, cb); |
29 | 0 | } |
30 | | |
31 | | /*- |
32 | | * We generate DH parameters as follows |
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. |
40 | | * |
41 | | * Having said all that, |
42 | | * there is another special case method for the generators 2, 3 and 5. |
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 |
49 | | * |
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 |
56 | | * |
57 | | * However for compatibility with previous versions we use: |
58 | | * for 2, p mod 24 == 11 |
59 | | * for 5, p mod 60 == 23 |
60 | | */ |
61 | | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, |
62 | | BN_GENCB *cb) |
63 | 0 | { |
64 | 0 | BIGNUM *t1, *t2; |
65 | 0 | int g, ok = -1; |
66 | 0 | BN_CTX *ctx = NULL; |
67 | |
|
68 | 0 | ctx = BN_CTX_new(); |
69 | 0 | if (ctx == NULL) |
70 | 0 | goto err; |
71 | 0 | BN_CTX_start(ctx); |
72 | 0 | t1 = BN_CTX_get(ctx); |
73 | 0 | t2 = BN_CTX_get(ctx); |
74 | 0 | if (t2 == NULL) |
75 | 0 | goto err; |
76 | | |
77 | | /* Make sure 'ret' has the necessary elements */ |
78 | 0 | if (!ret->p && ((ret->p = BN_new()) == NULL)) |
79 | 0 | goto err; |
80 | 0 | if (!ret->g && ((ret->g = BN_new()) == NULL)) |
81 | 0 | goto err; |
82 | | |
83 | 0 | if (generator <= 1) { |
84 | 0 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR); |
85 | 0 | goto err; |
86 | 0 | } |
87 | 0 | if (generator == DH_GENERATOR_2) { |
88 | 0 | if (!BN_set_word(t1, 24)) |
89 | 0 | goto err; |
90 | 0 | if (!BN_set_word(t2, 11)) |
91 | 0 | goto err; |
92 | 0 | g = 2; |
93 | 0 | } else if (generator == DH_GENERATOR_5) { |
94 | 0 | if (!BN_set_word(t1, 60)) |
95 | 0 | goto err; |
96 | 0 | if (!BN_set_word(t2, 23)) |
97 | 0 | goto err; |
98 | 0 | g = 5; |
99 | 0 | } else { |
100 | | /* |
101 | | * in the general case, don't worry if 'generator' is a generator or |
102 | | * not: since we are using safe primes, it will generate either an |
103 | | * order-q or an order-2q group, which both is OK |
104 | | */ |
105 | 0 | if (!BN_set_word(t1, 12)) |
106 | 0 | goto err; |
107 | 0 | if (!BN_set_word(t2, 11)) |
108 | 0 | goto err; |
109 | 0 | g = generator; |
110 | 0 | } |
111 | | |
112 | 0 | if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb)) |
113 | 0 | goto err; |
114 | 0 | if (!BN_GENCB_call(cb, 3, 0)) |
115 | 0 | goto err; |
116 | 0 | if (!BN_set_word(ret->g, g)) |
117 | 0 | goto err; |
118 | 0 | ok = 1; |
119 | 0 | err: |
120 | 0 | if (ok == -1) { |
121 | 0 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB); |
122 | 0 | ok = 0; |
123 | 0 | } |
124 | |
|
125 | 0 | BN_CTX_end(ctx); |
126 | 0 | BN_CTX_free(ctx); |
127 | 0 | return ok; |
128 | 0 | } |