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

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

文章目录

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

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

概述

使用GMAC算法, 设置参数(指定加密算法 e.g. AES-128-GCM, 设置iv)
用key执行初始化, 然后对明文生成MAC数据
官方注释给出建议, key, iv最好不要硬编码出现在程序中

笔记

/*!
\file gmac.c
\note openssl3.2 - 官方demo学习 - mac - gmac.c
使用GMAC算法, 设置参数(指定加密算法 e.g. AES-128-GCM, 设置iv)
用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 <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>#include "my_openSSL_lib.h"/** Taken from NIST's GCM Test Vectors* http://csrc.nist.gov/groups/STM/cavp/*//** Hard coding the key into an application is very bad.* It is done here solely for educational purposes.*/
static unsigned char key[] = {0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2,0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb
};/** The initialisation vector (IV) is better not being hard coded too.* Repeating password/IV pairs compromises the integrity of GMAC.* The IV is not considered secret information and is safe to store with* an encrypted password.*/
static unsigned char iv[] = {0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba,0x01, 0x36, 0xa7, 0x97, 0xf3
};static unsigned char data[] = {0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78,0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab
};static const unsigned char expected_output[] = {0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46
};/** A property query used for selecting the GMAC implementation and the* underlying GCM mode cipher.*/
static char* propq = NULL;int main(int argc, char** argv)
{int ret = EXIT_FAILURE;EVP_MAC* _evp_mac = NULL;EVP_MAC_CTX* _evp_mac_ctx = NULL;unsigned char out[16];OSSL_PARAM _ossl_param_ary[4], * p_ossl_param = _ossl_param_ary;OSSL_LIB_CTX* _ossl_lib_ctx = NULL;size_t out_len = 0;_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 GMAC implementation */_evp_mac = EVP_MAC_fetch(_ossl_lib_ctx, "GMAC", propq);if (_evp_mac == NULL) {fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");goto end;}/* Create a context for the GMAC 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;}/* GMAC requires a GCM mode cipher to be specified */*p_ossl_param++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,"AES-128-GCM", 0);/** If a non-default property query is required when fetching the GCM mode* cipher, it needs to be specified too.*/if (propq != NULL)*p_ossl_param++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,propq, 0);/* Set the initialisation vector (IV) */*p_ossl_param++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,iv, sizeof(iv));*p_ossl_param = OSSL_PARAM_construct_end();/* Initialise the GMAC 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 one call to the final to get the MAC */if (!EVP_MAC_final(_evp_mac_ctx, 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_MAC_CTX_free(_evp_mac_ctx);EVP_MAC_free(_evp_mac);OSSL_LIB_CTX_free(_ossl_lib_ctx);if (ret != EXIT_SUCCESS)ERR_print_errors_fp(stderr);return ret;
}

END

相关文章:

  • Visual Studio Code 1.67调整文件嵌套、Markdown导航
  • yolov7_Obb环境安装
  • YOLOv8改进 | 检测头篇 | 利用DBB重参数化模块魔改检测头实现暴力涨点 (支持检测、分割、关键点检测)
  • 红黑树(RBTree)
  • 如何在 Ubuntu 中更改时区设置
  • 谷歌地图多个maker标记点击显示当前信息弹窗infowindow
  • ardupilot开发 --- 固件定制(OEM) 篇
  • 任务12:使用Hadoop Streaming解压NCDC天气原始数据
  • Resize:最近邻插值、双线性插值、双三次插值
  • 1.环境部署
  • export 是一个在 Unix 和类 Unix 系统(比如 Linux 和 macOS)中常用的 shell 命令,主要用于设置或导出环境变量。
  • C++——STL标准模板库——容器详解——set
  • 亚马逊云科技 WAF 部署小指南(五):在客户端集成 Amazon WAF SDK 抵御 DDoS 攻击...
  • Abp vNext(一)说明
  • 鸿蒙系列--数据管理
  • create-react-app项目添加less配置
  • HTML5新特性总结
  • Java-详解HashMap
  • js面向对象
  • leetcode98. Validate Binary Search Tree
  • npx命令介绍
  • PAT A1120
  • Webpack 4 学习01(基础配置)
  • 闭包--闭包作用之保存(一)
  • 关于springcloud Gateway中的限流
  • 讲清楚之javascript作用域
  • 聚类分析——Kmeans
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • ​第20课 在Android Native开发中加入新的C++类
  • $.ajax()
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (规划)24届春招和25届暑假实习路线准备规划
  • (九)c52学习之旅-定时器
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • .Net 8.0 新的变化
  • .NET Compact Framework 多线程环境下的UI异步刷新
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .netcore 获取appsettings
  • .Net接口调试与案例
  • .py文件应该怎样打开?
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [100天算法】-不同路径 III(day 73)
  • [100天算法】-每个元音包含偶数次的最长子字符串(day 53)
  • [APUE]进程关系(下)
  • [ARC066F]Contest with Drinks Hard
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息
  • [CentOs7]iptables防火墙安装与设置