OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / libcrypto / libaes / aes.h
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.
8 //
9 // Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
10 //
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 
16 // 16, 24 and 32.
17
18 /*
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.
33  */
34
35 #ifndef _AES_H
36 #define _AES_H
37
38 #if defined(__linux__) && defined(__KERNEL__)
39 #  include <linux/types.h>
40 #else 
41 #  include <sys/types.h>
42 #endif
43 #include <linux/config.h>
44
45 // CONFIGURATION OPTIONS (see also aes.c)
46 //
47 // Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
48 // leave this undefined for dynamically variable block size (this will
49 // result in much slower code).
50 // IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
51 // left undefined a slower version providing variable block length is compiled
52
53 #define AES_BLOCK_SIZE  16
54
55 // The number of key schedule words for different block and key lengths
56 // allowing for method of computation which requires the length to be a
57 // multiple of the key length
58 //
59 // Nk =       4   6   8
60 //        -------------
61 // Nb = 4 |  60  60  64
62 //      6 |  96  90  96
63 //      8 | 120 120 120
64
65 #if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
66 #define AES_KS_LENGTH   120
67 #define AES_RC_LENGTH    29
68 #else
69 #define AES_KS_LENGTH   4 * AES_BLOCK_SIZE
70 #define AES_RC_LENGTH   (9 * AES_BLOCK_SIZE) / 8 - 8
71 #endif
72
73 typedef struct
74 {
75     u_int32_t    aes_Nkey;      // the number of words in the key input block
76     u_int32_t    aes_Nrnd;      // the number of cipher rounds
77     u_int32_t    aes_e_key[AES_KS_LENGTH];   // the encryption key schedule
78     u_int32_t    aes_d_key[AES_KS_LENGTH];   // the decryption key schedule
79 #if !defined(AES_BLOCK_SIZE)
80     u_int32_t    aes_Ncol;      // the number of columns in the cipher state
81 #endif
82 } aes_context;
83
84 // THE CIPHER INTERFACE
85
86 #if !defined(AES_BLOCK_SIZE)
87 extern void aes_set_blk(aes_context *, const int);
88 #endif
89 #ifdef CONFIG_IXP4XX_CRYPTO
90 extern void ike_aes_set_key(aes_context *, const unsigned char [], const int, const int);
91 #endif /* CONFIG_IXP4XX_CRYPTO */
92 extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
93 extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
94 extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
95
96 // The block length inputs to aes_set_block and aes_set_key are in numbers
97 // of bytes or bits.  The calls to subroutines must be made in the above
98 // order but multiple calls can be made without repeating earlier calls
99 // if their parameters have not changed.
100
101 #endif  // _AES_H