Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/ffc/ffc_backend.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2023 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 <openssl/core_names.h>
11
#include "internal/ffc.h"
12
#include "internal/sizes.h"
13
14
/*
15
 * The intention with the "backend" source file is to offer backend support
16
 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17
 * implementations alike.
18
 */
19
20
int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[])
21
33.6k
{
22
33.6k
    const OSSL_PARAM *prm;
23
33.6k
    const OSSL_PARAM *param_p, *param_q, *param_g;
24
33.6k
    BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
25
33.6k
    int i;
26
27
33.6k
    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
28
33.6k
    if (prm != NULL) {
29
        /*
30
         * In a no-dh build we just go straight to err because we have no
31
         * support for this.
32
         */
33
0
#ifndef OPENSSL_NO_DH
34
0
        const DH_NAMED_GROUP *group = NULL;
35
36
0
        if (prm->data_type != OSSL_PARAM_UTF8_STRING
37
0
            || prm->data == NULL
38
0
            || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL
39
0
            || !ossl_ffc_named_group_set(ffc, group))
40
0
#endif
41
0
            goto err;
42
0
    }
43
44
33.6k
    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
45
33.6k
    param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
46
33.6k
    param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
47
48
33.6k
    if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
49
33.6k
        || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
50
33.6k
        || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
51
0
        goto err;
52
53
33.6k
    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
54
33.6k
    if (prm != NULL) {
55
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
56
0
            goto err;
57
15.3k
        ffc->gindex =  i;
58
15.3k
    }
59
33.6k
    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
60
33.6k
    if (prm != NULL) {
61
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
62
0
            goto err;
63
15.3k
        ffc->pcounter = i;
64
15.3k
    }
65
33.6k
    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
66
33.6k
    if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
67
0
        goto err;
68
33.6k
    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
69
33.6k
    if (prm != NULL) {
70
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
71
0
            goto err;
72
15.3k
        ffc->h =  i;
73
15.3k
    }
74
33.6k
    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
75
33.6k
    if (prm != NULL) {
76
0
        if (prm->data_type != OSSL_PARAM_OCTET_STRING
77
0
            || !ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size))
78
0
            goto err;
79
0
    }
80
33.6k
    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ);
81
33.6k
    if (prm != NULL) {
82
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
83
0
            goto err;
84
15.3k
        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i);
85
15.3k
    }
86
33.6k
    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G);
87
33.6k
    if (prm != NULL) {
88
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
89
0
            goto err;
90
15.3k
        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i);
91
15.3k
    }
92
33.6k
    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY);
93
33.6k
    if (prm != NULL) {
94
15.3k
        if (!OSSL_PARAM_get_int(prm, &i))
95
0
            goto err;
96
15.3k
        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, i);
97
15.3k
    }
98
99
33.6k
    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
100
33.6k
    if (prm != NULL) {
101
0
        const OSSL_PARAM *p1;
102
0
        const char *props = NULL;
103
104
0
        if (prm->data_type != OSSL_PARAM_UTF8_STRING)
105
0
            goto err;
106
0
        p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
107
0
        if (p1 != NULL) {
108
0
            if (p1->data_type != OSSL_PARAM_UTF8_STRING)
109
0
                goto err;
110
0
            props = p1->data;
111
0
        }
112
0
        ossl_ffc_set_digest(ffc, prm->data, props);
113
0
    }
114
33.6k
    ossl_ffc_params_set0_pqg(ffc, p, q, g);
115
33.6k
    ossl_ffc_params_set0_j(ffc, j);
116
33.6k
    return 1;
117
118
0
 err:
119
0
    BN_free(j);
120
0
    BN_free(p);
121
0
    BN_free(q);
122
0
    BN_free(g);
123
0
    return 0;
124
33.6k
}