Blame crypto/dh/dh_gen.c

Packit c4476c
/*
Packit c4476c
 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * NB: These functions have been upgraded - the previous prototypes are in
Packit c4476c
 * dh_depr.c as wrappers to these ones.  - Geoff
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include <stdio.h>
Packit c4476c
#include "internal/cryptlib.h"
Packit c4476c
#include <openssl/bn.h>
Packit c4476c
#include "dh_local.h"
Packit c4476c
#ifdef OPENSSL_FIPS
Packit c4476c
# include <openssl/fips.h>
Packit c4476c
#endif
Packit c4476c
Packit c4476c
static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
Packit c4476c
                                BN_GENCB *cb);
Packit c4476c
Packit c4476c
int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
Packit c4476c
                              BN_GENCB *cb)
Packit c4476c
{
Packit c4476c
#ifdef OPENSSL_FIPS
Packit c4476c
    if (FIPS_mode()) {
Packit c4476c
        DHerr(DH_F_DH_GENERATE_PARAMETERS_EX, DH_R_NON_FIPS_METHOD);
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
#endif
Packit c4476c
    if (ret->meth->generate_params)
Packit c4476c
        return ret->meth->generate_params(ret, prime_len, generator, cb);
Packit c4476c
    return dh_builtin_genparams(ret, prime_len, generator, cb);
Packit c4476c
}
Packit c4476c
Packit c4476c
/*-
Packit c4476c
 * We generate DH parameters as follows
Packit c4476c
 * find a prime p which is prime_len bits long,
Packit c4476c
 * where q=(p-1)/2 is also prime.
Packit c4476c
 * In the following we assume that g is not 0, 1 or p-1, since it
Packit c4476c
 * would generate only trivial subgroups.
Packit c4476c
 * For this case, g is a generator of the order-q subgroup if
Packit c4476c
 * g^q mod p == 1.
Packit c4476c
 * Or in terms of the Legendre symbol: (g/p) == 1.
Packit c4476c
 *
Packit c4476c
 * Having said all that,
Packit c4476c
 * there is another special case method for the generators 2, 3 and 5.
Packit c4476c
 * Using the quadratic reciprocity law it is possible to solve
Packit c4476c
 * (g/p) == 1 for the special values 2, 3, 5:
Packit c4476c
 * (2/p) == 1 if p mod 8 == 1 or 7.
Packit c4476c
 * (3/p) == 1 if p mod 12 == 1 or 11.
Packit c4476c
 * (5/p) == 1 if p mod 5 == 1 or 4.
Packit c4476c
 * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
Packit c4476c
 *
Packit c4476c
 * Since all safe primes > 7 must satisfy p mod 12 == 11
Packit c4476c
 * and all safe primes > 11 must satisfy p mod 5 != 1
Packit c4476c
 * we can further improve the condition for g = 2, 3 and 5:
Packit c4476c
 * for 2, p mod 24 == 23
Packit c4476c
 * for 3, p mod 12 == 11
Packit c4476c
 * for 5, p mod 60 == 59
Packit c4476c
 *
Packit c4476c
 * However for compatibility with previous versions we use:
Packit c4476c
 * for 2, p mod 24 == 11
Packit c4476c
 * for 5, p mod 60 == 23
Packit c4476c
 */
Packit c4476c
static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
Packit c4476c
                                BN_GENCB *cb)
Packit c4476c
{
Packit c4476c
    BIGNUM *t1, *t2;
Packit c4476c
    int g, ok = -1;
Packit c4476c
    BN_CTX *ctx = NULL;
Packit c4476c
Packit c4476c
    ctx = BN_CTX_new();
Packit c4476c
    if (ctx == NULL)
Packit c4476c
        goto err;
Packit c4476c
    BN_CTX_start(ctx);
Packit c4476c
    t1 = BN_CTX_get(ctx);
Packit c4476c
    t2 = BN_CTX_get(ctx);
Packit c4476c
    if (t2 == NULL)
Packit c4476c
        goto err;
Packit c4476c
Packit c4476c
    /* Make sure 'ret' has the necessary elements */
Packit c4476c
    if (!ret->p && ((ret->p = BN_new()) == NULL))
Packit c4476c
        goto err;
Packit c4476c
    if (!ret->g && ((ret->g = BN_new()) == NULL))
Packit c4476c
        goto err;
Packit c4476c
Packit c4476c
    if (generator <= 1) {
Packit c4476c
        DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
Packit c4476c
        goto err;
Packit c4476c
    }
Packit c4476c
    if (generator == DH_GENERATOR_2) {
Packit c4476c
        if (!BN_set_word(t1, 24))
Packit c4476c
            goto err;
Packit c4476c
        if (!BN_set_word(t2, 11))
Packit c4476c
            goto err;
Packit c4476c
        g = 2;
Packit c4476c
    } else if (generator == DH_GENERATOR_5) {
Packit c4476c
        if (!BN_set_word(t1, 60))
Packit c4476c
            goto err;
Packit c4476c
        if (!BN_set_word(t2, 23))
Packit c4476c
            goto err;
Packit c4476c
        g = 5;
Packit c4476c
    } else {
Packit c4476c
        /*
Packit c4476c
         * in the general case, don't worry if 'generator' is a generator or
Packit c4476c
         * not: since we are using safe primes, it will generate either an
Packit c4476c
         * order-q or an order-2q group, which both is OK
Packit c4476c
         */
Packit c4476c
        if (!BN_set_word(t1, 12))
Packit c4476c
            goto err;
Packit c4476c
        if (!BN_set_word(t2, 11))
Packit c4476c
            goto err;
Packit c4476c
        g = generator;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))
Packit c4476c
        goto err;
Packit c4476c
    if (!BN_GENCB_call(cb, 3, 0))
Packit c4476c
        goto err;
Packit c4476c
    if (!BN_set_word(ret->g, g))
Packit c4476c
        goto err;
Packit c4476c
    ok = 1;
Packit c4476c
 err:
Packit c4476c
    if (ok == -1) {
Packit c4476c
        DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
Packit c4476c
        ok = 0;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    BN_CTX_end(ctx);
Packit c4476c
    BN_CTX_free(ctx);
Packit c4476c
    return ok;
Packit c4476c
}