2
0

dsa_gen.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 1995-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. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/opensslconf.h>
  15. #include <stdio.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/evp.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/sha.h>
  21. #include "crypto/dsa.h"
  22. #include "dsa_local.h"
  23. int ossl_dsa_generate_ffc_parameters(DSA *dsa, int type, int pbits, int qbits,
  24. BN_GENCB *cb)
  25. {
  26. int ret = 0, res;
  27. #ifndef FIPS_MODULE
  28. if (type == DSA_PARAMGEN_TYPE_FIPS_186_2)
  29. ret = ossl_ffc_params_FIPS186_2_generate(dsa->libctx, &dsa->params,
  30. FFC_PARAM_TYPE_DSA,
  31. pbits, qbits, &res, cb);
  32. else
  33. #endif
  34. ret = ossl_ffc_params_FIPS186_4_generate(dsa->libctx, &dsa->params,
  35. FFC_PARAM_TYPE_DSA,
  36. pbits, qbits, &res, cb);
  37. if (ret > 0)
  38. dsa->dirty_cnt++;
  39. return ret;
  40. }
  41. #ifndef FIPS_MODULE
  42. int DSA_generate_parameters_ex(DSA *dsa, int bits,
  43. const unsigned char *seed_in, int seed_len,
  44. int *counter_ret, unsigned long *h_ret,
  45. BN_GENCB *cb)
  46. {
  47. if (dsa->meth->dsa_paramgen)
  48. return dsa->meth->dsa_paramgen(dsa, bits, seed_in, seed_len,
  49. counter_ret, h_ret, cb);
  50. if (seed_in != NULL
  51. && !ossl_ffc_params_set_validate_params(&dsa->params, seed_in, seed_len,
  52. -1))
  53. return 0;
  54. /* The old code used FIPS 186-2 DSA Parameter generation */
  55. if (bits < 2048 && seed_len <= 20) {
  56. if (!ossl_dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_2,
  57. bits, 160, cb))
  58. return 0;
  59. } else {
  60. if (!ossl_dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_4,
  61. bits, 0, cb))
  62. return 0;
  63. }
  64. if (counter_ret != NULL)
  65. *counter_ret = dsa->params.pcounter;
  66. if (h_ret != NULL)
  67. *h_ret = dsa->params.h;
  68. return 1;
  69. }
  70. #endif