chen
2024-10-31 5602928a381e3aeec3d7e1c3f55b0db6a2924cbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "aes.h"
 
 
const unsigned char kTable[32] =
{
    0x21, 0x05, 0x04, 0x19, 0x89, 0x12, 0x13, 0x00,
    0x18, 0xDE, 0xCA, 0x01, 0x30, 0x52, 0x01, 0x23,
    0x13, 0x05, 0x33, 0x19, 0x93, 0x07, 0x08, 0x00,
    0x20, 0x5B, 0xA3, 0x4A, 0x21, 0xBA, 0xF9, 0xFC
};
/*
21 05 04 19 89 12 13 00 18 DE CA 01 30 52 01 23 13 05 33 19 93 07 08 00 20 5B A3 4A 21 BA F9 FC
*/
 
unsigned char block1[256]; //!< Workspace 1.
unsigned char block2[256]; //!< Worksapce 2.
unsigned char tempbuf[256];
 
unsigned char *powTbl; //!< Final location of exponentiation lookup table.
unsigned char *logTbl; //!< Final location of logarithm lookup table.
unsigned char *sBox; //!< Final location of s-box.
unsigned char *sBoxInv; //!< Final location of inverse s-box.
unsigned char *expandedKey; //!< Final location of expanded key.
 
void CalcPowLog(unsigned char *powTbl, unsigned char *logTbl)
{
    unsigned char i = 0;
    unsigned char t = 1;
 
    do {
        // Use 0x03 as root for exponentiation and logarithms.
        powTbl[i] = t;
        logTbl[t] = i;
        i++;
 
        // Muliply t by 3 in GF(2^8).
        t ^= (t << 1) ^ (t & 0x80 ? BPOLY : 0);
    } while( t != 1 ); // Cyclic properties ensure that i < 255.
 
    powTbl[255] = powTbl[0]; // 255 = '-0', 254 = -1, etc.
}
 
 
 
void CalcSBox( unsigned char * sBox )
{
    unsigned char i, rot;
    unsigned char temp;
    unsigned char result;
 
    // Fill all entries of sBox[].
    i = 0;
    do {
        //Inverse in GF(2^8).
        if( i > 0 )
        {
            temp = powTbl[ 255 - logTbl[i] ];
        }
        else
        {
            temp = 0;
        }
 
        // Affine transformation in GF(2).
        result = temp ^ 0x63; // Start with adding a vector in GF(2).
        for( rot = 4; rot > 0; rot-- )
        {
            // Rotate left.
            temp = (temp<<1) | (temp>>7);
 
            // Add rotated byte in GF(2).
            result ^= temp;
        }
 
        // Put result in table.
        sBox[i] = result;
    } while( ++i != 0 );
}
 
 
 
void CalcSBoxInv( unsigned char * sBox, unsigned char * sBoxInv )
{
    unsigned char i = 0;
    unsigned char j = 0;
 
    // Iterate through all elements in sBoxInv using  i.
    do {
        // Search through sBox using j.
        do {
            // Check if current j is the inverse of current i.
            if( sBox[ j ] == i )
            {
                // If so, set sBoxInc and indicate search finished.
                sBoxInv[ i ] = j;
                j = 255;
            }
        } while( ++j != 0 );
    } while( ++i != 0 );
}
 
 
 
void CycleLeft( unsigned char * row )
{
    // Cycle 4 bytes in an array left once.
    unsigned char temp = row[0];
 
    row[0] = row[1];
    row[1] = row[2];
    row[2] = row[3];
    row[3] = temp;
}
 
 
void CalcCols(unsigned char *col)
{
    unsigned char i;
 
    for(i = 4; i > 0; i--)
    {
        *col = (*col << 1) ^ (*col & 0x80 ? BPOLY : 0);
        col++;
    }
}
 
void InvMixColumn( unsigned char * column )
{
    unsigned char r[4];
 
    r[0] = column[1] ^ column[2] ^ column[3];
    r[1] = column[0] ^ column[2] ^ column[3];
    r[2] = column[0] ^ column[1] ^ column[3];
    r[3] = column[0] ^ column[1] ^ column[2];
 
    /*column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
    column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
    column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
    column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);*/
    CalcCols(column);
 
    r[0] ^= column[0] ^ column[1];
    r[1] ^= column[1] ^ column[2];
    r[2] ^= column[2] ^ column[3];
    r[3] ^= column[0] ^ column[3];
 
    /*column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
    column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
    column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
    column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);*/
    CalcCols(column);
 
    r[0] ^= column[0] ^ column[2];
    r[1] ^= column[1] ^ column[3];
    r[2] ^= column[0] ^ column[2];
    r[3] ^= column[1] ^ column[3];
 
    /*column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
    column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
    column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
    column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);*/
    CalcCols(column);
 
    column[0] ^= column[1] ^ column[2] ^ column[3];
    r[0] ^= column[0];
    r[1] ^= column[0];
    r[2] ^= column[0];
    r[3] ^= column[0];
 
    column[0] = r[0];
    column[1] = r[1];
    column[2] = r[2];
    column[3] = r[3];
 
    //CopyBytes(column, r, 4);
}
 
 
 
void SubBytes( unsigned char * bytes, unsigned char count )
{
    do {
        *bytes = sBox[ *bytes ]; // Substitute every byte in state.
        bytes++;
    } while( --count );
}
 
 
 
void InvSubBytesAndXOR( unsigned char * bytes, unsigned char * key, unsigned char count )
{
    do {
        // *bytes = sBoxInv[ *bytes ] ^ *key; // Inverse substitute every byte in state and add key.
        *bytes = block2[ *bytes ] ^ *key; // Use block2 directly. Increases speed.
        bytes++;
        key++;
    } while( --count );
}
 
 
void InvShiftRows( unsigned char * state )
{
    unsigned char temp;
 
    // Note: State is arranged column by column.
 
    // Cycle second row right one time.
    temp = state[ 1 + 3*4 ];
    state[ 1 + 3*4 ] = state[ 1 + 2*4 ];
    state[ 1 + 2*4 ] = state[ 1 + 1*4 ];
    state[ 1 + 1*4 ] = state[ 1 + 0*4 ];
    state[ 1 + 0*4 ] = temp;
 
    // Cycle third row right two times.
    temp = state[ 2 + 0*4 ];
    state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
    state[ 2 + 2*4 ] = temp;
    temp = state[ 2 + 1*4 ];
    state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
    state[ 2 + 3*4 ] = temp;
 
    // Cycle fourth row right three times, ie. left once.
    temp = state[ 3 + 0*4 ];
    state[ 3 + 0*4 ] = state[ 3 + 1*4 ];
    state[ 3 + 1*4 ] = state[ 3 + 2*4 ];
    state[ 3 + 2*4 ] = state[ 3 + 3*4 ];
    state[ 3 + 3*4 ] = temp;
}
 
 
/*
void InvMixColumns( unsigned char * state )
{
  InvMixColumn( state + 0*4 );
  InvMixColumn( state + 1*4 );
  InvMixColumn( state + 2*4 );
  InvMixColumn( state + 3*4 );
}
*/
 
 
void XORBytes( unsigned char * bytes1, unsigned char * bytes2, unsigned char count )
{
    do {
        *bytes1 ^= *bytes2; // Add in GF(2), ie. XOR.
        bytes1++;
        bytes2++;
    } while( --count );
}
 
 
 
void CopyBytes( unsigned char * to, unsigned char * from, unsigned char count )
{
    do {
        *to = *from;
        to++;
        from++;
    } while( --count );
}
 
 
 
void KeyExpansion( unsigned char * expandedKey )
{
    unsigned char temp[4];
    unsigned char i;
    unsigned char Rcon[4] = { 0x01, 0x00, 0x00, 0x00 }; // Round constant.
 
    const unsigned char * key = kTable;
 
    // Copy key to start of expanded key.
    i = KEYLENGTH;
    do {
        *expandedKey = *key;
        expandedKey++;
        key++;
    } while( --i );
 
    // Prepare last 4 bytes of key in temp.
    /*expandedKey -= 4;
    temp[0] = *(expandedKey++);
    temp[1] = *(expandedKey++);
    temp[2] = *(expandedKey++);
    temp[3] = *(expandedKey++);*/
    CopyBytes(temp, expandedKey-4, 4);
 
    // Expand key.
    i = KEYLENGTH;
    //j = BLOCKSIZE*(ROUNDS+1) - KEYLENGTH;
    while( i < BLOCKSIZE*(ROUNDS+1) )
    {
        // Are we at the start of a multiple of the key size?
        if( (i % KEYLENGTH) == 0 )
        {
            CycleLeft( temp ); // Cycle left once.
            SubBytes( temp, 4 ); // Substitute each byte.
            XORBytes( temp, Rcon, 4 ); // Add constant in GF(2).
            *Rcon = (*Rcon << 1) ^ (*Rcon & 0x80 ? BPOLY : 0);
        }
 
        // Keysize larger than 24 bytes, ie. larger that 192 bits?
#if KEYLENGTH > 24
        // Are we right past a block size?
        else if( (i % KEYLENGTH) == BLOCKSIZE ) {
            SubBytes( temp, 4 ); // Substitute each byte.
        }
#endif
 
        // Add bytes in GF(2) one KEYLENGTH away.
        XORBytes( temp, expandedKey - KEYLENGTH, 4 );
 
        // Copy result to current 4 bytes.
        *(expandedKey++) = temp[ 0 ];
        *(expandedKey++) = temp[ 1 ];
        *(expandedKey++) = temp[ 2 ];
        *(expandedKey++) = temp[ 3 ];
        //CopyBytes(expandedKey, temp, 4);
        //expandedKey += 4;
 
        i += 4; // Next 4 bytes.
    }
}
 
 
 
void InvCipher( unsigned char * block, unsigned char * expandedKey )
{
    unsigned char i, j;
    unsigned char round = ROUNDS-1;
    expandedKey += BLOCKSIZE * ROUNDS;
 
    XORBytes( block, expandedKey, 16 );
    expandedKey -= BLOCKSIZE;
 
    do {
        InvShiftRows( block );
        InvSubBytesAndXOR( block, expandedKey, 16 );
        expandedKey -= BLOCKSIZE;
        //InvMixColumns( block );
        for(i = 4, j = 0; i > 0; i--, j+=4)
            InvMixColumn( block + j );
    } while( --round );
 
    InvShiftRows( block );
    InvSubBytesAndXOR( block, expandedKey, 16 );
}
 
 
void aesDecInit(void)
{
    powTbl = block1;
    logTbl = block2;
    CalcPowLog( powTbl, logTbl );
 
    sBox = tempbuf;
    CalcSBox( sBox );
 
    expandedKey = block1;
    KeyExpansion( expandedKey );
 
    sBoxInv = block2; // Must be block2.
    CalcSBoxInv( sBox, sBoxInv );
}
 
void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock )
{
    aesDecInit();
    CopyBytes( tempbuf, buffer, BLOCKSIZE );
    InvCipher( buffer, expandedKey );
    XORBytes( buffer, chainBlock, BLOCKSIZE );
    CopyBytes( chainBlock, tempbuf, BLOCKSIZE );
}
 
unsigned char Multiply( unsigned char num, unsigned char factor )
{
    unsigned char mask = 1;
    unsigned char result = 0;
 
    while( mask != 0 )
    {
        // Check bit of factor given by mask.
        if( mask & factor )
        {
            // Add current multiple of num in GF(2).
            result ^= num;
        }
 
        // Shift mask to indicate next bit.
        mask <<= 1;
 
        // Double num.
        num = (num << 1) ^ (num & 0x80 ? BPOLY : 0);
    }
 
    return result;
}
 
 
unsigned char DotProduct( const unsigned char * vector1, unsigned char * vector2 )
{
    unsigned char result = 0 ,i;
 
    //result ^= Multiply( *vector1++, *vector2++ );
    //result ^= Multiply( *vector1++, *vector2++ );
    //result ^= Multiply( *vector1++, *vector2++ );
    //result ^= Multiply( *vector1  , *vector2   );
 
    for(i = 4; i > 0; i--)
        result ^= Multiply( *vector1++, *vector2++ );
 
    return result;
}
 
 
void MixColumn( unsigned char * column )
{
    const unsigned char row[8] = {
        0x02, 0x03, 0x01, 0x01,
        0x02, 0x03, 0x01, 0x01
    }; // Prepare first row of matrix twice, to eliminate need for cycling.
 
    unsigned char result[4];
 
    // Take dot products of each matrix row and the column vector.
    result[0] = DotProduct( row+0, column );
    result[1] = DotProduct( row+3, column );
    result[2] = DotProduct( row+2, column );
    result[3] = DotProduct( row+1, column );
 
    // Copy temporary result to original column.
    //column[0] = result[0];
    //column[1] = result[1];
    //column[2] = result[2];
    //column[3] = result[3];
    CopyBytes(column, result, 4);
}
/*
void MixColumns( unsigned char * state )
{
  MixColumn( state + 0*4 );
  MixColumn( state + 1*4 );
  MixColumn( state + 2*4 );
  MixColumn( state + 3*4 );
}
*/
void ShiftRows( unsigned char * state )
{
    unsigned char temp;
 
    // Note: State is arranged column by column.
 
    // Cycle second row left one time.
    temp = state[ 1 + 0*4 ];
    state[ 1 + 0*4 ] = state[ 1 + 1*4 ];
    state[ 1 + 1*4 ] = state[ 1 + 2*4 ];
    state[ 1 + 2*4 ] = state[ 1 + 3*4 ];
    state[ 1 + 3*4 ] = temp;
 
    // Cycle third row left two times.
    temp = state[ 2 + 0*4 ];
    state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
    state[ 2 + 2*4 ] = temp;
    temp = state[ 2 + 1*4 ];
    state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
    state[ 2 + 3*4 ] = temp;
 
    // Cycle fourth row left three times, ie. right once.
    temp = state[ 3 + 3*4 ];
    state[ 3 + 3*4 ] = state[ 3 + 2*4 ];
    state[ 3 + 2*4 ] = state[ 3 + 1*4 ];
    state[ 3 + 1*4 ] = state[ 3 + 0*4 ];
    state[ 3 + 0*4 ] = temp;
}
 
 
void Cipher( unsigned char * block, unsigned char * expandedKey )
{
    unsigned char i, j;
    unsigned char round = ROUNDS-1;
 
    XORBytes( block, expandedKey, 16 );
    expandedKey += BLOCKSIZE;
 
    do {
        SubBytes( block, 16 );
        ShiftRows( block );
        //MixColumns( block );
        for(i = 4, j = 0; i > 0; i--, j+=4)
            MixColumn( block + j );
        XORBytes( block, expandedKey, 16 );
        expandedKey += BLOCKSIZE;
    } while( --round );
 
    SubBytes( block, 16 );
    ShiftRows( block );
    XORBytes( block, expandedKey, 16 );
}
 
void aesEncInit(void)
{
    powTbl = block1;
    logTbl = block2;
    CalcPowLog( powTbl, logTbl );
 
    sBox = block2;
    CalcSBox( sBox );
 
    expandedKey = block1;
    KeyExpansion( expandedKey );
}
 
void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock )
{
    aesEncInit();
    XORBytes( buffer, chainBlock, BLOCKSIZE );
    Cipher( buffer, expandedKey );
    CopyBytes( chainBlock, buffer, BLOCKSIZE );
}