Blame | Last modification | View Log | Download
An AES (Rijndael) Implementation in C/C++ (as specified in FIPS-197)====================================================================Changes in this Version (14/11/2013)====================================1. Added the ability to use Intel's hardware support for AESon Windows using Microsoft Visual Studio.2. Added the include 'stdint.h' and used the uint<xx>_t insteadof the old uint_<xx>t (e.g. uint_32t is now uint32_t).3. Added a missing .text directive in aes_x86_v2.asm that causedruntime errors in one build configuration.Changes in this Version (16/04/2007)====================================These changes remove errors in the VC++ build files and add someimprovements in file naming consitency and portability. There areno changes to overcome reported bugs in the code.1. gen_tabs() has been renamed to aes_init() to better decribe itsfunction to those not familiar with AES internals.2. via_ace.h has been renamed to aes_via_ace.h.3. Minor changes have been made to aestab.h and aestab.c to enableall the code to be compiled in either C or C++.4. The code for detecting memory alignment in aesmdoes.c has beensimplified and a new routine has been added:aes_test_alignment_detection()to check that the aligment test is likely to be correct.5. The addition of support for Structured Exception Handling (SEH)to YASM (well done Peter and Michael!) has allowed the AMD64x64 assembler code to be changed to comply with SEH requriements.6. Corrections to build files (for win32 debug build).Overview========This code implements AES for both 32 and 64 bit systems with optionalassembler support for x86 and AMD64/EM64T (but optimised for AMD64).The basic AES source code files are as follows:aes.h the header file needed to use AES in Caescpp.h the header file required with to use AES in C++aesopt.h the header file for setting options (and some common code)aestab.h the header file for the AES table declarationaescrypt.c the main C source code file for encryption and decryptionaeskey.c the main C source code file for the key scheduleaestab.c the main file for the AES tablesbrg_types.h a header defining some standard types and DLL definesbrg_endian.h a header containing code to detect or define endiannessaes_x86_v1.asm x86 assembler (YASM) alternative to aescrypt.c usinglarge tablesaes_x86_v2.asm x86 assembler (YASM) alternative to aescrypt.c usingcompressed tablesaes_amd64.asm AMD64 assembler (YASM) alternative to aescrypt.c usingcompressed tablesIn addition AES modes are implemented in the files:aes_modes.c AES modes with optional support for VIA ACE detection and useaes_via_ace.h the header file for VIA ACE supportand Intel hardware support for AES (AES_NI) is implemented in the filesaes_ni.h defines for AES_NI implementationaes_ni.c the AES_NI implementationOther associated files for testing and support are:aesaux.h header for auxilliary routines for testsingaesaux.c auxilliary routines for testsingtaestst.h header file for setting the testing environmentrdtsc.h a header file that provides access to the Time Stamp Counteraestst.c a simple test program for quick tests of the AES codeaesgav.c a program to generate and verify the test vector filesaesrav.c a program to verify output against the test vector filesaestmr.c a program to time the code on x86 systemsmodetest.c a program to test the AES modes supportvbxam.doc a demonstration of AES DLL use from Visual Basic in Microsoft Wordvb.txt Visual Basic code from the above example (win32 only)aesxam.c an example of AES usetablegen.c a program to generate a simplified 'aestab.c' file foruse with compilers that find aestab.c too complexyasm.rules the YASM build rules file for Microsoft Visual Studio 2005via_ace.txt describes support for the VIA ACE cryptography engineaes.txt this fileBuilding The AES Libraries--------------------------A. Versions-----------The code can be used to build static and dynamic libraries, each in fiveversions:Key scheduling code in C, encrypt/decrypt in:C C source code (win32 and x64)ASM_X86_V1C large table x86 assembler code (win32)ASM_X86_V2C compressed table x86 assembler code (win32)ASM_AMD64 compressed table x64 assembler code (x64)Key scheduling and encrypt/decrypt code in assembler:ASM_X86_V2 compressed table x86 assembler (win32)The C version can be compiled for Win32 or x64 whereas the x86 and x64assembler versions are for Win32 and x64 respectively.If Intel's hardware support for AES (AES_NI) is available, it can be usedwith either the C or the ASM_AMD64 version. If ASM_AMD64 is to be used, itis important that the define USE_INTEL_AES_IF_PRESENT in asm_amd64.asm isset to the same value as it has in aesopt.hB. YASM-------If you wish to use the x86 assembler files you will also need the YASM opensource x86 assembler (r1331 or later) for Windows which can be obtained from:http://www.tortall.net/projects/yasm/This assembler should be placed in the bin directory used by VC++, which, forVisual Stduio 2005, is typically:C:\Program Files (x86)\Microsoft Visual Studio 8\VC\binYou will also need to move the yasm.rules file from this distribution intothe directory where Visual Studio 2005 expects to find it, which is typically:C:\Program Files (x86)\Microsoft Visual Studio 8\VC\VCProjectDefaultsAlternatively you can configure the path for rules files within Visual Studio.C. Configuration----------------The following configurations are available as projects for Visual Studiobut the following descriptions should allow them to be built in other x86environmentslib_generic_c Win32 and x64headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.h(+ aes_ni.h for AES_NI)C source: aescrypt.c, aeskey.c, aestab.c, aes_modes.c(+ aes_ni.c for AES_NI)definesdll_generic_c Win32 and x64headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.h(+ aes_ni.h for AES_NI)C source: aescrypt.c, aeskey.c, aestab.c, aes_modes.c(+ aes_ni.c for AES_NI)defines DLL_EXPORTlib_asm_x86_v1c Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aeskey.c, aestab.c, aes_modes.cx86 assembler: aes_x86_v1.asmdefines ASM_X86_V1C (set for C and assembler files)dll_asm_x86_v1c Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aeskey.c, aestab.c, aes_modes.cx86 assembler: aes_x86_v1.asmdefines DLL_EXPORT, ASM_X86_V1C (set for C and assembler files)lib_asm_x86_v2c Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aeskey.c, aestab.c, aes_modes.cx86 assembler: aes_x86_v2.asmdefines ASM_X86_V2C (set for C and assembler files)dll_asm_x86_v2c Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aeskey.c, aestab.c, aes_modes.cx86 assembler: aes_x86_v1.asmdefines DLL_EXPORT, ASM_X86_V2C (set for C and assembler files)lib_asm_x86_v2 Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aes_modes.cx86 assembler: aes_x86_v1.asmdefines ASM_X86_V2 (set for C and assembler files)dll_asm_x86_v2 Win32headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.hC source: aes_modes.cx86 assembler: aes_x86_v1.asmdefines DLL_EXPORT, ASM_AMD64_C (set for C and assembler files)lib_asm_amd64_c x64headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.h(+ aes_ni.h for AES_NI)C source: aes_modes.c (+ aes_ni.c for AES_NI)x86 assembler: aes_amd64.asmdefines ASM_X86_V2 (set for C and assembler files)dll_asm_amd64_c x64headers: aes.h, aesopt.h, aestab.h, brg_endian.h, tdefs.h(+ aes_ni.h for AES_NI)C source: aes_modes.c (+ aes_ni.c for AES_NI)x86 assembler: aes_amd64.asmdefines DLL_EXPORT, ASM_AMD64_C (set for C and assembler files)Notes:ASM_X86_V1C is defined if using the version 1 assembler code (aescrypt1.asm).The defines in the assember file must match those in aes.h andaesopt.h). Also remember to include/exclude the right assemblerand C files in the build to avoid undefined or multiply definedsymbols - include aes_x86_v1.asm and exclude aescrypt.cASM_X86_V2 is defined if using the version 2 assembler code (aes_x86_v2.asm).This version provides a full, self contained assembler versionand does not use any C source code files except for the mutipleblock encryption modes that are provided by aes_modes.c. The defineASM_X86_V2 must be set on the YASM command line (or in aes_x86_v2.asm)to use this version and all C files except aec_modes.c and, for theDLL build, aestab.c must be excluded from the build.ASM_X86_V2C is defined when using the version 2 assembler code (aes_x86_v2.asm)with faster key scheduling provided by the in C code (the options inthe assember file must match those in aes.h and aesopt.h). In thiscase aeskey.c and aestab.c are needed with aes_x86_v2.asm and thedefine ASM_X86_V2C must be set for both the C files and foraes_x86_v2.asm in the build commands(or in aesopt.h and aes_x86_v2.asm).Include aes_x86_v2.asm, aeskey.c and aestab.c, exclude aescrypt.c forthis option.ASM_AMD64_C is defined when using the AMD64 assembly code because the C keyscheduling is used in this case.DLL_EXPORT must be defined to generate the DLL version of the code andto run tests on itDLL_IMPORT must be defined to use the DLL version of the code in anapplication programDirectories the paths for the various directories for test vector input andoutput have to be set in aestst.hVIA ACE see the via_ace.txt for this itemStatic The static libraries are named:Librariesaes_lib_generic_c.libaes_lib_asm_x86_v1c.libaes_lib_asm_x86_v2.libaes_lib_asm_x86_v2c.libaes_lib_asm_amd64_c.liband placed in one of the the directories:lib\win32\release\lib\win32\debug\lib\x64\release\lib\x64\debug\in the aes root directory depending on the platform(win32 orx64) and the build (release or debug). After any of these isbuilt it is then copied into the aes\lib directory, which isthe library location that is subsequently used for testing.Hence testing is always for the last static library built.Dynamic These libraries are named:Librariesaes_lib_generic_c.dllaes_lib_asm_x86_v1c.dllaes_lib_asm_x86_v2.dllaes_lib_asm_x86_v2c.dllaes_lib_asm_amd64_c.dlland placed in one of the the directories:dll\win32\release\dll\win32\debug\dll\x64\release\dll\x64\debug\in the aes root directory depending on the platform(win32 orx64) and the build (release or debug). Each DLL library:aes_<ext>.dllhas three associated files:aes_dll_<ext>.lib the library file for implicit linkingaes_dll_<ext>.exp the exports fileaes_dll_<ext>.pdb the symbol fileAfter any DLL is built it and its three related files are thencopied to the aes\dll directory, which is the library locationused in subsequent testing. Hence testing is always for thelast DLL built.D. Testing----------These tests require that the test vector files are placed in the 'testvals'subdirectory. If the AES Algorithm Validation Suite tests are used thenthe *.fax files need to be put in the 'testvals\fax' subdirectory. This iscovered in more detail below.The projects test_lib and time_lib are used to test and time the last staticlibrary built. They use the files:test_lib: Win32 (x64 for the C and AMD64 versions)headers: aes.h, aescpp.h, brg_types.h, aesaux.h and aestst.hC source: aesaux.c, aesrav.cdefines:time_lib: Win32 (x64 for the C and AMD64 versions)headers: aes.h, aescpp.h, brg_types.h, aesaux.h, aestst.h and rdtsc.hC source: aesaux.c, aestmr.cdefines:The projects test_dll and time_dll are used to test and time the last DLLbuilt. These use the files:test_dll: Win32 (x64 for the C and AMD64 versions)headers: aes.h, aescpp.h, brg_types.h, aesaux.h and aestst.hC source: aesaux.c, aesrav.cdefines: DLL_IMPORTtime_dll: Win32 (x64 for the C and AMD64 versions)headers: aes.h, aescpp.h, brg_types.h, aesaux.h aestst.h and rdtsc.hC source: aesaux.c, aestmr.cdefines: DLL_IMPORTand link to the DLL using dynamic (run-time) linking. However, if the lib fileassociated with the DLL is linked into this project and the symbol DYNAMIC_LINKin aestst.h is left undefined, then implicit linking will be usedThe above tests take command line arguments that determine which test are runas follows:test_lib /t:[knec] /k:[468]test_dll /t:[knec] /k:[468]where the symbols in square brackets can be used in any combination (withoutthe brackets) and have the following meanings:/t:[knec] selects which tests are used/k:[468] selects the key lengths used/c compares output with reference (see later)k: generate ECB Known Answer Test filesn: generate ECB Known Answer Test files (new)e: generate ECB Monte Carlo Test filesc: generate CBC Monte Carlo Test filesand the characters giving the lengths are digits representing the key lengthsin 32-bit units (4, 6, 8 for lengths of 128, 192 or 256 bits respectively).The project test_modes tests the AES modes. It uses the files:test_modes: Win32 or x64headers: aes.h, aescpp.h, brg_types.h, aesaux,h and aestst.hC source: aesaux.c, modetest.cdefines: none for static library test, DLL_IMPORT for DLL testwhich again links to the last library built.E. Other Applications---------------------These are:gen_tests builds the test_vector files. The commad line isgen_tests /t:knec /k:468 /cas described earliertest_aes_avs run the AES Algorithm Validation Suite tests forECB, CBC, CFB and OFB modesgen_tables builds a simple version of aes_tab.c (in aestab2.c)for compilers that cannot handle the normal versionaes_example provides an example of AES useThese applications are linked to the last static library built or, ifDLL_IMPORT is defined during compilation, to the last DLL built.F. Use of the VIA ACE Cryptography Engine (x86 only)----------------------------------------------------The use of the code with the VIA ACE cryptography engine in described in thefile via_ace.txt. In outline aes_modes.c is used and USE_VIA_ACE_IF_PRESENTis defined either in section 2 of aesopt.h or as a compilation option in VisualStudio. If in addition ASSUME_VIA_ACE_PRESENT is also defined then all normalAES code will be removed if not needed to support VIA ACE use. If VIA ACEsupport is needed and AES assembler is being used only the ASM_X86_V1C andASM_X86_V2C versions should be used since ASM_X86_V2 and ASM_AMD64 do notsupport the VIA ACE engine.G. The AES Test Vector Files----------------------------These files fall in the following groups (where <nn> is a two digitnumber):1. ecbvk<nn>.txt ECB vectors with variable key2. ecbvt<nn>.txt ECB vectors with variable text3. ecbnk<nn>.txt new ECB vectors with variable key4. ecbnt<nn>.txt new ECB vectors with variable text5. ecbme<nn>.txt ECB monte carlo encryption test vectors6. ecbmd<nn>.txt ECB monte carlo decryption test vectors7. cbcme<nn>.txt CBC monte carlo encryption test vectors8. cbcmd<nn>.txt CBC monte carlo decryption test vectorsThe first digit of the numeric suffix on the filename gives the block sizein 32 bit units and the second numeric digit gives the key size. For example,the file ecbvk44.txt provides the test vectors for ECB encryption with a 128bit block size and a 128 bit key size. The test routines expect to find thesefiles in the 'testvals' subdirectory within the aes root directory. The'outvals' subdirectory is used for outputs that are compared with the filesin 'testvals'. Note that the monte carlo test vectors are the result ofapplying AES iteratively 10000 times, not just once.The AES Algorithm Validation Suite tests can be run for ECB, CBC, CFB andOFB modes (CFB1 and CFB8 are not implemented). The test routine uses the*.fax test files, which should be placed in the 'testvals\fax' subdirectory.H. The Basic AES Calling Interface----------------------------------The basic AES code keeps its state in a context, there being differentcontexts for encryption and decryption:aes_encrypt_ctxaes_decrypt_ctxThe AES code is initialised with the callaes_init(void)although this is only essential if the option to generate the AES tables atrun-time has been set in the options (i.e.fixed tables are not being used).The AES encryption key is set by one of the calls:aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])or by:aes_encrypt_key(const unsigned char *key, int key_len,aes_encrypt_ctx cx[1])where the key length is set by 'key_len', which can be the length in bitsor bytes.Similarly, the AES decryption key is set by one of:aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])or by:aes_decrypt_key(const unsigned char *key, int key_len,aes_decrypt_ctx cx[1])Encryption and decryption for a single 16 byte block is then achieved using:aes_encrypt(const unsigned char *in, unsigned char *out,const aes_encrypt_ctx cx[1])aes_decrypt(const unsigned char *in, unsigned char *out,const aes_decrypt_ctx cx[1])The above subroutines return a value of EXIT_SUCCESS or EXIT_FAILUREdepending on whether the operation succeeded or failed.I. The Calling Interface for the AES Modes------------------------------------------The subroutines for the AES modes, ECB, CBC, CFB, OFB and CTR, each processblocks of variable length and can also be called several times to completesingle mode operations incrementally on long messages (or those messages,not all of which are available at the same time). The calls:aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,int len, const aes_encrypt_ctx cx[1])aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,int len, const aes_decrypt_ctx cx[1])for ECB operations and those for CBC:aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, const aes_encrypt_ctx cx[1])aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, const aes_decrypt_ctx cx[1])can only process blocks whose lengths are multiples of 16 bytes but the callsfor CFB, OFB and CTR mode operations:aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, aes_encrypt_ctx cx[1])aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, aes_encrypt_ctx cx[1])aes_ofb_encrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, aes_encrypt_ctx cx[1])aes_ofb_decrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *iv, aes_encrypt_ctx cx[1])aes_ctr_encrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1])aes_ctr_decrypt(const unsigned char *ibuf, unsigned char *obuf,int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1])can process blocks of any length. Note also that CFB, OFB and CTR mode calls onlyuse AES encryption contexts even during decryption operations.The calls CTR mode operations use a buffer (cbuf) which holds the counter valuetogether with a function parameter:void cbuf_inc(unsigned char *cbuf);that is ued to update the counter value after each 16 byte AES operation. Thecounter buffer is updated appropriately to allow for incremental operations.Please note the following IMPORTANT points about the AES mode subroutines:1. All modes are reset when a new AES key is set.2. Incremental calls to the different modes cannotbe mixed. If a change of mode is needed a newkey must be set or a reset must be issued (seebelow).3. For modes with IVs, the IV value is an input ANDan ouTput since it is updated after each call tothe value needed for any subsequent incrementalcall(s). If the mode is reset, the IV hence hasto be set (or reset) as well.4. ECB operations must be multiples of 16 bytesbut do not need to be reset for new operations.5. CBC operations must also be multiples of 16bytes and are reset for a new operation bysetting the IV.6. CFB, OFB and CTR mode must be reset by settinga new IV value AND by calling:aes_mode_reset(aes_encrypt_ctx cx[1])For CTR mode the cbuf value also has to be reset.7. CFB, OFB and CTR modes only use AES encryptionoperations and contexts and do not need AESdecrytpion operations.8. AES keys remain valid across resets and changesof mode (but encryption and decryption keys mustboth be set if they are needed).Brian Gladman 14/11/2013