ffc.h 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#ifndef OSSL_INTERNAL_FFC_H
# define OSSL_INTERNAL_FFC_H

13
# include <openssl/core.h>
14
# include <openssl/bn.h>
S
Shane Lontis 已提交
15 16
# include <openssl/evp.h>
# include <openssl/dh.h> /* Uses Error codes from DH */
S
Shane Lontis 已提交
17 18
# include <openssl/params.h>
# include <openssl/param_build.h>
S
Shane Lontis 已提交
19
# include "internal/sizes.h"
S
Shane Lontis 已提交
20 21 22 23 24 25 26 27

/* Default value for gindex when canonical generation of g is not used */
# define FFC_UNVERIFIABLE_GINDEX -1

/* The different types of FFC keys */
# define FFC_PARAM_TYPE_DSA  0
# define FFC_PARAM_TYPE_DH   1

S
Shane Lontis 已提交
28 29 30 31 32 33 34
/*
 * The mode used by functions that share code for both generation and
 * verification. See ffc_params_FIPS186_4_gen_verify().
 */
#define FFC_PARAM_MODE_VERIFY   0
#define FFC_PARAM_MODE_GENERATE 1

S
Shane Lontis 已提交
35
/* Return codes for generation and validation of FFC parameters */
S
Shane Lontis 已提交
36 37
#define FFC_PARAM_RET_STATUS_FAILED         0
#define FFC_PARAM_RET_STATUS_SUCCESS        1
S
Shane Lontis 已提交
38
/* Returned if validating and g is only partially verifiable */
S
Shane Lontis 已提交
39
#define FFC_PARAM_RET_STATUS_UNVERIFIABLE_G 2
S
Shane Lontis 已提交
40 41

/* Validation flags */
S
Shane Lontis 已提交
42 43 44 45
# define FFC_PARAM_FLAG_VALIDATE_PQ  0x01
# define FFC_PARAM_FLAG_VALIDATE_G   0x02
# define FFC_PARAM_FLAG_VALIDATE_ALL                                           \
    (FFC_PARAM_FLAG_VALIDATE_PQ | FFC_PARAM_FLAG_VALIDATE_G)
S
Shane Lontis 已提交
46

M
Matt Caswell 已提交
47 48 49 50 51 52 53 54 55 56 57 58
/*
 * NB: These values must align with the equivalently named macros in
 * openssl/dh.h. We cannot use those macros here in case DH has been disabled.
 */
# define FFC_CHECK_P_NOT_PRIME                0x00001
# define FFC_CHECK_P_NOT_SAFE_PRIME           0x00002
# define FFC_CHECK_UNKNOWN_GENERATOR          0x00004
# define FFC_CHECK_NOT_SUITABLE_GENERATOR     0x00008
# define FFC_CHECK_Q_NOT_PRIME                0x00010
# define FFC_CHECK_INVALID_Q_VALUE            0x00020
# define FFC_CHECK_INVALID_J_VALUE            0x00040

S
Shane Lontis 已提交
59 60 61 62 63 64 65 66 67 68
# define FFC_CHECK_BAD_LN_PAIR                0x00080
# define FFC_CHECK_INVALID_SEED_SIZE          0x00100
# define FFC_CHECK_MISSING_SEED_OR_COUNTER    0x00200
# define FFC_CHECK_INVALID_G                  0x00400
# define FFC_CHECK_INVALID_PQ                 0x00800
# define FFC_CHECK_INVALID_COUNTER            0x01000
# define FFC_CHECK_P_MISMATCH                 0x02000
# define FFC_CHECK_Q_MISMATCH                 0x04000
# define FFC_CHECK_G_MISMATCH                 0x08000
# define FFC_CHECK_COUNTER_MISMATCH           0x10000
69

S
Shane Lontis 已提交
70 71 72 73 74 75 76 77
/* Validation Return codes */
# define FFC_ERROR_PUBKEY_TOO_SMALL       0x01
# define FFC_ERROR_PUBKEY_TOO_LARGE       0x02
# define FFC_ERROR_PUBKEY_INVALID         0x04
# define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
# define FFC_ERROR_PRIVKEY_TOO_SMALL      0x10
# define FFC_ERROR_PRIVKEY_TOO_LARGE      0x20

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
 * Refer to FIPS186_4 Appendix A & B.
 */
typedef struct ffc_params_st {
    /* Primes */
    BIGNUM *p;
    BIGNUM *q;
    /* Generator */
    BIGNUM *g;
    /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */
    BIGNUM *j;

    /* Required for FIPS186_4 validation of p, q and optionally canonical g */
    unsigned char *seed;
    /* If this value is zero the hash size is used as the seed length */
    size_t seedlen;
    /* Required for FIPS186_4 validation of p and q */
    int pcounter;
97
    int nid; /* The identity of a named group */
98

S
Shane Lontis 已提交
99 100 101 102 103 104
    /*
     * Required for FIPS186_4 generation & validation of canonical g.
     * It uses unverifiable g if this value is -1.
     */
    int gindex;
    int h; /* loop counter for unverifiable g */
S
Shane Lontis 已提交
105 106 107 108 109 110 111 112

    unsigned int flags; /* See FFC_PARAM_FLAG_VALIDATE_ALL */
    /*
     * The digest to use for generation or validation. If this value is NULL,
     * then the digest is chosen using the value of N.
     */
    const char *mdname;
    const char *mdprops;
113 114 115 116 117 118 119 120
} FFC_PARAMS;

void ffc_params_init(FFC_PARAMS *params);
void ffc_params_cleanup(FFC_PARAMS *params);
void ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q, BIGNUM *g);
void ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
                         const BIGNUM **q, const BIGNUM **g);
void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
S
Shane Lontis 已提交
121 122 123 124 125
int ffc_params_set_seed(FFC_PARAMS *params,
                        const unsigned char *seed, size_t seedlen);
void ffc_params_set_gindex(FFC_PARAMS *params, int index);
void ffc_params_set_pcounter(FFC_PARAMS *params, int index);
void ffc_params_set_h(FFC_PARAMS *params, int index);
S
Shane Lontis 已提交
126 127
void ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);
int ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);
S
Shane Lontis 已提交
128

129 130 131 132 133 134 135 136 137 138
int ffc_params_set_validate_params(FFC_PARAMS *params,
                                   const unsigned char *seed, size_t seedlen,
                                   int counter);
void ffc_params_get_validate_params(const FFC_PARAMS *params,
                                    unsigned char **seed, size_t *seedlen,
                                    int *pcounter);

int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);

139
#ifndef FIPS_MODULE
140
int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
141
#endif /* FIPS_MODULE */
142

S
Shane Lontis 已提交
143 144 145

int ffc_params_FIPS186_4_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
                                  int type, size_t L, size_t N,
S
Shane Lontis 已提交
146
                                  int *res, BN_GENCB *cb);
S
Shane Lontis 已提交
147 148
int ffc_params_FIPS186_2_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
                                  int type, size_t L, size_t N,
S
Shane Lontis 已提交
149
                                  int *res, BN_GENCB *cb);
S
Shane Lontis 已提交
150

S
Shane Lontis 已提交
151
int ffc_params_FIPS186_4_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
S
Shane Lontis 已提交
152
                                    int mode, int type, size_t L, size_t N,
S
Shane Lontis 已提交
153 154
                                    int *res, BN_GENCB *cb);
int ffc_params_FIPS186_2_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
S
Shane Lontis 已提交
155
                                    int mode, int type, size_t L, size_t N,
S
Shane Lontis 已提交
156 157
                                    int *res, BN_GENCB *cb);

158
int ffc_params_simple_validate(OPENSSL_CTX *libctx, FFC_PARAMS *params, int type);
S
Shane Lontis 已提交
159 160 161 162
int ffc_params_FIPS186_4_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
                                  int type, int *res, BN_GENCB *cb);
int ffc_params_FIPS186_2_validate(OPENSSL_CTX *libctx, const FFC_PARAMS *params,
                                  int type, int *res, BN_GENCB *cb);
S
Shane Lontis 已提交
163 164 165 166 167 168 169 170

int ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
                             int N, int s, BIGNUM *priv);

int ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
                                       const BIGNUM *p, const BIGNUM *q,
                                       const BIGNUM *g, BIGNUM *tmp, int *ret);

S
Shane Lontis 已提交
171 172 173 174 175 176 177
int ffc_validate_public_key(const FFC_PARAMS *params, const BIGNUM *pub_key,
                            int *ret);
int ffc_validate_public_key_partial(const FFC_PARAMS *params,
                                    const BIGNUM *pub_key, int *ret);
int ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
                             int *ret);

S
Shane Lontis 已提交
178
int ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,
S
Shane Lontis 已提交
179
                      OSSL_PARAM params[]);
S
Shane Lontis 已提交
180
int ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
S
Shane Lontis 已提交
181 182 183
int ffc_set_group_pqg(FFC_PARAMS *ffc, const char *group_name);
int ffc_named_group_to_uid(const char *name);
const char *ffc_named_group_from_uid(int nid);
S
Shane Lontis 已提交
184
int ffc_set_group_pqg(FFC_PARAMS *ffc, const char *group_name);
S
Shane Lontis 已提交
185 186
const char *ffc_params_flags_to_name(int flags);
int ffc_params_flags_from_name(const char *name);
187

188
#endif /* OSSL_INTERNAL_FFC_H */