列举 OpenSSL 中内建的椭圆曲线

    要查看 OpenSSL 中内建的椭圆曲线,有两种方式。第一种是使用命令行,命令是: openssl ecparam -list_curves  。第二种是自己编写程序,调用 OpenSSL 中的函数。

    对于第二种方式,详细介绍如下:这里使用的 OpenSSL 版本是 1.1.1,在 OpenSSL 源码的 include\openssl\ec.h 中定义了结构体 EC_builtin_curve,内容是:

typedef struct {
    int nid;
    const char *comment;
} EC_builtin_curve;

    还声明了函数 EC_get_builtin_curves( ),该函数的作用是获取所有内建曲线的信息(包括 nid 和 comment),函数的实现在 ec_curve.c 中,如下:

size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems)
{
    size_t i, min;

    if (r == NULL || nitems == 0)
        return curve_list_length;

    min = nitems < curve_list_length ? nitems : curve_list_length;

    for (i = 0; i < min; i++) {
        r[i].nid = curve_list[i].nid;
        r[i].comment = curve_list[i].comment;
    }

    return curve_list_length;
}

    从该函数实现中可以看出:如果参数 r 为空指针,或 nitmes 值为 0, 该函数返回内建曲线条数。如果参数 nitems 的值大于 0 并且小于内建曲线条数,则只获取前 nitems 条曲线的信息。
    函数执行成功时,该函数返回内建曲线条数,执行失败返回 0。

    调用函数 EC_get_builtin_curves( ),列举当前 OpenSSL 版本中内建所有椭圆曲线信息的示例程序如下:

/**************************************************
* File name: list_built_in_curves.c
* Author: HAN Wei
* Author's blog: http://blog.csdn.net/henter/
* Date: Aug 22nd, 2018
* Description: demonstrate how to list all built-in
               EC curves in OpenSSL
**************************************************/

#include <stdio.h>
#include <openssl/ec.h>

int main()
{
	EC_builtin_curve *curves = NULL, *p;
	int curves_count, i;
	
	if ( !(curves_count = EC_get_builtin_curves(NULL, 0)) )
	{
		printf("Get built-in EC curves count failed!\n");
		return (-1);
	}
	if ( !(curves = (EC_builtin_curve *)malloc(sizeof(EC_builtin_curve) * curves_count)) )
	{
		printf("Allocate memory failed!\n");
		return (-1);
	}
	if ( !(curves_count = EC_get_builtin_curves(curves, curves_count)) )
	{
		printf("Get built-in EC curves info failed!\n");
		free(curves);
		return (-1);
	}
	
	printf("Total built-in EC curves count: %d\n", curves_count);
	printf("Built-in EC curves info:\n");
	p = curves;
	for (i = 0; i < curves_count; i++)
	{
		printf("EC curve item: %d\n", (i+1));
		printf("NID: %d\n", p->nid);
		printf("Comment: %s\n", p->comment);
		p++;
	}
	
	free(curves);
	return 0;
}


程序执行结果的部分截图如下:

 

可以看出,当前版本 OpenSSL 内建 82 种椭圆曲线,其中包括了中国国密 SM2 算法的曲线,其 nid 值为 1172。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值