xref: /freebsd-src/crypto/openssl/demos/pkey/EVP_PKEY_DSA_keygen.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 an DSA key pair.
12*e0c4386eSCy Schubert  */
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include <openssl/evp.h>
15*e0c4386eSCy Schubert #include "dsa.inc"
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert /*
18*e0c4386eSCy Schubert  * Generate dsa params using default values.
19*e0c4386eSCy Schubert  * See the EVP_PKEY_DSA_param_fromdata demo if you need
20*e0c4386eSCy Schubert  * to load DSA params from raw values.
21*e0c4386eSCy Schubert  * See the EVP_PKEY_DSA_paramgen demo if you need to
22*e0c4386eSCy Schubert  * use non default parameters.
23*e0c4386eSCy Schubert  */
dsa_genparams(OSSL_LIB_CTX * libctx,const char * propq)24*e0c4386eSCy Schubert EVP_PKEY *dsa_genparams(OSSL_LIB_CTX *libctx, const char *propq)
25*e0c4386eSCy Schubert {
26*e0c4386eSCy Schubert     EVP_PKEY *dsaparamkey = NULL;
27*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx = NULL;
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert     /* Use the dsa params in a EVP_PKEY ctx */
30*e0c4386eSCy Schubert     ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
31*e0c4386eSCy Schubert     if (ctx == NULL) {
32*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
33*e0c4386eSCy Schubert         return NULL;
34*e0c4386eSCy Schubert     }
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert     if (EVP_PKEY_paramgen_init(ctx) <= 0
37*e0c4386eSCy Schubert             || EVP_PKEY_paramgen(ctx, &dsaparamkey) <= 0) {
38*e0c4386eSCy Schubert         fprintf(stderr, "DSA paramgen failed\n");
39*e0c4386eSCy Schubert         goto cleanup;
40*e0c4386eSCy Schubert     }
41*e0c4386eSCy Schubert cleanup:
42*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx);
43*e0c4386eSCy Schubert     return dsaparamkey;
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert 
main(int argc,char ** argv)46*e0c4386eSCy Schubert int main(int argc, char **argv)
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert     int rv = EXIT_FAILURE;
49*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
50*e0c4386eSCy Schubert     const char *propq = NULL;
51*e0c4386eSCy Schubert     EVP_PKEY *dsaparamskey = NULL;
52*e0c4386eSCy Schubert     EVP_PKEY *dsakey = NULL;
53*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx = NULL;
54*e0c4386eSCy Schubert 
55*e0c4386eSCy Schubert     /* Generate random dsa params */
56*e0c4386eSCy Schubert     dsaparamskey = dsa_genparams(libctx, propq);
57*e0c4386eSCy Schubert     if (dsaparamskey == NULL)
58*e0c4386eSCy Schubert         goto cleanup;
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert     /* Use the dsa params in a EVP_PKEY ctx */
61*e0c4386eSCy Schubert     ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);
62*e0c4386eSCy Schubert     if (ctx == NULL) {
63*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
64*e0c4386eSCy Schubert         goto cleanup;
65*e0c4386eSCy Schubert     }
66*e0c4386eSCy Schubert 
67*e0c4386eSCy Schubert     /* Generate a key using the dsa params */
68*e0c4386eSCy Schubert     if (EVP_PKEY_keygen_init(ctx) <= 0
69*e0c4386eSCy Schubert             || EVP_PKEY_keygen(ctx, &dsakey) <= 0) {
70*e0c4386eSCy Schubert         fprintf(stderr, "DSA keygen failed\n");
71*e0c4386eSCy Schubert         goto cleanup;
72*e0c4386eSCy Schubert     }
73*e0c4386eSCy Schubert 
74*e0c4386eSCy Schubert     if (!dsa_print_key(dsakey, 1, libctx, propq))
75*e0c4386eSCy Schubert         goto cleanup;
76*e0c4386eSCy Schubert 
77*e0c4386eSCy Schubert     rv = EXIT_SUCCESS;
78*e0c4386eSCy Schubert cleanup:
79*e0c4386eSCy Schubert     EVP_PKEY_free(dsakey);
80*e0c4386eSCy Schubert     EVP_PKEY_free(dsaparamskey);
81*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx);
82*e0c4386eSCy Schubert     return rv;
83*e0c4386eSCy Schubert }
84