openssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c

文章目录

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

openssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c

概述

用RSA私钥签名
d2i_PrivateKey_ex()可以从内存载入私钥数据, 得到私钥EVP_PKEY*
从私钥产生ctx, 对ctx进行签名初始化, 设置ctx的padding填充模式
摘要算法选用SHA256, 对ctx设置摘要算法
尝试签名, 得到签名长度, 然后进行私钥签名, 得到私钥签名buffer.

用RSA公钥验签
d2i_PublicKey()可以从内存载入公钥数据, 得到公钥EVP_PKEY*
验签时使用的摘要算法要和签名时一样.
验签初始化, 对ctx进行验签初始化, 这是ctx的padding填充模式(要和签名时一样)
进行验签

笔记

/*!
\file rsa_pss_direct.c
\noteopenssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c用RSA私钥签名
d2i_PrivateKey_ex()可以从内存载入私钥数据, 得到私钥EVP_PKEY*
从私钥产生ctx, 对ctx进行签名初始化, 设置ctx的padding填充模式
摘要算法选用SHA256, 对ctx设置摘要算法
尝试签名, 得到签名长度, 然后进行私钥签名, 得到私钥签名buffer.用RSA公钥验签
d2i_PublicKey()可以从内存载入公钥数据, 得到公钥EVP_PKEY*
验签时使用的摘要算法要和签名时一样.
验签初始化, 对ctx进行验签初始化, 这是ctx的padding填充模式(要和签名时一样)
进行验签*//** 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 <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include "rsa_pss.h"#include "my_openSSL_lib.h"/** The digest to be signed. This should be the output of a hash function.* Here we sign an all-zeroes digest for demonstration purposes.*/
static const unsigned char test_digest[32] = { 0 };/* A property query used for selecting algorithm implementations. */
static const char* propq = NULL;/** This function demonstrates RSA signing of a SHA-256 digest using the PSS* padding scheme. You must already have hashed the data you want to sign.* For a higher-level demonstration which does the hashing for you, see* rsa_pss_hash.c.** For more information, see RFC 8017 section 9.1. The digest passed in* (test_digest above) corresponds to the 'mHash' value.*/
static int sign(OSSL_LIB_CTX* libctx, unsigned char** sig, size_t* sig_len)
{int ret = 0;EVP_PKEY* pkey = NULL;EVP_PKEY_CTX* ctx = NULL;EVP_MD* md = NULL;const unsigned char* ppriv_key = NULL;*sig = NULL;/* Load DER-encoded RSA private key. */ppriv_key = rsa_priv_key;pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,sizeof(rsa_priv_key), libctx, propq);if (pkey == NULL) {fprintf(stderr, "Failed to load private key\n");goto end;}/* Fetch hash algorithm we want to use. */md = EVP_MD_fetch(libctx, "SHA256", propq);if (md == NULL) {fprintf(stderr, "Failed to fetch hash algorithm\n");goto end;}/* Create signing context. */ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);if (ctx == NULL) {fprintf(stderr, "Failed to create signing context\n");goto end;}/* Initialize context for signing and set options. */if (EVP_PKEY_sign_init(ctx) == 0) {fprintf(stderr, "Failed to initialize signing context\n");goto end;}if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {fprintf(stderr, "Failed to configure padding\n");goto end;}if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {fprintf(stderr, "Failed to configure digest type\n");goto end;}/* Determine length of signature. */if (EVP_PKEY_sign(ctx, NULL, sig_len,test_digest, sizeof(test_digest)) == 0) {fprintf(stderr, "Failed to get signature length\n");goto end;}/* Allocate memory for signature. */*sig = OPENSSL_malloc(*sig_len);if (*sig == NULL) {fprintf(stderr, "Failed to allocate memory for signature\n");goto end;}/* Generate signature. */if (EVP_PKEY_sign(ctx, *sig, sig_len,test_digest, sizeof(test_digest)) != 1) {fprintf(stderr, "Failed to sign\n");goto end;}ret = 1;
end:EVP_PKEY_CTX_free(ctx);EVP_PKEY_free(pkey);EVP_MD_free(md);if (ret == 0)OPENSSL_free(*sig);return ret;
}/** This function demonstrates verification of an RSA signature over a SHA-256* digest using the PSS signature scheme.*/
static int verify(OSSL_LIB_CTX* libctx, const unsigned char* sig, size_t sig_len)
{int ret = 0;const unsigned char* ppub_key = NULL;EVP_PKEY* pkey = NULL;EVP_PKEY_CTX* ctx = NULL;EVP_MD* md = NULL;/* Load DER-encoded RSA public key. */ppub_key = rsa_pub_key;pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));if (pkey == NULL) {fprintf(stderr, "Failed to load public key\n");goto end;}/* Fetch hash algorithm we want to use. */md = EVP_MD_fetch(libctx, "SHA256", propq);if (md == NULL) {fprintf(stderr, "Failed to fetch hash algorithm\n");goto end;}/* Create verification context. */ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);if (ctx == NULL) {fprintf(stderr, "Failed to create verification context\n");goto end;}/* Initialize context for verification and set options. */if (EVP_PKEY_verify_init(ctx) == 0) {fprintf(stderr, "Failed to initialize verification context\n");goto end;}if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {fprintf(stderr, "Failed to configure padding\n");goto end;}if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {fprintf(stderr, "Failed to configure digest type\n");goto end;}/* Verify signature. */if (EVP_PKEY_verify(ctx, sig, sig_len,test_digest, sizeof(test_digest)) == 0) {fprintf(stderr, "Failed to verify signature; ""signature may be invalid\n");goto end;}ret = 1;
end:EVP_PKEY_CTX_free(ctx);EVP_PKEY_free(pkey);EVP_MD_free(md);return ret;
}int main(int argc, char** argv)
{int ret = EXIT_FAILURE;OSSL_LIB_CTX* libctx = NULL;unsigned char* sig = NULL;size_t sig_len = 0;if (sign(libctx, &sig, &sig_len) == 0)goto end;if (verify(libctx, sig, sig_len) == 0)goto end;ret = EXIT_SUCCESS;
end:OPENSSL_free(sig);OSSL_LIB_CTX_free(libctx);return ret;
}

END

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

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

相关文章

mavavi显示 3d姿态

目录 mayavi安装&#xff1a; mavavi显示 3d姿态 mayavi安装&#xff1a; 第1步 从这里下载两个whl文件&#xff0c; https://www.lfd.uci.edu/~gohlke/pythonlibs/ * mayavi&#xff1a;*xxx.whl * vtk: VTK‑9.1.0‑cp310‑cp310‑win_amd64.whl 第2步 pip install py…

linux-挂载Samba共享

linux-挂载Samba共享 1、linux服务器启动Samba共享服务 2、客户端电脑安装cifs-utils dnf install cifs-utils # 或 yum install cifs-utils3、挂载共享目录 # 创建挂目录 mkdir /share # 使用mount命令挂在共享目录&#xff0c;-t协议类型 -o用户名密码 共享目录访问地址 挂…

无监督学习 - 均值聚类(K-Means Clustering)

什么是机器学习 K-Means聚类是一种无监督学习算法&#xff0c;用于将数据集分成K个不同的组&#xff08;簇&#xff09;&#xff0c;每个组内的数据点与组内其他点的相似度较高&#xff0c;而与其他组内的点相似度较低。这是通过迭代地调整簇中心和将数据点分配到最近的簇来实…

热压机PLC数据采集远程监控物联网解决方案

热压机PLC数据采集远程监控物联网解决方案 随着工业4.0时代的到来&#xff0c;智能制造已经成为制造业发展的重要方向。在热压机领域&#xff0c;PLC数据采集远程监控物联网解决方案为提高生产效率、降低维护成本、优化生产工艺提供了有效的手段。 一、热压机PLC数据采集远程…

canvas绘制美队盾牌

查看专栏目录 canvas示例教程100专栏&#xff0c;提供canvas的基础知识&#xff0c;高级动画&#xff0c;相关应用扩展等信息。canvas作为html的一部分&#xff0c;是图像图标地图可视化的一个重要的基础&#xff0c;学好了canvas&#xff0c;在其他的一些应用上将会起到非常重…

【Rust学习】安装Rust环境

本笔记为了记录学习Rust过程&#xff0c;内容如有错误请大佬指教 使用IDE&#xff1a;vs code 参考教程&#xff1a;菜鸟教程链接: 菜鸟教程链接: Rust学习 Rust入门安装Rust编译环境Rust 编译工具 构建Rust 工程目录 Rust入门 安装Rust编译环境 因为我已经安装过VSCode了&am…

解决若依Vue3前后端分离---路由切换时显示白屏

解决若依Vue3前后端分离---路由切换时显示白屏 1.问题重述 解决基于Vue3若依前后端分离项目中出现的路由正常切换但是就是不显示数据的问题&#xff0c;也就是不发起网络请求的问题。 找到如下位置中AppMain.vue文件 将除了css中的代码进行替换成如下的代码。 <template&g…

kylin集群负载均衡(kylin3,hbaseRIF问题)

hbase历险记 目录 hbase历险记 寻找问题 分析原因 解决方案 方案1&#xff08;资源问题、失败&#xff09; 方案2&#xff08;成功&#xff09; 寻找问题 不知道你是不是有这样的疑惑。我kylin是个单机&#xff0c;我使用的hbase是个集群&#xff0c;但内存全在某一台机…

vue2使用qiankun微前端(跟着步骤走可实现)

需求&#xff1a;做一个vue2的微前端&#xff0c;以vue2为主应用&#xff0c;其他技术栈为子应用&#xff0c;比如vue3&#xff0c;本文章只是做vue2一套的微前端应用实现&#xff0c;之后解决的一些问题。vue3子应用可以看我另一篇vue3vitets实现qiankun微前端子应用-CSDN博客…

Spring Boot多环境配置

Spring Boot的针对不同的环境创建不同的配置文件&#xff0c; 语法结构&#xff1a;application-{profile}.properties profile:代表的就是一套环境 需求 application-dev.yml 开发环境 端口8090 application-test.yml 测试环境 端口8091 application-prod.yml 生产环境 端口80…

前端八股文(性能优化篇)

目录 1.CDN的概念 2.CDN的作用 3.CDN的原理 4.CDN的使用场景 5.懒加载的概念 6.懒加载的特点 7.懒加载的实现原理 8.懒加载与预加载的区别 9.回流与重绘的概念及触发条件 &#xff08;1&#xff09;回流 &#xff08;2&#xff09;重绘 10. 如何避免回流与重绘&#…

如何在ubuntu18.04安装python3.8.6

目录 一.前言 二.教程 2.1环境配置 2.2下载安装包 2.3编译安装 2.4验证安装

Ubuntu服务器上使用tmux

&#xff08;1&#xff09;服务器上安装 $ sudo apt-get install tmux &#xff08;2&#xff09;新建会话 &#xff08;之后可以正常运行程序&#xff09; $ tmux new -s session_name &#xff08;3&#xff09;查看当前所有的tmux会话 $ tmux ls &#xff08;4&#xff09;退…

Deep MultimodalLearningA survey on recent advances and trends

深度多模态学习&#xff1a;对近期进展和趋势的综述 深度学习的成功已经成为解决越来越复杂的机器学习问题的催化剂&#xff0c;这些问题通常涉及多个数据模态。我们回顾了深度多模态学习的最新进展&#xff0c;并突出了该活跃研究领域的现状&#xff0c;以及存在的差距和挑战…

【动态规划】【C++算法】639 解码方法 II

作者推荐 【矩阵快速幂】封装类及测试用例及样例 涉及知识点 动态规划 字符串 滚动向量 LeetCode 639. 解码方法 II 一条包含字母 A-Z 的消息通过以下的方式进行了 编码 &#xff1a; ‘A’ -> “1” ‘B’ -> “2” … ‘Z’ -> “26” 要 解码 一条已编码的消息…

【已解决】c语言const/指针学习笔记

本博文源于笔者正在复习const在左与在右&#xff0c;指针优先级、a,&a,*a的区别。 1、const在左与在右 int const *p const int *p int * const p int const * const p const int * const p* 在const右边&#xff0c;指向的数据不可以改变&#xff0c;可以改变地址 * 在c…

不同打包工具下的环境变量配置方式对比

本文作者为 360 奇舞团前端开发工程师 天明 前言 在现代的JavaScript应用程序开发中&#xff0c;环境变量的配置是至关重要的。不同的应用场景和部署环境可能需要不同的配置&#xff0c;例如开发、测试和生产环境。最常见的需求是根据不同的环境&#xff0c;配置如是否开启sour…

基于stm32的智慧家庭健康医疗系统设计

标题&#xff1a;基于STM32的智慧家庭健康医疗系统设计 摘要&#xff1a; 随着人们生活水平的提高和健康意识的增强&#xff0c;智慧家庭健康医疗系统成为了当前研究的热点之一。本论文旨在设计并实现一种基于STM32的智慧家庭健康医疗系统&#xff0c;该系统能够监测和管理家庭…

企业微信上传临时素材errcode:44001,errmsg:empty media data

企业微信&#xff0c;上传临时素材&#xff0c;报错&#xff1a; {“errcode”:44001,“errmsg”:“empty media data [logid:]”}&#xff0c; 开发语言C# 重点代码&#xff1a; formData.Headers.ContentType new MediaTypeHeaderValue(“application/octet-stream”); 解…

数据分析中常用的指标或方法

一、方差与标准差二、协方差三、皮尔逊系数四、斯皮尔曼系数 一、方差与标准差 总体方差 V a r ( x ) σ 2 ∑ i 1 n ( x i − x ˉ ) 2 n ∑ i 1 n x i 2 − n x ˉ 2 n E ( x 2 ) − [ E ( x ) ] 2 Var(x)\sigma^2\frac {\sum\limits_{i1}^{n} (x_i - \bar{x})^2} {n…