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

2024-01-16 07:52

本文主要是介绍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

这篇关于openssl3.2 - 官方demo学习 - mac - gmac.c的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/611821

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Mac备忘录怎么导出/备份和云同步? Mac备忘录使用技巧

《Mac备忘录怎么导出/备份和云同步?Mac备忘录使用技巧》备忘录作为iOS里简单而又不可或缺的一个系统应用,上手容易,可以满足我们日常生活中各种记录的需求,今天我们就来看看Mac备忘录的导出、... 「备忘录」是 MAC 上的一款常用应用,它可以帮助我们捕捉灵感、记录待办事项或保存重要信息。为了便于在不同

电脑蓝牙连不上怎么办? 5 招教你轻松修复Mac蓝牙连接问题的技巧

《电脑蓝牙连不上怎么办?5招教你轻松修复Mac蓝牙连接问题的技巧》蓝牙连接问题是一些Mac用户经常遇到的常见问题之一,在本文章中,我们将提供一些有用的提示和技巧,帮助您解决可能出现的蓝牙连接问... 蓝牙作为一种流行的无线技术,已经成为我们连接各种设备的重要工具。在 MAC 上,你可以根据自己的需求,轻松地

如何关闭Mac的Safari通知? 3招教你关闭Safari浏览器网站通知的技巧

《如何关闭Mac的Safari通知?3招教你关闭Safari浏览器网站通知的技巧》当我们在使用Mac电脑专注做一件事情的时候,总是会被一些消息推送通知所打扰,这时候,我们就希望关闭这些烦人的Mac通... Safari 浏览器的「通知」功能本意是为了方便用户及时获取最新资讯,但很容易被一些网站滥用,导致我们

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

ubuntu系统使用官方操作命令升级Dify指南

《ubuntu系统使用官方操作命令升级Dify指南》Dify支持自动化执行、日志记录和结果管理,适用于数据处理、模型训练和部署等场景,今天我们就来看看ubuntu系统中使用官方操作命令升级Dify的方... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen