dsa_gen.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 1995-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. * 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 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 = 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 = 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. && !ffc_params_set_validate_params(&dsa->params, seed_in, seed_len, -1))
  52. return 0;
  53. /* The old code used FIPS 186-2 DSA Parameter generation */
  54. if (bits <= 1024 && seed_len == 20) {
  55. if (!dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_2,
  56. bits, 160, cb))
  57. return 0;
  58. } else {
  59. if (!dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_4,
  60. bits, 0, cb))
  61. return 0;
  62. }
  63. if (counter_ret != NULL)
  64. *counter_ret = dsa->params.pcounter;
  65. if (h_ret != NULL)
  66. *h_ret = dsa->params.h;
  67. return 1;
  68. }
  69. #endif