openssl3.2 - 官方demo学习 - keyexch - x25519.c

文章目录

    • openssl3.2 - 官方demo学习 - keyexch - x25519.c
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - keyexch - x25519.c

概述

官方程序中演示了私钥2种key交换的情况:

  1. 产生X25519的key对(私钥/公钥), 并交换公钥给对方, 并分别产生会话密钥, 使双方都能持有相同的会话密钥

  2. 产生X25519的key对(私钥/公钥)时, 产生私钥时, 可以随机产生. 这个私钥是为会话准备的, 然后根据会话私钥产生会话公钥.
    然后交换公钥给对方, 并分别产生会话密钥, 使双方都能持有相同的会话密钥

可以看到case2更安全, 密钥不在程序中定义.

笔记

/*!
\file x25519.c
\note openssl3.2 - 官方demo学习 - keyexch - x25519.c官方程序中演示了私钥2种key交换的情况:1. 产生X25519的key对(私钥/公钥), 并交换公钥给对方, 并分别产生会话密钥, 使双方都能持有相同的会话密钥2. 产生X25519的key对(私钥/公钥)时, 产生私钥时, 可以随机产生. 这个私钥是为会话准备的, 然后根据会话私钥产生会话公钥.
然后交换公钥给对方, 并分别产生会话密钥, 使双方都能持有相同的会话密钥可以看到case2更安全, 密钥不在程序中定义.
*//** 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*/#include <stdio.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>#include "my_openSSL_lib.h"/** This is a demonstration of key exchange using X25519.** The variables beginning `peer1_` / `peer2_` are data which would normally be* accessible to that peer.** Ordinarily you would use random keys, which are demonstrated* below when use_kat=0. A known answer test is demonstrated* when use_kat=1.*//* A property query used for selecting the X25519 implementation. */
static const char* propq = NULL;static const unsigned char peer1_privk_data[32] = {0x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,0xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,0x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,0x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d
};static const unsigned char peer2_privk_data[32] = {0xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,0xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,0x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,0x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x43
};static const unsigned char expected_result[32] = {0x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,0x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,0x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,0x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x09
};typedef struct peer_data_st {const char* name;               /* name of peer */EVP_PKEY* privk;                /* privk generated for peer */unsigned char pubk_data[32];    /* generated pubk to send to other peer */unsigned char* secret;          /* allocated shared secret buffer */size_t secret_len;
} PEER_DATA;/** Prepare for X25519 key exchange. The public key to be sent to the remote peer* is put in pubk_data, which should be a 32-byte buffer. Returns 1 on success.*/
static int keyexch_x25519_before(OSSL_LIB_CTX* libctx,const unsigned char* kat_privk_data,PEER_DATA* local_peer)
{int ret = 0;size_t pubk_data_len = 0;/* Generate or load X25519 key for the peer */if (kat_privk_data != NULL)local_peer->privk =EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,kat_privk_data,sizeof(peer1_privk_data));elselocal_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");if (local_peer->privk == NULL) {fprintf(stderr, "Could not load or generate private key\n");goto end;}/* Get public key corresponding to the private key */if (EVP_PKEY_get_octet_string_param(local_peer->privk,OSSL_PKEY_PARAM_PUB_KEY,local_peer->pubk_data,sizeof(local_peer->pubk_data),&pubk_data_len) == 0) {fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");goto end;}/* X25519 public keys are always 32 bytes */if (pubk_data_len != 32) {fprintf(stderr, "EVP_PKEY_get_octet_string_param() ""yielded wrong length\n");goto end;}ret = 1;
end:if (ret == 0) {EVP_PKEY_free(local_peer->privk);local_peer->privk = NULL;}return ret;
}/** Complete X25519 key exchange. remote_peer_pubk_data should be the 32 byte* public key value received from the remote peer. On success, returns 1 and the* secret is pointed to by *secret. The caller must free it.*/
static int keyexch_x25519_after(OSSL_LIB_CTX* libctx,int use_kat,PEER_DATA* local_peer,const unsigned char* remote_peer_pubk_data)
{int ret = 0;EVP_PKEY* remote_peer_pubk = NULL;EVP_PKEY_CTX* ctx = NULL;local_peer->secret = NULL;/* Load public key for remote peer. */remote_peer_pubk =EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,remote_peer_pubk_data, 32);if (remote_peer_pubk == NULL) {fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");goto end;}/* Create key exchange context. */ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);if (ctx == NULL) {fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");goto end;}/* Initialize derivation process. */if (EVP_PKEY_derive_init(ctx) == 0) {fprintf(stderr, "EVP_PKEY_derive_init() failed\n");goto end;}/* Configure each peer with the other peer's public key. */if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");goto end;}/* Determine the secret length. */if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {fprintf(stderr, "EVP_PKEY_derive() failed\n");goto end;}/** We are using X25519, so the secret generated will always be 32 bytes.* However for exposition, the code below demonstrates a generic* implementation for arbitrary lengths.*/if (local_peer->secret_len != 32) { /* unreachable */fprintf(stderr, "Secret is always 32 bytes for X25519\n");goto end;}/* Allocate memory for shared secrets. */local_peer->secret = OPENSSL_malloc(local_peer->secret_len);if (local_peer->secret == NULL) {fprintf(stderr, "Could not allocate memory for secret\n");goto end;}/* Derive the shared secret. */if (EVP_PKEY_derive(ctx, local_peer->secret,&local_peer->secret_len) == 0) {fprintf(stderr, "EVP_PKEY_derive() failed\n");goto end;}printf("Shared secret (%s):\n", local_peer->name);BIO_dump_indent_fp(stdout, local_peer->secret, (int)local_peer->secret_len, 2);putchar('\n');ret = 1;
end:EVP_PKEY_CTX_free(ctx);EVP_PKEY_free(remote_peer_pubk);if (ret == 0) {OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);local_peer->secret = NULL;}return ret;
}static int keyexch_x25519(int use_kat)
{int ret = 0;OSSL_LIB_CTX* libctx = NULL;PEER_DATA peer1 = { "peer 1" }, peer2 = { "peer 2" };/** Each peer generates its private key and sends its public key* to the other peer. The private key is stored locally for* later use.*/if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,&peer1) == 0)return 0;if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,&peer2) == 0)return 0;/** Each peer uses the other peer's public key to perform key exchange.* After this succeeds, each peer has the same secret in its* PEER_DATA.*/if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)return 0;if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)return 0;/** Here we demonstrate the secrets are equal for exposition purposes.** Although in practice you will generally not need to compare secrets* produced through key exchange, if you do compare cryptographic secrets,* always do so using a constant-time function such as CRYPTO_memcmp, never* using memcmp(3).*/if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {fprintf(stderr, "Negotiated secrets do not match\n");goto end;}/* If we are doing the KAT, the secret should equal our reference result. */if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,peer1.secret_len) != 0) {fprintf(stderr, "Did not get expected result\n");goto end;}ret = 1;
end:/* The secrets are sensitive, so ensure they are erased before freeing. */OPENSSL_clear_free(peer1.secret, peer1.secret_len);OPENSSL_clear_free(peer2.secret, peer2.secret_len);EVP_PKEY_free(peer1.privk);EVP_PKEY_free(peer2.privk);OSSL_LIB_CTX_free(libctx);return ret;
}int main(int argc, char** argv)
{/* Test X25519 key exchange with known result. */printf("Key exchange using known answer (deterministic):\n");if (keyexch_x25519(1) == 0)return EXIT_FAILURE;/* Test X25519 key exchange with random keys. */printf("Key exchange using random keys:\n");if (keyexch_x25519(0) == 0)return EXIT_FAILURE;return EXIT_SUCCESS;
}

END

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/624444.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP AES加解密示例

以下是一个使用PHP的openssl扩展进行AES加密和解密的示例代码&#xff1a; php复制代码 <?php // 密钥和初始向量 $key your_secret_key; $iv your_initial_vector; // 加密函数 function encryptAES($data, $key, $iv) { $encrypted openssl_encrypt($data, AES-256-C…

解读Vue的原型及原型链

在 JavaScript 中&#xff0c;每个对象都有一个关联的原型&#xff08;prototype&#xff09;。原型是一个对象&#xff0c;其他对象可以通过原型实现属性和方法的继承。原型链是一种由对象组成的链式结构&#xff0c;它通过原型的引用连接了一系列对象&#xff0c;形成了一种继…

克服大模型(LLM)部署障碍,全面理解LLM当前状态

近日&#xff0c;CMU Catalyst 团队推出了一篇关于高效 LLM 推理的综述&#xff0c;覆盖了 300 余篇相关论文&#xff0c;从 MLSys 的研究视角介绍了算法创新和系统优化两个方面的相关进展。 在人工智能&#xff08;AI&#xff09;的快速发展背景下&#xff0c;大语言模型&…

从文本文件或 csv 文件读取信息的示例

如下表格说明文本文件或 csv 文件中的信息如何在 WinCC (TIA Portal) 中显示。 IO 域用作于显示&#xff0c;只有最有一个条目被输出。 注意 在此例中由于最后一条条目被搜索&#xff0c;脚本的运行系统会随着文件的尺寸增长而增长。先前示例中的配置在该示例中不是必须的。但是…

Linux 系统上,你可以使用 cron 定时任务来定期备份 MySQL 数据库

在 Linux 系统上&#xff0c;你可以使用 cron 定时任务来定期备份 MySQL 数据库。以下是一个基本的步骤&#xff0c;假设你已经安装了 MySQL 数据库和使用了 mysqldump 工具来进行备份。 步骤&#xff1a; 创建备份脚本&#xff1a; 创建一个包含备份命令的脚本。在这个例子中…

leetcode 1两数之和

题目 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复出现。 你可以按任意顺…

手把手教你使用Django如何连接Mysql

目录 一、引言 二、准备工作 三、配置Django连接MySQL 1、安装MySQL驱动&#xff1a; 2、配置数据库设置&#xff1a; 3、 创建数据库迁移&#xff1a; 四、编写Django模型和视图函数 1、编写模型&#xff1a; 2. 编写视图函数&#xff1a; 3. 编写模板&#xff1a; …

前端解析文件流

第一种方法&#xff0c;转blob对象 this.$axios({url: /md2api/attachment/c/${val.code},method: "GET",responseType: blob, //设置响应格式headers: {"Content-Type": "application/x-www-form-urlencoded",}, }) .then(function (respons…

软件测试感悟2

沟通 a.在测试前期与开发沟通&#xff0c;确认测试重点 &#xff0c;确认测试的优先级 b.了解开发人员技术和业务背景 业务水平 技术水平 代码质量 人员流动性 在测试结束后 对已发现的bug进行统计 ,知道高发概率bug ,在新项目中要进行重点测试 针对代码 代码复杂…

开关电源如何覆铜

开关电源如何覆铜 开关电源覆铜是一个很重要的技术方法&#xff0c;如果没有很好的覆铜&#xff0c;就有可能会造成开关电源芯片的损坏。先介绍常见的开关电源电路&#xff1a; 图 1开关电源电路 从左到右分别是非同步整流Buck电路和同步整流Buck电路&#xff0c;第二排从左到…

MIinW-W64交叉编译找不到‘mutex‘问题解决

问题 在linux下安装mingw-w64来交叉编译Windows的程序和库. 就像我之前的一篇博客提到的来进行mingw的交叉编译 这样默认安装的线程模型是win32模型.这个线程模型不支持mutex. 一般查找问题的过程: 线程模型通常包含互斥锁&#xff08;mutex&#xff09;作为线程同步的基本工…

Arduino快速上手esp8266方案开发

认识ESP8266 ESP8266 是 Espressif Systems 生产的 Wi-Fi 片上系统 (SoC)。它非常适合物联网和家庭自动化项目&#xff0c;目前有非常高的市场普及率&#xff0c;还有更加高端的同时支持wifi和蓝牙的双核心芯片ESP32&#xff0c;可以在乐鑫官网查看完整的芯片列表。 ESP8266芯…

Keepalived双机热备

学会构建双机热备系统学会构建LVSHA高可用群集 1.1 Keepalived概述及安装 Keepalived的官方网站位于http://www.keepalived.org/&#xff0c;本章将以yum方式讲解Keepalived的安装、配置和使用过程。在非LVS群集环境中使用时&#xff0c;Keepalived也可以作为热备软件使用 1.…

解决PS“暂存盘已满”错误

问题&#xff1a;PS“暂存盘已满”错误 原因&#xff1a; PS在运行时会将文件的相关数据参数保存到暂存区。当提醒暂存盘满时&#xff0c;说明你当前PS运行的使用盘符空间不足&#xff0c;所以在运行时一定要保留有足够的盘符空间来运行PS。 效果图 解决方案 注意: 我们在使用P…

光纤和光缆有何不同之处?

很多人会有这样的疑问&#xff0c;光纤和光缆有何不同之处&#xff1f;主要是因为光纤和光缆这两个名词容易引起混淆。在严格的定义下&#xff0c;光纤和光缆是两种不同的东西&#xff0c;然而在现实生活中&#xff0c;许多人仍然会混淆这两者。为了更好地理解光纤和光缆之间的…

C#基础:利用LINQ进行复杂排序

一、场景 请你写出linq对表格排序&#xff0c;CODE3排前面&#xff0c;其余按照CODE降序排序&#xff0c;CODE一样再按照字母升序排序 IDCODEVALUEA00011AA00021BA00031CA00042DA00052EA00062FA00073GA00083HA00093IA00104J 二、代码 using System; using System.Collectio…

进程 p.close和p.join的区别

p是指的进程 p.close()和p.join()是multiprocessing.Process类的两个方法&#xff0c;用于管理子进程的行为。 p.close(): 这个方法用于关闭子进程。当调用p.close()后&#xff0c;子进程将不再接受新的任务。在子进程执行完当前任务后&#xff0c;它将自动退出。这个方法通常…

Dubbo 模块探秘:深入了解每个组件的独特功能【二】

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Dubbo 模块探秘&#xff1a;深入了解每个组件的独特功能 前言Dubbo-common公共逻辑模块Dubbo-remoting 远程通讯模块Dubbo-rpc 远程调用模块Dubbo-cluster 集群模块Dubbo-registry 注册中心模块Dubbo-…

【Leetcode】239. 滑动窗口最大值

【Leetcode】239. 滑动窗口最大值 题目链接代码 题目链接 【Leetcode】239. 滑动窗口最大值 代码 func maxSlidingWindow(nums []int, k int) []int {// 单调队列使用的栈q : []int{}n : len(nums)// 结果切片ans : []int{}// 枚举切片for i : 0; i < n; i {// 如果栈顶元…

【LeetCode】202. 快乐数(简单)——代码随想录算法训练营Day06

题目链接&#xff1a;202. 快乐数 题目描述 编写一个算法来判断一个数 n 是不是快乐数。 「快乐数」 定义为&#xff1a; 对于一个正整数&#xff0c;每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为 1&#xff0c;也可能是 无限循环 但始终…