]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ffc/ffc_backend.c
EVP_PKEY & DH: Make DH EVP_PKEY_CTX parameter ctrls / setters more available
[thirdparty/openssl.git] / crypto / ffc / ffc_backend.c
1 /*
2 * Copyright 2020 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 {
22 const OSSL_PARAM *prm;
23 const OSSL_PARAM *param_p, *param_q, *param_g;
24 BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
25 int i;
26
27 if (ffc == NULL)
28 return 0;
29
30 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
31 if (prm != NULL) {
32 /*
33 * In a no-dh build we just go straight to err because we have no
34 * support for this.
35 */
36 #ifndef OPENSSL_NO_DH
37 const DH_NAMED_GROUP *group = NULL;
38
39 if (prm->data_type != OSSL_PARAM_UTF8_STRING
40 || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL
41 || !ossl_ffc_named_group_set_pqg(ffc, group))
42 #endif
43 goto err;
44 }
45
46 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
47 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
48 param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
49
50 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
51 || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
52 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
53 goto err;
54
55 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
56 if (prm != NULL) {
57 if (!OSSL_PARAM_get_int(prm, &i))
58 goto err;
59 ffc->gindex = i;
60 }
61 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
62 if (prm != NULL) {
63 if (!OSSL_PARAM_get_int(prm, &i))
64 goto err;
65 ffc->pcounter = i;
66 }
67 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
68 if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
69 goto err;
70 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
71 if (prm != NULL) {
72 if (!OSSL_PARAM_get_int(prm, &i))
73 goto err;
74 ffc->h = i;
75 }
76 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
77 if (prm != NULL) {
78 if (prm->data_type != OSSL_PARAM_OCTET_STRING)
79 goto err;
80 if (!ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size))
81 goto err;
82 }
83 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE);
84 if (prm != NULL) {
85 if (prm->data_type != OSSL_PARAM_UTF8_STRING)
86 goto err;
87 ossl_ffc_params_set_flags(ffc, ossl_ffc_params_flags_from_name(prm->data));
88 }
89 prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
90 if (prm != NULL) {
91 const OSSL_PARAM *p1;
92 const char *props = NULL;
93
94 if (prm->data_type != OSSL_PARAM_UTF8_STRING)
95 goto err;
96 p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
97 if (p1 != NULL) {
98 if (p1->data_type != OSSL_PARAM_UTF8_STRING)
99 goto err;
100 }
101 if (!ossl_ffc_set_digest(ffc, prm->data, props))
102 goto err;
103 }
104
105 ossl_ffc_params_set0_pqg(ffc, p, q, g);
106 ossl_ffc_params_set0_j(ffc, j);
107 return 1;
108
109 err:
110 BN_free(j);
111 BN_free(p);
112 BN_free(q);
113 BN_free(g);
114 return 0;
115 }