ffc.h 8.5 KB

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