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