Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / crypto / ffc / ffc_params_validate.c
1 /*
2  * Copyright 2019-2020 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  * 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.
14  */
15
16 #include "internal/ffc.h"
17
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)
22 {
23     /*
24      * A.2.2 Step (1) AND
25      * A.2.4 Step (2)
26      * Verify that 2 <= g <= (p - 1)
27      */
28     if (BN_cmp(g, BN_value_one()) <= 0 || BN_cmp(g, p) >= 0) {
29         *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
30         return 0;
31     }
32
33     /*
34      * A.2.2 Step (2) AND
35      * A.2.4 Step (3)
36      * Check g^q mod p = 1
37      */
38     if (!BN_mod_exp_mont(tmp, g, q, p, ctx, mont))
39         return 0;
40     if (BN_cmp(tmp, BN_value_one()) != 0) {
41         *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
42         return 0;
43     }
44     return 1;
45 }
46
47 int ffc_params_FIPS186_4_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
48                                   int type, int *res, BN_GENCB *cb)
49 {
50     size_t L, N;
51
52     if (params == NULL || params->p == NULL || params->q == NULL)
53         return FFC_PARAM_RET_STATUS_FAILED;
54
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,
60                                            L, N, res, cb);
61 }
62
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)
66 {
67     size_t L, N;
68
69     if (params->p == NULL || params->q == NULL) {
70         *res = FFC_CHECK_INVALID_PQ;
71         return FFC_PARAM_RET_STATUS_FAILED;
72     }
73
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,
79                                            L, N, res, cb);
80 }