openssl3.2 - 官方demo学习 - mac - cmac-aes256.c

文章目录

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

openssl3.2 - 官方demo学习 - mac - cmac-aes256.c

概述

指定加密算法(e.g. AES-256-CBC), 对明文生成MAC数据

笔记

/*!
\file cmac-aes256.c
\note openssl3.2 - 官方demo学习 - mac - cmac-aes256.c
指定加密算法(e.g. AES-256-CBC), 对明文生成MAC数据
*//*-* 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*//** Example of using EVP_MAC_ methods to calculate* a CMAC of static buffers*/#include <string.h>
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/cmac.h>
#include <openssl/params.h>#include "my_openSSL_lib.h"/** Hard coding the key into an application is very bad.* It is done here solely for educational purposes.*/
static unsigned char key[] = {0x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf,0x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46,0x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85,0xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce,
};static const unsigned char data[] =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The ſlings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
"Devoutly to be wished. To die to sleep,\n"
"To sleepe, perchance to dreame, Aye, there's the rub,\n"
"For in that sleep of death what dreams may come\n"
"When we haue shuffled off this mortal coil\n"
"Must give us pause. There's the respect\n"
"That makes calamity of so long life:\n"
"For who would bear the Ships and Scorns of time,\n"
"The oppressor's wrong, the proud man's Contumely,\n"
"The pangs of dispised love, the Law's delay,\n"
;/* The known value of the CMAC/AES256 MAC of the above soliloqy */
static const unsigned char expected_output[] = {0x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba,0x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56,
};/** A property query used for selecting the MAC implementation.*/
static const char* propq = NULL;int main(void)
{int ret = EXIT_FAILURE;OSSL_LIB_CTX* _ossl_lib_ctx = NULL;EVP_MAC* _evp_mac = NULL;EVP_MAC_CTX* _evp_mac_ctx = NULL;unsigned char* out = NULL;size_t out_len = 0;OSSL_PARAM _ossl_param_ary[4], * p_ossl_param = _ossl_param_ary;char cipher_name[] = "AES-256-CBC";_ossl_lib_ctx = OSSL_LIB_CTX_new();if (_ossl_lib_ctx == NULL) {fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");goto end;}/* Fetch the CMAC implementation */_evp_mac = EVP_MAC_fetch(_ossl_lib_ctx, "CMAC", propq);if (_evp_mac == NULL) {fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");goto end;}/* Create a context for the CMAC operation */_evp_mac_ctx = EVP_MAC_CTX_new(_evp_mac);if (_evp_mac_ctx == NULL) {fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");goto end;}/* The underlying cipher to be used */*p_ossl_param++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name,sizeof(cipher_name));*p_ossl_param = OSSL_PARAM_construct_end();/* Initialise the CMAC operation */if (!EVP_MAC_init(_evp_mac_ctx, key, sizeof(key), _ossl_param_ary)) {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(_evp_mac_ctx, data, sizeof(data))) {fprintf(stderr, "EVP_MAC_update() failed\n");goto end;}/* Make a call to the final with a NULL buffer to get the length of the MAC */if (!EVP_MAC_final(_evp_mac_ctx, NULL, &out_len, 0)) {fprintf(stderr, "EVP_MAC_final() failed\n");goto end;}out = OPENSSL_malloc(out_len);if (out == NULL) {fprintf(stderr, "malloc failed\n");goto end;}/* Make one call to the final to get the MAC */if (!EVP_MAC_final(_evp_mac_ctx, out, &out_len, out_len)) {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 != (size_t)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:if (ret != EXIT_SUCCESS)ERR_print_errors_fp(stderr);/* OpenSSL free functions will ignore NULL arguments */OPENSSL_free(out);EVP_MAC_CTX_free(_evp_mac_ctx);EVP_MAC_free(_evp_mac);OSSL_LIB_CTX_free(_ossl_lib_ctx);return ret;
}

END

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

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

相关文章

SAP中采购文档价格条件可以删除吗?

首先要声名&#xff0c;基于采购价格条件的严谨性和历史追朔需求&#xff0c;删除属于危险操作。不建议普通用户去执行操作。如果有兴趣&#xff0c;在测试系统中自行测试一下即可。正式系统中&#xff0c;还请慎重处理。 笔者公司日常不会去删除采购价格&#xff0c;日常处理…

Word2Vec的CBOW模型

Word2Vec中的CBOW&#xff08;Continuous Bag of Words&#xff09;模型是一种用于学习词向量的神经网络模型。CBOW的核心思想是根据上下文中的周围单词来预测目标单词。 例如&#xff0c;对于句子“The cat climbed up the tree”&#xff0c;如果窗口大小为5&#xff0c;那么…

字符串与数组的异同

Java 中的字符串&#xff08;String&#xff09;和数组&#xff08;Array&#xff09;是两种不同类型的数据结构&#xff0c;它们有一些相似之处&#xff0c;同时也有一些显著的区别。 相同之处&#xff1a; 存储多个元素&#xff1a; 字符串和数组都用于存储多个元素。 使用…

微信小程序(四)页面跳转

注释很详细&#xff0c;直接上代码 新增内容 1.相对路径页面跳转 2. 绝对路径页面跳转 index.wxml <!-- navigator是块级元素&#xff0c;占一整行 --> <!-- 页面跳转url&#xff0c;相对路径 --> <navigator url"../logs/logs"><button type&…

赋值运算符和关系运算符

赋值运算符和关系运算符 赋值运算符 分类 符号作用说明赋值int a 10&#xff0c; 将10赋值给变量a加后赋值a b&#xff0c;将a b的值赋值给a-减后赋值a - b&#xff0c;将a - b的值赋值给a*乘后赋值a * b&#xff0c;将a b的值赋值给a/除后赋值a / b&#xff0c;将a b的…

运维知识点-Sqlite

Sqlite 引入 依赖 引入 依赖 <dependency><groupId>org.xerial</groupId><artifactId>sqlite-jdbc</artifactId><version>3.36.0.3</version></dependency>import javafx.scene.control.Alert; import java.sql.*;public clas…

第二证券:抢占技术前沿 中国光伏企业结伴“走出去”

2024年新年前后&#xff0c;光伏职业分外忙碌。据证券时报记者不完全统计&#xff0c;晶澳科技、华晟新动力、高测股份、华民股份等多家企业宣告新建项目投产&#xff0c;安徽皇氏绿能等企业的项目也迎来设备安装的重要节点。 证券时报记者采访多家企业的负责人后了解到&#…

AUTOSAR OS详细介绍及配置说明(更新版20240115)

前言 AUTOSAR OS扩展了OSEK/VDX标准中的操作系统,所以本文结合OSEK/VDX的标准来介绍AUTOSAR OS,并借助Vector Configurator讲解AUTOSAR OS的配置。 OSEK源于德语,英文意思是:“车载电子设备的开发系统和接口”,它是一个标准,用来产生嵌入式操作系统的规范,通讯协议栈,…

宝塔面板打不开,记录一下解决办法

由于在服务器宝塔内安装Apache&#xff0c;提示需要卸载nginx&#xff0c;卸载过后宝塔通过网址访问不了&#xff0c;特此记录一下问题。 1、检查宝塔端口会不会被占用 面板默许使用8888端口&#xff0c;使用命令查看8888端口会不会被占用&#xff1a; netstat -apn|grep 88…

tessreact训练字库

tessreact主要用于字符识别&#xff0c;除了使用软件自带的中英文识别库&#xff0c;还可以使用Tesseract OCR训练属于自己的字库。 一、软件环境搭建 使用Tesseract OCR训练自己的字库&#xff0c;需要安装Tesseract OCR和jTessBoxEditor(配套训练工具)。jTessBoxEditor需要…

接口以及多态

什么是接口 接口是一种抽象的数据类型&#xff0c;它定义了一组方法的规范&#xff0c;但没有具体的实现。接口可以被类实现&#xff0c;一个类实现了接口后&#xff0c;必须实现接口中定义的所有方法。接口可以被多个类实现&#xff0c;用以实现多重继承。 接口的定义使用关键…

基于SSM的社区老年人关怀服务系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

【Vue自定义指令详细介绍】

Vue自定义指令详细介绍 1. 自定义指令1.1 局部1.2 全局 1. 自定义指令 在 Vue.js 中&#xff0c;除了默认提供的核心指令&#xff08;如 v-model、v-show、v-if 等&#xff09;&#xff0c;Vue.js 也允许注册自定义指令&#xff0c;自定义指令给你提供了一种方法来扩展 Vue 的…

【23种设计模式应用场景汇总】

23种设计模式应用场景汇总 设计模式是一种在软件开发中解决特定问题的通用解决方案。下面我将尝试将23种设计模式融入到一个场景中&#xff1a; 假设我们正在开发一个在线购物系统&#xff0c;我们可以使用以下设计模式&#xff1a; 1. 工厂方法模式&#xff1a;当用户在网站上…

力扣hot100 只出现一次的数字 位运算

Problem: 136. 只出现一次的数字 文章目录 思路复杂度Code 思路 复杂度 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( n ) O(n) O(n) Code class Solution {public int singleNumber(int[] nums) {int res 0;for(int x : nums)res ^ x;return res;} }

UI自动化测试框架

文章目录 UI自动化基础什么是UI自动化测试框架UI自动化测试框架的模式数据驱动测试框架关键字驱动测试框架行为驱动测试框架 UI自动化测试框架的作用UI自动化测试框架的核心思想UI自动化测试框架的步骤UI自动化测试框架的构成UtilsLog.javaReadProperties.Java coreBaseTest.ja…

【分布式技术】监控技术zabbix实操

目录 一、脚本监控nginx的连接状态 步骤一&#xff1a;做好nginx的配置 步骤二&#xff1a;完成监控数据脚本编写&#xff0c;并使用zabbix_get测试 步骤三&#xff1a;在zabbix agent配置目录中&#xff0c;编写以conf结尾的用户参数文件 步骤四&#xff1a;在zabbix web…

Python 网络编程之TCP详细讲解

【一】传输层 【1】概念 传输层是OSI五层模型中的第四层&#xff0c;负责在网络中的两个端系统之间提供数据传输服务主要协议包括**TCP&#xff08;传输控制协议&#xff09;和UDP&#xff08;用户数据报协议&#xff09;** 【2】功能 **端到端通信&#xff1a;**传输层负责…

HackerGPTWhiteRabbitNeo的使用及体验对比

1. 简介 WhiteRabbitNeo&#xff08;https://www.whiterabbitneo.com/&#xff09;是基于Meta的LLaMA 2模型进行特化的网络安全AI模型。通过专门的数据训练&#xff0c;它在理解和生成网络安全相关内容方面具有深入的专业能力&#xff0c;可广泛应用于教育、专业培训和安全研究…

MongoDB系统性能调优(持续更新)

cache_size 指定WT存储引擎内部cache的内存用量上限。 需要注意的是&#xff0c;仅作用于WiredTiger cache&#xff0c;而非mongod进程的内存用量上限。MongoDB同时使用WT cache和文件系统cache&#xff0c;往往mongod进程的内存用量高于该值。cache_size相对于物理内存总量不要…