ffc.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /* Default key length for known named groups according to RFC7919 */
  102. int keylength;
  103. } FFC_PARAMS;
  104. void ossl_ffc_params_init(FFC_PARAMS *params);
  105. void ossl_ffc_params_cleanup(FFC_PARAMS *params);
  106. void ossl_ffc_params_set0_pqg(FFC_PARAMS *params, BIGNUM *p, BIGNUM *q,
  107. BIGNUM *g);
  108. void ossl_ffc_params_get0_pqg(const FFC_PARAMS *params, const BIGNUM **p,
  109. const BIGNUM **q, const BIGNUM **g);
  110. void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j);
  111. int ossl_ffc_params_set_seed(FFC_PARAMS *params,
  112. const unsigned char *seed, size_t seedlen);
  113. void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index);
  114. void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index);
  115. void ossl_ffc_params_set_h(FFC_PARAMS *params, int index);
  116. void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags);
  117. void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
  118. int enable);
  119. int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props);
  120. int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
  121. const unsigned char *seed,
  122. size_t seedlen, int counter);
  123. void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
  124. unsigned char **seed, size_t *seedlen,
  125. int *pcounter);
  126. int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src);
  127. int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q);
  128. #ifndef FIPS_MODULE
  129. int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent);
  130. #endif /* FIPS_MODULE */
  131. int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
  132. int type, size_t L, size_t N,
  133. int *res, BN_GENCB *cb);
  134. int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
  135. int type, size_t L, size_t N,
  136. int *res, BN_GENCB *cb);
  137. int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx,
  138. FFC_PARAMS *params, int mode, int type,
  139. size_t L, size_t N, int *res,
  140. BN_GENCB *cb);
  141. int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx,
  142. FFC_PARAMS *params, int mode, int type,
  143. size_t L, size_t N, int *res,
  144. BN_GENCB *cb);
  145. int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx,
  146. const FFC_PARAMS *params,
  147. int paramstype, int *res);
  148. int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx,
  149. const FFC_PARAMS *params,
  150. int paramstype, int *res);
  151. int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,
  152. const FFC_PARAMS *params,
  153. int type, int *res, BN_GENCB *cb);
  154. int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,
  155. const FFC_PARAMS *params,
  156. int type, int *res, BN_GENCB *cb);
  157. int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
  158. int N, int s, BIGNUM *priv);
  159. int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
  160. const BIGNUM *p, const BIGNUM *q,
  161. const BIGNUM *g, BIGNUM *tmp,
  162. int *ret);
  163. int ossl_ffc_validate_public_key(const FFC_PARAMS *params,
  164. const BIGNUM *pub_key, int *ret);
  165. int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
  166. const BIGNUM *pub_key, int *ret);
  167. int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv_key,
  168. int *ret);
  169. int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *tmpl,
  170. OSSL_PARAM params[]);
  171. int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]);
  172. typedef struct dh_named_group_st DH_NAMED_GROUP;
  173. const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name);
  174. const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid);
  175. #ifndef OPENSSL_NO_DH
  176. const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
  177. const BIGNUM *q,
  178. const BIGNUM *g);
  179. #endif
  180. int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group);
  181. const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *);
  182. #ifndef OPENSSL_NO_DH
  183. int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group);
  184. const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group);
  185. int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group);
  186. #endif
  187. #endif /* OSSL_INTERNAL_FFC_H */