]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ffc/ffc_params.c
2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <string.h> /* memset */
11 #include "internal/ffc.h"
13 # include <openssl/asn1.h> /* ffc_params_print */
16 void ffc_params_init(FFC_PARAMS
*params
)
18 memset(params
, 0, sizeof(FFC_PARAMS
));
19 params
->pcounter
= -1;
22 void ffc_params_cleanup(FFC_PARAMS
*params
)
28 OPENSSL_free(params
->seed
);
29 ffc_params_init(params
);
32 void ffc_params_set0_pqg(FFC_PARAMS
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
34 if (p
!= NULL
&& p
!= d
->p
) {
38 if (q
!= NULL
&& q
!= d
->q
) {
42 if (g
!= NULL
&& g
!= d
->g
) {
48 void ffc_params_get0_pqg(const FFC_PARAMS
*d
, const BIGNUM
**p
,
49 const BIGNUM
**q
, const BIGNUM
**g
)
60 /* j is the 'cofactor' that is optionally output for ASN1. */
61 void ffc_params_set0_j(FFC_PARAMS
*d
, BIGNUM
*j
)
69 int ffc_params_set_validate_params(FFC_PARAMS
*params
,
70 const unsigned char *seed
, size_t seedlen
,
76 if (params
->seed
!= NULL
)
77 OPENSSL_free(params
->seed
);
79 if (seed
!= NULL
&& seedlen
> 0) {
80 params
->seed
= OPENSSL_memdup(seed
, seedlen
);
81 if (params
->seed
== NULL
)
83 params
->seedlen
= seedlen
;
88 params
->pcounter
= counter
;
92 void ffc_params_get_validate_params(const FFC_PARAMS
*params
,
93 unsigned char **seed
, size_t *seedlen
,
99 *seedlen
= params
->seedlen
;
100 if (pcounter
!= NULL
)
101 *pcounter
= params
->pcounter
;
104 static int ffc_bn_cpy(BIGNUM
**dst
, const BIGNUM
*src
)
109 * If source is read only just copy the pointer, so
110 * we don't have to reallocate it.
114 else if (BN_get_flags(src
, BN_FLG_STATIC_DATA
)
115 && !BN_get_flags(src
, BN_FLG_MALLOCED
))
117 else if ((a
= BN_dup(src
)) == NULL
)
124 int ffc_params_copy(FFC_PARAMS
*dst
, const FFC_PARAMS
*src
)
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
))
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
)
141 dst
->pcounter
= src
->pcounter
;
145 int ffc_params_cmp(const FFC_PARAMS
*a
, const FFC_PARAMS
*b
, int ignore_q
)
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 */
153 int ffc_params_print(BIO
*bp
, const FFC_PARAMS
*ffc
, int indent
)
155 if (!ASN1_bn_print(bp
, "prime P:", ffc
->p
, NULL
, indent
))
157 if (!ASN1_bn_print(bp
, "generator G:", ffc
->g
, NULL
, indent
))
160 && !ASN1_bn_print(bp
, "subgroup order Q:", ffc
->q
, NULL
, indent
))
163 && !ASN1_bn_print(bp
, "subgroup factor:", ffc
->j
, NULL
, indent
))
165 if (ffc
->seed
!= NULL
) {
167 BIO_indent(bp
, indent
, 128);
168 BIO_puts(bp
, "seed:");
169 for (i
= 0; i
< ffc
->seedlen
; i
++) {
171 if (BIO_puts(bp
, "\n") <= 0
172 || !BIO_indent(bp
, indent
+ 4, 128))
175 if (BIO_printf(bp
, "%02x%s", ffc
->seed
[i
],
176 ((i
+ 1) == ffc
->seedlen
) ? "" : ":") <= 0)
179 if (BIO_write(bp
, "\n", 1) <= 0)
182 if (ffc
->pcounter
!= -1) {
183 BIO_indent(bp
, indent
, 128);
184 if (BIO_printf(bp
, "counter: %d\n", ffc
->pcounter
) <= 0)
191 #endif /* FIPS_MODE */