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

openssl3.2 - 官方demo学习 - mac - poly1305.c

文章目录

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

openssl3.2 - 官方demo学习 - mac - poly1305.c

概述

MAC算法为Poly1305,
加密算法为AES-128-ECB, 用key初始化加密算法
加密算法进行padding填充

对加密算法的key加密, 放入MAC_key后16字节, 将MAC_key的前16字节清空, 作为要用的MAC_key
拿MAC_key来初始化MAC上下文
对明文进行MAC操作.

官方建议:
Poly1305不能单独使用, 必须和其他加密算法一起对输入(MAC_key)进行处理
绝对禁止将nonce(MAC_key)直接传给Poly1305
不同会话的nonce(MAC_key)禁止重用(相同).
在实际应用绝对禁止将nonce(MAC_key)硬编码
看来nonce对于Poly1305应用的安全性影响很大(知道了MAC_key, 就可以伪造MAC值)

笔记

/*!
\file poly1305.c
\note 
openssl3.2 - 官方demo学习 - mac - poly1305.cMAC算法为Poly1305, 
加密算法为AES-128-ECB, 用key初始化加密算法
加密算法进行padding填充对加密算法的key加密, 放入MAC_key后16字节, 将MAC_key的前16字节清空, 作为要用的MAC_key
拿MAC_key来初始化MAC上下文
对明文进行MAC操作.官方建议:
Poly1305不能单独使用, 必须和其他加密算法一起对输入(MAC_key)进行处理
绝对禁止将nonce(MAC_key)直接传给Poly1305
不同会话的nonce(MAC_key)禁止重用(相同).
在实际应用绝对禁止将nonce(MAC_key)硬编码
看来nonce对于Poly1305应用的安全性影响很大(知道了MAC_key, 就可以伪造MAC值)
*//** Copyright 2021-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 <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>#include "my_openSSL_lib.h"/** This is a demonstration of how to compute Poly1305-AES using the OpenSSL* Poly1305 and AES providers and the EVP API.** Please note that:**   - Poly1305 must never be used alone and must be used in conjunction with*     another primitive which processes the input nonce to be secure;**   - you must never pass a nonce to the Poly1305 primitive directly;**   - Poly1305 exhibits catastrophic failure (that is, can be broken) if a*     nonce is ever reused for a given key.** If you are looking for a general purpose MAC, you should consider using a* different MAC and looking at one of the other examples, unless you have a* good familiarity with the details and caveats of Poly1305.** This example uses AES, as described in the original paper, "The Poly1305-AES* message authentication code":*   https://cr.yp.to/mac/poly1305-20050329.pdf** The test vectors below are from that paper.*//** Hard coding the key into an application is very bad.* It is done here solely for educational purposes.* These are the "r" and "k" inputs to Poly1305-AES.*/
static const unsigned char test_r[] = {0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b,0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00
};static const unsigned char test_k[] = {0xec, 0x07, 0x4c, 0x83, 0x55, 0x80, 0x74, 0x17,0x01, 0x42, 0x5b, 0x62, 0x32, 0x35, 0xad, 0xd6
};/** Hard coding a nonce must not be done under any circumstances and is done here* purely for demonstration purposes. Please note that Poly1305 exhibits* catastrophic failure (that is, can be broken) if a nonce is ever reused for a* given key.*/
static const unsigned char test_n[] = {0xfb, 0x44, 0x73, 0x50, 0xc4, 0xe8, 0x68, 0xc5,0x2a, 0xc3, 0x27, 0x5c, 0xf9, 0xd4, 0x32, 0x7e
};/* Input message. */
static const unsigned char test_m[] = {0xf3, 0xf6
};static const unsigned char expected_output[] = {0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45,0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde
};/** A property query used for selecting the POLY1305 implementation.*/
static char *propq = NULL;int main(int argc, char **argv)
{int ret = EXIT_FAILURE;EVP_CIPHER *aes = NULL;EVP_CIPHER_CTX *aesctx = NULL;EVP_MAC *mac = NULL;EVP_MAC_CTX *mctx = NULL;unsigned char composite_key[32];unsigned char out[16];OSSL_LIB_CTX *library_context = NULL;size_t out_len = 0;int aes_len = 0;library_context = OSSL_LIB_CTX_new();if (library_context == NULL) {fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");goto end;}/* Fetch the Poly1305 implementation */mac = EVP_MAC_fetch(library_context, "POLY1305", propq);if (mac == NULL) {fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");goto end;}/* Create a context for the Poly1305 operation */mctx = EVP_MAC_CTX_new(mac);if (mctx == NULL) {fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");goto end;}/* Fetch the AES implementation */aes = EVP_CIPHER_fetch(library_context, "AES-128-ECB", propq);if (aes == NULL) {fprintf(stderr, "EVP_CIPHER_fetch() returned NULL\n");goto end;}/* Create a context for AES */aesctx = EVP_CIPHER_CTX_new();if (aesctx == NULL) {fprintf(stderr, "EVP_CIPHER_CTX_new() returned NULL\n");goto end;}/* Initialize the AES cipher with the 128-bit key k */if (!EVP_EncryptInit_ex(aesctx, aes, NULL, test_k, NULL)) {fprintf(stderr, "EVP_EncryptInit_ex() failed\n");goto end;}/** Disable padding for the AES cipher. We do not strictly need to do this as* we are encrypting a single block and thus there are no alignment or* padding concerns, but this ensures that the operation below fails if* padding would be required for some reason, which in this circumstance* would indicate an implementation bug.*/if (!EVP_CIPHER_CTX_set_padding(aesctx, 0)) {fprintf(stderr, "EVP_CIPHER_CTX_set_padding() failed\n");goto end;}/** Computes the value AES_k(n) which we need for our Poly1305-AES* computation below.*/if (!EVP_EncryptUpdate(aesctx, composite_key + 16, &aes_len,test_n, sizeof(test_n))) {fprintf(stderr, "EVP_EncryptUpdate() failed\n");goto end;}/** The Poly1305 provider expects the key r to be passed as the first 16* bytes of the "key" and the processed nonce (that is, AES_k(n)) to be* passed as the second 16 bytes of the "key". We already put the processed* nonce in the correct place above, so copy r into place.*/memcpy(composite_key, test_r, 16);/* Initialise the Poly1305 operation */if (!EVP_MAC_init(mctx, composite_key, sizeof(composite_key), NULL)) {fprintf(stderr, "EVP_MAC_init() failed\n");goto end;}/* Make one or more calls to process the data to be authenticated */if (!EVP_MAC_update(mctx, test_m, sizeof(test_m))) {fprintf(stderr, "EVP_MAC_update() failed\n");goto end;}/* Make one call to the final to get the MAC */if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {fprintf(stderr, "EVP_MAC_final() failed\n");goto end;}printf("Generated MAC:\n");BIO_dump_indent_fp(stdout, out, (int)out_len, 2);putchar('\n');if (out_len != sizeof(expected_output)) {fprintf(stderr, "Generated MAC has an unexpected length\n");goto end;}if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {fprintf(stderr, "Generated MAC does not match expected value\n");goto end;}ret = EXIT_SUCCESS;
end:EVP_CIPHER_CTX_free(aesctx);EVP_CIPHER_free(aes);EVP_MAC_CTX_free(mctx);EVP_MAC_free(mac);OSSL_LIB_CTX_free(library_context);if (ret != EXIT_SUCCESS)ERR_print_errors_fp(stderr);return ret;
}

END

相关文章:

  • CSS 设置背景图片
  • 【JavaEE】网络原理:网络中的一些基本概念
  • AI+量化02_金融市场的基础概念
  • 蓝桥oj3272小蓝的漆房
  • 【SpringCloud】微服务框架后端部署详细过程记录20240119
  • Unity - transform使用
  • C++核心编程
  • unity webgl 系列(2):从webgl内存中下载文件到本地硬盘
  • Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用短曝光功能(C#)
  • 有线桥接|Wifi隔了一堵墙就没信号?房间的网线口利用起来,让房间死角也有网!
  • JVM篇--垃圾回收高频面试题
  • 解决国内Linux服务器无法使用Github的方法
  • Java三大集合
  • 003-90-15【SparkSQLDFDS】慈航寺庙山脚下八卦田旁油菜花海深处人家王大爷家女儿用GPT学习DataSet的基本操作
  • 【通知】我的教学文章《Rust跟我学》已全部上线
  • [nginx文档翻译系列] 控制nginx
  • Android 架构优化~MVP 架构改造
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • express + mock 让前后台并行开发
  • HTML中设置input等文本框为不可操作
  • JavaScript-Array类型
  • Javascript编码规范
  • java概述
  • Laravel核心解读--Facades
  • python 装饰器(一)
  • React-生命周期杂记
  • spring boot 整合mybatis 无法输出sql的问题
  • 从零搭建Koa2 Server
  • 大型网站性能监测、分析与优化常见问题QA
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 基于axios的vue插件,让http请求更简单
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 检测对象或数组
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 悄悄地说一个bug
  • 系统认识JavaScript正则表达式
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 在Unity中实现一个简单的消息管理器
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • NLPIR智能语义技术让大数据挖掘更简单
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​什么是bug?bug的源头在哪里?
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • (9)STL算法之逆转旋转
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (转)创业家杂志:UCWEB天使第一步
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • ../depcomp: line 571: exec: g++: not found
  • .CSS-hover 的解释
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .NET Project Open Day(2011.11.13)