openssl3.2 - 官方demo学习 - cipher - aesccm.c

文章目录

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

openssl3.2 - 官方demo学习 - cipher - aesccm.c

概述

aesccm.c 是 AES-192-CCM 的加解密应用例子, 用的EVP接口.
看到不仅仅要用到key, iv, data, 在此之前还要设置 nonce, tag, 认证数据.
为啥需要设置tag和认证数据啊? 普通的对称加解密, 有key, iv, data不就足够了么? 问题先放这里, 看看往后实验是否能有答案?

笔记

/*! \file aesccm.c *//** Copyright 2013-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*//** Simple AES CCM authenticated encryption with additional data (AEAD)* demonstration program.*/#include <stdio.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>#include "my_openSSL_lib.h"/* AES-CCM test data obtained from NIST public test vectors *//* AES key */
static const unsigned char ccm_key[] = {0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8
};/* Unique nonce to be used for this message */
static const unsigned char ccm_nonce[] = {0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
};/** Example of Additional Authenticated Data (AAD), i.e. unencrypted data* which can be authenticated using the generated Tag value.*/
static const unsigned char ccm_adata[] = {0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4
};/* Example plaintext to encrypt */
static const unsigned char ccm_pt[] = {0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10
};/* Expected ciphertext value */
static const unsigned char ccm_ct[] = {0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd
};/* Expected AEAD Tag value */
static const unsigned char ccm_tag[] = {0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,0x14, 0xdb, 0x50, 0x6d
};/** A library context and property query can be used to select & filter* algorithm implementations. If they are NULL then the default library* context and properties are used.*/
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;int aes_ccm_encrypt(void)
{int ret = 0;EVP_CIPHER_CTX *ctx;EVP_CIPHER *cipher = NULL;int outlen, tmplen;size_t ccm_nonce_len = sizeof(ccm_nonce);size_t ccm_tag_len = sizeof(ccm_tag);unsigned char outbuf[1024];unsigned char outtag[16];OSSL_PARAM params[3] = {OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END};printf("AES CCM Encrypt:\n");printf("Plaintext:\n");BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt)); /*!< 对给定的buffer[len], 进行16进制的格式打印, 类似于 winhex *//*! 0000 - c8 d2 75 f9 19 e1 7d 7f-e6 9c 2a 1f 58 93 9d fe   ..u...}...*.X...0010 - 4d 40 37 91 b5 df 13 10-                          M@7.....*//* Create a context for the encrypt operation */if ((ctx = EVP_CIPHER_CTX_new()) == NULL)goto err;/* Fetch the cipher implementation */if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)goto err;/* Set nonce length if default 96 bits is not appropriate */params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,&ccm_nonce_len);/* Set tag length */params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,NULL, ccm_tag_len);/** Initialise encrypt operation with the cipher & mode,* nonce length and tag length parameters.*/if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params))goto err;/* Initialise key and nonce */if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))goto err;/* Set plaintext length: only needed if AAD is used *//*! 参数4是NULL ? 那加密啥数据 ?内部调用 ccm_set_iv(ctx, len), 设置向量, 用的是ctx中的向量和sizeof(ccm_pt)* 跟到内部, 发现 ((out == NULL) && (in == NULL)) 时, 设置向量*/if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt)))goto err;/* Zero or one call to specify any AAD *//*! 参数2为空, 是输出为空. 应该也是设置一个参数* 看给出的后2个参数, 是设置未加密的附加认证数据.* 跟到内部, 发现 ((out == NULL) && (in != NULL)) 时, 设置认证数据*/if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))goto err;/* Encrypt plaintext: can only be called once *//*! EVP_EncryptUpdate 可以调用多次, 为啥官方注释(上面一行)说只能调用一次? 等一会试试, 将明文拆成2块, 加密update2次试试 *//*! 跟到内部, 发现 ((out != NULL) && (in != NULL)) 时, 是加密明文 */if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt)))goto err;/* Output encrypted block */printf("Ciphertext:\n");BIO_dump_fp(stdout, outbuf, outlen);/* Finalise: note get no output for CCM */if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen))goto err;/* Get tag */params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,outtag, ccm_tag_len);params[1] = OSSL_PARAM_construct_end();if (!EVP_CIPHER_CTX_get_params(ctx, params))goto err;/* Output tag */printf("Tag:\n");BIO_dump_fp(stdout, outtag, ccm_tag_len);ret = 1;
err:if (!ret)ERR_print_errors_fp(stderr);EVP_CIPHER_free(cipher); /*! EVP_CIPHER_fetch() 出来的东西要释放 */EVP_CIPHER_CTX_free(ctx); /*! EVP_CIPHER_CTX_new() 出来的东西要释放 */return ret;
}int aes_ccm_decrypt(void)
{int ret = 0;EVP_CIPHER_CTX *ctx;EVP_CIPHER *cipher = NULL;int outlen, rv;unsigned char outbuf[1024];size_t ccm_nonce_len = sizeof(ccm_nonce);OSSL_PARAM params[3] = {OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END};printf("AES CCM Decrypt:\n");printf("Ciphertext:\n");BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));if ((ctx = EVP_CIPHER_CTX_new()) == NULL)goto err;/* Fetch the cipher implementation */if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)goto err;/* Set nonce length if default 96 bits is not appropriate */params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,&ccm_nonce_len);/* Set tag length */params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,(unsigned char *)ccm_tag,sizeof(ccm_tag));/** Initialise decrypt operation with the cipher & mode,* nonce length and expected tag parameters.*/if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params))goto err;/* Specify key and IV */if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))goto err;/* Set ciphertext length: only needed if we have AAD */if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct)))goto err;/* Zero or one call to specify any AAD */if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))goto err;/* Decrypt plaintext, verify tag: can only be called once */rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));/* Output decrypted block: if tag verify failed we get nothing */if (rv > 0) {printf("Tag verify successful!\nPlaintext:\n");BIO_dump_fp(stdout, outbuf, outlen);} else {printf("Tag verify failed!\nPlaintext not available\n");goto err;}ret = 1;
err:if (!ret)ERR_print_errors_fp(stderr);EVP_CIPHER_free(cipher);EVP_CIPHER_CTX_free(ctx);return ret;
}int main(int argc, char **argv)
{if (!aes_ccm_encrypt())return EXIT_FAILURE;if (!aes_ccm_decrypt())return EXIT_FAILURE;return EXIT_SUCCESS;
}

END

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

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

相关文章

TensorRT量化

系列文章目录 第一章 YOLOv5模型训练集标注、训练流程 第二章 YOLOv5模型转ONNX,ONNX转TensorRT Engine 第三章 TensorRT量化 文章目录 系列文章目录前言一、量化二、量化在TensorRT中的实现三、预处理&#xff08;Preprocess)和后处理(Postprocess)总结 前言 学习笔记–恩培…

linuxshell日常脚本命令(1)

Linux 清理make、configure生成的文件&#xff08;灵感来自于quilt安装&#xff09; make clean #make clean 可以清除make失败的内容Linux 清理make、configure生成的文件 make clean #清除上一次make命令生成的文件 make distclean #清除上一次make以及configure命令生成的…

【MATLAB】 多元变分模态分解MVMD信号分解算法

有意向获取代码&#xff0c;请转文末观看代码获取方式~ 1 基本定义 多元变分模态分解&#xff08;MVMD&#xff09;是一种信号分解方法&#xff0c;可以自适应地实现信号的频域剖分及各分量的有效分离。 MVMD算法的具体步骤如下&#xff1a; 假设原始信号S被分解为K个分量μ…

代码随想录 Leetcode160. 相交链表

题目&#xff1a; 代码(首刷看解析 2024年1月13日&#xff09;&#xff1a; class Solution { public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *A headA, *B headB;while (A ! B) {A A ! nullptr ? A->next : headB;B B ! nullpt…

【力扣·每日一题】2182.构造限制重复的字符串(模拟 贪心 优先队列 C++ Go)

题目链接 题意 给你一个字符串 s 和一个整数 repeatLimit &#xff0c;用 s 中的字符构造一个新字符串 repeatLimitedString &#xff0c;使任何字母 连续 出现的次数都不超过 repeatLimit 次。你不必使用 s 中的全部字符。 返回 字典序最大的 repeatLimitedString 。 如果…

[易语言]易语言部署yolox的onnx模型

【官方框架地址】 https://github.com/Megvii-BaseDetection/YOLOX 【算法介绍】 YOLOX是YOLO系列目标检测算法的进一步演变和优化。它由Megvii Technology的研究团队开发&#xff0c;是一个高性能、可扩展的对象检测器。YOLOX在保留快速处理速度的同时&#xff0c;通过引入一…

textarea文本框根据输入内容自动适应高度

第一种&#xff1a; <el-input auto-completeoff typetextarea :autosize"{minRows:3,maxRows:10}" class"no-scroll"> </el-input> /* 页面的样式表 */ .no-scroll textarea {overflow: hidden; /* 禁用滚动条 */resize: none; /* 禁止用户…

NetApp E系列(E-Series)OEM产品介绍以及如何收集日志和保存配置信息

NetApp E系列是NetApp收购LSI存储后建立的一条新的产品线&#xff0c;由于LSI存储的历史悠久&#xff0c;所以这条产品线给NetApp带来了很多的OEM产品&#xff0c;可以说E系列是世界上OEM给最多公司的存储产品线也不为过&#xff0c;因为最早LSI的产品销售测率就是OEM&#xff…

2024.1.9 Spark SQL day06 homework,数据清洗

目录 一. Spark SQL中数据清洗的API有哪些&#xff0c;各自作用是什么&#xff1f; 二. 设置Spark SQL的shuffle分区数的方式有哪几种 三. 数据写出到数据库需要注意什么? 四. Spark程序运行集群分类 一. Spark SQL中数据清洗的API有哪些&#xff0c;各自作用是什么&#x…

JQuery过滤选择器-如何让某个元素换颜色(俩种方式)

目录 一、过滤选择器&#xff1a;eq二、过滤选择器 : lt 前言 : 在做项目时经常会遇到列表或者选择某个元素 一、过滤选择器&#xff1a;eq :eq (index)匹配一个给定索引值的元素 $("ul li:eq(0)").css("color","red");二、过滤选择器 : lt …

2024-01-11 部署Stable Diffusion遇挫记

点击 <C 语言编程核心突破> 快速C语言入门 部署Stable Diffusion遇挫记 前言一、一如既往的GitHub部署二、使用的感受总结 create by Stable Diffusion; prompt: fire water llama 前言 要解决问题: 由于近期的努力, 已经实现语音转文字模型, 通用chat迷你大模型的本地…

怎么把workspace的数据导入到simulink进行FFT分析?

怎么把数据导入到simulink在这篇博客已经阐述了&#xff0c;那么如何把数据导入到simulink还能进行FFT分析呢&#xff1f; 首先我们看simulink的FFT分析界面&#xff0c;&#xff08;前置步骤&#xff1a;导入powergui模块&#xff0c;双击powergui模块&#xff0c;Tool选项卡…

使用curl发送时间参数

# 获取当前日期 current_date$(date %Y-%m-%d)# 获取前一天的0点和23:59:59的时间&#xff0c;并格式化为yyyy-MM-dd 24hh:mm:ss begin_time$(date -d "yesterday 00:00:00" %Y-%m-%d\ %H:%M:%S) end_time$(date -d "yesterday 23:59:59" %Y-%m-%d\ %H:%M:…

发动机装备3d虚拟在线云展馆360度展示每处细节

在当今数字化的时代&#xff0c;消费者对于线上购物的需求与期待日益增长。尤其在购车这一大宗消费行为上&#xff0c;消费者不再满足于传统的图片与文字介绍。为了满足这一市场需求&#xff0c;我们引入了3D线上展示技术。 3D汽车模型实景互动展示是一种通过先进的三维建模技术…

【密码学】python密码学库pycryptodome

记录了一本几乎是10年前的书&#xff08;python绝技–用python成为顶级黑客&#xff09;中过时的内容 p20 UNIX口令破解机 里面提到了python标准库中自带的crypt库&#xff0c;经验证Python 3.12.1中并没有这个自带的库&#xff0c;密码学相关的库目前&#xff08;2024.1.12&a…

生成函数——裴蜀定理

有三种数量无限的砝码和一个天平&#xff0c;天平的一端有一个质量为 m 的物品&#xff0c;问能否通过放置砝码使得天平平衡&#xff1f; 输入 第一行包含一个整数 T (1 ≤ T ≤ 1e5)&#xff0c;表示测试用例的组数。 每组测试用例的第一行包含四个整数 a,b,c,m (1 ≤ a,b,c,…

R语言【paleobioDB】——pbdb_occurrences():从PBDB获取多个化石记录号的基本信息

Package paleobioDB version 0.7.0 paleobioDB 包在2020年已经停止更新&#xff0c;该包依赖PBDB v1 API。 可以选择在Index of /src/contrib/Archive/paleobioDB (r-project.org)下载安装包后&#xff0c;执行本地安装。 Usage pbdb_occurrences(...) Arguments 参数【...】…

一杯干红葡萄酒的酿造

一杯干红葡萄酒的酿造 一、什么是干红葡萄酒&#xff1f; 干红葡萄酒是指葡萄酒在酿造后&#xff0c;酿酒原料(葡萄汁)中的糖分完全转化成酒精&#xff0c;残糖量小于或等于4.00/L的红葡萄酒。 干红葡萄酒按颜色分可以分为 1&#xff0c;白葡萄酒:选择用白葡萄或浅色果皮的酿…

Linux命令行系列:Netcat网络工具

在大多数Linux发行版中&#xff0c;Netcat已经预装。如果需要安装或确保最新版本&#xff0c;请使用系统特定的包管理工具。例如&#xff0c;在Ubuntu上&#xff0c;可以使用以下命令安装Netcat&#xff1a; sudo apt-get install netcat 1、基本用法是在两台计算机之间建立简…

C++随机数生成:std标准库和Qt自带方法(未完待续)

std标准库 std::rand()是C中的一个随机数函数&#xff0c;它生成一个范围在0到RAND_MAX之间的伪随机整数。 在使用std::rand()之前&#xff0c;需要包含<cstdlib>头文件。 #include <cstdlib> 设置种子 在每次程序运行时&#xff0c;通常需要使用不同的种子值…