1
// I retain copyright in this code but I encourage its free use provided
2
// that I don't carry any responsibility for the results. I am especially
3
// happy to see it used in free and open source software. If you do use
4
// it I would appreciate an acknowledgement of its origin in the code or
5
// the product that results and I would also appreciate knowing a little
6
// about the use to which it is being put. I am grateful to Frank Yellin
7
// for some ideas that are used in this implementation.
9
// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
11
// This is an implementation of the AES encryption algorithm (Rijndael)
12
// designed by Joan Daemen and Vincent Rijmen. This version is designed
13
// to provide both fixed and dynamic block and key lengths and can also
14
// run with either big or little endian internal byte order (see aes.h).
15
// It inputs block and key lengths in bytes with the legal values being
19
* Modified by Jari Ruusu, May 1 2001
20
* - Fixed some compile warnings, code was ok but gcc warned anyway.
21
* - Changed basic types: byte -> unsigned char, word -> u_int32_t
22
* - Major name space cleanup: Names visible to outside now begin
23
* with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
24
* - Removed C++ and DLL support as part of name space cleanup.
25
* - Eliminated unnecessary recomputation of tables. (actual bug fix)
26
* - Merged precomputed constant tables to aes.c file.
27
* - Removed data alignment restrictions for portability reasons.
28
* - Made block and key lengths accept bit count (128/192/256)
29
* as well byte count (16/24/32).
30
* - Removed all error checks. This change also eliminated the need
31
* to preinitialize the context struct to zero.
32
* - Removed some totally unused constants.
38
#if defined(__linux__) && defined(__KERNEL__)
39
# include <linux/types.h>
41
# include <sys/types.h>
44
// CONFIGURATION OPTIONS (see also aes.c)
46
// Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
47
// leave this undefined for dynamically variable block size (this will
48
// result in much slower code).
49
// IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
50
// left undefined a slower version providing variable block length is compiled
52
#define AES_BLOCK_SIZE 16
54
// The number of key schedule words for different block and key lengths
55
// allowing for method of computation which requires the length to be a
56
// multiple of the key length
64
#if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
65
#define AES_KS_LENGTH 120
66
#define AES_RC_LENGTH 29
68
#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
69
#define AES_RC_LENGTH (9 * AES_BLOCK_SIZE) / 8 - 8
74
u_int32_t aes_Nkey; // the number of words in the key input block
75
u_int32_t aes_Nrnd; // the number of cipher rounds
76
u_int32_t aes_e_key[AES_KS_LENGTH]; // the encryption key schedule
77
u_int32_t aes_d_key[AES_KS_LENGTH]; // the decryption key schedule
78
#if !defined(AES_BLOCK_SIZE)
79
u_int32_t aes_Ncol; // the number of columns in the cipher state
83
// THE CIPHER INTERFACE
85
#if !defined(AES_BLOCK_SIZE)
86
extern void aes_set_blk(aes_context *, const int);
88
extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
89
extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
90
extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
92
// The block length inputs to aes_set_block and aes_set_key are in numbers
93
// of bytes or bits. The calls to subroutines must be made in the above
94
// order but multiple calls can be made without repeating earlier calls
95
// if their parameters have not changed.