EVP_PKEY_DSA_paramgen.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*-
  2. * Copyright 2022-2023 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. * Example showing how to generate DSA params using
  11. * FIPS 186-4 DSA FFC parameter generation.
  12. */
  13. #include <openssl/evp.h>
  14. #include "dsa.inc"
  15. int main(int argc, char **argv)
  16. {
  17. int ret = EXIT_FAILURE;
  18. OSSL_LIB_CTX *libctx = NULL;
  19. const char *propq = NULL;
  20. EVP_PKEY_CTX *ctx = NULL;
  21. EVP_PKEY *dsaparamkey = NULL;
  22. OSSL_PARAM params[7];
  23. unsigned int pbits = 2048;
  24. unsigned int qbits = 256;
  25. int gindex = 42;
  26. ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
  27. if (ctx == NULL)
  28. goto cleanup;
  29. /*
  30. * Demonstrate how to set optional DSA fields as params.
  31. * See doc/man7/EVP_PKEY-FFC.pod and doc/man7/EVP_PKEY-DSA.pod
  32. * for more information.
  33. */
  34. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
  35. "fips186_4", 0);
  36. params[1] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
  37. params[2] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_QBITS, &qbits);
  38. params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
  39. params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
  40. "SHA384", 0);
  41. params[5] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  42. "provider=default", 0);
  43. params[6] = OSSL_PARAM_construct_end();
  44. /* Generate a dsa param key using optional params */
  45. if (EVP_PKEY_paramgen_init(ctx) <= 0
  46. || EVP_PKEY_CTX_set_params(ctx, params) <= 0
  47. || EVP_PKEY_paramgen(ctx, &dsaparamkey) <= 0) {
  48. fprintf(stderr, "DSA paramgen failed\n");
  49. goto cleanup;
  50. }
  51. if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
  52. goto cleanup;
  53. ret = EXIT_SUCCESS;
  54. cleanup:
  55. EVP_PKEY_free(dsaparamkey);
  56. EVP_PKEY_CTX_free(ctx);
  57. return ret;
  58. }