xref: /freebsd/crypto/openssl/include/internal/ffc.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #ifndef OSSL_INTERNAL_FFC_H
11*b077aed3SPierre Pronchery # define OSSL_INTERNAL_FFC_H
12*b077aed3SPierre Pronchery # pragma once
13*b077aed3SPierre Pronchery 
14*b077aed3SPierre Pronchery # include <openssl/core.h>
15*b077aed3SPierre Pronchery # include <openssl/bn.h>
16*b077aed3SPierre Pronchery # include <openssl/evp.h>
17*b077aed3SPierre Pronchery # include <openssl/dh.h> /* Uses Error codes from DH */
18*b077aed3SPierre Pronchery # include <openssl/params.h>
19*b077aed3SPierre Pronchery # include <openssl/param_build.h>
20*b077aed3SPierre Pronchery # include "internal/sizes.h"
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery /* Default value for gindex when canonical generation of g is not used */
23*b077aed3SPierre Pronchery # define FFC_UNVERIFIABLE_GINDEX -1
24*b077aed3SPierre Pronchery 
25*b077aed3SPierre Pronchery /* The different types of FFC keys */
26*b077aed3SPierre Pronchery # define FFC_PARAM_TYPE_DSA  0
27*b077aed3SPierre Pronchery # define FFC_PARAM_TYPE_DH   1
28*b077aed3SPierre Pronchery 
29*b077aed3SPierre Pronchery /*
30*b077aed3SPierre Pronchery  * The mode used by functions that share code for both generation and
31*b077aed3SPierre Pronchery  * verification. See ossl_ffc_params_FIPS186_4_gen_verify().
32*b077aed3SPierre Pronchery  */
33*b077aed3SPierre Pronchery #define FFC_PARAM_MODE_VERIFY   0
34*b077aed3SPierre Pronchery #define FFC_PARAM_MODE_GENERATE 1
35*b077aed3SPierre Pronchery 
36*b077aed3SPierre Pronchery /* Return codes for generation and validation of FFC parameters */
37*b077aed3SPierre Pronchery #define FFC_PARAM_RET_STATUS_FAILED         0
38*b077aed3SPierre Pronchery #define FFC_PARAM_RET_STATUS_SUCCESS        1
39*b077aed3SPierre Pronchery /* Returned if validating and g is only partially verifiable */
40*b077aed3SPierre Pronchery #define FFC_PARAM_RET_STATUS_UNVERIFIABLE_G 2
41*b077aed3SPierre Pronchery 
42*b077aed3SPierre Pronchery /* Validation flags */
43*b077aed3SPierre Pronchery # define FFC_PARAM_FLAG_VALIDATE_PQ    0x01
44*b077aed3SPierre Pronchery # define FFC_PARAM_FLAG_VALIDATE_G     0x02
45*b077aed3SPierre Pronchery # define FFC_PARAM_FLAG_VALIDATE_PQG                                           \
46*b077aed3SPierre Pronchery     (FFC_PARAM_FLAG_VALIDATE_PQ | FFC_PARAM_FLAG_VALIDATE_G)
47*b077aed3SPierre Pronchery #define FFC_PARAM_FLAG_VALIDATE_LEGACY 0x04
48*b077aed3SPierre Pronchery 
49*b077aed3SPierre Pronchery /*
50*b077aed3SPierre Pronchery  * NB: These values must align with the equivalently named macros in
51*b077aed3SPierre Pronchery  * openssl/dh.h. We cannot use those macros here in case DH has been disabled.
52*b077aed3SPierre Pronchery  */
53*b077aed3SPierre Pronchery # define FFC_CHECK_P_NOT_PRIME                0x00001
54*b077aed3SPierre Pronchery # define FFC_CHECK_P_NOT_SAFE_PRIME           0x00002
55*b077aed3SPierre Pronchery # define FFC_CHECK_UNKNOWN_GENERATOR          0x00004
56*b077aed3SPierre Pronchery # define FFC_CHECK_NOT_SUITABLE_GENERATOR     0x00008
57*b077aed3SPierre Pronchery # define FFC_CHECK_Q_NOT_PRIME                0x00010
58*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_Q_VALUE            0x00020
59*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_J_VALUE            0x00040
60*b077aed3SPierre Pronchery 
61*b077aed3SPierre Pronchery # define FFC_CHECK_BAD_LN_PAIR                0x00080
62*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_SEED_SIZE          0x00100
63*b077aed3SPierre Pronchery # define FFC_CHECK_MISSING_SEED_OR_COUNTER    0x00200
64*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_G                  0x00400
65*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_PQ                 0x00800
66*b077aed3SPierre Pronchery # define FFC_CHECK_INVALID_COUNTER            0x01000
67*b077aed3SPierre Pronchery # define FFC_CHECK_P_MISMATCH                 0x02000
68*b077aed3SPierre Pronchery # define FFC_CHECK_Q_MISMATCH                 0x04000
69*b077aed3SPierre Pronchery # define FFC_CHECK_G_MISMATCH                 0x08000
70*b077aed3SPierre Pronchery # define FFC_CHECK_COUNTER_MISMATCH           0x10000
71*b077aed3SPierre Pronchery 
72*b077aed3SPierre Pronchery /* Validation Return codes */
73*b077aed3SPierre Pronchery # define FFC_ERROR_PUBKEY_TOO_SMALL       0x01
74*b077aed3SPierre Pronchery # define FFC_ERROR_PUBKEY_TOO_LARGE       0x02
75*b077aed3SPierre Pronchery # define FFC_ERROR_PUBKEY_INVALID         0x04
76*b077aed3SPierre Pronchery # define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
77*b077aed3SPierre Pronchery # define FFC_ERROR_PRIVKEY_TOO_SMALL      0x10
78*b077aed3SPierre Pronchery # define FFC_ERROR_PRIVKEY_TOO_LARGE      0x20
79*b077aed3SPierre Pronchery # define FFC_ERROR_PASSED_NULL_PARAM      0x40
80*b077aed3SPierre Pronchery 
81*b077aed3SPierre Pronchery /*
82*b077aed3SPierre Pronchery  * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
83*b077aed3SPierre Pronchery  * Refer to FIPS186_4 Appendix A & B.
84*b077aed3SPierre Pronchery  */
85*b077aed3SPierre Pronchery typedef struct ffc_params_st {
86*b077aed3SPierre Pronchery     /* Primes */
87*b077aed3SPierre Pronchery     BIGNUM *p;
88*b077aed3SPierre Pronchery     BIGNUM *q;
89*b077aed3SPierre Pronchery     /* Generator */
90*b077aed3SPierre Pronchery     BIGNUM *g;
91*b077aed3SPierre Pronchery     /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */
92*b077aed3SPierre Pronchery     BIGNUM *j;
93*b077aed3SPierre Pronchery 
94*b077aed3SPierre Pronchery     /* Required for FIPS186_4 validation of p, q and optionally canonical g */
95*b077aed3SPierre Pronchery     unsigned char *seed;
96*b077aed3SPierre Pronchery     /* If this value is zero the hash size is used as the seed length */
97*b077aed3SPierre Pronchery     size_t seedlen;
98*b077aed3SPierre Pronchery     /* Required for FIPS186_4 validation of p and q */
99*b077aed3SPierre Pronchery     int pcounter;
100*b077aed3SPierre Pronchery     int nid; /* The identity of a named group */
101*b077aed3SPierre Pronchery 
102*b077aed3SPierre Pronchery     /*
103*b077aed3SPierre Pronchery      * Required for FIPS186_4 generation & validation of canonical g.
104*b077aed3SPierre Pronchery      * It uses unverifiable g if this value is -1.
105*b077aed3SPierre Pronchery      */
106*b077aed3SPierre Pronchery     int gindex;
107*b077aed3SPierre Pronchery     int h; /* loop counter for unverifiable g */
108*b077aed3SPierre Pronchery 
109*b077aed3SPierre Pronchery     unsigned int flags;
110*b077aed3SPierre Pronchery     /*
111*b077aed3SPierre Pronchery      * The digest to use for generation or validation. If this value is NULL,
112*b077aed3SPierre Pronchery      * then the digest is chosen using the value of N.
113*b077aed3SPierre Pronchery      */
114*b077aed3SPierre Pronchery     const char *mdname;
115*b077aed3SPierre Pronchery     const char *mdprops;
116*b077aed3SPierre Pronchery     /* Default key length for known named groups according to RFC7919 */
117*b077aed3SPierre Pronchery     int keylength;
118*b077aed3SPierre Pronchery } FFC_PARAMS;
119*b077aed3SPierre Pronchery 
120*b077aed3SPierre Pronchery void ossl_ffc_params_init(FFC_PARAMS *params);
121*b077aed3SPierre Pronchery void ossl_ffc_params_cleanup(FFC_PARAMS *params);
122*b077aed3SPierre Pronchery void ossl_ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q,
123*b077aed3SPierre Pronchery                               BIGNUM *g);
124*b077aed3SPierre Pronchery void ossl_ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
125*b077aed3SPierre Pronchery                               const BIGNUM **q, const BIGNUM **g);
126*b077aed3SPierre Pronchery void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
127*b077aed3SPierre Pronchery int ossl_ffc_params_set_seed(FFC_PARAMS *params,
128*b077aed3SPierre Pronchery                              const unsigned char *seed, size_t seedlen);
129*b077aed3SPierre Pronchery void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index);
130*b077aed3SPierre Pronchery void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index);
131*b077aed3SPierre Pronchery void ossl_ffc_params_set_h(FFC_PARAMS *params, int index);
132*b077aed3SPierre Pronchery void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);
133*b077aed3SPierre Pronchery void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
134*b077aed3SPierre Pronchery                                   int enable);
135*b077aed3SPierre Pronchery int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);
136*b077aed3SPierre Pronchery 
137*b077aed3SPierre Pronchery int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
138*b077aed3SPierre Pronchery                                         const unsigned char *seed,
139*b077aed3SPierre Pronchery                                         size_t seedlen, int counter);
140*b077aed3SPierre Pronchery void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
141*b077aed3SPierre Pronchery                                          unsigned char **seed, size_t *seedlen,
142*b077aed3SPierre Pronchery                                          int *pcounter);
143*b077aed3SPierre Pronchery 
144*b077aed3SPierre Pronchery int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
145*b077aed3SPierre Pronchery int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);
146*b077aed3SPierre Pronchery 
147*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
148*b077aed3SPierre Pronchery int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
149*b077aed3SPierre Pronchery #endif /* FIPS_MODULE */
150*b077aed3SPierre Pronchery 
151*b077aed3SPierre Pronchery 
152*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
153*b077aed3SPierre Pronchery                                        int type, size_t L, size_t N,
154*b077aed3SPierre Pronchery                                        int *res, BN_GENCB *cb);
155*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
156*b077aed3SPierre Pronchery                                        int type, size_t L, size_t N,
157*b077aed3SPierre Pronchery                                        int *res, BN_GENCB *cb);
158*b077aed3SPierre Pronchery 
159*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx,
160*b077aed3SPierre Pronchery                                          FFC_PARAMS *params, int mode, int type,
161*b077aed3SPierre Pronchery                                          size_t L, size_t N, int *res,
162*b077aed3SPierre Pronchery                                          BN_GENCB *cb);
163*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx,
164*b077aed3SPierre Pronchery                                          FFC_PARAMS *params, int mode, int type,
165*b077aed3SPierre Pronchery                                          size_t L, size_t N, int *res,
166*b077aed3SPierre Pronchery                                          BN_GENCB *cb);
167*b077aed3SPierre Pronchery 
168*b077aed3SPierre Pronchery int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx,
169*b077aed3SPierre Pronchery                                     const FFC_PARAMS *params,
170*b077aed3SPierre Pronchery                                     int paramstype, int *res);
171*b077aed3SPierre Pronchery int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx,
172*b077aed3SPierre Pronchery                                   const FFC_PARAMS *params,
173*b077aed3SPierre Pronchery                                   int paramstype, int *res);
174*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,
175*b077aed3SPierre Pronchery                                        const FFC_PARAMS *params,
176*b077aed3SPierre Pronchery                                        int type, int *res, BN_GENCB *cb);
177*b077aed3SPierre Pronchery int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,
178*b077aed3SPierre Pronchery                                        const FFC_PARAMS *params,
179*b077aed3SPierre Pronchery                                        int type, int *res, BN_GENCB *cb);
180*b077aed3SPierre Pronchery 
181*b077aed3SPierre Pronchery int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
182*b077aed3SPierre Pronchery                                   int N, int s, BIGNUM *priv);
183*b077aed3SPierre Pronchery 
184*b077aed3SPierre Pronchery int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
185*b077aed3SPierre Pronchery                                             const BIGNUM *p, const BIGNUM *q,
186*b077aed3SPierre Pronchery                                             const BIGNUM *g, BIGNUM *tmp,
187*b077aed3SPierre Pronchery                                             int *ret);
188*b077aed3SPierre Pronchery 
189*b077aed3SPierre Pronchery int ossl_ffc_validate_public_key(const FFC_PARAMS *params,
190*b077aed3SPierre Pronchery                                  const BIGNUM *pub_key, int *ret);
191*b077aed3SPierre Pronchery int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
192*b077aed3SPierre Pronchery                                          const BIGNUM *pub_key, int *ret);
193*b077aed3SPierre Pronchery int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
194*b077aed3SPierre Pronchery                                  int *ret);
195*b077aed3SPierre Pronchery 
196*b077aed3SPierre Pronchery int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,
197*b077aed3SPierre Pronchery                            OSSL_PARAM params[]);
198*b077aed3SPierre Pronchery int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
199*b077aed3SPierre Pronchery 
200*b077aed3SPierre Pronchery typedef struct dh_named_group_st DH_NAMED_GROUP;
201*b077aed3SPierre Pronchery const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name);
202*b077aed3SPierre Pronchery const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid);
203*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DH
204*b077aed3SPierre Pronchery const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
205*b077aed3SPierre Pronchery                                                          const BIGNUM *q,
206*b077aed3SPierre Pronchery                                                          const BIGNUM *g);
207*b077aed3SPierre Pronchery #endif
208*b077aed3SPierre Pronchery int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group);
209*b077aed3SPierre Pronchery const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *);
210*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DH
211*b077aed3SPierre Pronchery int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group);
212*b077aed3SPierre Pronchery const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group);
213*b077aed3SPierre Pronchery int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group);
214*b077aed3SPierre Pronchery #endif
215*b077aed3SPierre Pronchery 
216*b077aed3SPierre Pronchery #endif /* OSSL_INTERNAL_FFC_H */
217