1e5c7ca2c92b79fff2efc34bfe4850d483510030
[openssl.git] / crypto / dh / dh_gen.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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_locl.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 {
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 }
30
31 /*-
32  * We generate DH parameters as follows
33  * find a prime q which is prime_len/2 bits long.
34  * p=(2*q)+1 or (p-1)/2 = q
35  * For this case, g is a generator if
36  * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
37  * Since the factors of p-1 are q and 2, we just need to check
38  * g^2 mod p != 1 and g^q mod p != 1.
39  *
40  * Having said all that,
41  * there is another special case method for the generators 2, 3 and 5.
42  * for 2, p mod 24 == 11
43  * for 3, p mod 12 == 5  <<<<< does not work for safe primes.
44  * for 5, p mod 10 == 3 or 7
45  *
46  * Thanks to Phil Karn for the pointers about the
47  * special generators and for answering some of my questions.
48  *
49  * I've implemented the second simple method :-).
50  * Since DH should be using a safe prime (both p and q are prime),
51  * this generator function can take a very very long time to run.
52  */
53 /*
54  * Actually there is no reason to insist that 'generator' be a generator.
55  * It's just as OK (and in some sense better) to use a generator of the
56  * order-q subgroup.
57  */
58 static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
59                                 BN_GENCB *cb)
60 {
61     BIGNUM *t1, *t2;
62     int g, ok = -1;
63     BN_CTX *ctx = NULL;
64
65     ctx = BN_CTX_new();
66     if (ctx == NULL)
67         goto err;
68     BN_CTX_start(ctx);
69     t1 = BN_CTX_get(ctx);
70     t2 = BN_CTX_get(ctx);
71     if (t2 == NULL)
72         goto err;
73
74     /* Make sure 'ret' has the necessary elements */
75     if (!ret->p && ((ret->p = BN_new()) == NULL))
76         goto err;
77     if (!ret->g && ((ret->g = BN_new()) == NULL))
78         goto err;
79
80     if (generator <= 1) {
81         DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
82         goto err;
83     }
84     if (generator == DH_GENERATOR_2) {
85         if (!BN_set_word(t1, 24))
86             goto err;
87         if (!BN_set_word(t2, 11))
88             goto err;
89         g = 2;
90     } else if (generator == DH_GENERATOR_5) {
91         if (!BN_set_word(t1, 10))
92             goto err;
93         if (!BN_set_word(t2, 3))
94             goto err;
95         /*
96          * BN_set_word(t3,7); just have to miss out on these ones :-(
97          */
98         g = 5;
99     } 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         if (!BN_set_word(t1, 2))
106             goto err;
107         if (!BN_set_word(t2, 1))
108             goto err;
109         g = generator;
110     }
111
112     if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))
113         goto err;
114     if (!BN_GENCB_call(cb, 3, 0))
115         goto err;
116     if (!BN_set_word(ret->g, g))
117         goto err;
118     ok = 1;
119  err:
120     if (ok == -1) {
121         DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
122         ok = 0;
123     }
124
125     BN_CTX_end(ctx);
126     BN_CTX_free(ctx);
127     return ok;
128 }