历史上的今天
今天是:2025年07月30日(星期三)
2021年07月30日 | mbedtls | 09 - 数字签名算法的配置与使用
2021-07-30 来源:eefocus
一、数字签名算法
1. 什么是数字签名
数字签名类似于盖章和签字,数字签名可以检查消息是否被篡改、并验证消息的可靠性,因为私钥只有签名者持有,所以还可以防止否认。
2. 数字签名算法
① RSA数字签名
RSA数字签名算法基于RSA非对称加密算法,在用于数字签名时公钥和私钥的用法刚好相反:
发送方使用私钥对消息执行加密操作 = 对消息进行签名;
接收方使用公钥对签名进行解密 = 对签名进行认证;
RSA签名算法还需要包括填充方法,有两种:PKCS1-V1.5和PSS(使用随机数填充,推荐使用)。
② DSA算法
③ ECDSA算法
在相同的安全等级下,ECDSA算法具有秘钥短、执行效率高的特点,更适合物联网应用。
二、RSA数字签名功能模块的配置与使用
1. 配置宏
使用RSA功能需要提前开启伪随机数生成器(依赖AES算法、SHA256算法、MD通用接口),其中伪随机数生成器的硬件适配移植实现已经讲述,请参考对应的第二篇博客,不再赘述。
综合上述,本实验中需要开启的宏如下表:
| 宏定义 | 功能 |
|---|---|
| MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES | 不使用默认熵源(如果已有、要屏蔽该宏) |
| MBEDTLS_NO_PLATFORM_ENTROPY | 不使用系统内置熵源 |
| MBEDTLS_AES_C | 使用AES算法 |
| MBEDTLS_ENTROPY_C | 使能熵源模块 |
| MBEDTLS_CTR_DRBG_C | 使能随机数模块 |
| MBEDTLS_SHA256_C | 使能SHA256算法 |
| MBEDTLS_MD_C | 开启MD通用接口 |
| MBEDTLS_AES_ROM_TABLES | 使能预定义S盒(节约内存空间) |
| MBEDTLS_BIGNUM_C | 开启大数运算 |
| MBEDTLS_GENPRIME | 开启生成素数 |
| MBEDTLS_OID_C | 开启OID数据结构模块 |
| MBEDTLS_RSA_C | 开启RSA算法 |
| MBEDTLS_PKCS1_V21 | 开启PKCS#1 v2.1填充方案(OAEP) |
新建配置文件mbedtls_config_rsa_sign.h,编写以下内容:
/**
* @brief Minimal configuration for RSA Sign Function
* @author mculover666
* @date 2020/10/03
*/
#ifndef _MBEDTLS_CONFIG_RSA_SIGN_H_
#define _MBEDTLS_CONFIG_RSA_SIGN_H_
/* System support */
#define MBEDTLS_HAVE_ASM
//#define MBEDTLS_HAVE_TIME
/* mbed feature support */
#define MBEDTLS_ENTROPY_HARDWARE_ALT
//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
#define MBEDTLS_NO_PLATFORM_ENTROPY
/* mbed modules */
#define MBEDTLS_AES_C
#define MBEDTLS_AES_ROM_TABLES
#define MBEDTLS_CTR_DRBG_C
#define MBEDTLS_ENTROPY_C
#define MBEDTLS_SHA256_C
#define MBEDTLS_MD_C
#define MBEDTLS_BIGNUM_C
#define MBEDTLS_GENPRIME
#define MBEDTLS_OID_C
#define MBEDTLS_RSA_C
#define MBEDTLS_PKCS1_V21
#include "mbedtls/check_config.h"
#endif /* _MBEDTLS_CONFIG_RSA_SIGN_H_ */
在MDK中设置使用该配置文件:
2. RSA数字签名功能模块API说明
使用该功能模块需要包含头文件:
#include "mbedtls/rsa.h"
① RSA签名接口
/**
* brief This function performs a private RSA operation to sign
* a message digest using PKCS#1.
*
* It is the generic wrapper for performing a PKCS#1
* signature using the p mode from the context.
*
* note The p sig buffer must be as large as the size
* of p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* note For PKCS#1 v2.1 encoding, see comments on
* mbedtls_rsa_rsassa_pss_sign() for details on
* p md_alg and p hash_id.
*
* deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* are likely to remove the p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PRIVATE.
*
* note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
*
* param ctx The initialized RSA context to use.
* param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
* this must be provided. If the padding mode is PKCS#1 v1.5 and
* p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
* and should be provided; see mbedtls_rsa_private() for more
* more. It is ignored otherwise.
* param p_rng The RNG context to be passed to p f_rng. This may be c NULL
* if p f_rng is c NULL or doesn't need a context argument.
* param mode The mode of operation. This must be either
* #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
* param md_alg The message-digest algorithm used to hash the original data.
* Use #MBEDTLS_MD_NONE for signing raw data.
* param hashlen The length of the message digest.
* Ths is only used if p md_alg is #MBEDTLS_MD_NONE.
* param hash The buffer holding the message digest or raw data.
* If p md_alg is #MBEDTLS_MD_NONE, this must be a readable
* buffer of length p hashlen Bytes. If p md_alg is not
* #MBEDTLS_MD_NONE, it must be a readable buffer of length
* the size of the hash corresponding to p md_alg.
* param sig The buffer to hold the signature. This must be a writable
* buffer of length c ctx->len Bytes. For example, c 256 Bytes
* for an 2048-bit RSA modulus. A buffer length of
* #MBEDTLS_MPI_MAX_SIZE is always safe.
*
* return c 0 if the signing operation was successful.
* return An c MBEDTLS_ERR_RSA_XXX error code on failure.
*/
int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig );
② RSA验证签名接口
/**
* brief This function performs a public RSA operation and checks
* the message digest.
*
* This is the generic wrapper for performing a PKCS#1
* verification using the mode from the context.
*
* note For PKCS#1 v2.1 encoding, see comments on
* mbedtls_rsa_rsassa_pss_verify() about p md_alg and
* p hash_id.
*
* deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* are likely to remove the p mode argument and have it
* set to #MBEDTLS_RSA_PUBLIC.
*
* note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
*
* param ctx The initialized RSA public key context to use.
* param f_rng The RNG function to use. If p mode is #MBEDTLS_RSA_PRIVATE,
* this is used for blinding and should be provided; see
* mbedtls_rsa_private() for more. Otherwise, it is ignored.
* param p_rng The RNG context to be passed to p f_rng. This may be
* c NULL if p f_rng is c NULL or doesn't need a context.
* param mode The mode of operation. This must be either
* #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
* param md_alg The message-digest algorithm used to hash the original data.
* Use #MBEDTLS_MD_NONE for signing raw data.
* param hashlen The length of the message digest.
* This is only used if p md_alg is #MBEDTLS_MD_NONE.
* param hash The buffer holding the message digest or raw data.
* If p md_alg is #MBEDTLS_MD_NONE, this must be a readable
* buffer of length p hashlen Bytes. If p md_alg is not
史海拾趣
|
手机FM天线,到底是内置的好还是拉杆好? 其实各有他的有点,内置的体积小,拉杆的信号好。 就是不完美啊了。为了这个完美的,本公司花了数年研究这个问题,终于解决了,一种新的材料制作的内置FM天线,效果好且体积小,真是神一般的,呵呵 ,有 ...… 查看全部问答> |
|
最近在做监控软件的设计。新手啊,通过视频服务器提供的SDK,功能是实现了。 但想到电脑摄像头放QQ上直接就能取图像了,电脑摄像头里装有转换卡了?把模拟信号转化成数字信号。怎么接收摄像头传过来的图像的?不解,工作原理不解?提取图像的函数 ...… 查看全部问答> |
|
9650输出640*480,在横屏下可以不失真显示。可是我在竖屏下如果想要全屏显示图像会失真(就是图像变长了),因为竖屏分辨率为240*320。不知有没有高手知道怎么样才能不失真显示图像。… 查看全部问答> |
|
手机键盘中,挂机键和电源键是同一个键,有一个问题就是:GPIO设为高电平时候可以作为挂机键不能作为电源键,GPIO设为低电平时候可以作为电源键却不能作为挂机键,现在需要解决这个问题。 想到可以做个后台程序检测手机的状态,当处于通话状态时候 ...… 查看全部问答> |
|
学校有一块STAREAST开发板。完全按照光盘所付说明编译出内核后(其中有若干BUG,查网上已排除),通过REDBOOT(网上下载的 redboot prebuilt image for ixp425,因为光盘所付PATCH有错)烧录进FLASH后启动无反应: load -r -v -b 0x01600000 zImag ...… 查看全部问答> |
|
MeeGo移植到SEED-DIM3517请注意,尚未测试通过0. 开发平台操 作 系 统:Ubuntu10.10交叉编译工具:arm-2009q1(光盘里带的)MeeGo 版本:1.1.90.1.20110201.11. 开发环境搭建1.1 安装MeeGo开发工具MeeGo开发工具的安装步骤还是很多,这里就不写了。 ...… 查看全部问答> |
|
各位大侠,我现在用28335的I2C模块与EEPROM AT24C08进行通信,程序没有错误,但是用示波器不能测出SDA和SCL信号。例程里的从机地址是0x50,因为我这里用的是AT24C08,所以从机地址应该是0xA9,是不是只需要修改这个地址就可以了?希望各 ...… 查看全部问答> |
|
最近刚开始32位单片机的学习,呵呵,有点小不适应,固件库的确不错,但是需要适应时间,一个本来很简单的usart,却让我debug了两天。问题很简单,X-CUT接受不到USART发送的字符,我用的是神州IV,F107。目前我怀疑是时钟频率的问题,USART2 设置好后 ...… 查看全部问答> |




