1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The redistribution and use of this software (with or without changes)
8  is allowed without the payment of fees or royalties provided that:
9 
10   1. source code distributions include the above copyright notice, this
11      list of conditions and the following disclaimer;
12 
13   2. binary distributions include the above copyright notice, this list
14      of conditions and the following disclaimer in their documentation;
15 
16   3. the name of the copyright holder is not used to endorse products
17      built using this software without specific written permission.
18 
19  DISCLAIMER
20 
21  This software is provided 'as is' with no explicit or implied warranties
22  in respect of its properties, including, but not limited to, correctness
23  and/or fitness for purpose.
24  ---------------------------------------------------------------------------
25  Issue 09/09/2006
26 
27  This is an AES implementation that uses only 8-bit byte operations on the
28  cipher state.
29  */
30 
31 #ifndef AES_H
32 #define AES_H
33 
34 #if 1
35 #define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule  */
36 #endif
37 #if 1
38 #define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule  */
39 #endif
40 #if 1
41 #define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
42 #endif
43 #if 1
44 #define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
45 #endif
46 #if 1
47 #define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
48 #endif
49 #if 1
50 #define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
51 #endif
52 
53 #define N_ROW 4
54 #define N_COL 4
55 #define N_BLOCK (N_ROW * N_COL)
56 #define N_MAX_ROUNDS 14
57 
58 typedef unsigned char uint_8t;
59 
60 typedef uint_8t return_type;
61 
62 /*  Warning: The key length for 256 bit keys overflows a byte
63     (see comment below)
64 */
65 
66 typedef uint_8t length_type;
67 
68 typedef struct {
69   uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
70   uint_8t rnd;
71 } aes_context;
72 
73 /*  The following calls are for a precomputed key schedule
74 
75     NOTE: If the length_type used for the key length is an
76     unsigned 8-bit character, a key length of 256 bits must
77     be entered as a length in bytes (valid inputs are hence
78     128, 192, 16, 24 and 32).
79 */
80 
81 #if defined(AES_ENC_PREKEYED) || defined(AES_DEC_PREKEYED)
82 
83 return_type aes_set_key(const unsigned char key[], length_type keylen, aes_context ctx[1]);
84 #endif
85 
86 #if defined(AES_ENC_PREKEYED)
87 
88 return_type aes_encrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
89                         const aes_context ctx[1]);
90 
91 return_type aes_cbc_encrypt(const unsigned char* in, unsigned char* out, int n_block,
92                             unsigned char iv[N_BLOCK], const aes_context ctx[1]);
93 #endif
94 
95 #if defined(AES_DEC_PREKEYED)
96 
97 return_type aes_decrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
98                         const aes_context ctx[1]);
99 
100 return_type aes_cbc_decrypt(const unsigned char* in, unsigned char* out, int n_block,
101                             unsigned char iv[N_BLOCK], const aes_context ctx[1]);
102 #endif
103 
104 /*  The following calls are for 'on the fly' keying.  In this case the
105     encryption and decryption keys are different.
106 
107     The encryption subroutines take a key in an array of bytes in
108     key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
109     192, and 256 bits respectively.  They then encrypts the input
110     data, in[] with this key and put the reult in the output array
111     out[].  In addition, the second key array, o_key[L], is used
112     to output the key that is needed by the decryption subroutine
113     to reverse the encryption operation.  The two key arrays can
114     be the same array but in this case the original key will be
115     overwritten.
116 
117     In the same way, the decryption subroutines output keys that
118     can be used to reverse their effect when used for encryption.
119 
120     Only 128 and 256 bit keys are supported in these 'on the fly'
121     modes.
122 */
123 
124 #if defined(AES_ENC_128_OTFK)
125 void aes_encrypt_128(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
126                      const unsigned char key[N_BLOCK], uint_8t o_key[N_BLOCK]);
127 #endif
128 
129 #if defined(AES_DEC_128_OTFK)
130 void aes_decrypt_128(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
131                      const unsigned char key[N_BLOCK], unsigned char o_key[N_BLOCK]);
132 #endif
133 
134 #if defined(AES_ENC_256_OTFK)
135 void aes_encrypt_256(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
136                      const unsigned char key[2 * N_BLOCK], unsigned char o_key[2 * N_BLOCK]);
137 #endif
138 
139 #if defined(AES_DEC_256_OTFK)
140 void aes_decrypt_256(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
141                      const unsigned char key[2 * N_BLOCK], unsigned char o_key[2 * N_BLOCK]);
142 #endif
143 
144 #endif
145