?? aesopt.h
字號(hào):
/*
---------------------------------------------------------------------------
Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
All rights reserved.
LICENSE TERMS
The free distribution and use of this software in both source and binary
form is allowed (with or without changes) provided that:
1. distributions of this source code include the above copyright
notice, this list of conditions and the following disclaimer;
2. distributions in binary form include the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other associated materials;
3. the copyright holder's name is not used to endorse products
built using this software without specific written permission.
ALTERNATIVELY, provided that this notice is retained in full, this product
may be distributed under the terms of the GNU General Public License (GPL),
in which case the provisions of the GPL apply INSTEAD OF those given above.
DISCLAIMER
This software is provided 'as is' with no explicit or implied warranties
in respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 1/08/2003
My thanks go to Dag Arne Osvik for devising the schemes used here for key
length derivation from the form of the key schedule
This file contains the compilation options for AES (Rijndael) and code
that is common across encryption, key scheduling and table generation.
OPERATION
These source code files implement the AES algorithm Rijndael designed by
Joan Daemen and Vincent Rijmen. This version is designed for the standard
block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
and 32 bytes).
This version is designed for flexibility and speed using operations on
32-bit words rather than operations on bytes. It can be compiled with
either big or little endian internal byte order but is faster when the
native byte order for the processor is used.
THE CIPHER INTERFACE
The cipher interface is implemented as an array of bytes in which lower
AES bit sequence indexes map to higher numeric significance within bytes.
aes_08t (an unsigned 8-bit type)
aes_32t (an unsigned 32-bit type)
struct aes_encrypt_ctx (structure for the cipher encryption context)
struct aes_decrypt_ctx (structure for the cipher decryption context)
aes_rval the function return type
C subroutine calls:
aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]);
aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]);
aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]);
aes_rval aes_encrypt(const void *in_blk,
void *out_blk, const aes_encrypt_ctx cx[1]);
aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]);
aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]);
aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]);
aes_rval aes_decrypt(const void *in_blk,
void *out_blk, const aes_decrypt_ctx cx[1]);
IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that
you call genTabs() before AES is used so that the tables are initialised.
C++ aes class subroutines:
Class AESencrypt for encryption
Construtors:
AESencrypt(void)
AESencrypt(const void *in_key) - 128 bit key
Members:
void key128(const void *in_key)
void key192(const void *in_key)
void key256(const void *in_key)
void encrypt(const void *in_blk, void *out_blk) const
Class AESdecrypt for encryption
Construtors:
AESdecrypt(void)
AESdecrypt(const void *in_key) - 128 bit key
Members:
void key128(const void *in_key)
void key192(const void *in_key)
void key256(const void *in_key)
void decrypt(const void *in_blk, void *out_blk) const
COMPILATION
The files used to provide AES (Rijndael) are
a. aes.h for the definitions needed for use in C.
b. aescpp.h for the definitions needed for use in C++.
c. aesopt.h for setting compilation options (also includes common code).
d. aescrypt.c for encryption and decrytpion, or
e. aeskey.c for key scheduling.
f. aestab.c for table loading or generation.
g. aescrypt.asm for encryption and decryption using assembler code.
h. aescrypt.mmx.asm for encryption and decryption using MMX assembler.
To compile AES (Rijndael) for use in C code use aes.h and set the
defines here for the facilities you need (key lengths, encryption
and/or decryption). Do not define AES_DLL or AES_CPP. Set the options
for optimisations and table sizes here.
To compile AES (Rijndael) for use in in C++ code use aescpp.h but do
not define AES_DLL
To compile AES (Rijndael) in C as a Dynamic Link Library DLL) use
aes.h and include the AES_DLL define.
CONFIGURATION OPTIONS (here and in aes.h)
a. set AES_DLL in aes.h if AES (Rijndael) is to be compiled as a DLL
b. You may need to set PLATFORM_BYTE_ORDER to define the byte order.
c. If you want the code to run in a specific internal byte order, then
ALGORITHM_BYTE_ORDER must be set accordingly.
d. set other configuration options decribed below.
*/
#ifndef _AESOPT_H
#define _AESOPT_H
#include "aes.h"
/* START OF CONFIGURATION OPTIONS
USE OF DEFINES
Later in this section there are a number of defines that control the
operation of the code. In each section, the purpose of each define is
explained so that the relevant form can be included or excluded by
setting either 1's or 0's respectively on the branches of the related
#if clauses.
*/
/* DO NOT CHANGE THE FOLLOWING EIGHT DEFINES */
#define NO_TABLES 0
#define ONE_TABLE 1
#define FOUR_TABLES 4
#define NONE 0
#define PARTIAL 1
#define FULL 2
#define AES_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
#define AES_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
/* 1. PLATFORM SPECIFIC INCLUDES */
#if defined( __FreeBSD__ ) || defined( __OpenBSD__ )
# include <sys/endian.h>
#elif defined( BSD ) && ( BSD >= 199103 )
# include <machine/endian.h>
#elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
# include <endian.h>
# include <byteswap.h>
#elif defined( linux )
# include <endian.h>
#endif
#if defined(bswap32)
#define aes_sw32 bswap32
#elif defined(bswap_32)
#define aes_sw32 bswap_32
#endif
/* 2. BYTE ORDER IN 32-BIT WORDS
To obtain the highest speed on processors with 32-bit words, this code
needs to determine the order in which bytes are packed into such words.
The following block of code is an attempt to capture the most obvious
ways in which various environemnts define byte order. It may well fail,
in which case the definitions will need to be set by editing at the
points marked **** EDIT HERE IF NECESSARY **** below. My thanks to
Peter Gutmann for some of these defines.
*/
#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
defined( vax ) || defined( vms ) || defined( VMS ) || \
defined( __VMS )
#define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
#endif
#if defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )
#define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
#endif
/* if the platform is not known, try to find its byte order from */
/* definitions in the headers included earlier */
#if !defined(PLATFORM_BYTE_ORDER)
#if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
# if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# if defined(BYTE_ORDER)
# if (BYTE_ORDER == LITTLE_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif (BYTE_ORDER == BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
# endif
# elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
#elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
# if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
# if defined(_BYTE_ORDER)
# if (_BYTE_ORDER == _LITTLE_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif (_BYTE_ORDER == _BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
# endif
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
#elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)
# if defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
# if defined(__BYTE_ORDER__)
# if (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif (__BYTE_ORDER__ == __BIG_ENDIAN__)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
# endif
# elif defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
# elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
# endif
#elif 0 /* **** EDIT HERE IF NECESSARY **** */
#define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN
#elif 0 /* **** EDIT HERE IF NECESSARY **** */
#define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN
#else
# error Please edit aesopt.h (line 263 or 266) to set the platform byte order
#endif
#endif
/* 3. FUNCTIONS REQUIRED
This implementation provides subroutines for encryption, decryption
and for setting the three key lengths (separately) for encryption
and decryption. When the assembler code is not being used the following
definition blocks allow the selection of the routines that are to be
included in the compilation.
*/
#ifdef AES_ENCRYPT
#define ENCRYPTION
#define ENCRYPTION_KEY_SCHEDULE
#endif
#ifdef AES_DECRYPT
#define DECRYPTION
#define DECRYPTION_KEY_SCHEDULE
#endif
/* 4. ASSEMBLER SUPPORT
This define (which can be on the command line) enables the use of the
assembler code routines for encryption and decryption with the C code
only providing key scheduling
*/
#if 0
#define AES_ASM
#endif
/* 5. BYTE ORDER WITHIN 32 BIT WORDS
The fundamental data processing units in Rijndael are 8-bit bytes. The
input, output and key input are all enumerated arrays of bytes in which
bytes are numbered starting at zero and increasing to one less than the
number of bytes in the array in question. This enumeration is only used
for naming bytes and does not imply any adjacency or order relationship
from one byte to another. When these inputs and outputs are considered
as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
In this implementation bits are numbered from 0 to 7 starting at the
numerically least significant end of each byte (bit n represents 2^n).
However, Rijndael can be implemented more efficiently using 32-bit
words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
into word[n]. While in principle these bytes can be assembled into words
in any positions, this implementation only supports the two formats in
which bytes in adjacent positions within words also have adjacent byte
numbers. This order is called big-endian if the lowest numbered bytes
in words have the highest numeric significance and little-endian if the
opposite applies.
This code can work in either order irrespective of the order used by the
machine on which it runs. Normally the internal byte order will be set
to the order of the processor on which the code is to be run but this
define can be used to reverse this in special situations
NOTE: Assembler code versions rely on PLATFORM_BYTE_ORDER being set
*/
#if 1 || defined(AES_ASM)
#define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
#elif 0
#define ALGORITHM_BYTE_ORDER AES_LITTLE_ENDIAN
#elif 0
#define ALGORITHM_BYTE_ORDER AES_BIG_ENDIAN
#else
#error The algorithm byte order is not defined
#endif
/* 6. FAST INPUT/OUTPUT OPERATIONS.
On some machines it is possible to improve speed by transferring the
bytes in the input and output arrays to and from the internal 32-bit
variables by addressing these arrays as if they are arrays of 32-bit
words. On some machines this will always be possible but there may
be a large performance penalty if the byte arrays are not aligned on
the normal word boundaries. On other machines this technique will
lead to memory access errors when such 32-bit word accesses are not
properly aligned. The option SAFE_IO avoids such problems but will
often be slower on those machines that support misaligned access
(especially so if care is taken to align the input and output byte
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -