2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Finite Field cryptography (FFC) is used for DSA and DH.
12 * This file contains methods for validation of FFC parameters.
13 * It calls the same functions as the generation as the code is very similar.
16 #include "internal/ffc.h"
18 /* FIPS186-4 A.2.2 Unverifiable partial validation of Generator g */
19 int ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
20 const BIGNUM *p, const BIGNUM *q,
21 const BIGNUM *g, BIGNUM *tmp, int *ret)
26 * Verify that 2 <= g <= (p - 1)
28 if (BN_cmp(g, BN_value_one()) <= 0 || BN_cmp(g, p) >= 0) {
29 *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
38 if (!BN_mod_exp_mont(tmp, g, q, p, ctx, mont))
40 if (BN_cmp(tmp, BN_value_one()) != 0) {
41 *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
47 int ffc_params_FIPS186_4_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
48 int type, int *res, BN_GENCB *cb)
52 if (params == NULL || params->p == NULL || params->q == NULL)
53 return FFC_PARAM_RET_STATUS_FAILED;
55 /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
56 L = BN_num_bits(params->p);
57 N = BN_num_bits(params->q);
58 return ffc_params_FIPS186_4_gen_verify(libctx, (FFC_PARAMS *)params,
59 FFC_PARAM_MODE_VERIFY, type,
63 /* This may be used in FIPS mode to validate deprecated FIPS-186-2 Params */
64 int ffc_params_FIPS186_2_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
65 int type, int *res, BN_GENCB *cb)
69 if (params->p == NULL || params->q == NULL) {
70 *res = FFC_CHECK_INVALID_PQ;
71 return FFC_PARAM_RET_STATUS_FAILED;
74 /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
75 L = BN_num_bits(params->p);
76 N = BN_num_bits(params->q);
77 return ffc_params_FIPS186_2_gen_verify(libctx, (FFC_PARAMS *)params,
78 FFC_PARAM_MODE_VERIFY, type,