AES-GCM算法

2023-05-16

AES-GCM算法简介

AES-GCM算法是带认证和加密的算法,同时可以对给定的原文,生成加密数据和认证码。参数如下:

1)带加密的原文、2)存储加密后密文、3)IV向量、4)生成的消息验证码tag、5)额外的消息认证数据aad,通信双方需要共享;

整个AES-GCM算法代码如下:

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <ica_api.h>

#define AES_CIPHER_BLOCK_SIZE  16

/* 本测试demo用了一个静态的key
 *
 * Note: AES-128 key size is 16 bytes (AES_KEY_LEN128)
 */
unsigned char aes_key[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};

/* 带加密的数据原文.
 */
unsigned char plain_data[] = {
	0x55, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x6c, 0x69,
	0x62, 0x69, 0x63, 0x61, 0x20, 0x69, 0x73, 0x20,
	0x73, 0x6d, 0x61, 0x72, 0x74, 0x20, 0x61, 0x6e,
	0x64, 0x20, 0x65, 0x61, 0x73, 0x79, 0x21, 0x00
};

/* 初始化的IV向量. The initialization vector
 * size must be greater than 0 and less than 2**61. A length of
 * 12 is recommended.
 */
unsigned char iv[12] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0A, 0x0B
};

/* aad是额外的认证数据,aad数组用于计算生成AES-GCM消息的认证码,不用于加密。
 */
unsigned char aad[] = {
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
};

/* Prints hex values to standard out. */
static void dump_data(unsigned char *data, unsigned long length);
/* Prints a description of the return value to standard out. */
static int handle_ica_error(int rc);

int main(char **argv, int argc)
{
	int rc;

	/* tag数组存储者明文数据和aad变量的消息验证码.
	 * Note: The authentication strength depends on the length of the
	 *       authentication tag
	 */
	unsigned char tag[16];

	unsigned char cipher_data[sizeof(plain_data)];     //存储者AES-GCM加密后的数据
	unsigned char decrypt_data[sizeof(plain_data)];    //存储者AES-GCM解密后的数据

	/* Dump key, iv, aad and plain data to standard output, just for
	 * a visual control.
	 */
	printf("AES key:\n");
	dump_data(aes_key, sizeof(aes_key));
	printf("IV:\n");
	dump_data(iv, sizeof(iv));
	printf("AAD:\n");
	dump_data(aad, sizeof(aad));
	printf("plain data:\n");
	dump_data(plain_data, sizeof(plain_data));	

	/* 使用AES-GCM将明文数据plain_data加密成密文cipher_data,同时根据plain_data和aad变量生成消息验证码tag变量*/
    //ICA_ENCRYPT=0
	rc = ica_aes_gcm(plain_data, sizeof(plain_data), cipher_data,
				iv, sizeof(iv),
				aad, sizeof(aad),
				tag, sizeof(tag),
				aes_key, AES_KEY_LEN128,
				ICA_ENCRYPT);

	/* Error handling (if necessary). */
	if (rc)
		return handle_ica_error(rc);

	/* Dump encrypted data.
	 */
	printf("encrypted data:\n");
	dump_data(cipher_data, sizeof(plain_data));
	printf("Authetication code:\n");
	dump_data(tag, sizeof(tag));

	/* GCM解密,将cipher data解密到decrypted data.
	 * Note: 跟GCM加密必须使用相同的AES key, IV and AAD.
	 * tag消息验证码用于认证校验解密后的decrypt_data和aad变量是否一致.
	 * 如消息验证码tag认证不通过, 将返回EFAULT.
	 */
	rc = ica_aes_gcm(decrypt_data, sizeof(plain_data), cipher_data,
				iv, sizeof(iv),
				aad, sizeof(aad),
				tag, sizeof(tag),
				aes_key, AES_KEY_LEN128,
				ICA_DECRYPT);

	/* Error handling (if necessary). */
	if (rc)
		return handle_ica_error(rc);

	/* Dump decrypted data.
	 * Note: Please compare output with the plain data, they are the same.
	 */
	printf("decrypted data:\n");
	dump_data(decrypt_data, sizeof(plain_data));

	/* Surprise... :-)
	 * Note: The following will only work in this example!
	 */
	printf("%s\n", decrypt_data);
}

static void dump_data(unsigned char *data, unsigned long length)
{
	unsigned char *ptr;
	int i;

	for (ptr = data, i = 1; ptr < (data+length); ptr++, i++) {
		printf("0x%02x ", *ptr);
		if ((i % AES_CIPHER_BLOCK_SIZE) == 0)
			printf("\n");
	}
	if (i % AES_CIPHER_BLOCK_SIZE)
		printf("\n");
}

static int handle_ica_error(int rc)
{
	switch (rc) {
	case 0:
		printf("OK\n");
		break;
	case EINVAL:
		printf("Incorrect parameter.\n");
		break;
	case EPERM:
		printf("Operation not permitted by Hardware (CPACF).\n");
		break;
	case EIO:
		printf("I/O error.\n");
		break;
	case EFAULT:
		printf("The verification of the message authentication code has failed.\n");
		break;
	default:
		printf("unknown error.\n");
	}

	return rc;
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AES-GCM算法 的相关文章

随机推荐

  • 串口通信基本接线方法

    目前较为常用的串口有9针串口 xff08 DB9 xff09 和25针串口 xff08 DB25 xff09 xff0c 通信距离较近时 lt 12m xff0c 可以用电缆线直接连接标准RS232端口 RS422 RS485较远 xff0
  • 字符串库函数大全

    打开string h头文件 xff0c 网址 xff1a https en cppreference com w c string byte 抽出了常用的字符串操作函数 xff0c 权当手册用了 1 字符串基本操作 strcpy xff1a
  • RPlidar(一)——雷达试用

    先附一张RPlidar的图片 以下为本人初次通过ubuntu使用RPlidar 首先将RPlidar连接到电脑上 然后在terminal输入 ls dev ttyUSB 更改com port 权限 sudo chmod 666 dev tt
  • RPlidar学习(三)——RPlidar源代码库

    1 头文件简介 rplidar h 一般情况下开发的项目中仅需要引入该头文件即可使用 RPLIDAR SDK 的所有功能 rptypes h 平台无关的结构和常量定义 rplidar protocol h 定义了 RPLIDAR 通讯协议文
  • PC读写西门子PLC寄存器的值

    https blog csdn net weixin 29482793 article details 81873280
  • 编码的历史

    快下班时 xff0c 爱问问题的小朋友Nico又问了一个问题 xff1a 34 sqlserver里面有char和nchar xff0c 那个n据说是指unicode的数据 xff0c 这个是什么意思 34 并不是所有简单的问题都很容易回答
  • 教你如何通过MCU配置S2E为TCP Server的工作模式

    在上一篇文章中讲了 如何通过MCU配置S2E为UDP模式 xff0c 相信小伙伴们已经了解了S2E的基本功能 xff0c 在这一篇中 xff0c 我们再接再厉 xff0c 继续学习一下 如何通过MCU配置S2E为TCP Server的工作模
  • ubuntu配置USB权限

    usbcan设备在ubutu下使用 xff0c 每次必须动用root权限才能使用 xff0c 比较繁琐 参考如下文章进行配置 xff1a https blog csdn net bigdog 1027 article details 790
  • SocketCAN学习笔记

    1 对SocketCAN的理解 69条消息 对Socket CAN的理解 xff08 1 xff09 CAN总线原理 yuzeze的博客 CSDN博客 can socket 对Socket CAN的理解 xff08 2 xff09 Sock
  • Golang写Post请求并获取返回值

    直接上代码 xff0c 其中可以在下面代码中添加 package main import fmt net http io ioutil net url func main 需要post的数据 以key value形式提交 data 61 m
  • postMan 汉化

    1 下载postman解压安装 xff0c 下载地址Postman 2 下载汉化包 Release 9 2 0 hlmd Postman cn GitHub 选择postMan对应的版本 3 找到postman根目录我的在c盘 xff0c
  • 2022 小学组试题

    1 史密斯数 若一个合数的质因数分解式逐为位相加之和等于其本身逐位相加之和 则称这个数为smith数 如4937775 3 5 5 65837 而3 5 5 6 5 8 3 7 42 4 9 3 7 7 7 5 42 所以4937775是s
  • usaco 1 回文素数快速筛选

    求指定区间的回文素数 include lt bits stdc 43 43 h gt define N 10000010 using namespace std bool st N int cnt 61 0 primes N n m voi
  • C++ STL 学习笔记

    MAP的用法 xff1a 389找不同 给定两个字符串 s 和 t xff0c 它们只包含小写字母 字符串 t 由字符串 s 随机重排 xff0c 然后在随机位置添加一个字母 请找出在 t 中被添加的字母 示例 输入 xff1a s 61
  • 大模型平台

    大模型训练平台的算力急速增长 xff1a 据报道 xff0c GPT3 5 的训练使用了微软专门建设的 AI 计算系统 xff0c 由 1 万个 V100 GPU 组成的高性能网络集群 xff0c 总算力消耗约 3640 PF days 即
  • Float IP 浮动IP地址

    xfeff xfeff 就是多个主机工作在 同一个集群中 xff0c 即两台主机以上 每台机器除了自己的实IP外 xff0c 会设置一个浮动IP xff0c 浮动IP与主机的服务 xff08 HTTP服务 邮箱服务 xff09 绑在一起的
  • linux 下安装 c++编译器的方法

    xfeff xfeff 假设你的电脑能够访问internet xff0c 这个命令会自动从网络下载并把软件安装到本地 超级用户权限 xff1a yum y install gcc c 43 43 另外一定不要天真的认为linux 下面 编译
  • linux 内核编译错误的原因

    xfeff xfeff 直接编译centos 7 内核目录下的代码出现如下错误 xff1a root 64 localhost 3 10 0 123 el7 x86 64 make make 1 No rule to make target
  • ifconfig 看到Rx error overrun 如何解决

    xfeff xfeff 一台机器经常收到丢包的报警 xff0c 先看看最底层的有没有问题 ethtool em2 egrep 39 Speed Duplex 39 Speed 1000Mb s Duplex Full ethtool S e
  • AES-GCM算法

    AES GCM算法简介 AES GCM算法是带认证和加密的算法 xff0c 同时可以对给定的原文 xff0c 生成加密数据和认证码 参数如下 xff1a 1 xff09 带加密的原文 2 xff09 存储加密后密文 3 xff09 IV向量