/src/openssl31/crypto/ffc/ffc_params_validate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2021 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 <openssl/err.h> |
17 | | #include <openssl/bn.h> |
18 | | #include <openssl/dsaerr.h> |
19 | | #include <openssl/dherr.h> |
20 | | #include "internal/ffc.h" |
21 | | |
22 | | /* FIPS186-4 A.2.2 Unverifiable partial validation of Generator g */ |
23 | | int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, |
24 | | const BIGNUM *p, const BIGNUM *q, |
25 | | const BIGNUM *g, BIGNUM *tmp, |
26 | | int *ret) |
27 | 103 | { |
28 | | /* |
29 | | * A.2.2 Step (1) AND |
30 | | * A.2.4 Step (2) |
31 | | * Verify that 2 <= g <= (p - 1) |
32 | | */ |
33 | 103 | if (BN_cmp(g, BN_value_one()) <= 0 || BN_cmp(g, p) >= 0) { |
34 | 55 | *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR; |
35 | 55 | return 0; |
36 | 55 | } |
37 | | |
38 | | /* |
39 | | * A.2.2 Step (2) AND |
40 | | * A.2.4 Step (3) |
41 | | * Check g^q mod p = 1 |
42 | | */ |
43 | 48 | if (!BN_mod_exp_mont(tmp, g, q, p, ctx, mont)) |
44 | 0 | return 0; |
45 | 48 | if (BN_cmp(tmp, BN_value_one()) != 0) { |
46 | 35 | *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR; |
47 | 35 | return 0; |
48 | 35 | } |
49 | 13 | return 1; |
50 | 48 | } |
51 | | |
52 | | int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx, |
53 | | const FFC_PARAMS *params, int type, |
54 | | int *res, BN_GENCB *cb) |
55 | 554 | { |
56 | 554 | size_t L, N; |
57 | | |
58 | 554 | if (params == NULL || params->p == NULL || params->q == NULL) |
59 | 0 | return FFC_PARAM_RET_STATUS_FAILED; |
60 | | |
61 | | /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */ |
62 | 554 | L = BN_num_bits(params->p); |
63 | 554 | N = BN_num_bits(params->q); |
64 | 554 | return ossl_ffc_params_FIPS186_4_gen_verify(libctx, (FFC_PARAMS *)params, |
65 | 554 | FFC_PARAM_MODE_VERIFY, type, |
66 | 554 | L, N, res, cb); |
67 | 554 | } |
68 | | |
69 | | /* This may be used in FIPS mode to validate deprecated FIPS-186-2 Params */ |
70 | | int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx, |
71 | | const FFC_PARAMS *params, int type, |
72 | | int *res, BN_GENCB *cb) |
73 | 0 | { |
74 | 0 | size_t L, N; |
75 | |
|
76 | 0 | if (params == NULL || params->p == NULL || params->q == NULL) { |
77 | 0 | *res = FFC_CHECK_INVALID_PQ; |
78 | 0 | return FFC_PARAM_RET_STATUS_FAILED; |
79 | 0 | } |
80 | | |
81 | | /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */ |
82 | 0 | L = BN_num_bits(params->p); |
83 | 0 | N = BN_num_bits(params->q); |
84 | 0 | return ossl_ffc_params_FIPS186_2_gen_verify(libctx, (FFC_PARAMS *)params, |
85 | 0 | FFC_PARAM_MODE_VERIFY, type, |
86 | 0 | L, N, res, cb); |
87 | 0 | } |
88 | | |
89 | | /* |
90 | | * This does a simple check of L and N and partial g. |
91 | | * It makes no attempt to do a full validation of p, q or g since these require |
92 | | * extra parameters such as the digest and seed, which may not be available for |
93 | | * this test. |
94 | | */ |
95 | | int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params, |
96 | | int paramstype, int *res) |
97 | 554 | { |
98 | 554 | int ret; |
99 | 554 | int tmpres = 0; |
100 | 554 | FFC_PARAMS tmpparams = {0}; |
101 | | |
102 | 554 | if (params == NULL) |
103 | 0 | return 0; |
104 | | |
105 | 554 | if (res == NULL) |
106 | 0 | res = &tmpres; |
107 | | |
108 | 554 | if (!ossl_ffc_params_copy(&tmpparams, params)) |
109 | 0 | return 0; |
110 | | |
111 | 554 | tmpparams.flags = FFC_PARAM_FLAG_VALIDATE_G; |
112 | 554 | tmpparams.gindex = FFC_UNVERIFIABLE_GINDEX; |
113 | | |
114 | 554 | #ifndef FIPS_MODULE |
115 | 554 | if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) |
116 | 0 | ret = ossl_ffc_params_FIPS186_2_validate(libctx, &tmpparams, paramstype, |
117 | 0 | res, NULL); |
118 | 554 | else |
119 | 554 | #endif |
120 | 554 | ret = ossl_ffc_params_FIPS186_4_validate(libctx, &tmpparams, paramstype, |
121 | 554 | res, NULL); |
122 | 554 | #ifndef OPENSSL_NO_DH |
123 | 554 | if (ret == FFC_PARAM_RET_STATUS_FAILED |
124 | 554 | && (*res & FFC_ERROR_NOT_SUITABLE_GENERATOR) != 0) { |
125 | 90 | ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR); |
126 | 90 | } |
127 | 554 | #endif |
128 | | |
129 | 554 | ossl_ffc_params_cleanup(&tmpparams); |
130 | | |
131 | 554 | return ret != FFC_PARAM_RET_STATUS_FAILED; |
132 | 554 | } |
133 | | |
134 | | /* |
135 | | * If possible (or always in FIPS_MODULE) do full FIPS 186-4 validation. |
136 | | * Otherwise do simple check but in addition also check the primality of the |
137 | | * p and q. |
138 | | */ |
139 | | int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params, |
140 | | int paramstype, int *res) |
141 | 554 | { |
142 | 554 | int tmpres = 0; |
143 | | |
144 | 554 | if (params == NULL) |
145 | 0 | return 0; |
146 | | |
147 | 554 | if (res == NULL) |
148 | 0 | res = &tmpres; |
149 | | |
150 | | #ifdef FIPS_MODULE |
151 | | return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype, |
152 | | res, NULL); |
153 | | #else |
154 | 554 | if (params->seed != NULL) { |
155 | 0 | if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) |
156 | 0 | return ossl_ffc_params_FIPS186_2_validate(libctx, params, paramstype, |
157 | 0 | res, NULL); |
158 | 0 | else |
159 | 0 | return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype, |
160 | 0 | res, NULL); |
161 | 554 | } else { |
162 | 554 | int ret = 0; |
163 | | |
164 | 554 | ret = ossl_ffc_params_simple_validate(libctx, params, paramstype, res); |
165 | 554 | if (ret) { |
166 | 1 | BN_CTX *ctx; |
167 | | |
168 | 1 | if ((ctx = BN_CTX_new_ex(libctx)) == NULL) |
169 | 0 | return 0; |
170 | 1 | if (BN_check_prime(params->q, ctx, NULL) != 1) { |
171 | 1 | # ifndef OPENSSL_NO_DSA |
172 | 1 | ERR_raise(ERR_LIB_DSA, DSA_R_Q_NOT_PRIME); |
173 | 1 | # endif |
174 | 1 | ret = 0; |
175 | 1 | } |
176 | 1 | if (ret && BN_check_prime(params->p, ctx, NULL) != 1) { |
177 | 0 | # ifndef OPENSSL_NO_DSA |
178 | 0 | ERR_raise(ERR_LIB_DSA, DSA_R_P_NOT_PRIME); |
179 | 0 | # endif |
180 | 0 | ret = 0; |
181 | 0 | } |
182 | 1 | BN_CTX_free(ctx); |
183 | 1 | } |
184 | 554 | return ret; |
185 | 554 | } |
186 | 554 | #endif |
187 | 554 | } |