xref: /freebsd/crypto/openssl/demos/pkey/EVP_PKEY_DSA_paramgen.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*-
2*e0c4386eSCy Schubert  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert  * Example showing how to generate DSA params using
12*e0c4386eSCy Schubert  * FIPS 186-4 DSA FFC parameter generation.
13*e0c4386eSCy Schubert  */
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #include <openssl/evp.h>
16*e0c4386eSCy Schubert #include "dsa.inc"
17*e0c4386eSCy Schubert 
main(int argc,char ** argv)18*e0c4386eSCy Schubert int main(int argc, char **argv)
19*e0c4386eSCy Schubert {
20*e0c4386eSCy Schubert     int rv = EXIT_FAILURE;
21*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
22*e0c4386eSCy Schubert     const char *propq = NULL;
23*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx = NULL;
24*e0c4386eSCy Schubert     EVP_PKEY *dsaparamkey = NULL;
25*e0c4386eSCy Schubert     OSSL_PARAM params[7];
26*e0c4386eSCy Schubert     unsigned int pbits = 2048;
27*e0c4386eSCy Schubert     unsigned int qbits = 256;
28*e0c4386eSCy Schubert     int gindex = 42;
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert     ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
31*e0c4386eSCy Schubert     if (ctx == NULL)
32*e0c4386eSCy Schubert         goto cleanup;
33*e0c4386eSCy Schubert 
34*e0c4386eSCy Schubert     /*
35*e0c4386eSCy Schubert      * Demonstrate how to set optional DSA fields as params.
36*e0c4386eSCy Schubert      * See doc/man7/EVP_PKEY-FFC.pod and doc/man7/EVP_PKEY-DSA.pod
37*e0c4386eSCy Schubert      * for more information.
38*e0c4386eSCy Schubert      */
39*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
40*e0c4386eSCy Schubert                                                  "fips186_4", 0);
41*e0c4386eSCy Schubert     params[1] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
42*e0c4386eSCy Schubert     params[2] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_QBITS, &qbits);
43*e0c4386eSCy Schubert     params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
44*e0c4386eSCy Schubert     params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
45*e0c4386eSCy Schubert                                                  "SHA384", 0);
46*e0c4386eSCy Schubert     params[5] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
47*e0c4386eSCy Schubert                                                  "provider=default", 0);
48*e0c4386eSCy Schubert     params[6] = OSSL_PARAM_construct_end();
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     /* Generate a dsa param key using optional params */
51*e0c4386eSCy Schubert     if (EVP_PKEY_paramgen_init(ctx) <= 0
52*e0c4386eSCy Schubert             || EVP_PKEY_CTX_set_params(ctx, params) <= 0
53*e0c4386eSCy Schubert             || EVP_PKEY_paramgen(ctx, &dsaparamkey) <= 0) {
54*e0c4386eSCy Schubert         fprintf(stderr, "DSA paramgen failed\n");
55*e0c4386eSCy Schubert         goto cleanup;
56*e0c4386eSCy Schubert     }
57*e0c4386eSCy Schubert 
58*e0c4386eSCy Schubert     if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
59*e0c4386eSCy Schubert         goto cleanup;
60*e0c4386eSCy Schubert 
61*e0c4386eSCy Schubert     rv = EXIT_SUCCESS;
62*e0c4386eSCy Schubert cleanup:
63*e0c4386eSCy Schubert     EVP_PKEY_free(dsaparamkey);
64*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx);
65*e0c4386eSCy Schubert     return rv;
66*e0c4386eSCy Schubert }
67