当前位置: 首页 > news >正文

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

相关文章:

  • 第十二章 Java内存模型与线程(二)
  • 逸学Docker【java工程师基础】1.认识docker并且安装
  • Scrum敏捷研发管理解决方案
  • swift对接环信sdk
  • SC20-EVB ubuntu14.04 Andriod 5.1 SDK编译下载
  • day19【LeetCode力扣】160.相交链表
  • 【数据结构】排序之归并排序与计数排序
  • MySQL深入——13
  • MYSQL单表查询
  • Vue Axios——前端技术栈
  • C语言——小细节和小知识9
  • 【论文阅读】Consistency Models
  • 最新AI绘画Midjourney绘画提示词Prompt大全
  • Java http 响应式请求和非响应式请求有什么区别
  • Spring Boot - Application Events 的发布顺序_ApplicationFailedEvent
  • 《深入 React 技术栈》
  • Docker下部署自己的LNMP工作环境
  • httpie使用详解
  • Invalidate和postInvalidate的区别
  • JavaScript对象详解
  • Java多线程(4):使用线程池执行定时任务
  • java小心机(3)| 浅析finalize()
  • LintCode 31. partitionArray 数组划分
  • PHP CLI应用的调试原理
  • PHP的类修饰符与访问修饰符
  • Shell编程
  • SpringCloud集成分布式事务LCN (一)
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • use Google search engine
  • Webpack 4x 之路 ( 四 )
  • Yii源码解读-服务定位器(Service Locator)
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 让你的分享飞起来——极光推出社会化分享组件
  • 通过git安装npm私有模块
  • 物联网链路协议
  • 小程序button引导用户授权
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • ​业务双活的数据切换思路设计(下)
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #QT(串口助手-界面)
  • (1)虚拟机的安装与使用,linux系统安装
  • (第61天)多租户架构(CDB/PDB)
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十八)SpringBoot之发送QQ邮件
  • (转)shell调试方法
  • (转)负载均衡,回话保持,cookie
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • ***通过什么方式***网吧
  • .jks文件(JAVA KeyStore)
  • .NET Remoting学习笔记(三)信道
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .NET 材料检测系统崩溃分析