11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <openssl/core_names.h>
111cb0ef41Sopenharmony_ci#include "internal/ffc.h"
121cb0ef41Sopenharmony_ci#include "internal/sizes.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci/*
151cb0ef41Sopenharmony_ci * The intention with the "backend" source file is to offer backend support
161cb0ef41Sopenharmony_ci * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
171cb0ef41Sopenharmony_ci * implementations alike.
181cb0ef41Sopenharmony_ci */
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciint ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[])
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci    const OSSL_PARAM *prm;
231cb0ef41Sopenharmony_ci    const OSSL_PARAM *param_p, *param_q, *param_g;
241cb0ef41Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
251cb0ef41Sopenharmony_ci    int i;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    if (ffc == NULL)
281cb0ef41Sopenharmony_ci        return 0;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
311cb0ef41Sopenharmony_ci    if (prm != NULL) {
321cb0ef41Sopenharmony_ci        /*
331cb0ef41Sopenharmony_ci         * In a no-dh build we just go straight to err because we have no
341cb0ef41Sopenharmony_ci         * support for this.
351cb0ef41Sopenharmony_ci         */
361cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_DH
371cb0ef41Sopenharmony_ci        const DH_NAMED_GROUP *group = NULL;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci        if (prm->data_type != OSSL_PARAM_UTF8_STRING
401cb0ef41Sopenharmony_ci            || prm->data == NULL
411cb0ef41Sopenharmony_ci            || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL
421cb0ef41Sopenharmony_ci            || !ossl_ffc_named_group_set(ffc, group))
431cb0ef41Sopenharmony_ci#endif
441cb0ef41Sopenharmony_ci            goto err;
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
481cb0ef41Sopenharmony_ci    param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
491cb0ef41Sopenharmony_ci    param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
521cb0ef41Sopenharmony_ci        || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
531cb0ef41Sopenharmony_ci        || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
541cb0ef41Sopenharmony_ci        goto err;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
571cb0ef41Sopenharmony_ci    if (prm != NULL) {
581cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
591cb0ef41Sopenharmony_ci            goto err;
601cb0ef41Sopenharmony_ci        ffc->gindex =  i;
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
631cb0ef41Sopenharmony_ci    if (prm != NULL) {
641cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
651cb0ef41Sopenharmony_ci            goto err;
661cb0ef41Sopenharmony_ci        ffc->pcounter = i;
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
691cb0ef41Sopenharmony_ci    if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
701cb0ef41Sopenharmony_ci        goto err;
711cb0ef41Sopenharmony_ci    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
721cb0ef41Sopenharmony_ci    if (prm != NULL) {
731cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
741cb0ef41Sopenharmony_ci            goto err;
751cb0ef41Sopenharmony_ci        ffc->h =  i;
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
781cb0ef41Sopenharmony_ci    if (prm != NULL) {
791cb0ef41Sopenharmony_ci        if (prm->data_type != OSSL_PARAM_OCTET_STRING)
801cb0ef41Sopenharmony_ci            goto err;
811cb0ef41Sopenharmony_ci        if (!ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size))
821cb0ef41Sopenharmony_ci            goto err;
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ);
851cb0ef41Sopenharmony_ci    if (prm != NULL) {
861cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
871cb0ef41Sopenharmony_ci            goto err;
881cb0ef41Sopenharmony_ci        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i);
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G);
911cb0ef41Sopenharmony_ci    if (prm != NULL) {
921cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
931cb0ef41Sopenharmony_ci            goto err;
941cb0ef41Sopenharmony_ci        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i);
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci    prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY);
971cb0ef41Sopenharmony_ci    if (prm != NULL) {
981cb0ef41Sopenharmony_ci        if (!OSSL_PARAM_get_int(prm, &i))
991cb0ef41Sopenharmony_ci            goto err;
1001cb0ef41Sopenharmony_ci        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, i);
1011cb0ef41Sopenharmony_ci    }
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci    prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
1041cb0ef41Sopenharmony_ci    if (prm != NULL) {
1051cb0ef41Sopenharmony_ci        const OSSL_PARAM *p1;
1061cb0ef41Sopenharmony_ci        const char *props = NULL;
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci        if (prm->data_type != OSSL_PARAM_UTF8_STRING)
1091cb0ef41Sopenharmony_ci            goto err;
1101cb0ef41Sopenharmony_ci        p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
1111cb0ef41Sopenharmony_ci        if (p1 != NULL) {
1121cb0ef41Sopenharmony_ci            if (p1->data_type != OSSL_PARAM_UTF8_STRING)
1131cb0ef41Sopenharmony_ci                goto err;
1141cb0ef41Sopenharmony_ci            props = p1->data;
1151cb0ef41Sopenharmony_ci        }
1161cb0ef41Sopenharmony_ci        if (!ossl_ffc_set_digest(ffc, prm->data, props))
1171cb0ef41Sopenharmony_ci            goto err;
1181cb0ef41Sopenharmony_ci    }
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    ossl_ffc_params_set0_pqg(ffc, p, q, g);
1211cb0ef41Sopenharmony_ci    ossl_ffc_params_set0_j(ffc, j);
1221cb0ef41Sopenharmony_ci    return 1;
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci err:
1251cb0ef41Sopenharmony_ci    BN_free(j);
1261cb0ef41Sopenharmony_ci    BN_free(p);
1271cb0ef41Sopenharmony_ci    BN_free(q);
1281cb0ef41Sopenharmony_ci    BN_free(g);
1291cb0ef41Sopenharmony_ci    return 0;
1301cb0ef41Sopenharmony_ci}
131