Extend Travis build time-out
[oweals/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(const FFC_PARAMS *params, int type,
48                                   const EVP_MD *evpmd, int validate_flags,
49                                   int *res, BN_GENCB *cb)
50 {
51     size_t L, N;
52
53     if (params == NULL || params->p == NULL || params->q == NULL)
54         return FFC_PARAMS_RET_STATUS_FAILED;
55
56     /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
57     L = BN_num_bits(params->p);
58     N = BN_num_bits(params->q);
59     return ffc_params_FIPS186_4_gen_verify(NULL, (FFC_PARAMS *)params, type, L, N,
60                                            evpmd, validate_flags, 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(const FFC_PARAMS *params, int type,
65                                   const EVP_MD *evpmd, int validate_flags,
66                                   int *res, BN_GENCB *cb)
67 {
68     size_t L, N;
69
70     if (params->p == NULL || params->q == NULL) {
71         *res = FFC_CHECK_INVALID_PQ;
72         return FFC_PARAMS_RET_STATUS_FAILED;
73     }
74
75     /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
76     L = BN_num_bits(params->p);
77     N = BN_num_bits(params->q);
78     return ffc_params_FIPS186_2_gen_verify(NULL, (FFC_PARAMS *)params, type, L, N,
79                                            evpmd, validate_flags, res, cb);
80 }