155714Skris/* crypto/dh/dh_gen.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59280304Sjkim/*
60280304Sjkim * NB: These functions have been upgraded - the previous prototypes are in
61280304Sjkim * dh_depr.c as wrappers to these ones.  - Geoff
62160814Ssimon */
63160814Ssimon
6455714Skris#include <stdio.h>
6555714Skris#include "cryptlib.h"
6655714Skris#include <openssl/bn.h>
6755714Skris#include <openssl/dh.h>
6855714Skris
69238405Sjkim#ifdef OPENSSL_FIPS
70280304Sjkim# include <openssl/fips.h>
71238405Sjkim#endif
72194206Ssimon
73280304Sjkimstatic int dh_builtin_genparams(DH *ret, int prime_len, int generator,
74280304Sjkim                                BN_GENCB *cb);
75160814Ssimon
76280304Sjkimint DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
77280304Sjkim                              BN_GENCB *cb)
78280304Sjkim{
79238405Sjkim#ifdef OPENSSL_FIPS
80280304Sjkim    if (FIPS_mode() && !(ret->meth->flags & DH_FLAG_FIPS_METHOD)
81280304Sjkim        && !(ret->flags & DH_FLAG_NON_FIPS_ALLOW)) {
82280304Sjkim        DHerr(DH_F_DH_GENERATE_PARAMETERS_EX, DH_R_NON_FIPS_METHOD);
83280304Sjkim        return 0;
84280304Sjkim    }
85238405Sjkim#endif
86280304Sjkim    if (ret->meth->generate_params)
87280304Sjkim        return ret->meth->generate_params(ret, prime_len, generator, cb);
88238405Sjkim#ifdef OPENSSL_FIPS
89280304Sjkim    if (FIPS_mode())
90280304Sjkim        return FIPS_dh_generate_parameters_ex(ret, prime_len, generator, cb);
91238405Sjkim#endif
92280304Sjkim    return dh_builtin_genparams(ret, prime_len, generator, cb);
93280304Sjkim}
94160814Ssimon
95280304Sjkim/*-
96280304Sjkim * We generate DH parameters as follows
9755714Skris * find a prime q which is prime_len/2 bits long.
9855714Skris * p=(2*q)+1 or (p-1)/2 = q
9955714Skris * For this case, g is a generator if
10055714Skris * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
10155714Skris * Since the factors of p-1 are q and 2, we just need to check
10255714Skris * g^2 mod p != 1 and g^q mod p != 1.
10355714Skris *
10455714Skris * Having said all that,
10555714Skris * there is another special case method for the generators 2, 3 and 5.
10655714Skris * for 2, p mod 24 == 11
10759191Skris * for 3, p mod 12 == 5  <<<<< does not work for safe primes.
10855714Skris * for 5, p mod 10 == 3 or 7
10955714Skris *
11055714Skris * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
11155714Skris * special generators and for answering some of my questions.
11255714Skris *
11355714Skris * I've implemented the second simple method :-).
11459191Skris * Since DH should be using a safe prime (both p and q are prime),
11555714Skris * this generator function can take a very very long time to run.
11655714Skris */
117280304Sjkim/*
118280304Sjkim * Actually there is no reason to insist that 'generator' be a generator.
119100928Snectar * It's just as OK (and in some sense better) to use a generator of the
120100928Snectar * order-q subgroup.
121100928Snectar */
122280304Sjkimstatic int dh_builtin_genparams(DH *ret, int prime_len, int generator,
123280304Sjkim                                BN_GENCB *cb)
124280304Sjkim{
125280304Sjkim    BIGNUM *t1, *t2;
126280304Sjkim    int g, ok = -1;
127280304Sjkim    BN_CTX *ctx = NULL;
12855714Skris
129280304Sjkim    ctx = BN_CTX_new();
130280304Sjkim    if (ctx == NULL)
131280304Sjkim        goto err;
132280304Sjkim    BN_CTX_start(ctx);
133280304Sjkim    t1 = BN_CTX_get(ctx);
134280304Sjkim    t2 = BN_CTX_get(ctx);
135280304Sjkim    if (t1 == NULL || t2 == NULL)
136280304Sjkim        goto err;
137160814Ssimon
138280304Sjkim    /* Make sure 'ret' has the necessary elements */
139280304Sjkim    if (!ret->p && ((ret->p = BN_new()) == NULL))
140280304Sjkim        goto err;
141280304Sjkim    if (!ret->g && ((ret->g = BN_new()) == NULL))
142280304Sjkim        goto err;
143280304Sjkim
144280304Sjkim    if (generator <= 1) {
145280304Sjkim        DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
146280304Sjkim        goto err;
147280304Sjkim    }
148280304Sjkim    if (generator == DH_GENERATOR_2) {
149280304Sjkim        if (!BN_set_word(t1, 24))
150280304Sjkim            goto err;
151280304Sjkim        if (!BN_set_word(t2, 11))
152280304Sjkim            goto err;
153280304Sjkim        g = 2;
154280304Sjkim    }
155280304Sjkim#if 0                           /* does not work for safe primes */
156280304Sjkim    else if (generator == DH_GENERATOR_3) {
157280304Sjkim        if (!BN_set_word(t1, 12))
158280304Sjkim            goto err;
159280304Sjkim        if (!BN_set_word(t2, 5))
160280304Sjkim            goto err;
161280304Sjkim        g = 3;
162280304Sjkim    }
16355714Skris#endif
164280304Sjkim    else if (generator == DH_GENERATOR_5) {
165280304Sjkim        if (!BN_set_word(t1, 10))
166280304Sjkim            goto err;
167280304Sjkim        if (!BN_set_word(t2, 3))
168280304Sjkim            goto err;
169280304Sjkim        /*
170280304Sjkim         * BN_set_word(t3,7); just have to miss out on these ones :-(
171280304Sjkim         */
172280304Sjkim        g = 5;
173280304Sjkim    } else {
174280304Sjkim        /*
175280304Sjkim         * in the general case, don't worry if 'generator' is a generator or
176280304Sjkim         * not: since we are using safe primes, it will generate either an
177280304Sjkim         * order-q or an order-2q group, which both is OK
178280304Sjkim         */
179280304Sjkim        if (!BN_set_word(t1, 2))
180280304Sjkim            goto err;
181280304Sjkim        if (!BN_set_word(t2, 1))
182280304Sjkim            goto err;
183280304Sjkim        g = generator;
184280304Sjkim    }
18555714Skris
186280304Sjkim    if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))
187280304Sjkim        goto err;
188280304Sjkim    if (!BN_GENCB_call(cb, 3, 0))
189280304Sjkim        goto err;
190280304Sjkim    if (!BN_set_word(ret->g, g))
191280304Sjkim        goto err;
192280304Sjkim    ok = 1;
193280304Sjkim err:
194280304Sjkim    if (ok == -1) {
195280304Sjkim        DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
196280304Sjkim        ok = 0;
197280304Sjkim    }
198280304Sjkim
199280304Sjkim    if (ctx != NULL) {
200280304Sjkim        BN_CTX_end(ctx);
201280304Sjkim        BN_CTX_free(ctx);
202280304Sjkim    }
203280304Sjkim    return ok;
204280304Sjkim}
205