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