openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

发布时间:2024年01月16日

openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

概述

开源工程包含的头文件类型是.inc, 要重命名为.h, 否则没有c++头文件语法提示

从数组中载入大数p,q,g
建立ossl_param, 设置参数的p,q,g
用参数初始化_evp_pkey_ctx, 得到_evp_pkey_dsa
打印_evp_pkey_dsa的共有数据(p,q,g …) + 参数

笔记

/*!
\file EVP_PKEY_DSA_paramfromdata.c
\note
openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

开源工程包含的头文件类型是.inc, 要重命名为.h, 否则没有c++头文件语法提示

从数组中载入大数p,q,g
建立ossl_param, 设置参数的p,q,g 
用参数初始化_evp_pkey_ctx, 得到_evp_pkey_dsa
打印_evp_pkey_dsa的共有数据(p,q,g ...) + 参数
*/

/*-
 * 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 load DSA params from raw data
  * using EVP_PKEY_fromdata()
  */

#include <openssl/param_build.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include "dsa.h"

#include "my_openSSL_lib.h"

int main(int argc, char** argv)
{
	int ret = EXIT_FAILURE;
	OSSL_LIB_CTX* _ossl_lib_ctx = NULL;
	const char* propq = NULL;
	EVP_PKEY_CTX* _evp_pkey_ctx = NULL;
	EVP_PKEY* _evp_pkey_dsa = NULL;
	OSSL_PARAM_BLD* _ossl_param_bld = NULL;
	OSSL_PARAM* _ossl_param = NULL;
	BIGNUM* p = NULL, * q = NULL, * g = NULL;

	p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);
	q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);
	g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);
	if (p == NULL || q == NULL || g == NULL)
		goto cleanup;

	/* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */
	_ossl_param_bld = OSSL_PARAM_BLD_new();
	if (_ossl_param_bld == NULL)
		goto cleanup;
	if (!OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_P, p)
		|| !OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_Q, q)
		|| !OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_G, g))
		goto cleanup;
	_ossl_param = OSSL_PARAM_BLD_to_param(_ossl_param_bld);
	if (_ossl_param == NULL)
		goto cleanup;

	_evp_pkey_ctx = EVP_PKEY_CTX_new_from_name(_ossl_lib_ctx, "DSA", propq);
	if (_evp_pkey_ctx == NULL) {
		fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
		goto cleanup;
	}

	if (EVP_PKEY_fromdata_init(_evp_pkey_ctx) <= 0
		|| EVP_PKEY_fromdata(_evp_pkey_ctx, &_evp_pkey_dsa, EVP_PKEY_KEY_PARAMETERS, _ossl_param) <= 0) {
		fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
		goto cleanup;
	}

	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_CTX_free(_evp_pkey_ctx);
	OSSL_PARAM_free(_ossl_param);
	OSSL_PARAM_BLD_free(_ossl_param_bld);
	BN_free(g);
	BN_free(q);
	BN_free(p);

	return ret;
}

END

文章来源:https://blog.csdn.net/LostSpeed/article/details/135621653
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。