openssl3.2 - exp - generate prime

openssl3.2 - exp - generate prime

概述

openssl3.2.命令行可以生成质数

openssl prime -generate -bits 256 -hex -safe

单步调试openssl.exe工程, 整理了一个函数, 用openssl API来产生质数.
openssl命令行是将结果打印到UI上, 我是要将大数结果放到buffer中.
查了openssl源码, 可以用BN_bn2binpad()来干这个活.

笔记

/*!
* \file main.cpp
* \note exp014_gen_prime
* openssl3.2 - exp - generate prime
* 用命令行产生质数 => openssl prime -generate -bits 256 -hex -safe
* openssl源码处理主函数 prime_main()
*/

#include "my_openSSL_lib.h"
#include <openssl/crypto.h>
#include <openssl/bio.h>

#include <openssl/bn.h>

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "CMemHookRec.h"

void my_openssl_app();
bool my_gen_prime(int bits, int safe, UCHAR*& pBuf, int&lenBuf);
bool bn_data_to_bio(BIGNUM* bn, BIO* bio);

int main(int argc, char** argv)
{
	setvbuf(stdout, NULL, _IONBF, 0); // 清掉stdout缓存, 防止调用printf时阻塞
	mem_hook();

	my_openssl_app();

	mem_unhook();

	/*! run result
	generate safe prime(256bits) below:
	0000 - d8 a6 2b e1 a9 c2 42 de-f4 e1 c9 36 c6 e9 8e 7e   ..+...B....6...~
	0010 - a3 ec 19 03 55 dd e8 f9-17 61 7c f2 03 d4 1a 6b   ....U....a|....k
	free map, g_mem_hook_map.size() = 0
	*/

	return 0;
}

void my_openssl_app()
{
	UCHAR* pBuf = NULL;
	int lenBuf = 0;
	bool b_rc = false;

	do {
		printf("generate safe prime(256bits) below:\n");
		b_rc = my_gen_prime(8 * 32, 1, pBuf, lenBuf);
		if (!b_rc)
		{
			printf("err\n");
			break;
		}
				
		BIO_dump_fp(stdout, pBuf, lenBuf);
	} while (false);
	
	if (NULL != pBuf)
	{
		OPENSSL_free(pBuf);
		pBuf = NULL;
	}
}

bool my_gen_prime(int bits, int safe, UCHAR*& pBuf, int& lenBuf)
{
	BIGNUM* bn = NULL;
	int i_rc = 0;
	bool b_rc = false;

	do {
		bn = BN_new();
		if (NULL == bn)
		{
			break;
		}

		i_rc = BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL);
		if (1 != i_rc)
		{
			break;
		}

		lenBuf = BN_num_bytes(bn);
		if (lenBuf <= 0)
		{
			break;
		}

		pBuf = (UCHAR*)OPENSSL_zalloc(lenBuf);
		if (NULL == pBuf)
		{
			break;
		}

		i_rc = BN_bn2binpad(bn, pBuf, lenBuf);
		if (lenBuf != i_rc)
		{
			break;
		}

		b_rc = true;
	} while (false);

	if (NULL != bn)
	{
		BN_free(bn);
		bn = NULL;
	}

	return b_rc;
}

END

相关推荐

  1. openssl3.2 - exp - RAND_bytes_ex

    2024-03-13 00:42:04       29 阅读
  2. openssl+EVP详解

    2024-03-13 00:42:04       28 阅读
  3. openssl3.2 - exp - get openssl version info

    2024-03-13 00:42:04       9 阅读
  4. openssl3.2 - exp - PEM <==> DER

    2024-03-13 00:42:04       28 阅读
  5. openssl3.2 - exp - AES-256-GCM

    2024-03-13 00:42:04       16 阅读
  6. openssl3.2 - exp - generate prime

    2024-03-13 00:42:04       22 阅读
  7. openssl3.2 - exp - aes-128-cbc

    2024-03-13 00:42:04       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-13 00:42:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-13 00:42:04       20 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-13 00:42:04       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-13 00:42:04       20 阅读

热门阅读

  1. 深入探讨C#中的递归算法

    2024-03-13 00:42:04       19 阅读
  2. 竹云3.6(日常实习)面经(20min)

    2024-03-13 00:42:04       24 阅读
  3. 计算器系统基础知识-校验码

    2024-03-13 00:42:04       22 阅读
  4. 互联网常见专业词汇汇总

    2024-03-13 00:42:04       23 阅读
  5. C++面试题和笔试题(三)

    2024-03-13 00:42:04       22 阅读
  6. SQL常用函数

    2024-03-13 00:42:04       19 阅读
  7. 【Node.js从基础到高级运用】七、基本的网络编程

    2024-03-13 00:42:04       27 阅读
  8. 全栈开发的必备利器 Next.js

    2024-03-13 00:42:04       24 阅读
  9. Linux应用程序对异步通知的处理

    2024-03-13 00:42:04       23 阅读
  10. 框架和函数库的区别

    2024-03-13 00:42:04       22 阅读
  11. android pdf框架-5,生成pdf

    2024-03-13 00:42:04       22 阅读
  12. 深入理解Nginx日志级别

    2024-03-13 00:42:04       22 阅读
  13. 库表设计基本字段

    2024-03-13 00:42:04       20 阅读