]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ffc/ffc_params.c
Modify DSA and DH keys to use a shared FFC_PARAMS struct
[thirdparty/openssl.git] / crypto / ffc / ffc_params.c
1 /*
2 * Copyright 2019-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 <string.h> /* memset */
11 #include "internal/ffc.h"
12 #ifndef FIPS_MODE
13 # include <openssl/asn1.h> /* ffc_params_print */
14 #endif
15
16 void ffc_params_init(FFC_PARAMS *params)
17 {
18 memset(params, 0, sizeof(FFC_PARAMS));
19 params->pcounter = -1;
20 }
21
22 void ffc_params_cleanup(FFC_PARAMS *params)
23 {
24 BN_free(params->p);
25 BN_free(params->q);
26 BN_free(params->g);
27 BN_free(params->j);
28 OPENSSL_free(params->seed);
29 ffc_params_init(params);
30 }
31
32 void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
33 {
34 if (p != NULL && p != d->p) {
35 BN_free(d->p);
36 d->p = p;
37 }
38 if (q != NULL && q != d->q) {
39 BN_free(d->q);
40 d->q = q;
41 }
42 if (g != NULL && g != d->g) {
43 BN_free(d->g);
44 d->g = g;
45 }
46 }
47
48 void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
49 const BIGNUM **q, const BIGNUM **g)
50 {
51 if (p != NULL)
52 *p = d->p;
53 if (q != NULL)
54 *q = d->q;
55 if (g != NULL)
56 *g = d->g;
57 }
58
59
60 /* j is the 'cofactor' that is optionally output for ASN1. */
61 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
62 {
63 BN_free(d->j);
64 d->j = NULL;
65 if (j != NULL)
66 d->j = j;
67 }
68
69 int ffc_params_set_validate_params(FFC_PARAMS *params,
70 const unsigned char *seed, size_t seedlen,
71 int counter)
72 {
73 if (params == NULL)
74 return 0;
75
76 if (params->seed != NULL)
77 OPENSSL_free(params->seed);
78
79 if (seed != NULL && seedlen > 0) {
80 params->seed = OPENSSL_memdup(seed, seedlen);
81 if (params->seed == NULL)
82 return 0;
83 params->seedlen = seedlen;
84 } else {
85 params->seed = NULL;
86 params->seedlen = 0;
87 }
88 params->pcounter = counter;
89 return 1;
90 }
91
92 void ffc_params_get_validate_params(const FFC_PARAMS *params,
93 unsigned char **seed, size_t *seedlen,
94 int *pcounter)
95 {
96 if (seed != NULL)
97 *seed = params->seed;
98 if (seedlen != NULL)
99 *seedlen = params->seedlen;
100 if (pcounter != NULL)
101 *pcounter = params->pcounter;
102 }
103
104 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
105 {
106 BIGNUM *a;
107
108 /*
109 * If source is read only just copy the pointer, so
110 * we don't have to reallocate it.
111 */
112 if (src == NULL)
113 a = NULL;
114 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
115 && !BN_get_flags(src, BN_FLG_MALLOCED))
116 a = (BIGNUM *)src;
117 else if ((a = BN_dup(src)) == NULL)
118 return 0;
119 BN_clear_free(*dst);
120 *dst = a;
121 return 1;
122 }
123
124 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
125 {
126 if (!ffc_bn_cpy(&dst->p, src->p)
127 || !ffc_bn_cpy(&dst->g, src->g)
128 || !ffc_bn_cpy(&dst->q, src->q)
129 || !ffc_bn_cpy(&dst->j, src->j))
130 return 0;
131
132 OPENSSL_free(dst->seed);
133 dst->seedlen = src->seedlen;
134 if (src->seed != NULL) {
135 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
136 if (dst->seed == NULL)
137 return 0;
138 } else {
139 dst->seed = NULL;
140 }
141 dst->pcounter = src->pcounter;
142 return 1;
143 }
144
145 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
146 {
147 return BN_cmp(a->p, b->p) == 0
148 && BN_cmp(a->g, b->g) == 0
149 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
150 }
151
152 #ifndef FIPS_MODE
153 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
154 {
155 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
156 goto err;
157 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
158 goto err;
159 if (ffc->q != NULL
160 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
161 goto err;
162 if (ffc->j != NULL
163 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
164 goto err;
165 if (ffc->seed != NULL) {
166 size_t i;
167 BIO_indent(bp, indent, 128);
168 BIO_puts(bp, "seed:");
169 for (i = 0; i < ffc->seedlen; i++) {
170 if ((i % 15) == 0) {
171 if (BIO_puts(bp, "\n") <= 0
172 || !BIO_indent(bp, indent + 4, 128))
173 goto err;
174 }
175 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
176 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
177 goto err;
178 }
179 if (BIO_write(bp, "\n", 1) <= 0)
180 return 0;
181 }
182 if (ffc->pcounter != -1) {
183 BIO_indent(bp, indent, 128);
184 if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
185 goto err;
186 }
187 return 1;
188 err:
189 return 0;
190 }
191 #endif /* FIPS_MODE */