Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/dsa/dsa_check.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
 * DSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/bn.h>
19
#include "dsa_local.h"
20
#include "crypto/dsa.h"
21
22
static int dsa_precheck_params(const DSA *dsa, int *ret)
23
12.0k
{
24
12.0k
    if (dsa->params.p == NULL || dsa->params.q == NULL) {
25
0
        ERR_raise(ERR_LIB_DSA, DSA_R_BAD_FFC_PARAMETERS);
26
0
        *ret = FFC_CHECK_INVALID_PQ;
27
0
        return 0;
28
0
    }
29
30
12.0k
    if (BN_num_bits(dsa->params.p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
31
923
        ERR_raise(ERR_LIB_DSA, DSA_R_MODULUS_TOO_LARGE);
32
923
        *ret = FFC_CHECK_INVALID_PQ;
33
923
        return 0;
34
923
    }
35
36
11.1k
    if (BN_num_bits(dsa->params.q) >= BN_num_bits(dsa->params.p)) {
37
824
        ERR_raise(ERR_LIB_DSA, DSA_R_BAD_Q_VALUE);
38
824
        *ret = FFC_CHECK_INVALID_PQ;
39
824
        return 0;
40
824
    }
41
42
10.3k
    return 1;
43
11.1k
}
44
45
int ossl_dsa_check_params(const DSA *dsa, int checktype, int *ret)
46
3.72k
{
47
3.72k
    if (!dsa_precheck_params(dsa, ret))
48
700
        return 0;
49
50
3.02k
    if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
51
0
        return ossl_ffc_params_simple_validate(dsa->libctx, &dsa->params,
52
0
            FFC_PARAM_TYPE_DSA, ret);
53
3.02k
    else
54
        /*
55
         * Do full FFC domain params validation according to FIPS-186-4
56
         *  - always in FIPS_MODULE
57
         *  - only if possible (i.e., seed is set) in default provider
58
         */
59
3.02k
        return ossl_ffc_params_full_validate(dsa->libctx, &dsa->params,
60
3.02k
            FFC_PARAM_TYPE_DSA, ret);
61
3.02k
}
62
63
/*
64
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
65
 */
66
int ossl_dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret)
67
6.31k
{
68
6.31k
    if (!dsa_precheck_params(dsa, ret))
69
867
        return 0;
70
71
5.45k
    return ossl_ffc_validate_public_key(&dsa->params, pub_key, ret)
72
5.45k
        && *ret == 0;
73
6.31k
}
74
75
/*
76
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
77
 * To only be used with ephemeral FFC public keys generated using the approved
78
 * safe-prime groups.
79
 */
80
int ossl_dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key, int *ret)
81
0
{
82
0
    if (!dsa_precheck_params(dsa, ret))
83
0
        return 0;
84
85
0
    return ossl_ffc_validate_public_key_partial(&dsa->params, pub_key, ret)
86
0
        && *ret == 0;
87
0
}
88
89
int ossl_dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret)
90
1.99k
{
91
1.99k
    *ret = 0;
92
93
1.99k
    if (!dsa_precheck_params(dsa, ret))
94
180
        return 0;
95
96
1.81k
    return ossl_ffc_validate_private_key(dsa->params.q, priv_key, ret);
97
1.99k
}
98
99
/*
100
 * FFC pairwise check from SP800-56A R3.
101
 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
102
 */
103
int ossl_dsa_check_pairwise(const DSA *dsa)
104
34
{
105
34
    int ret = 0;
106
34
    BN_CTX *ctx = NULL;
107
34
    BIGNUM *pub_key = NULL;
108
109
34
    if (!dsa_precheck_params(dsa, &ret))
110
0
        return 0;
111
112
34
    if (dsa->params.g == NULL
113
34
        || dsa->priv_key == NULL
114
34
        || dsa->pub_key == NULL)
115
0
        return 0;
116
117
34
    ctx = BN_CTX_new_ex(dsa->libctx);
118
34
    if (ctx == NULL)
119
0
        goto err;
120
34
    pub_key = BN_new();
121
34
    if (pub_key == NULL)
122
0
        goto err;
123
124
    /* recalculate the public key = (g ^ priv) mod p */
125
34
    if (!ossl_dsa_generate_public_key(ctx, dsa, dsa->priv_key, pub_key))
126
5
        goto err;
127
    /* check it matches the existing public_key */
128
29
    ret = BN_cmp(pub_key, dsa->pub_key) == 0;
129
34
err:
130
34
    BN_free(pub_key);
131
34
    BN_CTX_free(ctx);
132
34
    return ret;
133
29
}