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

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

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

相关文章

jvm -Djava.library.path 无法打开共享对象文件:

项目代码修改 java -jar -Xms1024m -Xmx1024m -Dloader.path/data/encrypt/lib -Djava.library.path/data/encrypt/libVtExtAPI.so server-1.0.0-SNAPSHOT.jar 重新启动

C#设计模式教程(1):简单工厂模式

在C#中,工厂模式可以分为三种主要类型:简单工厂模式、工厂方法模式和抽象工厂模式。 简单工厂模式(Simple Factory Pattern): 简单工厂模式并不属于23种经典设计模式之一,但它是工厂模式的一种简单形式。在简单工厂模式中,有一个工厂类负责根据传入的参数决定创建哪种产…

bevy the book 20140118翻译(全)

源自&#xff1a;Bevy Book: Introduction 主要用 有道 翻译。 Introduction 介绍 Getting Started 开始 Setup 设置 Apps 应用程序 ECS Plugins 插件 Resources 资源 Next Steps 下一个步骤 Contributing 贡献 Code 代码 Docs 文档 Building Bevys Ecosystem 构建 b…

VScode远程开发

VScode远程开发 在SSH远程连接一文中&#xff0c;我么介绍了如何使用ssh远程连接Jetson nano端&#xff0c;但是也存在诸多不便&#xff0c;比如:编辑文件内容时&#xff0c;需要使用vi编辑器&#xff0c;且在一个终端内&#xff0c;无法同时编辑多个文件。本节将介绍一较为实用…

基于ORB算法的图像匹配

基础理论 2006年Rosten和Drummond提出一种使用决策树学习方法加速的角点检测算法&#xff0c;即FAST算法&#xff0c;该算法认为若某点像素值与其周围某邻域内一定数量的点的像素值相差较大&#xff0c;则该像素可能是角点。 其计算步骤如下&#xff1a; 1&#xff09;基于F…

VMware Workstation Pro虚拟机搭建

下载链接&#xff1a;Download VMware Workstation Pro 点击上方下载&#xff0c;安装过程很简单&#xff0c;我再图片里面说明 等待安装中。。。。。是不是再考虑怎样激活&#xff0c;我都给你想好了&#xff0c;在下面这个链接&#xff0c;点赞收藏拿走不谢。 https://downl…

Jupyter-Notebook无法创建ipynb文件

文章目录 概述排查问题恢复方法参考资料 概述 用户反馈在 Notebook 上无法创建 ipynb 文件&#xff0c;并且会返回以下的错误。 报错的信息是: Unexpected error while saving file: Untitled5.ipynb attempt to write a readonly database 排查问题 这个是一个比较新的问…

lodash 的 _.groupBy 函数是怎么实现的?

说在前面 &#x1f388;lodash的_.groupBy函数可以将一个数组按照给定的函数分组&#xff0c;返回一个新对象。该函数接收两个参数&#xff1a;第一个参数是要进行分组的数组&#xff0c;第二个参数是用于分组的函数。该函数会对数组中的每个元素进行处理&#xff0c;返回一个值…

物联网孢子捕捉分析仪在农田起到什么作用

TH-BZ03随着科技的飞速发展&#xff0c;物联网技术在农业领域的应用越来越广泛。其中&#xff0c;物联网孢子捕捉分析仪作为一种先进的设备&#xff0c;在农田中发挥着不可或缺的作用。本文将详细介绍物联网孢子捕捉分析仪在农田中的作用。 一、实时监测与预警 物联网孢子捕捉分…

【webrtc】GCC 7: call模块创建的ReceiveSideCongestionController

webrtc 代码学习&#xff08;三十二&#xff09; video RTT 作用笔记 从call模块说起 call模块创建的时候&#xff0c;会创建 src\call\call.h 线程&#xff1a; 统计 const std::unique_ptr<CallStats> call_stats_;SendDelayStats &#xff1a; 发送延迟统计 const…

WebGL中开发VR(虚拟现实)应用

WebGL&#xff08;Web Graphics Library&#xff09;是一种用于在浏览器中渲染交互式3D和2D图形的JavaScript API。要在WebGL中开发VR&#xff08;虚拟现实&#xff09;应用程序&#xff0c;您可以遵循以下一般步骤&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&a…

手把手带你死磕ORBSLAM3源代码(五十一) FrameDrawer.cc DrawTextInfo

目录 一.前言 二.代码 2.1完整代码 2.2 cv::Mat::zeros介绍 2.3 cv::putText介绍 2.4 cv::Point介绍

word vba自动化排版-设置标题模板样式、标题、正文、图表、页面、上下标等设置、删除空白行、删除分页符分节符、删除空格等

word vba自动化排版-设置标题模板样式、标题、正文、图表、页面、上下标等设置、删除空白行、删除分页符分节符、删除空格等 目录 1.前提 2.思路 3.word中设置 4.效果图 5.经验教训 6.直接上代码 1.前提 需求&#xff1a;工作中涉及自动识别大量的文字报告&#xff08;o…

实验笔记之——基于TUM-RGBD数据集的SplaTAM测试

之前博客对SplaTAM进行了配置&#xff0c;并对其源码进行解读。 学习笔记之——3D Gaussian SLAM&#xff0c;SplaTAM配置&#xff08;Linux&#xff09;与源码解读-CSDN博客SplaTAM全称是《SplaTAM: Splat, Track & Map 3D Gaussians for Dense RGB-D SLAM》&#xff0c;…

NAT配置

IPV4地址中在A/B/C三的单播地址中&#xff0c;还存在私有ip 与公有的区分&#xff1b; 公有&#xff1a;具有全球唯一性&#xff0c;可以在互联网中通讯&#xff0c;需要付费使用 私有&#xff1a;具有本地唯一性&#xff0c;不能在互联网中通信&#xff0c;无需付费使用 私…

在红墙下的冬日幻想:Pygame库实现下雪动画

在红墙下的冬日幻想&#xff1a;借助Pygame库实现下雪动画 寒风轻拂着故宫红墙&#xff0c;我静静地思念着你。这个冬天&#xff0c;借助 Python 的 Pygame 库&#xff0c;我为你呈现一场梦幻般的下雪动画&#xff0c;让雪花在故宫红墙的映衬下在屏幕上翩翩起舞。 准备 首先…

【C++】:STL序列式容器list源码剖析

一、list概述 总的来说&#xff1a;环形双向链表 特点&#xff1a; 底层是使用链表实现的&#xff0c;支持双向顺序访问 在list中任何位置进行插入和删除的速度都很快 不支持随机访问&#xff0c;为了访问一个元素&#xff0c;必须遍历整个容器 与其他容器相比&#xff0c;额外…

【AI预测】破晓未来教育市场:如何精准定位、精选师资并启动高潜力培训项目

在当前全球化和技术快速迭代的背景下&#xff0c;各行业正面临巨大的人才缺口和新的发展机遇。 全球化浪潮&#xff0c;各行业如同搭乘上了一列高速列车&#xff0c;不断深入探索并广泛应用AI技术以提升产业效率、创新服务模式。在智能制造领域&#xff0c;工业4.0时代犹如给…

理解pytorch系列:transpose是怎么实现的

在PyTorch中&#xff0c;transpose()是一种操作&#xff0c;它交换张量中两个指定维度的位置。实现这一点的关键在于不实际移动数据&#xff0c;而是通过改变张量的元数据&#xff08;包括步长&#xff08;stride&#xff09;和尺寸&#xff08;size&#xff09;&#xff09;来…

【leetcode】消失的数字

大家好&#xff0c;我是苏貝&#xff0c;本篇博客带大家刷题&#xff0c;如果你觉得我写的还不错的话&#xff0c;可以给我一个赞&#x1f44d;吗&#xff0c;感谢❤️ 目录 1.暴力求解法2.采用异或的方法&#xff08;同单身狗问题&#xff09;3.先求和再减去数组元素 点击查看…