ffc_params_validate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /*
  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 "internal/ffc.h"
  15. /* FIPS186-4 A.2.2 Unverifiable partial validation of Generator g */
  16. int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont,
  17. const BIGNUM *p, const BIGNUM *q,
  18. const BIGNUM *g, BIGNUM *tmp,
  19. int *ret)
  20. {
  21. /*
  22. * A.2.2 Step (1) AND
  23. * A.2.4 Step (2)
  24. * Verify that 2 <= g <= (p - 1)
  25. */
  26. if (BN_cmp(g, BN_value_one()) <= 0 || BN_cmp(g, p) >= 0) {
  27. *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
  28. return 0;
  29. }
  30. /*
  31. * A.2.2 Step (2) AND
  32. * A.2.4 Step (3)
  33. * Check g^q mod p = 1
  34. */
  35. if (!BN_mod_exp_mont(tmp, g, q, p, ctx, mont))
  36. return 0;
  37. if (BN_cmp(tmp, BN_value_one()) != 0) {
  38. *ret |= FFC_ERROR_NOT_SUITABLE_GENERATOR;
  39. return 0;
  40. }
  41. return 1;
  42. }
  43. int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx,
  44. const FFC_PARAMS *params, int type,
  45. int *res, BN_GENCB *cb)
  46. {
  47. size_t L, N;
  48. if (params == NULL || params->p == NULL || params->q == NULL)
  49. return FFC_PARAM_RET_STATUS_FAILED;
  50. /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
  51. L = BN_num_bits(params->p);
  52. N = BN_num_bits(params->q);
  53. return ossl_ffc_params_FIPS186_4_gen_verify(libctx, (FFC_PARAMS *)params,
  54. FFC_PARAM_MODE_VERIFY, type,
  55. L, N, res, cb);
  56. }
  57. /* This may be used in FIPS mode to validate deprecated FIPS-186-2 Params */
  58. int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx,
  59. const FFC_PARAMS *params, int type,
  60. int *res, BN_GENCB *cb)
  61. {
  62. size_t L, N;
  63. if (params == NULL || params->p == NULL || params->q == NULL) {
  64. *res = FFC_CHECK_INVALID_PQ;
  65. return FFC_PARAM_RET_STATUS_FAILED;
  66. }
  67. /* A.1.1.3 Step (1..2) : L = len(p), N = len(q) */
  68. L = BN_num_bits(params->p);
  69. N = BN_num_bits(params->q);
  70. return ossl_ffc_params_FIPS186_2_gen_verify(libctx, (FFC_PARAMS *)params,
  71. FFC_PARAM_MODE_VERIFY, type,
  72. L, N, res, cb);
  73. }
  74. /*
  75. * This does a simple check of L and N and partial g.
  76. * It makes no attempt to do a full validation of p, q or g since these require
  77. * extra parameters such as the digest and seed, which may not be available for
  78. * this test.
  79. */
  80. int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params,
  81. int type)
  82. {
  83. int ret, res = 0;
  84. int save_gindex;
  85. unsigned int save_flags;
  86. if (params == NULL)
  87. return 0;
  88. save_flags = params->flags;
  89. save_gindex = params->gindex;
  90. params->flags = FFC_PARAM_FLAG_VALIDATE_G;
  91. params->gindex = FFC_UNVERIFIABLE_GINDEX;
  92. #ifndef FIPS_MODULE
  93. if (save_flags & FFC_PARAM_FLAG_VALIDATE_LEGACY)
  94. ret = ossl_ffc_params_FIPS186_2_validate(libctx, params, type, &res,
  95. NULL);
  96. else
  97. #endif
  98. ret = ossl_ffc_params_FIPS186_4_validate(libctx, params, type, &res,
  99. NULL);
  100. params->flags = save_flags;
  101. params->gindex = save_gindex;
  102. return ret != FFC_PARAM_RET_STATUS_FAILED;
  103. }