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

2024-01-17 12:12

本文主要是介绍openssl3.2 - 官方demo学习 - signature - rsa_pss_hash.c,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

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

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

概述

对私钥对明文做签名(摘要算法为SHA256)
用公钥对密文做验签(摘要算法为SHA256)

笔记

/*!
\file rsa_pss_hash.c
\note 
openssl3.2 - 官方demo学习 - signature - rsa_pss_hash.c对私钥对明文做签名(摘要算法为SHA256)
用公钥对密文做验签(摘要算法为SHA256)
*//** 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 data to be signed. This will be hashed. */
static const char test_message[] ="This is an example message to be signed.";/* A property query used for selecting algorithm implementations. */
static const char *propq = NULL;/** This function demonstrates RSA signing of an arbitrary-length message.* Hashing is performed automatically. In this example, SHA-256 is used. If you* have already hashed your message and simply want to sign the hash directly,* see rsa_pss_direct.c.*/
static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
{int ret = 0;EVP_PKEY *pkey = NULL;EVP_MD_CTX *mctx = NULL;OSSL_PARAM params[2], *p = params;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;}/* Create MD context used for signing. */mctx = EVP_MD_CTX_new();if (mctx == NULL) {fprintf(stderr, "Failed to create MD context\n");goto end;}/* Initialize MD context for signing. */*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,OSSL_PKEY_RSA_PAD_MODE_PSS, 0);*p = OSSL_PARAM_construct_end();if (EVP_DigestSignInit_ex(mctx, NULL, "SHA256", libctx, propq,pkey, params) == 0) {fprintf(stderr, "Failed to initialize signing context\n");goto end;}/** Feed data to be signed into the algorithm. This may* be called multiple times.*/if (EVP_DigestSignUpdate(mctx, test_message, sizeof(test_message)) == 0) {fprintf(stderr, "Failed to hash message into signing context\n");goto end;}/* Determine signature length. */if (EVP_DigestSignFinal(mctx, NULL, sig_len) == 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_DigestSignFinal(mctx, *sig, sig_len) == 0) {fprintf(stderr, "Failed to sign\n");goto end;}ret = 1;
end:EVP_MD_CTX_free(mctx);EVP_PKEY_free(pkey);if (ret == 0)OPENSSL_free(*sig);return ret;
}/** This function demonstrates verification of an RSA signature over an* arbitrary-length message using the PSS signature scheme. Hashing is performed* automatically.*/
static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
{int ret = 0;EVP_PKEY *pkey = NULL;EVP_MD_CTX *mctx = NULL;OSSL_PARAM params[2], *p = params;const unsigned char *ppub_key = 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;}/* Create MD context used for verification. */mctx = EVP_MD_CTX_new();if (mctx == NULL) {fprintf(stderr, "Failed to create MD context\n");goto end;}/* Initialize MD context for verification. */*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,OSSL_PKEY_RSA_PAD_MODE_PSS, 0);*p = OSSL_PARAM_construct_end();if (EVP_DigestVerifyInit_ex(mctx, NULL, "SHA256", libctx, propq,pkey, params) == 0) {fprintf(stderr, "Failed to initialize signing context\n");goto end;}/** Feed data to be signed into the algorithm. This may* be called multiple times.*/if (EVP_DigestVerifyUpdate(mctx, test_message, sizeof(test_message)) == 0) {fprintf(stderr, "Failed to hash message into signing context\n");goto end;}/* Verify signature. */if (EVP_DigestVerifyFinal(mctx, sig, sig_len) == 0) {fprintf(stderr, "Failed to verify signature; ""signature may be invalid\n");goto end;}ret = 1;
end:EVP_MD_CTX_free(mctx);EVP_PKEY_free(pkey);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

这篇关于openssl3.2 - 官方demo学习 - signature - rsa_pss_hash.c的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/616002

相关文章

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

Redis的Hash类型及相关命令小结

《Redis的Hash类型及相关命令小结》edisHash是一种数据结构,用于存储字段和值的映射关系,本文就来介绍一下Redis的Hash类型及相关命令小结,具有一定的参考价值,感兴趣的可以了解一下... 目录HSETHGETHEXISTSHDELHKEYSHVALSHGETALLHMGETHLENHSET

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

hdu1496(用hash思想统计数目)

作为一个刚学hash的孩子,感觉这道题目很不错,灵活的运用的数组的下标。 解题步骤:如果用常规方法解,那么时间复杂度为O(n^4),肯定会超时,然后参考了网上的解题方法,将等式分成两个部分,a*x1^2+b*x2^2和c*x3^2+d*x4^2, 各自作为数组的下标,如果两部分相加为0,则满足等式; 代码如下: #include<iostream>#include<algorithm

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来