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 combineSubBytesShiftRows 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)

最新文章

  1. android-sdk 开发连接不上
  2. 常用shell命令中你所不熟悉的参数
  3. Android自定义控件----RadioGroup实现APP首页底部Tab的切换
  4. 学习笔记——Maven settings.xml 配置详解
  5. Codevs 1689 建造高塔
  6. firebug js版
  7. web desktop在线演示
  8. 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo
  9. ZOJ 1967 POJ 2570 Fiber Network
  10. 拓扑排序&amp;关键路径
  11. intellij-添加文档注释模板
  12. [原]开源的视频转换器,支持gpu,绝对好用ffmpeg的GUI==》dmMediaConverter最新版本2.3
  13. 使用java开发spark的wordcount程序(多种实现)
  14. [dpdk] service core
  15. HBase源码分析之WAL
  16. 重新设置Linux的IP地址(该操作会永久更改ip地址)
  17. 玩DNF开启NVIDIA独显的方法
  18. Android shell command execute Demo
  19. Qt5_vs2013_error_C2001: 常量中有换行符__ZC
  20. spring框架 构造方法注入

热门文章

  1. PAT甲级——1118 Birds in Forest (并查集)
  2. [POJ1463] Strategic game
  3. 从navicat for mysql导出数据库语句时应该加上的两条语句
  4. [转] boost:lexical_cast用法
  5. IDEA自定义设置快捷键输出你想要的语句!
  6. JDk安装及环境变量的配置
  7. Spring框架学习-Spring的AOP概念详解
  8. hihocoder1032 最长回文子串
  9. cf580E. Kefa and Watch(线段树维护字符串hash)
  10. WebService学习之旅(三)JAX-WS与Spring整合发布WebService