Path: blob/main/crypto/openssl/include/internal/ffc.h
103878 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 */154155int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,156int type, size_t L, size_t N,157int *res, BN_GENCB *cb);158int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,159int type, size_t L, size_t N,160int *res, BN_GENCB *cb);161162int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx,163FFC_PARAMS *params, int mode, int type,164size_t L, size_t N, int *res,165BN_GENCB *cb);166int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx,167FFC_PARAMS *params, int mode, int type,168size_t L, size_t N, int *res,169BN_GENCB *cb);170171int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx,172const FFC_PARAMS *params,173int paramstype, int *res);174int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx,175const FFC_PARAMS *params,176int paramstype, int *res);177int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,178const FFC_PARAMS *params,179int type, int *res, BN_GENCB *cb);180int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,181const FFC_PARAMS *params,182int type, int *res, BN_GENCB *cb);183184int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,185int N, int s, BIGNUM *priv);186187int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,188const BIGNUM *p, const BIGNUM *q,189const BIGNUM *g, BIGNUM *tmp,190int *ret);191192int ossl_ffc_validate_public_key(const FFC_PARAMS *params,193const BIGNUM *pub_key, int *ret);194int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,195const BIGNUM *pub_key, int *ret);196int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,197int *ret);198199int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,200OSSL_PARAM params[]);201int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);202203typedef struct dh_named_group_st DH_NAMED_GROUP;204const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name);205const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid);206#ifndef OPENSSL_NO_DH207const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,208const BIGNUM *q,209const BIGNUM *g);210#endif211int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group);212const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *);213#ifndef OPENSSL_NO_DH214int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group);215const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group);216int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group);217#endif218219#endif /* OSSL_INTERNAL_FFC_H */220221222