KEYMGMT: Add functions to get param/key generation parameters
[oweals/openssl.git] / include / internal / ffc.h
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 #ifndef OSSL_INTERNAL_FFC_H
11 # define OSSL_INTERNAL_FFC_H
12
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
18 /* Default value for gindex when canonical generation of g is not used */
19 # define FFC_UNVERIFIABLE_GINDEX -1
20
21 /* The different types of FFC keys */
22 # define FFC_PARAM_TYPE_DSA  0
23 # define FFC_PARAM_TYPE_DH   1
24
25 /* Return codes for generation and validation of FFC parameters */
26 #define FFC_PARAMS_RET_STATUS_FAILED         0
27 #define FFC_PARAMS_RET_STATUS_SUCCESS        1
28 /* Returned if validating and g is only partially verifiable */
29 #define FFC_PARAMS_RET_STATUS_UNVERIFIABLE_G 2
30
31 /* Validation flags */
32 # define FFC_PARAMS_GENERATE     0x00
33 # define FFC_PARAMS_VALIDATE_PQ  0x01
34 # define FFC_PARAMS_VALIDATE_G   0x02
35 # define FFC_PARAMS_VALIDATE_ALL (FFC_PARAMS_VALIDATE_PQ | FFC_PARAMS_VALIDATE_G)
36
37 /*
38  * NB: These values must align with the equivalently named macros in
39  * openssl/dh.h. We cannot use those macros here in case DH has been disabled.
40  */
41 # define FFC_CHECK_P_NOT_PRIME                0x00001
42 # define FFC_CHECK_P_NOT_SAFE_PRIME           0x00002
43 # define FFC_CHECK_UNKNOWN_GENERATOR          0x00004
44 # define FFC_CHECK_NOT_SUITABLE_GENERATOR     0x00008
45 # define FFC_CHECK_Q_NOT_PRIME                0x00010
46 # define FFC_CHECK_INVALID_Q_VALUE            0x00020
47 # define FFC_CHECK_INVALID_J_VALUE            0x00040
48
49 # define FFC_CHECK_BAD_LN_PAIR                0x00080
50 # define FFC_CHECK_INVALID_SEED_SIZE          0x00100
51 # define FFC_CHECK_MISSING_SEED_OR_COUNTER    0x00200
52 # define FFC_CHECK_INVALID_G                  0x00400
53 # define FFC_CHECK_INVALID_PQ                 0x00800
54 # define FFC_CHECK_INVALID_COUNTER            0x01000
55 # define FFC_CHECK_P_MISMATCH                 0x02000
56 # define FFC_CHECK_Q_MISMATCH                 0x04000
57 # define FFC_CHECK_G_MISMATCH                 0x08000
58 # define FFC_CHECK_COUNTER_MISMATCH           0x10000
59
60 /* Validation Return codes */
61 # define FFC_ERROR_PUBKEY_TOO_SMALL       0x01
62 # define FFC_ERROR_PUBKEY_TOO_LARGE       0x02
63 # define FFC_ERROR_PUBKEY_INVALID         0x04
64 # define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
65 # define FFC_ERROR_PRIVKEY_TOO_SMALL      0x10
66 # define FFC_ERROR_PRIVKEY_TOO_LARGE      0x20
67
68 /*
69  * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
70  * Refer to FIPS186_4 Appendix A & B.
71  */
72 typedef struct ffc_params_st {
73     /* Primes */
74     BIGNUM *p;
75     BIGNUM *q;
76     /* Generator */
77     BIGNUM *g;
78     /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */
79     BIGNUM *j;
80
81     /* Required for FIPS186_4 validation of p, q and optionally canonical g */
82     unsigned char *seed;
83     /* If this value is zero the hash size is used as the seed length */
84     size_t seedlen;
85     /* Required for FIPS186_4 validation of p and q */
86     int pcounter;
87     int nid; /* The identity of a named group */
88
89     /*
90      * Required for FIPS186_4 generation & validation of canonical g.
91      * It uses unverifiable g if this value is -1.
92      */
93     int gindex;
94     int h; /* loop counter for unverifiable g */
95 } FFC_PARAMS;
96
97 void ffc_params_init(FFC_PARAMS *params);
98 void ffc_params_cleanup(FFC_PARAMS *params);
99 void ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q, BIGNUM *g);
100 void ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
101                          const BIGNUM **q, const BIGNUM **g);
102 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
103 int ffc_params_set_validate_params(FFC_PARAMS *params,
104                                    const unsigned char *seed, size_t seedlen,
105                                    int counter);
106 void ffc_params_get_validate_params(const FFC_PARAMS *params,
107                                     unsigned char **seed, size_t *seedlen,
108                                     int *pcounter);
109
110 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
111 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);
112
113 #ifndef FIPS_MODE
114 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
115 #endif /* FIPS_MODE */
116
117
118 int ffc_params_FIPS186_4_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
119                                   int type, size_t L, size_t N,
120                                   const EVP_MD *evpmd, int *res, BN_GENCB *cb);
121 int ffc_params_FIPS186_2_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
122                                   int type, size_t L, size_t N,
123                                   const EVP_MD *evpmd, int *res, BN_GENCB *cb);
124
125 int ffc_params_FIPS186_4_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
126                                     int type, size_t L, size_t N,
127                                     const EVP_MD *evpmd, int validate_flags,
128                                     int *res, BN_GENCB *cb);
129 int ffc_params_FIPS186_2_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
130                                     int type, size_t L, size_t N,
131                                     const EVP_MD *evpmd, int validate_flags,
132                                     int *res, BN_GENCB *cb);
133
134 int ffc_params_FIPS186_4_validate(const FFC_PARAMS *params, int type,
135                                   const EVP_MD *evpmd, int validate_flags,
136                                   int *res, BN_GENCB *cb);
137 int ffc_params_FIPS186_2_validate(const FFC_PARAMS *params, int type,
138                                   const EVP_MD *evpmd, int validate_flags,
139                                   int *res, BN_GENCB *cb);
140
141
142 int ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
143                              int N, int s, BIGNUM *priv);
144 int ffc_generate_private_key_fips(BN_CTX *ctx, const FFC_PARAMS *params,
145                                   int N, int s, BIGNUM *priv);
146
147 int ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
148                                        const BIGNUM *p, const BIGNUM *q,
149                                        const BIGNUM *g, BIGNUM *tmp, int *ret);
150
151 int ffc_validate_public_key(const FFC_PARAMS *params, const BIGNUM *pub_key,
152                             int *ret);
153 int ffc_validate_public_key_partial(const FFC_PARAMS *params,
154                                     const BIGNUM *pub_key, int *ret);
155 int ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
156                              int *ret);
157
158 int ffc_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
159
160 #endif /* OSSL_INTERNAL_FFC_H */