A Fast and Easy to Use AES Library
http://www.codeproject.com/Articles/57478/A-Fast-and-Easy-to-Use-AES-Library
Introduction
EfAesLib is a highly optimized Advanced Encryption Standard (AES) library for the Windows platform 32-bit architecture. The Extreme Fast AES Library is implemented based on the official document:http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf.
The library is actually my personal work. I have decided to put it in the public domain and make it free. The size is a little on the higher side because of some optimization to use space in exchange of time.
I have provided the compiled DLL in VS2008, and the project files; or you can use the source in any other platform, it is just plain 'C'.
Using the code
AES is a 128-bit block encrypt/decrypt algorithm. That means you need to carefully handle the last block which is not 16 bytes aligned. Otherwise, you might be unable to decrypt correctly.
There are many block modes defined in the cipher realm. Different block modes have different characteristics. For example, the CRT mode only needs encryption logic, so it is suitable for low cost hardware implementations. The PCBC mode provides better error propagation. As for CFB, OFB modes, there is an extra parameter: 'feedback size'. You can treat it as the result size of each AES block process. That means, CFB 8-bits mode should be about 16 times slower than CFB 128-bits mode. And also, you can do stream ciphers by using the CFB 8-bits mode.
You can reference the EfAesLib.pdf in the package for details about how the different block modes work.
| Encode/Decode with same process | Need Initial Vector | Chain process | |
|---|---|---|---|
|
ECB |
X |
X |
X |
|
CBC |
X |
O |
O |
|
PCBC |
X |
O |
O |
|
CFB |
O |
O |
O |
|
OFB |
O |
O |
O |
|
CRT |
O |
O |
O |
AES always needs a 128-bit key to encrypt/decrypt. But it is also combined with an initial vector to work with, except in ECB mode. Each bit of the initial vector you use will double the possibilities of encrypted text from a given plain text, which means more safety.
EfAesLib supports ECB, CBC, PCBC, OFB, CFB, CRT block modes, and support OFB,CFB mode with [1..16] bytes feedback size. It also supports in-place encryption/decryption in each mode (source and destination buffer are the same).
The following sample uses Counter mode to encode a file:
#include "EfAes.h"
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char * argv[])
{
unsigned char key[16]={
0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88
};
unsigned char vector[16]={
0x1f,0x32,0x43,0x51,0x56,0x98,0xaf,0xed,
0xab,0xc8,0x21,0x45,0x63,0x72,0xac,0xfc
};
unsigned char buff[4096];
int rd_fd,wr_fd, rdsz;
AesCtx context;
AesSetKey( &context , AES_KEY_128BIT ,BLOCKMODE_CRT, key , vector );
rd_fd = open("test.dat", O_RDONLY);
wr_fd = open("test.encoded",O_WRONLY | O_CREAT);
setmode(rd_fd,O_BINARY);
setmode(wr_fd,O_BINARY);
while( (rdsz = read(rd_fd, buff ,4096)) > 0 )
{
// before last block , the block size
// should always be the multiply of 16
// the last block should be handled
// if the size is not a multiply of 16
AesEncryptCRT(&context , buff, buff, rdsz );
rdsz = AesRoundSize( rdsz, 16);
write( wr_fd , buff , rdsz );
}
close(rd_fd);
close(wr_fd);
}
The use of the AesCtx structure is mainly designed for thread issues. Each encryption session should have its own AesCtx. The EfAesLib APIs will always pad 0 to input data whose size is not a multiple of 16, or a multiple of the feedback size in the CFB, OFB modes.
Optimization
There are pre-defined functions in the AES algorithm. The first step, also proposed in the Wiki, is to combineSubBytes, ShiftRows with MixColumns. The follow is my sample implementation:
void SubAndShiftAndMixRound(uint8 * pState ,uint32 * pRoundKey , uint32 * pOutput)
{
uint32 a1,a2,a3,a4; a1=pState[0];
a2=pState[5];
a3=pState[10];
a4=pState[15]; *pOutput++ =
((SboxXTime2[a1] ^ SboxXTime3[a2] ^ FSB[a3] ^
FSB[a4]) |
((FSB[a1] ^ SboxXTime2[a2] ^ SboxXTime3[a3] ^
FSB[a4]) << 8) |
((FSB[a1] ^ FSB[a2] ^ SboxXTime2[a3] ^
SboxXTime3[a4]) << 16 )|
((SboxXTime3[a1] ^ FSB[a2] ^ FSB[a3] ^
SboxXTime2[a4]) << 24))^ *pRoundKey++; ...........
}
In the second step, notice the horizontal direction of a1, a2, a3, a4. We can reduce this by using a pre-build lookup table for each column.
for(i=0;i<256;i++)
{
TestTable1[i]=SboxXTime2[i] | FSB_8[i] | FSB_16[i] | SboxXTime3_24[i];
TestTable2[i]=SboxXTime3[i] | SboxXTime2_8[i] | FSB_16[i] | FSB_24[i];
TestTable3[i]=FSB[i] | SboxXTime3_8[i] | SboxXTime2_16[i] | FSB_24[i];
TestTable4[i]=FSB[i] | FSB_8[i] | SboxXTime3_16[i] | SboxXTime2_24[i];
}
The code in step one will be optimized to:
void SubAndShiftAndMixRound(uint8 * pState ,uint32 * pRoundKey , uint32 * pOutput)
{
uint32 a1,a2,a3,a4; a1=pState[0];
a2=pState[5];
a3=pState[10];
a4=pState[15]; *pOutput++ = TestTable1[a1] ^ TestTable2[a2] ^ TestTable3[a3] ^
TestTable4[a4] ^ *pRoundKey++;
...........
}
In the third step, notice a1=pState[0],a2=pState[5],a3=pState[10],a4=pState[15]; it is slow in the 32-bit architecture. We can change it to a 32-bit access and XOR the sequence.
Performance
The best performance EfAesLib has is 10M bytes in 78 milliseconds with my Pentium IV 3.0Ghz computer.
Reference
The official document: http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf.
The Wiki
History
v2.0: Extended the library to 128/192/256 bits key length, and also added a 64 bit DLL in addition.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
A Fast and Easy to Use AES Library的更多相关文章
- CoRR 2018 | Horovod: Fast and Easy Distributed Deep Learning in Tensorflow
将深度学习模型的训练从单GPU扩展到多GPU主要面临以下问题:(1)训练框架必须支持GPU间的通信,(2)用户必须更改大量代码以使用多GPU进行训练.为了克服这些问题,本文提出了Horovod,它通过 ...
- How to distribute your own Android library through jCenter and Maven Central from Android Studio
In Android Studio, if you wish to include any library to your application. You could just simply add ...
- Extending sparklyr to Compute Cost for K-means on YARN Cluster with Spark ML Library
Machine and statistical learning wizards are becoming more eager to perform analysis with Spark MLli ...
- [Yarn] Use Yarn to Create an Alternative Import Name of an Installed Library
In this lesson we'll show how to use yarn to alias the names of same npm libraries but install diffe ...
- Pyhton开源框架(加强版)
info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...
- Awesome C/C++
Awesome C/C++ A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. In ...
- C/C++ 框架,类库,资源集合
很棒的 C/C++ 框架,类库,资源集合. Awesome C/C++ Standard Libraries Frameworks Artificial Intelligence Asynchrono ...
- awesome cpp
https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...
- 【干货】国外程序员整理的 C++ 资源大全【转】
来自 https://github.com/fffaraz/awesome-cpp A curated list of awesome C/C++ frameworks, libraries, res ...
随机推荐
- 关于本科毕业设计期间对数据挖掘工具rapidminer的使用体验和心得,案例分享
1.前言:本科生毕业设计有好多人说没有什么用处,自己又做不出来什么新东西,全是抄抄改改的,浪费大家时间.但是对此事我的态度不同,我觉得就像我们小时候玩过家家一样,别的孩子都在玩,我不参与进去显得会有遗 ...
- 修正 FreeBSD 字体锯齿问题
如果你给 FreeBSD 安装完图形界面,一登录就被满屏幕不论中英全是锯齿且残缺不堪入目的文字吓了一跳,那一定是安装了文泉驿字体.先不必急着卸载文泉驿,只需简单修改相关配置即可恢复正常显示.这是因为文 ...
- Super Mario(线段树离线区间k值)
以前见过这题,没做出来,知道是离线处理,这次仔细想了下, 首先把出现的高度都map离散化一下,以离散化出来的数目g建树,把每个位置都开俩个vector,一个存以这个位置为L的询问,一个存以这个位置为R ...
- #118. 【UR #8】赴京赶考
链接:#118. [UR #8]赴京赶考 高中,高中,短暂的三年.NOI是高中结业考试,而高考在每年暑假举行. 高二暑假,这是你最后一次参加高考的机会.你已经为了高考停课很久了,OI的知识很久没管了. ...
- asp。Net 页面传值
00.引言 Web页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留.在用ASP.NET 设计开发一个Web系统时, 遇到一 ...
- 理解C#系列 / 前言
前言 索引 写什么? 为什么写? 怎么写? 写什么? 写和C#编程相关的知识. 写知识的定义,附加对知识的理解. 写知识的作用,使用的场景,使用的条件. 写知识的本质,技术的结构,工作的原理. 写知识 ...
- HDU4576 Robot(概率)
题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 多组输入n,m,l,r.表示在一个环上有n个格子.接下来输入m个w表示连续的一段 ...
- JQ中的问题
$(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...
- office word excel等图标显示异常
1.查看注册表:查看参数对应的路径被删除,计算机搜索新的文件路径更改路径即可.以此类推~ 计算机\HKEY_CLASSES_ROOT\Excel.Sheet.12\DefaultIcon 正常exce ...
- Python基础篇 -- if while 语句
2.7 if语句 # 单纯if if 条件: 代码块 当条件成立,执行代码块 # 二选一 if 条件: 代码块1 else: 代码块2 #当条件为真,执行代码块1,否则执行代码块2 # 多选一 没有e ...