ffc.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2019-2021 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. # define FFC_CHECK_BAD_LN_PAIR 0x00080
  53. # define FFC_CHECK_INVALID_SEED_SIZE 0x00100
  54. # define FFC_CHECK_MISSING_SEED_OR_COUNTER 0x00200
  55. # define FFC_CHECK_INVALID_G 0x00400
  56. # define FFC_CHECK_INVALID_PQ 0x00800
  57. # define FFC_CHECK_INVALID_COUNTER 0x01000
  58. # define FFC_CHECK_P_MISMATCH 0x02000
  59. # define FFC_CHECK_Q_MISMATCH 0x04000
  60. # define FFC_CHECK_G_MISMATCH 0x08000
  61. # define FFC_CHECK_COUNTER_MISMATCH 0x10000
  62. /* Validation Return codes */
  63. # define FFC_ERROR_PUBKEY_TOO_SMALL 0x01
  64. # define FFC_ERROR_PUBKEY_TOO_LARGE 0x02
  65. # define FFC_ERROR_PUBKEY_INVALID 0x04
  66. # define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
  67. # define FFC_ERROR_PRIVKEY_TOO_SMALL 0x10
  68. # define FFC_ERROR_PRIVKEY_TOO_LARGE 0x20
  69. /*
  70. * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
  71. * Refer to FIPS186_4 Appendix A & B.
  72. */
  73. typedef struct ffc_params_st {
  74. /* Primes */
  75. BIGNUM *p;
  76. BIGNUM *q;
  77. /* Generator */
  78. BIGNUM *g;
  79. /* DH X9.42 Optional Subgroup factor j >= 2 where p = j * q + 1 */
  80. BIGNUM *j;
  81. /* Required for FIPS186_4 validation of p, q and optionally canonical g */
  82. unsigned char *seed;
  83. /* If this value is zero the hash size is used as the seed length */
  84. size_t seedlen;
  85. /* Required for FIPS186_4 validation of p and q */
  86. int pcounter;
  87. int nid; /* The identity of a named group */
  88. /*
  89. * Required for FIPS186_4 generation & validation of canonical g.
  90. * It uses unverifiable g if this value is -1.
  91. */
  92. int gindex;
  93. int h; /* loop counter for unverifiable g */
  94. unsigned int flags;
  95. /*
  96. * The digest to use for generation or validation. If this value is NULL,
  97. * then the digest is chosen using the value of N.
  98. */
  99. const char *mdname;
  100. const char *mdprops;
  101. } FFC_PARAMS;
  102. void ossl_ffc_params_init(FFC_PARAMS *params);
  103. void ossl_ffc_params_cleanup(FFC_PARAMS *params);
  104. void ossl_ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q,
  105. BIGNUM *g);
  106. void ossl_ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
  107. const BIGNUM **q, const BIGNUM **g);
  108. void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
  109. int ossl_ffc_params_set_seed(FFC_PARAMS *params,
  110. const unsigned char *seed, size_t seedlen);
  111. void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index);
  112. void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index);
  113. void ossl_ffc_params_set_h(FFC_PARAMS *params, int index);
  114. void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);
  115. void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
  116. int enable);
  117. int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);
  118. int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
  119. const unsigned char *seed,
  120. size_t seedlen, int counter);
  121. void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
  122. unsigned char **seed, size_t *seedlen,
  123. int *pcounter);
  124. int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
  125. int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);
  126. #ifndef FIPS_MODULE
  127. int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
  128. #endif /* FIPS_MODULE */
  129. int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
  130. int type, size_t L, size_t N,
  131. int *res, BN_GENCB *cb);
  132. int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
  133. int type, size_t L, size_t N,
  134. int *res, BN_GENCB *cb);
  135. int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx,
  136. FFC_PARAMS *params, int mode, int type,
  137. size_t L, size_t N, int *res,
  138. BN_GENCB *cb);
  139. int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx,
  140. FFC_PARAMS *params, int mode, int type,
  141. size_t L, size_t N, int *res,
  142. BN_GENCB *cb);
  143. int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx,
  144. const FFC_PARAMS *params,
  145. int paramstype, int *res);
  146. int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx,
  147. const FFC_PARAMS *params,
  148. int paramstype, int *res);
  149. int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,
  150. const FFC_PARAMS *params,
  151. int type, int *res, BN_GENCB *cb);
  152. int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,
  153. const FFC_PARAMS *params,
  154. int type, int *res, BN_GENCB *cb);
  155. int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
  156. int N, int s, BIGNUM *priv);
  157. int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
  158. const BIGNUM *p, const BIGNUM *q,
  159. const BIGNUM *g, BIGNUM *tmp,
  160. int *ret);
  161. int ossl_ffc_validate_public_key(const FFC_PARAMS *params,
  162. const BIGNUM *pub_key, int *ret);
  163. int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
  164. const BIGNUM *pub_key, int *ret);
  165. int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
  166. int *ret);
  167. int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,
  168. OSSL_PARAM params[]);
  169. int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
  170. typedef struct dh_named_group_st DH_NAMED_GROUP;
  171. const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name);
  172. const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid);
  173. #ifndef OPENSSL_NO_DH
  174. const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
  175. const BIGNUM *q,
  176. const BIGNUM *g);
  177. #endif
  178. int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group);
  179. const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *);
  180. #ifndef OPENSSL_NO_DH
  181. const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group);
  182. int ossl_ffc_named_group_set_pqg(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group);
  183. #endif
  184. #endif /* OSSL_INTERNAL_FFC_H */