ffc.h 9.4 KB

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