openssl3.2 - 官方demo学习 - mac - hmac-sha512.c

这篇具有很好参考价值的文章主要介绍了openssl3.2 - 官方demo学习 - mac - hmac-sha512.c。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

openssl3.2 - 官方demo学习 - mac - hmac-sha512.c

概述

MAC算法为HMAC, 设置参数(摘要算法为SHA3-512), 用key初始化, 对明文做MAC数据.文章来源地址https://www.toymoban.com/news/detail-798976.html

笔记

/*!
\file hmac-sha512.c
\note
openssl3.2 - 官方demo学习 - mac - hmac-sha512.c
MAC算法为HMAC, 设置参数(摘要算法为SHA3-512), 用key初始化, 对明文做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 HMAC 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/hmac.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[] = {
	0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,
	0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,
	0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,
	0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,
	0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,
	0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,
	0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,
	0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,
};

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 HMAC/SHA3-512 MAC of the above soliloqy */
static const unsigned char expected_output[] = {
	0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,
	0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,
	0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,
	0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,
	0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,
	0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,
	0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,
	0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,
};

/*
 * 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;
	EVP_MD_CTX* _evp_md_ctx = NULL;
	unsigned char* out = NULL;
	size_t out_len = 0;
	OSSL_PARAM params[4], * p = params;
	char digest_name[] = "SHA3-512";

	_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 HMAC implementation */
	_evp_mac = EVP_MAC_fetch(_ossl_lib_ctx, "HMAC", propq);
	if (_evp_mac == NULL) {
		fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
		goto end;
	}

	/* Create a context for the HMAC 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 digest to be used */
	*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,
		sizeof(digest_name));
	*p = OSSL_PARAM_construct_end();

	/* Initialise the HMAC operation */
	if (!EVP_MAC_init(_evp_mac_ctx, key, sizeof(key), params)) {
		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 != 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_MD_CTX_free(_evp_md_ctx);
	EVP_MAC_CTX_free(_evp_mac_ctx);
	EVP_MAC_free(_evp_mac);
	OSSL_LIB_CTX_free(_ossl_lib_ctx);
	return ret;
}

END

到了这里,关于openssl3.2 - 官方demo学习 - mac - hmac-sha512.c的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • openssl3.2 - 官方demo学习 - 索引贴

    如果要将openssl在自己的业务逻辑中用起来, 只从网上找零星的代码片段肯定不行的. 且不说人家写的好不好, 主要是我们不知道人家为啥那么写. openSSL的API, 不同版本, API调用名称, 调用顺序. 是否为已经废弃, 都不同. 如果不是官方demo中推荐的写法, 如果有问题, 那也不好找问题

    2024年02月02日
    浏览(33)
  • openssl3.2 - 官方demo学习 - certs

    打开官方demos的certs目录, 没看到.c. 茫然了一下. 官方在这个目录中要展示啥呢? 看了readme, 懂了. 原来官方在这个目录中, 要展示如何使用openssl.exe的命令行来操作证书(建立证书, 证书入库, 吊销证书, 查询证书). 官方通过3个.sh来展示证书操作. mkcerts.sh - 一组操作, 用来建立证书

    2024年02月01日
    浏览(32)
  • openssl3.2 - 官方demo学习 - sconnect.c

    TLS客户端 - 使用根证书, 非阻塞, 向服务器要东西. 开始一个新demo学习时, 要从头配置包含路径, 麻烦. 直接拷贝上一个实现工程, 换掉实现.c方便一些. 换的新demo实现, 要加入库包含和头包含, 麻烦, 做一个公用头文件, 直接include方便一些.

    2024年02月01日
    浏览(34)
  • openssl3.2 - 官方demo学习 - test - certs

    官方demos目录有证书操作的例子 已经做了笔记 openssl3.2 - 官方demo学习 - certs 但是这个demos/certs目录的脚本, 并没有演示如何操作PKCS12证书. 在官方给的程序例子中, 有操作PKCS12证书的工程, 但是却没有配套的PKCS12证书. 这咋弄? 翻了一下openssl源码工程, 发现测试目录中有2个脚本

    2024年01月18日
    浏览(36)
  • openssl3.2 - 官方demo学习 - saccept.c

    建立TLSServer(使用了证书, 和证书中的私钥), 接收客户端的连接, 并将客户端发来的信息打印到屏幕 笔记

    2024年01月20日
    浏览(36)
  • openssl3.2 - 官方demo学习 - cipher - aesgcm.c

    AES-256-GCM 在这个实验中验证了EVP_CIPHER_fetch()中算法名称字符串的来源定位. 在工程中配置环境变量PATH, 且合并环境. 这样就不用将openSSL的DLL和配置文件拷贝到工程里面了. 提交代码时, 就能节省很多空间. 配置工程调试时, 也顺畅多了.

    2024年01月22日
    浏览(37)
  • openssl3.2 - 官方demo学习 - smime - smdec.c

    从pem证书中得到x509*和私钥, 用私钥和证书解密MIME格式的PKCS7密文, 并保存解密后的明文 MIME的数据操作, 都是PKCS7相关的

    2024年01月18日
    浏览(30)
  • openssl3.2 - 官方demo学习 - kdf - hkdf.c

    设置摘要算法HKDF的参数, 然后取key

    2024年01月17日
    浏览(32)
  • openssl3.2 - 官方demo学习 - smime - smenc.c

    读取X509证书, 用PKCS7加密明文(证书 + 明文 + 3DES_CBC), 保存为MIME格式的密文 openssl API的命名含义 BIO_new_file “new” a “file”, return a “BIO” object PEM_read_bio_X509() Read a certificate in PEM format from a BIO data format is “PEM”, “read” from “bio”, return a object type is “X509”

    2024年01月20日
    浏览(37)
  • openssl3.2 - 官方demo学习 - smime - smver.c

    对于签名文件(不管是单独签名, 还是联合签名), 都要用顶层证书进行验签(靠近根CA的证书) 读证书文件, 得到x509*, 添加到证书容器 读取签名密文, 得到pkcs7*和密文的bio 进行pkcs7验签, 并将验签得到的签名的明文写到文件.

    2024年01月19日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包