das.h 中有2个公共函数(给pkey目录的所有工程公用):
print_bn() 打印大数值
dsa_print_key() 打印key值
打印_evp_pkey_dsa的共有数据(p,q,g, seed, pcounter) + 公钥/私钥 + DSA参数
每次产生的pkey都不一样(因为seed不一样)
/*!
\file EVP_PKEY_DSA_keygen.c
\note
openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_keygen.c
das.h 中有2个公共函数(给pkey目录的所有工程公用):
print_bn() 打印大数值
dsa_print_key() 打印key值
打印_evp_pkey_dsa的共有数据(p,q,g, seed, pcounter) + 公钥/私钥 + DSA参数
每次产生的pkey都不一样(因为seed不一样)
*/
/*-
* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/*
* Example showing how to generate an DSA key pair.
*/
#include <openssl/evp.h>
#include "dsa.h"
#include "my_openSSL_lib.h"
/*
* Generate dsa params using default values.
* See the EVP_PKEY_DSA_param_fromdata demo if you need
* to load DSA params from raw values.
* See the EVP_PKEY_DSA_paramgen demo if you need to
* use non default parameters.
*/
EVP_PKEY* dsa_genparams(OSSL_LIB_CTX* libctx, const char* propq)
{
EVP_PKEY* _evp_pkey_dsa_param = NULL;
EVP_PKEY_CTX* _evp_pkey_ctx = NULL;
/* Use the dsa params in a EVP_PKEY ctx */
_evp_pkey_ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
if (_evp_pkey_ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
return NULL;
}
if (EVP_PKEY_paramgen_init(_evp_pkey_ctx) <= 0
|| EVP_PKEY_paramgen(_evp_pkey_ctx, &_evp_pkey_dsa_param) <= 0) {
fprintf(stderr, "DSA paramgen failed\n");
goto cleanup;
}
cleanup:
EVP_PKEY_CTX_free(_evp_pkey_ctx);
return _evp_pkey_dsa_param;
}
int main(int argc, char** argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX* _ossl_lib_ctx = NULL;
const char* propq = NULL;
EVP_PKEY* _evp_pkey_dsa_param = NULL;
EVP_PKEY* _evp_pkey_dsa = NULL;
EVP_PKEY_CTX* _evp_pkey_ctx = NULL;
/* Generate random dsa params */
_evp_pkey_dsa_param = dsa_genparams(_ossl_lib_ctx, propq);
if (_evp_pkey_dsa_param == NULL)
goto cleanup;
/* Use the dsa params in a EVP_PKEY ctx */
_evp_pkey_ctx = EVP_PKEY_CTX_new_from_pkey(_ossl_lib_ctx, _evp_pkey_dsa_param, propq);
if (_evp_pkey_ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto cleanup;
}
/* Generate a key using the dsa params */
if (EVP_PKEY_keygen_init(_evp_pkey_ctx) <= 0
|| EVP_PKEY_keygen(_evp_pkey_ctx, &_evp_pkey_dsa) <= 0) {
fprintf(stderr, "DSA keygen failed\n");
goto cleanup;
}
/*! 打印_evp_pkey_dsa的共有数据(p,q,g, seed, pcounter) + 公钥/私钥 */
if (!dsa_print_key(_evp_pkey_dsa, 1, _ossl_lib_ctx, propq))
goto cleanup;
/*! 打印_evp_pkey_dsa2的共有数据(p,q,g, seed, pcounter) + DSA参数 */
if (!dsa_print_key(_evp_pkey_dsa, 0, _ossl_lib_ctx, propq))
goto cleanup;
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(_evp_pkey_dsa);
EVP_PKEY_free(_evp_pkey_dsa_param);
EVP_PKEY_CTX_free(_evp_pkey_ctx);
return ret;
}