xref: /freebsd/crypto/openssl/crypto/dsa/dsa_check.c (revision 44096ebd22ddd0081a357011714eff8963614b65)
1b077aed3SPierre Pronchery /*
2*44096ebdSEnji Cooper  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * DSA low level APIs are deprecated for public use, but still ok for
12b077aed3SPierre Pronchery  * internal use.
13b077aed3SPierre Pronchery  */
14b077aed3SPierre Pronchery #include "internal/deprecated.h"
15b077aed3SPierre Pronchery 
16b077aed3SPierre Pronchery #include <stdio.h>
17b077aed3SPierre Pronchery #include "internal/cryptlib.h"
18b077aed3SPierre Pronchery #include <openssl/bn.h>
19b077aed3SPierre Pronchery #include "dsa_local.h"
20b077aed3SPierre Pronchery #include "crypto/dsa.h"
21b077aed3SPierre Pronchery 
22*44096ebdSEnji Cooper static int dsa_precheck_params(const DSA *dsa, int *ret)
23*44096ebdSEnji Cooper {
24*44096ebdSEnji Cooper     if (dsa->params.p == NULL || dsa->params.q == NULL) {
25*44096ebdSEnji Cooper         ERR_raise(ERR_LIB_DSA, DSA_R_BAD_FFC_PARAMETERS);
26*44096ebdSEnji Cooper         *ret = FFC_CHECK_INVALID_PQ;
27*44096ebdSEnji Cooper         return 0;
28*44096ebdSEnji Cooper     }
29*44096ebdSEnji Cooper 
30*44096ebdSEnji Cooper     if (BN_num_bits(dsa->params.p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
31*44096ebdSEnji Cooper         ERR_raise(ERR_LIB_DSA, DSA_R_MODULUS_TOO_LARGE);
32*44096ebdSEnji Cooper         *ret = FFC_CHECK_INVALID_PQ;
33*44096ebdSEnji Cooper         return 0;
34*44096ebdSEnji Cooper     }
35*44096ebdSEnji Cooper 
36*44096ebdSEnji Cooper     if (BN_num_bits(dsa->params.q) >= BN_num_bits(dsa->params.p)) {
37*44096ebdSEnji Cooper         ERR_raise(ERR_LIB_DSA, DSA_R_BAD_Q_VALUE);
38*44096ebdSEnji Cooper         *ret = FFC_CHECK_INVALID_PQ;
39*44096ebdSEnji Cooper         return 0;
40*44096ebdSEnji Cooper     }
41*44096ebdSEnji Cooper 
42*44096ebdSEnji Cooper     return 1;
43*44096ebdSEnji Cooper }
44*44096ebdSEnji Cooper 
45b077aed3SPierre Pronchery int ossl_dsa_check_params(const DSA *dsa, int checktype, int *ret)
46b077aed3SPierre Pronchery {
47*44096ebdSEnji Cooper     if (!dsa_precheck_params(dsa, ret))
48*44096ebdSEnji Cooper         return 0;
49*44096ebdSEnji Cooper 
50b077aed3SPierre Pronchery     if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
51b077aed3SPierre Pronchery         return ossl_ffc_params_simple_validate(dsa->libctx, &dsa->params,
52b077aed3SPierre Pronchery                                                FFC_PARAM_TYPE_DSA, ret);
53b077aed3SPierre Pronchery     else
54b077aed3SPierre Pronchery         /*
55b077aed3SPierre Pronchery          * Do full FFC domain params validation according to FIPS-186-4
56b077aed3SPierre Pronchery          *  - always in FIPS_MODULE
57b077aed3SPierre Pronchery          *  - only if possible (i.e., seed is set) in default provider
58b077aed3SPierre Pronchery          */
59b077aed3SPierre Pronchery         return ossl_ffc_params_full_validate(dsa->libctx, &dsa->params,
60b077aed3SPierre Pronchery                                              FFC_PARAM_TYPE_DSA, ret);
61b077aed3SPierre Pronchery }
62b077aed3SPierre Pronchery 
63b077aed3SPierre Pronchery /*
64b077aed3SPierre Pronchery  * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
65b077aed3SPierre Pronchery  */
66b077aed3SPierre Pronchery int ossl_dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret)
67b077aed3SPierre Pronchery {
68*44096ebdSEnji Cooper     if (!dsa_precheck_params(dsa, ret))
69*44096ebdSEnji Cooper         return 0;
70*44096ebdSEnji Cooper 
71ad991e4cSEd Maste     return ossl_ffc_validate_public_key(&dsa->params, pub_key, ret)
72ad991e4cSEd Maste            && *ret == 0;
73b077aed3SPierre Pronchery }
74b077aed3SPierre Pronchery 
75b077aed3SPierre Pronchery /*
76b077aed3SPierre Pronchery  * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
77b077aed3SPierre Pronchery  * To only be used with ephemeral FFC public keys generated using the approved
78b077aed3SPierre Pronchery  * safe-prime groups.
79b077aed3SPierre Pronchery  */
80b077aed3SPierre Pronchery int ossl_dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key, int *ret)
81b077aed3SPierre Pronchery {
82*44096ebdSEnji Cooper     if (!dsa_precheck_params(dsa, ret))
83*44096ebdSEnji Cooper         return 0;
84*44096ebdSEnji Cooper 
85ad991e4cSEd Maste     return ossl_ffc_validate_public_key_partial(&dsa->params, pub_key, ret)
86ad991e4cSEd Maste            && *ret == 0;
87b077aed3SPierre Pronchery }
88b077aed3SPierre Pronchery 
89b077aed3SPierre Pronchery int ossl_dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret)
90b077aed3SPierre Pronchery {
91b077aed3SPierre Pronchery     *ret = 0;
92b077aed3SPierre Pronchery 
93*44096ebdSEnji Cooper     if (!dsa_precheck_params(dsa, ret))
94*44096ebdSEnji Cooper         return 0;
95*44096ebdSEnji Cooper 
96*44096ebdSEnji Cooper     return ossl_ffc_validate_private_key(dsa->params.q, priv_key, ret);
97b077aed3SPierre Pronchery }
98b077aed3SPierre Pronchery 
99b077aed3SPierre Pronchery /*
100b077aed3SPierre Pronchery  * FFC pairwise check from SP800-56A R3.
101b077aed3SPierre Pronchery  *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
102b077aed3SPierre Pronchery  */
103b077aed3SPierre Pronchery int ossl_dsa_check_pairwise(const DSA *dsa)
104b077aed3SPierre Pronchery {
105b077aed3SPierre Pronchery     int ret = 0;
106b077aed3SPierre Pronchery     BN_CTX *ctx = NULL;
107b077aed3SPierre Pronchery     BIGNUM *pub_key = NULL;
108b077aed3SPierre Pronchery 
109*44096ebdSEnji Cooper     if (!dsa_precheck_params(dsa, &ret))
110*44096ebdSEnji Cooper         return 0;
111*44096ebdSEnji Cooper 
112*44096ebdSEnji Cooper     if (dsa->params.g == NULL
113b077aed3SPierre Pronchery         || dsa->priv_key == NULL
114b077aed3SPierre Pronchery         || dsa->pub_key == NULL)
115b077aed3SPierre Pronchery         return 0;
116b077aed3SPierre Pronchery 
117b077aed3SPierre Pronchery     ctx = BN_CTX_new_ex(dsa->libctx);
118b077aed3SPierre Pronchery     if (ctx == NULL)
119b077aed3SPierre Pronchery         goto err;
120b077aed3SPierre Pronchery     pub_key = BN_new();
121b077aed3SPierre Pronchery     if (pub_key == NULL)
122b077aed3SPierre Pronchery         goto err;
123b077aed3SPierre Pronchery 
124b077aed3SPierre Pronchery     /* recalculate the public key = (g ^ priv) mod p */
125b077aed3SPierre Pronchery     if (!ossl_dsa_generate_public_key(ctx, dsa, dsa->priv_key, pub_key))
126b077aed3SPierre Pronchery         goto err;
127b077aed3SPierre Pronchery     /* check it matches the existing pubic_key */
128b077aed3SPierre Pronchery     ret = BN_cmp(pub_key, dsa->pub_key) == 0;
129b077aed3SPierre Pronchery err:
130b077aed3SPierre Pronchery     BN_free(pub_key);
131b077aed3SPierre Pronchery     BN_CTX_free(ctx);
132b077aed3SPierre Pronchery     return ret;
133b077aed3SPierre Pronchery }
134