/src/openssl31/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 | 22.7k | { |
22 | 22.7k | const OSSL_PARAM *prm; |
23 | 22.7k | const OSSL_PARAM *param_p, *param_q, *param_g; |
24 | 22.7k | BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; |
25 | 22.7k | int i; |
26 | | |
27 | 22.7k | if (ffc == NULL) |
28 | 0 | return 0; |
29 | | |
30 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); |
31 | 22.7k | 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 | 0 | #ifndef OPENSSL_NO_DH |
37 | 0 | const DH_NAMED_GROUP *group = NULL; |
38 | |
|
39 | 0 | if (prm->data_type != OSSL_PARAM_UTF8_STRING |
40 | 0 | || prm->data == NULL |
41 | 0 | || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL |
42 | 0 | || !ossl_ffc_named_group_set(ffc, group)) |
43 | 0 | #endif |
44 | 0 | goto err; |
45 | 0 | } |
46 | | |
47 | 22.7k | param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P); |
48 | 22.7k | param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G); |
49 | 22.7k | param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q); |
50 | | |
51 | 22.7k | if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p)) |
52 | 22.7k | || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q)) |
53 | 22.7k | || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g))) |
54 | 0 | goto err; |
55 | | |
56 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX); |
57 | 22.7k | if (prm != NULL) { |
58 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
59 | 0 | goto err; |
60 | 10.0k | ffc->gindex = i; |
61 | 10.0k | } |
62 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER); |
63 | 22.7k | if (prm != NULL) { |
64 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
65 | 0 | goto err; |
66 | 10.0k | ffc->pcounter = i; |
67 | 10.0k | } |
68 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR); |
69 | 22.7k | if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j)) |
70 | 0 | goto err; |
71 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H); |
72 | 22.7k | if (prm != NULL) { |
73 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
74 | 0 | goto err; |
75 | 10.0k | ffc->h = i; |
76 | 10.0k | } |
77 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); |
78 | 22.7k | if (prm != NULL) { |
79 | 0 | if (prm->data_type != OSSL_PARAM_OCTET_STRING) |
80 | 0 | goto err; |
81 | 0 | if (!ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size)) |
82 | 0 | goto err; |
83 | 0 | } |
84 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ); |
85 | 22.7k | if (prm != NULL) { |
86 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
87 | 0 | goto err; |
88 | 10.0k | ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i); |
89 | 10.0k | } |
90 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G); |
91 | 22.7k | if (prm != NULL) { |
92 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
93 | 0 | goto err; |
94 | 10.0k | ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i); |
95 | 10.0k | } |
96 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY); |
97 | 22.7k | if (prm != NULL) { |
98 | 10.0k | if (!OSSL_PARAM_get_int(prm, &i)) |
99 | 0 | goto err; |
100 | 10.0k | ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, i); |
101 | 10.0k | } |
102 | | |
103 | 22.7k | prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST); |
104 | 22.7k | if (prm != NULL) { |
105 | 0 | const OSSL_PARAM *p1; |
106 | 0 | const char *props = NULL; |
107 | |
|
108 | 0 | if (prm->data_type != OSSL_PARAM_UTF8_STRING) |
109 | 0 | goto err; |
110 | 0 | p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS); |
111 | 0 | if (p1 != NULL) { |
112 | 0 | if (p1->data_type != OSSL_PARAM_UTF8_STRING) |
113 | 0 | goto err; |
114 | 0 | props = p1->data; |
115 | 0 | } |
116 | 0 | if (!ossl_ffc_set_digest(ffc, prm->data, props)) |
117 | 0 | goto err; |
118 | 0 | } |
119 | | |
120 | 22.7k | ossl_ffc_params_set0_pqg(ffc, p, q, g); |
121 | 22.7k | ossl_ffc_params_set0_j(ffc, j); |
122 | 22.7k | return 1; |
123 | | |
124 | 0 | err: |
125 | 0 | BN_free(j); |
126 | 0 | BN_free(p); |
127 | 0 | BN_free(q); |
128 | 0 | BN_free(g); |
129 | 0 | return 0; |
130 | 22.7k | } |