params.c
Go to the documentation of this file.
1 /* ====================================================================
2  * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  * software must display the following acknowledgment:
18  * "This product includes software developed by the OpenSSL Project
19  * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  * endorse or promote products derived from this software without
23  * prior written permission. For written permission, please contact
24  * licensing@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  * nor may "OpenSSL" appear in their names without prior written
28  * permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  * acknowledgment:
32  * "This product includes software developed by the OpenSSL Project
33  * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com). This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com). */
52 
53 #include <openssl/dh.h>
54 
55 #include <openssl/bn.h>
56 #include <openssl/err.h>
57 #include <openssl/mem.h>
58 
59 #include "../fipsmodule/bn/internal.h"
60 
61 
63  static const BN_ULONG kPrime1536Data[] = {
64  TOBN(0xffffffff, 0xffffffff), TOBN(0xf1746c08, 0xca237327),
65  TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
66  TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
67  TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
68  TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
69  TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
70  TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
71  TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
72  TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
73  TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
74  TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
75  TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
76  };
77 
78  static const BIGNUM kPrime1536BN = STATIC_BIGNUM(kPrime1536Data);
79 
80  BIGNUM *alloc = NULL;
81  if (ret == NULL) {
82  alloc = BN_new();
83  if (alloc == NULL) {
84  return NULL;
85  }
86  ret = alloc;
87  }
88 
89  if (!BN_copy(ret, &kPrime1536BN)) {
90  BN_free(alloc);
91  return NULL;
92  }
93 
94  return ret;
95 }
96 
97 int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator,
98  BN_GENCB *cb) {
99  // We generate DH parameters as follows
100  // find a prime q which is prime_bits/2 bits long.
101  // p=(2*q)+1 or (p-1)/2 = q
102  // For this case, g is a generator if
103  // g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
104  // Since the factors of p-1 are q and 2, we just need to check
105  // g^2 mod p != 1 and g^q mod p != 1.
106  //
107  // Having said all that,
108  // there is another special case method for the generators 2, 3 and 5.
109  // for 2, p mod 24 == 11
110  // for 3, p mod 12 == 5 <<<<< does not work for safe primes.
111  // for 5, p mod 10 == 3 or 7
112  //
113  // Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
114  // special generators and for answering some of my questions.
115  //
116  // I've implemented the second simple method :-).
117  // Since DH should be using a safe prime (both p and q are prime),
118  // this generator function can take a very very long time to run.
119 
120  // Actually there is no reason to insist that 'generator' be a generator.
121  // It's just as OK (and in some sense better) to use a generator of the
122  // order-q subgroup.
123 
124  BIGNUM *t1, *t2;
125  int g, ok = 0;
126  BN_CTX *ctx = NULL;
127 
128  ctx = BN_CTX_new();
129  if (ctx == NULL) {
130  goto err;
131  }
132  BN_CTX_start(ctx);
133  t1 = BN_CTX_get(ctx);
134  t2 = BN_CTX_get(ctx);
135  if (t1 == NULL || t2 == NULL) {
136  goto err;
137  }
138 
139  // Make sure |dh| has the necessary elements
140  if (dh->p == NULL) {
141  dh->p = BN_new();
142  if (dh->p == NULL) {
143  goto err;
144  }
145  }
146  if (dh->g == NULL) {
147  dh->g = BN_new();
148  if (dh->g == NULL) {
149  goto err;
150  }
151  }
152 
153  if (generator <= 1) {
155  goto err;
156  }
157  if (generator == DH_GENERATOR_2) {
158  if (!BN_set_word(t1, 24)) {
159  goto err;
160  }
161  if (!BN_set_word(t2, 11)) {
162  goto err;
163  }
164  g = 2;
165  } else if (generator == DH_GENERATOR_5) {
166  if (!BN_set_word(t1, 10)) {
167  goto err;
168  }
169  if (!BN_set_word(t2, 3)) {
170  goto err;
171  }
172  // BN_set_word(t3,7); just have to miss
173  // out on these ones :-(
174  g = 5;
175  } else {
176  // in the general case, don't worry if 'generator' is a
177  // generator or not: since we are using safe primes,
178  // it will generate either an order-q or an order-2q group,
179  // which both is OK
180  if (!BN_set_word(t1, 2)) {
181  goto err;
182  }
183  if (!BN_set_word(t2, 1)) {
184  goto err;
185  }
186  g = generator;
187  }
188 
189  if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
190  goto err;
191  }
192  if (!BN_GENCB_call(cb, 3, 0)) {
193  goto err;
194  }
195  if (!BN_set_word(dh->g, g)) {
196  goto err;
197  }
198  ok = 1;
199 
200 err:
201  if (!ok) {
203  }
204 
205  if (ctx != NULL) {
206  BN_CTX_end(ctx);
207  BN_CTX_free(ctx);
208  }
209  return ok;
210 }
211 
212 static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
213  BIGNUM *a = NULL;
214 
215  if (src) {
216  a = BN_dup(src);
217  if (!a) {
218  return 0;
219  }
220  }
221 
222  BN_free(*dst);
223  *dst = a;
224  return 1;
225 }
226 
227 static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
228  if (is_x942 == -1) {
229  is_x942 = !!from->q;
230  }
231  if (!int_dh_bn_cpy(&to->p, from->p) ||
232  !int_dh_bn_cpy(&to->g, from->g)) {
233  return 0;
234  }
235 
236  if (!is_x942) {
237  return 1;
238  }
239 
240  if (!int_dh_bn_cpy(&to->q, from->q) ||
241  !int_dh_bn_cpy(&to->j, from->j)) {
242  return 0;
243  }
244 
245  OPENSSL_free(to->seed);
246  to->seed = NULL;
247  to->seedlen = 0;
248 
249  if (from->seed) {
250  to->seed = OPENSSL_memdup(from->seed, from->seedlen);
251  if (!to->seed) {
252  return 0;
253  }
254  to->seedlen = from->seedlen;
255  }
256 
257  return 1;
258 }
259 
260 DH *DHparams_dup(const DH *dh) {
261  DH *ret = DH_new();
262  if (!ret) {
263  return NULL;
264  }
265 
266  if (!int_dh_param_copy(ret, dh, -1)) {
267  DH_free(ret);
268  return NULL;
269  }
270 
271  return ret;
272 }
DH_R_BAD_GENERATOR
#define DH_R_BAD_GENERATOR
Definition: dh.h:346
bn.h
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
DH_free
#define DH_free
Definition: boringssl_prefix_symbols.h:1224
ctx
Definition: benchmark-async.c:30
BN_dup
#define BN_dup
Definition: boringssl_prefix_symbols.h:919
DH_generate_parameters_ex
int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb)
Definition: params.c:97
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
int_dh_param_copy
static int int_dh_param_copy(DH *to, const DH *from, int is_x942)
Definition: params.c:227
error_ref_leak.err
err
Definition: error_ref_leak.py:35
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
bignum_ctx
Definition: ctx.c:91
BN_CTX_get
#define BN_CTX_get
Definition: boringssl_prefix_symbols.h:884
BN_set_word
#define BN_set_word
Definition: boringssl_prefix_symbols.h:992
BN_generate_prime_ex
#define BN_generate_prime_ex
Definition: boringssl_prefix_symbols.h:926
BN_get_rfc3526_prime_1536
BIGNUM * BN_get_rfc3526_prime_1536(BIGNUM *ret)
Definition: params.c:62
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
int_dh_bn_cpy
static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src)
Definition: params.c:212
dh.h
DHparams_dup
DH * DHparams_dup(const DH *dh)
Definition: params.c:260
err.h
STATIC_BIGNUM
#define STATIC_BIGNUM(x)
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h:203
BN_GENCB_call
#define BN_GENCB_call
Definition: boringssl_prefix_symbols.h:887
ERR_R_BN_LIB
#define ERR_R_BN_LIB
Definition: err.h:331
BN_CTX_new
#define BN_CTX_new
Definition: boringssl_prefix_symbols.h:885
g
struct @717 g
dh_st::g
BIGNUM * g
Definition: dh.h:307
bn_gencb_st
Definition: bn.h:656
BN_copy
#define BN_copy
Definition: boringssl_prefix_symbols.h:914
DH_GENERATOR_2
#define DH_GENERATOR_2
Definition: dh.h:150
DH_new
#define DH_new
Definition: boringssl_prefix_symbols.h:1236
BN_CTX_start
#define BN_CTX_start
Definition: boringssl_prefix_symbols.h:886
bignum_st
Definition: bn.h:957
BN_CTX_free
#define BN_CTX_free
Definition: boringssl_prefix_symbols.h:883
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
OPENSSL_memdup
#define OPENSSL_memdup
Definition: boringssl_prefix_symbols.h:1887
ok
bool ok
Definition: async_end2end_test.cc:197
DH_GENERATOR_5
#define DH_GENERATOR_5
Definition: dh.h:151
dh_st::p
BIGNUM * p
Definition: dh.h:306
BN_CTX_end
#define BN_CTX_end
Definition: boringssl_prefix_symbols.h:882
dh_st
Definition: dh.h:305
mem.h
t1
Table t1
Definition: abseil-cpp/absl/container/internal/raw_hash_set_allocator_test.cc:185
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
alloc
std::allocator< int > alloc
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:87
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:49