1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#ifndef OSSL_INTERNAL_FFC_H 11e1051a39Sopenharmony_ci# define OSSL_INTERNAL_FFC_H 12e1051a39Sopenharmony_ci# pragma once 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci# include <openssl/core.h> 15e1051a39Sopenharmony_ci# include <openssl/bn.h> 16e1051a39Sopenharmony_ci# include <openssl/evp.h> 17e1051a39Sopenharmony_ci# include <openssl/dh.h> /* Uses Error codes from DH */ 18e1051a39Sopenharmony_ci# include <openssl/params.h> 19e1051a39Sopenharmony_ci# include <openssl/param_build.h> 20e1051a39Sopenharmony_ci# include "internal/sizes.h" 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_ci/* Default value for gindex when canonical generation of g is not used */ 23e1051a39Sopenharmony_ci# define FFC_UNVERIFIABLE_GINDEX -1 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ci/* The different types of FFC keys */ 26e1051a39Sopenharmony_ci# define FFC_PARAM_TYPE_DSA 0 27e1051a39Sopenharmony_ci# define FFC_PARAM_TYPE_DH 1 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci/* 30e1051a39Sopenharmony_ci * The mode used by functions that share code for both generation and 31e1051a39Sopenharmony_ci * verification. See ossl_ffc_params_FIPS186_4_gen_verify(). 32e1051a39Sopenharmony_ci */ 33e1051a39Sopenharmony_ci#define FFC_PARAM_MODE_VERIFY 0 34e1051a39Sopenharmony_ci#define FFC_PARAM_MODE_GENERATE 1 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci/* Return codes for generation and validation of FFC parameters */ 37e1051a39Sopenharmony_ci#define FFC_PARAM_RET_STATUS_FAILED 0 38e1051a39Sopenharmony_ci#define FFC_PARAM_RET_STATUS_SUCCESS 1 39e1051a39Sopenharmony_ci/* Returned if validating and g is only partially verifiable */ 40e1051a39Sopenharmony_ci#define FFC_PARAM_RET_STATUS_UNVERIFIABLE_G 2 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci/* Validation flags */ 43e1051a39Sopenharmony_ci# define FFC_PARAM_FLAG_VALIDATE_PQ 0x01 44e1051a39Sopenharmony_ci# define FFC_PARAM_FLAG_VALIDATE_G 0x02 45e1051a39Sopenharmony_ci# define FFC_PARAM_FLAG_VALIDATE_PQG \ 46e1051a39Sopenharmony_ci (FFC_PARAM_FLAG_VALIDATE_PQ | FFC_PARAM_FLAG_VALIDATE_G) 47e1051a39Sopenharmony_ci#define FFC_PARAM_FLAG_VALIDATE_LEGACY 0x04 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ci/* 50e1051a39Sopenharmony_ci * NB: These values must align with the equivalently named macros in 51e1051a39Sopenharmony_ci * openssl/dh.h. We cannot use those macros here in case DH has been disabled. 52e1051a39Sopenharmony_ci */ 53e1051a39Sopenharmony_ci# define FFC_CHECK_P_NOT_PRIME 0x00001 54e1051a39Sopenharmony_ci# define FFC_CHECK_P_NOT_SAFE_PRIME 0x00002 55e1051a39Sopenharmony_ci# define FFC_CHECK_UNKNOWN_GENERATOR 0x00004 56e1051a39Sopenharmony_ci# define FFC_CHECK_NOT_SUITABLE_GENERATOR 0x00008 57e1051a39Sopenharmony_ci# define FFC_CHECK_Q_NOT_PRIME 0x00010 58e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_Q_VALUE 0x00020 59e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_J_VALUE 0x00040 60e1051a39Sopenharmony_ci 61e1051a39Sopenharmony_ci# define FFC_CHECK_BAD_LN_PAIR 0x00080 62e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_SEED_SIZE 0x00100 63e1051a39Sopenharmony_ci# define FFC_CHECK_MISSING_SEED_OR_COUNTER 0x00200 64e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_G 0x00400 65e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_PQ 0x00800 66e1051a39Sopenharmony_ci# define FFC_CHECK_INVALID_COUNTER 0x01000 67e1051a39Sopenharmony_ci# define FFC_CHECK_P_MISMATCH 0x02000 68e1051a39Sopenharmony_ci# define FFC_CHECK_Q_MISMATCH 0x04000 69e1051a39Sopenharmony_ci# define FFC_CHECK_G_MISMATCH 0x08000 70e1051a39Sopenharmony_ci# define FFC_CHECK_COUNTER_MISMATCH 0x10000 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci/* Validation Return codes */ 73e1051a39Sopenharmony_ci# define FFC_ERROR_PUBKEY_TOO_SMALL 0x01 74e1051a39Sopenharmony_ci# define FFC_ERROR_PUBKEY_TOO_LARGE 0x02 75e1051a39Sopenharmony_ci# define FFC_ERROR_PUBKEY_INVALID 0x04 76e1051a39Sopenharmony_ci# define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08 77e1051a39Sopenharmony_ci# define FFC_ERROR_PRIVKEY_TOO_SMALL 0x10 78e1051a39Sopenharmony_ci# define FFC_ERROR_PRIVKEY_TOO_LARGE 0x20 79e1051a39Sopenharmony_ci# define FFC_ERROR_PASSED_NULL_PARAM 0x40 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_ci/* 82e1051a39Sopenharmony_ci * Finite field cryptography (FFC) domain parameters are used by DH and DSA. 83e1051a39Sopenharmony_ci * Refer to FIPS186_4 Appendix A & B. 84e1051a39Sopenharmony_ci */ 85e1051a39Sopenharmony_citypedef struct ffc_params_st { 86e1051a39Sopenharmony_ci /* Primes */ 87e1051a39Sopenharmony_ci BIGNUM *p; 88e1051a39Sopenharmony_ci BIGNUM *q; 89e1051a39Sopenharmony_ci /* Generator */ 90e1051a39Sopenharmony_ci BIGNUM *g; 91e1051a39Sopenharmony_ci /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */ 92e1051a39Sopenharmony_ci BIGNUM *j; 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ci /* Required for FIPS186_4 validation of p, q and optionally canonical g */ 95e1051a39Sopenharmony_ci unsigned char *seed; 96e1051a39Sopenharmony_ci /* If this value is zero the hash size is used as the seed length */ 97e1051a39Sopenharmony_ci size_t seedlen; 98e1051a39Sopenharmony_ci /* Required for FIPS186_4 validation of p and q */ 99e1051a39Sopenharmony_ci int pcounter; 100e1051a39Sopenharmony_ci int nid; /* The identity of a named group */ 101e1051a39Sopenharmony_ci 102e1051a39Sopenharmony_ci /* 103e1051a39Sopenharmony_ci * Required for FIPS186_4 generation & validation of canonical g. 104e1051a39Sopenharmony_ci * It uses unverifiable g if this value is -1. 105e1051a39Sopenharmony_ci */ 106e1051a39Sopenharmony_ci int gindex; 107e1051a39Sopenharmony_ci int h; /* loop counter for unverifiable g */ 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_ci unsigned int flags; 110e1051a39Sopenharmony_ci /* 111e1051a39Sopenharmony_ci * The digest to use for generation or validation. If this value is NULL, 112e1051a39Sopenharmony_ci * then the digest is chosen using the value of N. 113e1051a39Sopenharmony_ci */ 114e1051a39Sopenharmony_ci const char *mdname; 115e1051a39Sopenharmony_ci const char *mdprops; 116e1051a39Sopenharmony_ci /* Default key length for known named groups according to RFC7919 */ 117e1051a39Sopenharmony_ci int keylength; 118e1051a39Sopenharmony_ci} FFC_PARAMS; 119e1051a39Sopenharmony_ci 120e1051a39Sopenharmony_civoid ossl_ffc_params_init(FFC_PARAMS *params); 121e1051a39Sopenharmony_civoid ossl_ffc_params_cleanup(FFC_PARAMS *params); 122e1051a39Sopenharmony_civoid ossl_ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q, 123e1051a39Sopenharmony_ci BIGNUM *g); 124e1051a39Sopenharmony_civoid ossl_ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p, 125e1051a39Sopenharmony_ci const BIGNUM **q, const BIGNUM **g); 126e1051a39Sopenharmony_civoid ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j); 127e1051a39Sopenharmony_ciint ossl_ffc_params_set_seed(FFC_PARAMS *params, 128e1051a39Sopenharmony_ci const unsigned char *seed, size_t seedlen); 129e1051a39Sopenharmony_civoid ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index); 130e1051a39Sopenharmony_civoid ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index); 131e1051a39Sopenharmony_civoid ossl_ffc_params_set_h(FFC_PARAMS *params, int index); 132e1051a39Sopenharmony_civoid ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags); 133e1051a39Sopenharmony_civoid ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags, 134e1051a39Sopenharmony_ci int enable); 135e1051a39Sopenharmony_ciint ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props); 136e1051a39Sopenharmony_ci 137e1051a39Sopenharmony_ciint ossl_ffc_params_set_validate_params(FFC_PARAMS *params, 138e1051a39Sopenharmony_ci const unsigned char *seed, 139e1051a39Sopenharmony_ci size_t seedlen, int counter); 140e1051a39Sopenharmony_civoid ossl_ffc_params_get_validate_params(const FFC_PARAMS *params, 141e1051a39Sopenharmony_ci unsigned char **seed, size_t *seedlen, 142e1051a39Sopenharmony_ci int *pcounter); 143e1051a39Sopenharmony_ci 144e1051a39Sopenharmony_ciint ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src); 145e1051a39Sopenharmony_ciint ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q); 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_ci#ifndef FIPS_MODULE 148e1051a39Sopenharmony_ciint ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent); 149e1051a39Sopenharmony_ci#endif /* FIPS_MODULE */ 150e1051a39Sopenharmony_ci 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params, 153e1051a39Sopenharmony_ci int type, size_t L, size_t N, 154e1051a39Sopenharmony_ci int *res, BN_GENCB *cb); 155e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params, 156e1051a39Sopenharmony_ci int type, size_t L, size_t N, 157e1051a39Sopenharmony_ci int *res, BN_GENCB *cb); 158e1051a39Sopenharmony_ci 159e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx, 160e1051a39Sopenharmony_ci FFC_PARAMS *params, int mode, int type, 161e1051a39Sopenharmony_ci size_t L, size_t N, int *res, 162e1051a39Sopenharmony_ci BN_GENCB *cb); 163e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx, 164e1051a39Sopenharmony_ci FFC_PARAMS *params, int mode, int type, 165e1051a39Sopenharmony_ci size_t L, size_t N, int *res, 166e1051a39Sopenharmony_ci BN_GENCB *cb); 167e1051a39Sopenharmony_ci 168e1051a39Sopenharmony_ciint ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, 169e1051a39Sopenharmony_ci const FFC_PARAMS *params, 170e1051a39Sopenharmony_ci int paramstype, int *res); 171e1051a39Sopenharmony_ciint ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, 172e1051a39Sopenharmony_ci const FFC_PARAMS *params, 173e1051a39Sopenharmony_ci int paramstype, int *res); 174e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx, 175e1051a39Sopenharmony_ci const FFC_PARAMS *params, 176e1051a39Sopenharmony_ci int type, int *res, BN_GENCB *cb); 177e1051a39Sopenharmony_ciint ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx, 178e1051a39Sopenharmony_ci const FFC_PARAMS *params, 179e1051a39Sopenharmony_ci int type, int *res, BN_GENCB *cb); 180e1051a39Sopenharmony_ci 181e1051a39Sopenharmony_ciint ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params, 182e1051a39Sopenharmony_ci int N, int s, BIGNUM *priv); 183e1051a39Sopenharmony_ci 184e1051a39Sopenharmony_ciint ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, 185e1051a39Sopenharmony_ci const BIGNUM *p, const BIGNUM *q, 186e1051a39Sopenharmony_ci const BIGNUM *g, BIGNUM *tmp, 187e1051a39Sopenharmony_ci int *ret); 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ciint ossl_ffc_validate_public_key(const FFC_PARAMS *params, 190e1051a39Sopenharmony_ci const BIGNUM *pub_key, int *ret); 191e1051a39Sopenharmony_ciint ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params, 192e1051a39Sopenharmony_ci const BIGNUM *pub_key, int *ret); 193e1051a39Sopenharmony_ciint ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key, 194e1051a39Sopenharmony_ci int *ret); 195e1051a39Sopenharmony_ci 196e1051a39Sopenharmony_ciint ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl, 197e1051a39Sopenharmony_ci OSSL_PARAM params[]); 198e1051a39Sopenharmony_ciint ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]); 199e1051a39Sopenharmony_ci 200e1051a39Sopenharmony_citypedef struct dh_named_group_st DH_NAMED_GROUP; 201e1051a39Sopenharmony_ciconst DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name); 202e1051a39Sopenharmony_ciconst DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid); 203e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 204e1051a39Sopenharmony_ciconst DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p, 205e1051a39Sopenharmony_ci const BIGNUM *q, 206e1051a39Sopenharmony_ci const BIGNUM *g); 207e1051a39Sopenharmony_ci#endif 208e1051a39Sopenharmony_ciint ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group); 209e1051a39Sopenharmony_ciconst char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *); 210e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH 211e1051a39Sopenharmony_ciint ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group); 212e1051a39Sopenharmony_ciconst BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group); 213e1051a39Sopenharmony_ciint ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group); 214e1051a39Sopenharmony_ci#endif 215e1051a39Sopenharmony_ci 216e1051a39Sopenharmony_ci#endif /* OSSL_INTERNAL_FFC_H */ 217