2 ---------------------------------------------------------------------------
3 Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 ---------------------------------------------------------------------------
31 Issue Date: 26/08/2003
37 * \brief This file contains the code for implementing encryption and decryption
38 * for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It
39 * can optionally be replaced by code written in assembler using NASM. For
40 * further details see the file aesopt.h
42 * \author Dr Brian Gladman <brg@gladman.me.uk>
45 #if defined(__cplusplus)
54 #define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])
55 #define so(y,x,c) word_out(y, c, s(x,c))
58 #define locals(y,x) x[4],y[4]
60 #define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
63 #define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
64 s(y,2) = s(x,2); s(y,3) = s(x,3);
65 #define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
66 #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
67 #define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
69 #if defined(ENCRYPTION) && !defined(AES_ASM)
71 /* Visual C++ .Net v7.1 provides the fastest encryption code when using
72 Pentium optimiation with small code but this is poor for decryption
73 so we need to control this with the following VC++ pragmas
77 #pragma optimize( "s", on )
80 /* Given the column (c) of the output state variable, the following
81 macros give the input state variables which are needed in its
82 computation for each row (r) of the state. All the alternative
83 macros give the same end values but expand into different ways
84 of calculating these values. In particular the complex macro
85 used for dynamically variable block sizes is designed to expand
86 to a compile time constant whenever possible but will expand to
87 conditional clauses on some branches (I am grateful to Frank
88 Yellin for this construction)
91 #define fwd_var(x,r,c)\
92 ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
93 : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\
94 : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
95 : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))
99 #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))
100 #elif defined(FT1_SET)
102 #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))
104 #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)))
108 #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))
109 #elif defined(FL1_SET)
110 #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))
112 #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))
115 aes_rval
aes_encrypt(const void *in_blk
, void *out_blk
, const aes_encrypt_ctx cx
[1])
116 { aes_32t
locals(b0
, b1
);
117 const aes_32t
*kp
= cx
->ks
;
119 dec_fmvars
; /* declare variables for fwd_mcol() if needed */
122 aes_32t nr
= (kp
[45] ^ kp
[52] ^ kp
[53] ? kp
[52] : 14);
125 if( (nr
!= 10 || !(kp
[0] | kp
[3] | kp
[4]))
126 && (nr
!= 12 || !(kp
[0] | kp
[5] | kp
[6]))
127 && (nr
!= 14 || !(kp
[0] | kp
[7] | kp
[8])) )
131 state_in(b0
, in_blk
, kp
);
133 #if (ENC_UNROLL == FULL)
138 round(fwd_rnd
, b1
, b0
, kp
+ 1 * N_COLS
);
139 round(fwd_rnd
, b0
, b1
, kp
+ 2 * N_COLS
);
142 round(fwd_rnd
, b1
, b0
, kp
+ 1 * N_COLS
);
143 round(fwd_rnd
, b0
, b1
, kp
+ 2 * N_COLS
);
146 round(fwd_rnd
, b1
, b0
, kp
+ 1 * N_COLS
);
147 round(fwd_rnd
, b0
, b1
, kp
+ 2 * N_COLS
);
148 round(fwd_rnd
, b1
, b0
, kp
+ 3 * N_COLS
);
149 round(fwd_rnd
, b0
, b1
, kp
+ 4 * N_COLS
);
150 round(fwd_rnd
, b1
, b0
, kp
+ 5 * N_COLS
);
151 round(fwd_rnd
, b0
, b1
, kp
+ 6 * N_COLS
);
152 round(fwd_rnd
, b1
, b0
, kp
+ 7 * N_COLS
);
153 round(fwd_rnd
, b0
, b1
, kp
+ 8 * N_COLS
);
154 round(fwd_rnd
, b1
, b0
, kp
+ 9 * N_COLS
);
155 round(fwd_lrnd
, b0
, b1
, kp
+10 * N_COLS
);
160 #if (ENC_UNROLL == PARTIAL)
162 for(rnd
= 0; rnd
< (nr
>> 1) - 1; ++rnd
)
165 round(fwd_rnd
, b1
, b0
, kp
);
167 round(fwd_rnd
, b0
, b1
, kp
);
170 round(fwd_rnd
, b1
, b0
, kp
);
173 for(rnd
= 0; rnd
< nr
- 1; ++rnd
)
176 round(fwd_rnd
, b1
, b0
, kp
);
181 round(fwd_lrnd
, b0
, b1
, kp
);
185 state_out(out_blk
, b0
);
193 #if defined(DECRYPTION) && !defined(AES_ASM)
195 /* Visual C++ .Net v7.1 provides the fastest encryption code when using
196 Pentium optimiation with small code but this is poor for decryption
197 so we need to control this with the following VC++ pragmas
200 #if defined(_MSC_VER)
201 #pragma optimize( "t", on )
204 /* Given the column (c) of the output state variable, the following
205 macros give the input state variables which are needed in its
206 computation for each row (r) of the state. All the alternative
207 macros give the same end values but expand into different ways
208 of calculating these values. In particular the complex macro
209 used for dynamically variable block sizes is designed to expand
210 to a compile time constant whenever possible but will expand to
211 conditional clauses on some branches (I am grateful to Frank
212 Yellin for this construction)
215 #define inv_var(x,r,c)\
216 ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
217 : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\
218 : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
219 : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))
223 #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))
224 #elif defined(IT1_SET)
226 #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))
228 #define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)))
232 #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))
233 #elif defined(IL1_SET)
234 #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))
236 #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))
239 aes_rval
aes_decrypt(const void *in_blk
, void *out_blk
, const aes_decrypt_ctx cx
[1])
240 { aes_32t
locals(b0
, b1
);
242 dec_imvars
; /* declare variables for inv_mcol() if needed */
245 aes_32t nr
= (cx
->ks
[45] ^ cx
->ks
[52] ^ cx
->ks
[53] ? cx
->ks
[52] : 14);
246 const aes_32t
*kp
= cx
->ks
+ nr
* N_COLS
;
249 if( (nr
!= 10 || !(cx
->ks
[0] | cx
->ks
[3] | cx
->ks
[4]))
250 && (nr
!= 12 || !(cx
->ks
[0] | cx
->ks
[5] | cx
->ks
[6]))
251 && (nr
!= 14 || !(cx
->ks
[0] | cx
->ks
[7] | cx
->ks
[8])) )
255 state_in(b0
, in_blk
, kp
);
257 #if (DEC_UNROLL == FULL)
262 round(inv_rnd
, b1
, b0
, kp
- 1 * N_COLS
);
263 round(inv_rnd
, b0
, b1
, kp
- 2 * N_COLS
);
266 round(inv_rnd
, b1
, b0
, kp
- 1 * N_COLS
);
267 round(inv_rnd
, b0
, b1
, kp
- 2 * N_COLS
);
270 round(inv_rnd
, b1
, b0
, kp
- 1 * N_COLS
);
271 round(inv_rnd
, b0
, b1
, kp
- 2 * N_COLS
);
272 round(inv_rnd
, b1
, b0
, kp
- 3 * N_COLS
);
273 round(inv_rnd
, b0
, b1
, kp
- 4 * N_COLS
);
274 round(inv_rnd
, b1
, b0
, kp
- 5 * N_COLS
);
275 round(inv_rnd
, b0
, b1
, kp
- 6 * N_COLS
);
276 round(inv_rnd
, b1
, b0
, kp
- 7 * N_COLS
);
277 round(inv_rnd
, b0
, b1
, kp
- 8 * N_COLS
);
278 round(inv_rnd
, b1
, b0
, kp
- 9 * N_COLS
);
279 round(inv_lrnd
, b0
, b1
, kp
- 10 * N_COLS
);
284 #if (DEC_UNROLL == PARTIAL)
286 for(rnd
= 0; rnd
< (nr
>> 1) - 1; ++rnd
)
289 round(inv_rnd
, b1
, b0
, kp
);
291 round(inv_rnd
, b0
, b1
, kp
);
294 round(inv_rnd
, b1
, b0
, kp
);
297 for(rnd
= 0; rnd
< nr
- 1; ++rnd
)
300 round(inv_rnd
, b1
, b0
, kp
);
305 round(inv_lrnd
, b0
, b1
, kp
);
309 state_out(out_blk
, b0
);
317 #endif /* !HAVE_CRYPTO */
319 #if defined(__cplusplus)