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 #ifndef OSSL_INTERNAL_FFC_H
11 # define OSSL_INTERNAL_FFC_H
13 # include <openssl/core.h>
14 # include <openssl/bn.h>
15 # include <openssl/evp.h>
16 # include <openssl/dh.h> /* Uses Error codes from DH */
17 # include <openssl/params.h>
18 # include <openssl/param_build.h>
19 # include "internal/sizes.h"
21 /* Default value for gindex when canonical generation of g is not used */
22 # define FFC_UNVERIFIABLE_GINDEX -1
24 /* The different types of FFC keys */
25 # define FFC_PARAM_TYPE_DSA 0
26 # define FFC_PARAM_TYPE_DH 1
29 * The mode used by functions that share code for both generation and
30 * verification. See ffc_params_FIPS186_4_gen_verify().
32 #define FFC_PARAM_MODE_VERIFY 0
33 #define FFC_PARAM_MODE_GENERATE 1
35 /* Return codes for generation and validation of FFC parameters */
36 #define FFC_PARAM_RET_STATUS_FAILED 0
37 #define FFC_PARAM_RET_STATUS_SUCCESS 1
38 /* Returned if validating and g is only partially verifiable */
39 #define FFC_PARAM_RET_STATUS_UNVERIFIABLE_G 2
41 /* Validation flags */
42 # define FFC_PARAM_FLAG_VALIDATE_PQ 0x01
43 # define FFC_PARAM_FLAG_VALIDATE_G 0x02
44 # define FFC_PARAM_FLAG_VALIDATE_ALL \
45 (FFC_PARAM_FLAG_VALIDATE_PQ | FFC_PARAM_FLAG_VALIDATE_G)
46 #define FFC_PARAM_FLAG_VALIDATE_LEGACY 0x04
49 * NB: These values must align with the equivalently named macros in
50 * openssl/dh.h. We cannot use those macros here in case DH has been disabled.
52 # define FFC_CHECK_P_NOT_PRIME 0x00001
53 # define FFC_CHECK_P_NOT_SAFE_PRIME 0x00002
54 # define FFC_CHECK_UNKNOWN_GENERATOR 0x00004
55 # define FFC_CHECK_NOT_SUITABLE_GENERATOR 0x00008
56 # define FFC_CHECK_Q_NOT_PRIME 0x00010
57 # define FFC_CHECK_INVALID_Q_VALUE 0x00020
58 # define FFC_CHECK_INVALID_J_VALUE 0x00040
60 # define FFC_CHECK_BAD_LN_PAIR 0x00080
61 # define FFC_CHECK_INVALID_SEED_SIZE 0x00100
62 # define FFC_CHECK_MISSING_SEED_OR_COUNTER 0x00200
63 # define FFC_CHECK_INVALID_G 0x00400
64 # define FFC_CHECK_INVALID_PQ 0x00800
65 # define FFC_CHECK_INVALID_COUNTER 0x01000
66 # define FFC_CHECK_P_MISMATCH 0x02000
67 # define FFC_CHECK_Q_MISMATCH 0x04000
68 # define FFC_CHECK_G_MISMATCH 0x08000
69 # define FFC_CHECK_COUNTER_MISMATCH 0x10000
71 /* Validation Return codes */
72 # define FFC_ERROR_PUBKEY_TOO_SMALL 0x01
73 # define FFC_ERROR_PUBKEY_TOO_LARGE 0x02
74 # define FFC_ERROR_PUBKEY_INVALID 0x04
75 # define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
76 # define FFC_ERROR_PRIVKEY_TOO_SMALL 0x10
77 # define FFC_ERROR_PRIVKEY_TOO_LARGE 0x20
80 * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
81 * Refer to FIPS186_4 Appendix A & B.
83 typedef struct ffc_params_st {
89 /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */
92 /* Required for FIPS186_4 validation of p, q and optionally canonical g */
94 /* If this value is zero the hash size is used as the seed length */
96 /* Required for FIPS186_4 validation of p and q */
98 int nid; /* The identity of a named group */
101 * Required for FIPS186_4 generation & validation of canonical g.
102 * It uses unverifiable g if this value is -1.
105 int h; /* loop counter for unverifiable g */
107 unsigned int flags; /* See FFC_PARAM_FLAG_VALIDATE_ALL */
109 * The digest to use for generation or validation. If this value is NULL,
110 * then the digest is chosen using the value of N.
116 void ffc_params_init(FFC_PARAMS *params);
117 void ffc_params_cleanup(FFC_PARAMS *params);
118 void ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q, BIGNUM *g);
119 void ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
120 const BIGNUM **q, const BIGNUM **g);
121 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
122 int ffc_params_set_seed(FFC_PARAMS *params,
123 const unsigned char *seed, size_t seedlen);
124 void ffc_params_set_gindex(FFC_PARAMS *params, int index);
125 void ffc_params_set_pcounter(FFC_PARAMS *params, int index);
126 void ffc_params_set_h(FFC_PARAMS *params, int index);
127 void ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);
128 void ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags, int enable);
129 int ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);
131 int ffc_params_set_validate_params(FFC_PARAMS *params,
132 const unsigned char *seed, size_t seedlen,
134 void ffc_params_get_validate_params(const FFC_PARAMS *params,
135 unsigned char **seed, size_t *seedlen,
138 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
139 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);
142 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
143 #endif /* FIPS_MODULE */
146 int ffc_params_FIPS186_4_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
147 int type, size_t L, size_t N,
148 int *res, BN_GENCB *cb);
149 int ffc_params_FIPS186_2_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
150 int type, size_t L, size_t N,
151 int *res, BN_GENCB *cb);
153 int ffc_params_FIPS186_4_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
154 int mode, int type, size_t L, size_t N,
155 int *res, BN_GENCB *cb);
156 int ffc_params_FIPS186_2_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
157 int mode, int type, size_t L, size_t N,
158 int *res, BN_GENCB *cb);
160 int ffc_params_simple_validate(OPENSSL_CTX *libctx, FFC_PARAMS *params, int type);
161 int ffc_params_FIPS186_4_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
162 int type, int *res, BN_GENCB *cb);
163 int ffc_params_FIPS186_2_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
164 int type, int *res, BN_GENCB *cb);
166 int ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
167 int N, int s, BIGNUM *priv);
169 int ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
170 const BIGNUM *p, const BIGNUM *q,
171 const BIGNUM *g, BIGNUM *tmp, int *ret);
173 int ffc_validate_public_key(const FFC_PARAMS *params, const BIGNUM *pub_key,
175 int ffc_validate_public_key_partial(const FFC_PARAMS *params,
176 const BIGNUM *pub_key, int *ret);
177 int ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
180 int ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,
181 OSSL_PARAM params[]);
182 int ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
183 int ffc_set_group_pqg(FFC_PARAMS *ffc, const char *group_name);
184 int ffc_named_group_to_uid(const char *name);
185 const char *ffc_named_group_from_uid(int nid);
186 int ffc_set_group_pqg(FFC_PARAMS *ffc, const char *group_name);
187 const char *ffc_params_flags_to_name(int flags);
188 int ffc_params_flags_from_name(const char *name);
190 #endif /* OSSL_INTERNAL_FFC_H */