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)

A Fast and Easy to Use AES Library的更多相关文章

  1. CoRR 2018 | Horovod: Fast and Easy Distributed Deep Learning in Tensorflow

    将深度学习模型的训练从单GPU扩展到多GPU主要面临以下问题:(1)训练框架必须支持GPU间的通信,(2)用户必须更改大量代码以使用多GPU进行训练.为了克服这些问题,本文提出了Horovod,它通过 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. Pyhton开源框架(加强版)

    info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...

  6. Awesome C/C++

    Awesome C/C++ A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. In ...

  7. C/C++ 框架,类库,资源集合

    很棒的 C/C++ 框架,类库,资源集合. Awesome C/C++ Standard Libraries Frameworks Artificial Intelligence Asynchrono ...

  8. awesome cpp

    https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...

  9. 【干货】国外程序员整理的 C++ 资源大全【转】

    来自 https://github.com/fffaraz/awesome-cpp A curated list of awesome C/C++ frameworks, libraries, res ...

随机推荐

  1. zh-cn、en-us、zh-tw等表示语言(文化)代码与国家地区对照表(最全的各国地区对照表)

    af 公用荷兰语 af-ZA 公用荷兰语 - 南非 sq 阿尔巴尼亚 sq-AL 阿尔巴尼亚 -阿尔巴尼亚 ar 阿拉伯语 ar-DZ 阿拉伯语 -阿尔及利亚 ar-BH 阿拉伯语 -巴林 ar-EG ...

  2. java isAssignableFrom instanceof 小结 专题

    一句话总结: isAssignableFrom()方法是从类继承的角度去判断,instanceof()方法是从实例继承的角度去判断. public native boolean isAssignabl ...

  3. zTree树插件动态加载

    需求: 由于项目中家谱图数据量超大,而一般加载方式是通过,页面加载时 zTree.init方法进行数据加载,将所有数据一次性加载到页面中.而在项目中家谱级别又非常广而深,成千上万级,因此一次加载,完全 ...

  4. storm trident的filter和函数

    目的:通过kafka输出的信息进行过滤,添加指定的字段后,进行打印 SentenceSpout: package Trident; import java.util.HashMap; import j ...

  5. Android.mk模板

    此文列出Android.mk的常用模板(部分内容源于多篇他人博客,这里不具体指出),如有错漏,还请在评论中指出,后期持续更新   #链接第三方动态库,在和部分android源码的编译中验证不过 LOC ...

  6. 【Web应用-FTP】FTP 容量显示说明

    现象描述 Azure 门户显示的文件系统存储容量跟网站本身的磁盘空间不符. 问题分析 Azure Web 应用的文件系统存储用量和网站本身有关,具体容量如下所示: 但目前门户预览中关于 FTP 的容量 ...

  7. XPath基本使用

    一.简介 1.什么是XPath  1)XPath是W3C的一个标准 2)XPath 是一门在 XML 文档中查找信息的语言. 3)XPath 用于在 XML 文档中通过元素和属性进行导航. 4)XPa ...

  8. COGS 1710. [POJ2406]字符串的幂

    ★☆   输入文件:powerstrings.in   输出文件:powerstrings.out   简单对比时间限制:3 s   内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...

  9. Asp.net Mvc 表单验证(气泡提示)

    将ASP.NET MVC或ASP.NET Core MVC的表单验证改成气泡提示: //新建一个js文件(如:jquery.validate.Bubble.js),在所有要验证的页面引用 (funct ...

  10. shell 简单脚本 2

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...