Path: blob/main/crypto/openssl/include/internal/ffc.h
34879 views
/*1* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_INTERNAL_FFC_H10# define OSSL_INTERNAL_FFC_H11# pragma once1213# 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"2021/* Default value for gindex when canonical generation of g is not used */22# define FFC_UNVERIFIABLE_GINDEX -12324/* The different types of FFC keys */25# define FFC_PARAM_TYPE_DSA 026# define FFC_PARAM_TYPE_DH 12728/*29* The mode used by functions that share code for both generation and30* verification. See ossl_ffc_params_FIPS186_4_gen_verify().31*/32#define FFC_PARAM_MODE_VERIFY 033#define FFC_PARAM_MODE_GENERATE 13435/* Return codes for generation and validation of FFC parameters */36#define FFC_PARAM_RET_STATUS_FAILED 037#define FFC_PARAM_RET_STATUS_SUCCESS 138/* Returned if validating and g is only partially verifiable */39#define FFC_PARAM_RET_STATUS_UNVERIFIABLE_G 24041/* Validation flags */42# define FFC_PARAM_FLAG_VALIDATE_PQ 0x0143# define FFC_PARAM_FLAG_VALIDATE_G 0x0244# define FFC_PARAM_FLAG_VALIDATE_PQG \45(FFC_PARAM_FLAG_VALIDATE_PQ | FFC_PARAM_FLAG_VALIDATE_G)46#define FFC_PARAM_FLAG_VALIDATE_LEGACY 0x044748/*49* NB: These values must align with the equivalently named macros in50* openssl/dh.h. We cannot use those macros here in case DH has been disabled.51*/52# define FFC_CHECK_P_NOT_PRIME 0x0000153# define FFC_CHECK_P_NOT_SAFE_PRIME 0x0000254# define FFC_CHECK_UNKNOWN_GENERATOR 0x0000455# define FFC_CHECK_NOT_SUITABLE_GENERATOR 0x0000856# define FFC_CHECK_Q_NOT_PRIME 0x0001057# define FFC_CHECK_INVALID_Q_VALUE 0x0002058# define FFC_CHECK_INVALID_J_VALUE 0x000405960/*61* 0x80, 0x100 reserved by include/openssl/dh.h with check bits that are not62* relevant for FFC.63*/6465# define FFC_CHECK_MISSING_SEED_OR_COUNTER 0x0020066# define FFC_CHECK_INVALID_G 0x0040067# define FFC_CHECK_INVALID_PQ 0x0080068# define FFC_CHECK_INVALID_COUNTER 0x0100069# define FFC_CHECK_P_MISMATCH 0x0200070# define FFC_CHECK_Q_MISMATCH 0x0400071# define FFC_CHECK_G_MISMATCH 0x0800072# define FFC_CHECK_COUNTER_MISMATCH 0x1000073# define FFC_CHECK_BAD_LN_PAIR 0x2000074# define FFC_CHECK_INVALID_SEED_SIZE 0x400007576/* Validation Return codes */77# define FFC_ERROR_PUBKEY_TOO_SMALL 0x0178# define FFC_ERROR_PUBKEY_TOO_LARGE 0x0279# define FFC_ERROR_PUBKEY_INVALID 0x0480# define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x0881# define FFC_ERROR_PRIVKEY_TOO_SMALL 0x1082# define FFC_ERROR_PRIVKEY_TOO_LARGE 0x2083# define FFC_ERROR_PASSED_NULL_PARAM 0x408485/*86* Finite field cryptography (FFC) domain parameters are used by DH and DSA.87* Refer to FIPS186_4 Appendix A & B.88*/89typedef struct ffc_params_st {90/* Primes */91BIGNUM *p;92BIGNUM *q;93/* Generator */94BIGNUM *g;95/* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */96BIGNUM *j;9798/* Required for FIPS186_4 validation of p, q and optionally canonical g */99unsigned char *seed;100/* If this value is zero the hash size is used as the seed length */101size_t seedlen;102/* Required for FIPS186_4 validation of p and q */103int pcounter;104int nid; /* The identity of a named group */105106/*107* Required for FIPS186_4 generation & validation of canonical g.108* It uses unverifiable g if this value is -1.109*/110int gindex;111int h; /* loop counter for unverifiable g */112113unsigned int flags;114/*115* The digest to use for generation or validation. If this value is NULL,116* then the digest is chosen using the value of N.117*/118const char *mdname;119const char *mdprops;120/* Default key length for known named groups according to RFC7919 */121int keylength;122} FFC_PARAMS;123124void ossl_ffc_params_init(FFC_PARAMS *params);125void ossl_ffc_params_cleanup(FFC_PARAMS *params);126void ossl_ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q,127BIGNUM *g);128void ossl_ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,129const BIGNUM **q, const BIGNUM **g);130void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);131int ossl_ffc_params_set_seed(FFC_PARAMS *params,132const unsigned char *seed, size_t seedlen);133void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index);134void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index);135void ossl_ffc_params_set_h(FFC_PARAMS *params, int index);136void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);137void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,138int enable);139void ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);140141int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,142const unsigned char *seed,143size_t seedlen, int counter);144void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,145unsigned char **seed, size_t *seedlen,146int *pcounter);147148int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);149int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);150151#ifndef FIPS_MODULE152int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);153#endif /* FIPS_MODULE */154155156int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,157int type, size_t L, size_t N,158int *res, BN_GENCB *cb);159int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,160int type, size_t L, size_t N,161int *res, BN_GENCB *cb);162163int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx,164FFC_PARAMS *params, int mode, int type,165size_t L, size_t N, int *res,166BN_GENCB *cb);167int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx,168FFC_PARAMS *params, int mode, int type,169size_t L, size_t N, int *res,170BN_GENCB *cb);171172int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx,173const FFC_PARAMS *params,174int paramstype, int *res);175int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx,176const FFC_PARAMS *params,177int paramstype, int *res);178int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,179const FFC_PARAMS *params,180int type, int *res, BN_GENCB *cb);181int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,182const FFC_PARAMS *params,183int type, int *res, BN_GENCB *cb);184185int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,186int N, int s, BIGNUM *priv);187188int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,189const BIGNUM *p, const BIGNUM *q,190const BIGNUM *g, BIGNUM *tmp,191int *ret);192193int ossl_ffc_validate_public_key(const FFC_PARAMS *params,194const BIGNUM *pub_key, int *ret);195int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,196const BIGNUM *pub_key, int *ret);197int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,198int *ret);199200int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,201OSSL_PARAM params[]);202int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);203204typedef struct dh_named_group_st DH_NAMED_GROUP;205const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name);206const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid);207#ifndef OPENSSL_NO_DH208const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,209const BIGNUM *q,210const BIGNUM *g);211#endif212int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group);213const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *);214#ifndef OPENSSL_NO_DH215int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group);216const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group);217int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group);218#endif219220#endif /* OSSL_INTERNAL_FFC_H */221222223