2
0

ffc_params_validate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /*
  10. * Finite Field cryptography (FFC) is used for DSA and DH.
  11. * This file contains methods for validation of FFC parameters.
  12. * It calls the same functions as the generation as the code is very similar.
  13. */
  14. #include <openssl/err.h>
  15. #include <openssl/bn.h>
  16. #include <openssl/dsaerr.h>
  17. #include <openssl/dherr.h>
  18. #include "internal/ffc.h"
  19. /* FIPS186-4 A.2.2 Unverifiable partial validation of Generator g */
  20. int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
  21. const BIGNUM *p, const BIGNUM *q,
  22. const BIGNUM *g, BIGNUM *tmp,
  23. int *ret)
  24. {
  25. /*
  26. * A.2.2 Step (1) AND
  27. * A.2.4 Step (2)
  28. * Verify that 2 <= g <= (p - 1)
  29. */
  30. if (BN_cmp(g, BN_value_one()) <= 0 || BN_cmp(g, p) >= 0) {
  31. *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
  32. return 0;
  33. }
  34. /*
  35. * A.2.2 Step (2) AND
  36. * A.2.4 Step (3)
  37. * Check g^q mod p = 1
  38. */
  39. if (!BN_mod_exp_mont(tmp, g, q, p, ctx, mont))
  40. return 0;
  41. if (BN_cmp(tmp, BN_value_one()) != 0) {
  42. *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
  43. return 0;
  44. }
  45. return 1;
  46. }
  47. int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,
  48. const FFC_PARAMS *params, int type,
  49. int *res, BN_GENCB *cb)
  50. {
  51. size_t L, N;
  52. if (params == NULL || params->p == NULL || params->q == NULL)
  53. return FFC_PARAM_RET_STATUS_FAILED;
  54. /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
  55. L = BN_num_bits(params->p);
  56. N = BN_num_bits(params->q);
  57. return ossl_ffc_params_FIPS186_4_gen_verify(libctx, (FFC_PARAMS *)params,
  58. FFC_PARAM_MODE_VERIFY, type,
  59. L, N, res, cb);
  60. }
  61. /* This may be used in FIPS mode to validate deprecated FIPS-186-2 Params */
  62. int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,
  63. const FFC_PARAMS *params, int type,
  64. int *res, BN_GENCB *cb)
  65. {
  66. size_t L, N;
  67. if (params == NULL || params->p == NULL || params->q == NULL) {
  68. *res = FFC_CHECK_INVALID_PQ;
  69. return FFC_PARAM_RET_STATUS_FAILED;
  70. }
  71. /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
  72. L = BN_num_bits(params->p);
  73. N = BN_num_bits(params->q);
  74. return ossl_ffc_params_FIPS186_2_gen_verify(libctx, (FFC_PARAMS *)params,
  75. FFC_PARAM_MODE_VERIFY, type,
  76. L, N, res, cb);
  77. }
  78. /*
  79. * This does a simple check of L and N and partial g.
  80. * It makes no attempt to do a full validation of p, q or g since these require
  81. * extra parameters such as the digest and seed, which may not be available for
  82. * this test.
  83. */
  84. int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params,
  85. int paramstype, int *res)
  86. {
  87. int ret;
  88. int tmpres = 0;
  89. FFC_PARAMS tmpparams = {0};
  90. if (params == NULL)
  91. return 0;
  92. if (res == NULL)
  93. res = &tmpres;
  94. if (!ossl_ffc_params_copy(&tmpparams, params))
  95. return 0;
  96. tmpparams.flags = FFC_PARAM_FLAG_VALIDATE_G;
  97. tmpparams.gindex = FFC_UNVERIFIABLE_GINDEX;
  98. #ifndef FIPS_MODULE
  99. if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY)
  100. ret = ossl_ffc_params_FIPS186_2_validate(libctx, &tmpparams, paramstype,
  101. res, NULL);
  102. else
  103. #endif
  104. ret = ossl_ffc_params_FIPS186_4_validate(libctx, &tmpparams, paramstype,
  105. res, NULL);
  106. #ifndef OPENSSL_NO_DH
  107. if (ret == FFC_PARAM_RET_STATUS_FAILED
  108. && (*res & FFC_ERROR_NOT_SUITABLE_GENERATOR) != 0) {
  109. ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
  110. }
  111. #endif
  112. ossl_ffc_params_cleanup(&tmpparams);
  113. return ret != FFC_PARAM_RET_STATUS_FAILED;
  114. }
  115. /*
  116. * If possible (or always in FIPS_MODULE) do full FIPS 186-4 validation.
  117. * Otherwise do simple check but in addition also check the primality of the
  118. * p and q.
  119. */
  120. int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params,
  121. int paramstype, int *res)
  122. {
  123. int tmpres = 0;
  124. if (params == NULL)
  125. return 0;
  126. if (res == NULL)
  127. res = &tmpres;
  128. #ifdef FIPS_MODULE
  129. return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype,
  130. res, NULL);
  131. #else
  132. if (params->seed != NULL) {
  133. if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY)
  134. return ossl_ffc_params_FIPS186_2_validate(libctx, params, paramstype,
  135. res, NULL);
  136. else
  137. return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype,
  138. res, NULL);
  139. } else {
  140. int ret = 0;
  141. ret = ossl_ffc_params_simple_validate(libctx, params, paramstype, res);
  142. if (ret) {
  143. BN_CTX *ctx;
  144. if ((ctx = BN_CTX_new_ex(libctx)) == NULL)
  145. return 0;
  146. if (BN_check_prime(params->q, ctx, NULL) != 1) {
  147. # ifndef OPENSSL_NO_DSA
  148. ERR_raise(ERR_LIB_DSA, DSA_R_Q_NOT_PRIME);
  149. # endif
  150. ret = 0;
  151. }
  152. if (ret && BN_check_prime(params->p, ctx, NULL) != 1) {
  153. # ifndef OPENSSL_NO_DSA
  154. ERR_raise(ERR_LIB_DSA, DSA_R_P_NOT_PRIME);
  155. # endif
  156. ret = 0;
  157. }
  158. BN_CTX_free(ctx);
  159. }
  160. return ret;
  161. }
  162. #endif
  163. }