Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/evp/dsa_ctrl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-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
#include <stdlib.h>
11
#include <openssl/core_names.h>
12
#include <openssl/err.h>
13
#include <openssl/dsa.h>
14
#include <openssl/evp.h>
15
#include "crypto/evp.h"
16
17
static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
18
0
{
19
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
20
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
21
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
22
0
        return -2;
23
0
    }
24
    /* If key type not DSA return error */
25
0
    if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
26
0
        return -1;
27
0
    return 1;
28
0
}
29
30
int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
31
0
{
32
0
    int ret;
33
0
    OSSL_PARAM params[2], *p = params;
34
35
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
36
0
        return ret;
37
38
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
39
0
                                            (char *)name, 0);
40
0
    *p++ = OSSL_PARAM_construct_end();
41
42
0
    return EVP_PKEY_CTX_set_params(ctx, params);
43
0
}
44
45
int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
46
0
{
47
0
    int ret;
48
0
    OSSL_PARAM params[2], *p = params;
49
50
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
51
0
        return ret;
52
53
0
    *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
54
0
    *p++ = OSSL_PARAM_construct_end();
55
56
0
    return EVP_PKEY_CTX_set_params(ctx, params);
57
0
}
58
59
int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
60
                                       const unsigned char *seed,
61
                                       size_t seedlen)
62
0
{
63
0
    int ret;
64
0
    OSSL_PARAM params[2], *p = params;
65
66
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
67
0
        return ret;
68
69
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
70
0
                                             (void *)seed, seedlen);
71
0
    *p++ = OSSL_PARAM_construct_end();
72
73
0
    return EVP_PKEY_CTX_set_params(ctx, params);
74
0
}
75
76
int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
77
0
{
78
0
    int ret;
79
0
    OSSL_PARAM params[2], *p = params;
80
0
    size_t bits = nbits;
81
82
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
83
0
        return ret;
84
85
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
86
0
    *p++ = OSSL_PARAM_construct_end();
87
88
0
    return EVP_PKEY_CTX_set_params(ctx, params);
89
0
}
90
91
int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
92
0
{
93
0
    int ret;
94
0
    OSSL_PARAM params[2], *p = params;
95
0
    size_t bits2 = qbits;
96
97
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
98
0
        return ret;
99
100
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
101
0
    *p++ = OSSL_PARAM_construct_end();
102
103
0
    return EVP_PKEY_CTX_set_params(ctx, params);
104
0
}
105
106
int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
107
                                           const char *md_name,
108
                                           const char *md_properties)
109
0
{
110
0
    int ret;
111
0
    OSSL_PARAM params[3], *p = params;
112
113
0
    if ((ret = dsa_paramgen_check(ctx)) <= 0)
114
0
        return ret;
115
116
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
117
0
                                            (char *)md_name, 0);
118
0
    if (md_properties != NULL)
119
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
120
0
                                                (char *)md_properties, 0);
121
0
    *p++ = OSSL_PARAM_construct_end();
122
123
0
    return EVP_PKEY_CTX_set_params(ctx, params);
124
0
}
125
126
#if !defined(FIPS_MODULE)
127
int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
128
0
{
129
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
130
0
                             EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
131
0
}
132
#endif