summaryrefslogtreecommitdiff
path: root/aes.c
diff options
context:
space:
mode:
Diffstat (limited to 'aes.c')
-rw-r--r--aes.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/aes.c b/aes.c
index b44d88b..ca56cd9 100644
--- a/aes.c
+++ b/aes.c
@@ -2,7 +2,7 @@
2 2
3 Written by Mike Scott 21st April 1999 3 Written by Mike Scott 21st April 1999
4 mike@compapp.dcu.ie 4 mike@compapp.dcu.ie
5 An alternative faster version is implemented in MIRACL 5 An alternative faster version is implemented in MIRACL
6 ftp://ftp.computing.dcu.ie/pub/crypto/miracl.zip 6 ftp://ftp.computing.dcu.ie/pub/crypto/miracl.zip
7 7
8 Copyright (c) 1999 Mike Scott 8 Copyright (c) 1999 Mike Scott
@@ -18,15 +18,15 @@
18 See rijndael documentation. The code follows the documentation as closely 18 See rijndael documentation. The code follows the documentation as closely
19 as possible, and where possible uses the same function and variable names. 19 as possible, and where possible uses the same function and variable names.
20 20
21 Permission for free direct or derivative use is granted subject 21 Permission for free direct or derivative use is granted subject
22 to compliance with any conditions that the originators of the 22 to compliance with any conditions that the originators of the
23 algorithm place on its exploitation. 23 algorithm place on its exploitation.
24 24
25 Inspiration from Brian Gladman's implementation is acknowledged. 25 Inspiration from Brian Gladman's implementation is acknowledged.
26 26
27 Written for clarity, rather than speed. 27 Written for clarity, rather than speed.
28 Assumes long is 32 bit quantity. 28 Assumes long is 32 bit quantity.
29 Full implementation. 29 Full implementation.
30 Endian indifferent. 30 Endian indifferent.
31*/ 31*/
32 32
@@ -95,14 +95,14 @@ static WORD SubByte(WORD a)
95 b[1]=fbsub[b[1]]; 95 b[1]=fbsub[b[1]];
96 b[2]=fbsub[b[2]]; 96 b[2]=fbsub[b[2]];
97 b[3]=fbsub[b[3]]; 97 b[3]=fbsub[b[3]];
98 return pack(b); 98 return pack(b);
99} 99}
100 100
101static BYTE product(WORD x,WORD y) 101static BYTE product(WORD x,WORD y)
102{ /* dot product of two 4-byte arrays */ 102{ /* dot product of two 4-byte arrays */
103 BYTE xb[4],yb[4]; 103 BYTE xb[4],yb[4];
104 unpack(x,xb); 104 unpack(x,xb);
105 unpack(y,yb); 105 unpack(y,yb);
106 return bmul(xb[0],yb[0])^bmul(xb[1],yb[1])^bmul(xb[2],yb[2])^bmul(xb[3],yb[3]); 106 return bmul(xb[0],yb[0])^bmul(xb[1],yb[1])^bmul(xb[2],yb[2])^bmul(xb[3],yb[3]);
107} 107}
108 108
@@ -143,13 +143,13 @@ void suhosin_aes_gentables()
143 143
144 ltab[0]=0; 144 ltab[0]=0;
145 ptab[0]=1; ltab[1]=0; 145 ptab[0]=1; ltab[1]=0;
146 ptab[1]=3; ltab[3]=1; 146 ptab[1]=3; ltab[3]=1;
147 for (i=2;i<256;i++) 147 for (i=2;i<256;i++)
148 { 148 {
149 ptab[i]=ptab[i-1]^xtime(ptab[i-1]); 149 ptab[i]=ptab[i-1]^xtime(ptab[i-1]);
150 ltab[ptab[i]]=i; 150 ltab[ptab[i]]=i;
151 } 151 }
152 152
153 /* affine transformation:- each bit is xored with itself shifted one bit */ 153 /* affine transformation:- each bit is xored with itself shifted one bit */
154 154
155 fbsub[0]=0x63; 155 fbsub[0]=0x63;
@@ -212,7 +212,7 @@ void suhosin_aes_gkey(int nb,int nk,char *key)
212 } 212 }
213 213
214 N=Nb*(Nr+1); 214 N=Nb*(Nr+1);
215 215
216 for (i=j=0;i<Nk;i++,j+=4) 216 for (i=j=0;i<Nk;i++,j+=4)
217 { 217 {
218 CipherKey[i]=pack((BYTE *)&key[j]); 218 CipherKey[i]=pack((BYTE *)&key[j]);
@@ -239,7 +239,7 @@ void suhosin_aes_gkey(int nb,int nk,char *key)
239 239
240 /* now for the expanded decrypt key in reverse order */ 240 /* now for the expanded decrypt key in reverse order */
241 241
242 for (j=0;j<Nb;j++) SUHOSIN7_G(rkey)[j+N-Nb]=SUHOSIN7_G(fkey)[j]; 242 for (j=0;j<Nb;j++) SUHOSIN7_G(rkey)[j+N-Nb]=SUHOSIN7_G(fkey)[j];
243 for (i=Nb;i<N-Nb;i+=Nb) 243 for (i=Nb;i<N-Nb;i+=Nb)
244 { 244 {
245 k=N-Nb-i; 245 k=N-Nb-i;
@@ -251,7 +251,7 @@ void suhosin_aes_gkey(int nb,int nk,char *key)
251 251
252/* There is an obvious time/space trade-off possible here. * 252/* There is an obvious time/space trade-off possible here. *
253 * Instead of just one ftable[], I could have 4, the other * 253 * Instead of just one ftable[], I could have 4, the other *
254 * 3 pre-rotated to save the ROTL8, ROTL16 and ROTL24 overhead */ 254 * 3 pre-rotated to save the ROTL8, ROTL16 and ROTL24 overhead */
255 255
256void suhosin_aes_encrypt(char *buff) 256void suhosin_aes_encrypt(char *buff)
257{ 257{
@@ -270,7 +270,7 @@ void suhosin_aes_encrypt(char *buff)
270 for (i=1;i<Nr;i++) 270 for (i=1;i<Nr;i++)
271 { /* Nr is number of rounds. May be odd. */ 271 { /* Nr is number of rounds. May be odd. */
272 272
273/* if Nb is fixed - unroll this next 273/* if Nb is fixed - unroll this next
274 loop and hard-code in the values of fi[] */ 274 loop and hard-code in the values of fi[] */
275 275
276 for (m=j=0;j<Nb;j++,m+=3) 276 for (m=j=0;j<Nb;j++,m+=3)
@@ -284,14 +284,14 @@ void suhosin_aes_encrypt(char *buff)
284 t=x; x=y; y=t; /* swap pointers */ 284 t=x; x=y; y=t; /* swap pointers */
285 } 285 }
286 286
287/* Last Round - unroll if possible */ 287/* Last Round - unroll if possible */
288 for (m=j=0;j<Nb;j++,m+=3) 288 for (m=j=0;j<Nb;j++,m+=3)
289 { 289 {
290 y[j]=SUHOSIN7_G(fkey)[k++]^(WORD)fbsub[(BYTE)x[j]]^ 290 y[j]=SUHOSIN7_G(fkey)[k++]^(WORD)fbsub[(BYTE)x[j]]^
291 ROTL8((WORD)fbsub[(BYTE)(x[SUHOSIN7_G(fi)[m]]>>8)])^ 291 ROTL8((WORD)fbsub[(BYTE)(x[SUHOSIN7_G(fi)[m]]>>8)])^
292 ROTL16((WORD)fbsub[(BYTE)(x[SUHOSIN7_G(fi)[m+1]]>>16)])^ 292 ROTL16((WORD)fbsub[(BYTE)(x[SUHOSIN7_G(fi)[m+1]]>>16)])^
293 ROTL24((WORD)fbsub[x[SUHOSIN7_G(fi)[m+2]]>>24]); 293 ROTL24((WORD)fbsub[x[SUHOSIN7_G(fi)[m+2]]>>24]);
294 } 294 }
295 for (i=j=0;i<Nb;i++,j+=4) 295 for (i=j=0;i<Nb;i++,j+=4)
296 { 296 {
297 unpack(y[i],(BYTE *)&buff[j]); 297 unpack(y[i],(BYTE *)&buff[j]);
@@ -317,7 +317,7 @@ void suhosin_aes_decrypt(char *buff)
317 for (i=1;i<Nr;i++) 317 for (i=1;i<Nr;i++)
318 { /* Nr is number of rounds. May be odd. */ 318 { /* Nr is number of rounds. May be odd. */
319 319
320/* if Nb is fixed - unroll this next 320/* if Nb is fixed - unroll this next
321 loop and hard-code in the values of ri[] */ 321 loop and hard-code in the values of ri[] */
322 322
323 for (m=j=0;j<Nb;j++,m+=3) 323 for (m=j=0;j<Nb;j++,m+=3)
@@ -330,14 +330,14 @@ void suhosin_aes_decrypt(char *buff)
330 t=x; x=y; y=t; /* swap pointers */ 330 t=x; x=y; y=t; /* swap pointers */
331 } 331 }
332 332
333/* Last Round - unroll if possible */ 333/* Last Round - unroll if possible */
334 for (m=j=0;j<Nb;j++,m+=3) 334 for (m=j=0;j<Nb;j++,m+=3)
335 { 335 {
336 y[j]=SUHOSIN7_G(rkey)[k++]^(WORD)rbsub[(BYTE)x[j]]^ 336 y[j]=SUHOSIN7_G(rkey)[k++]^(WORD)rbsub[(BYTE)x[j]]^
337 ROTL8((WORD)rbsub[(BYTE)(x[SUHOSIN7_G(ri)[m]]>>8)])^ 337 ROTL8((WORD)rbsub[(BYTE)(x[SUHOSIN7_G(ri)[m]]>>8)])^
338 ROTL16((WORD)rbsub[(BYTE)(x[SUHOSIN7_G(ri)[m+1]]>>16)])^ 338 ROTL16((WORD)rbsub[(BYTE)(x[SUHOSIN7_G(ri)[m+1]]>>16)])^
339 ROTL24((WORD)rbsub[x[SUHOSIN7_G(ri)[m+2]]>>24]); 339 ROTL24((WORD)rbsub[x[SUHOSIN7_G(ri)[m+2]]>>24]);
340 } 340 }
341 for (i=j=0;i<Nb;i++,j+=4) 341 for (i=j=0;i<Nb;i++,j+=4)
342 { 342 {
343 unpack(y[i],(BYTE *)&buff[j]); 343 unpack(y[i],(BYTE *)&buff[j]);
@@ -362,7 +362,7 @@ static int main()
362 362
363 for (nb=4;nb<=8;nb+=2) 363 for (nb=4;nb<=8;nb+=2)
364 for (nk=4;nk<=8;nk+=2) 364 for (nk=4;nk<=8;nk+=2)
365 { 365 {
366 printf("\nBlock Size= %d bits, Key Size= %d bits\n",nb*32,nk*32); 366 printf("\nBlock Size= %d bits, Key Size= %d bits\n",nb*32,nk*32);
367 gkey(nb,nk,key); 367 gkey(nb,nk,key);
368 printf("Plain= "); 368 printf("Plain= ");