annotate aes.c @ 19:5f9a40d6991b

Import SD handling from http://www.roland-riegel.de/sd-reader/index.html Use smaller build options
author Matt Johnston <matt@ucc.asn.au>
date Tue, 25 Jun 2013 13:55:11 +0800
parents 439b7aaaec9e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
1 #include "aes.h"
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
2 //#include "loader.h"
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 //
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
4 #define KEY_COUNT 1
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
5
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
6 #if KEY_COUNT > 0
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
7
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
8 //#include "aeskeys.inc"
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
9
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
11
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
12
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
13 typedef unsigned char byte;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
14
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
15
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
16
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
17 #define BPOLY 0x1b //!< Lower 8 bits of (x^8+x^4+x^3+x+1), ie. (x^4+x^3+x+1).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
18 #define BLOCKSIZE 16 //!< Block size in number of bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
19
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
20
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
21
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
22 #if KEY_COUNT == 1
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
23 #define KEYBITS 128 //!< Use AES128.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
24 #elif KEY_COUNT == 2
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
25 #define KEYBITS 192 //!< Use AES196.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
26 #elif KEY_COUNT == 3
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
27 #define KEYBITS 256 //!< Use AES256.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
28 #else
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
29 #error Use 1, 2 or 3 keys!
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
30 #endif
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
32 #if KEYBITS == 128
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
33 #define ROUNDS 10 //!< Number of rounds.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
34 #define KEYLENGTH 16 //!< Key length in number of bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
35 #elif KEYBITS == 192
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
36 #define ROUNDS 12 //!< Number of rounds.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
37 #define KEYLENGTH 24 //!< // Key length in number of bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
38 #elif KEYBITS == 256
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
39 #define ROUNDS 14 //!< Number of rounds.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
40 #define KEYLENGTH 32 //!< Key length in number of bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
41 #else
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
42 #error Key must be 128, 192 or 256 bits!
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
43 #endif
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
44
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
45 #define EXPANDED_KEY_SIZE (BLOCKSIZE * (ROUNDS+1)) //!< 176, 208 or 240 bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
46
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
47
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
48
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
49 byte block1[ 256 ]; //!< Workspace 1.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
50 byte block2[ 256 ]; //!< Worksapce 2.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
51
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
52
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
53
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
54 byte * powTbl; //!< Final location of exponentiation lookup table.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
55 byte * logTbl; //!< Final location of logarithm lookup table.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
56 byte * sBox; //!< Final location of s-box.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
57 byte * sBoxInv; //!< Final location of inverse s-box.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
58 byte * expandedKey; //!< Final location of expanded key.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
59
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
60
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
61
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
62 void CalcPowLog( byte * powTbl, byte * logTbl )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
63 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
64 byte i = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
65 byte t = 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
66
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
67 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
68 // Use 0x03 as root for exponentiation and logarithms.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
69 powTbl[i] = t;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
70 logTbl[t] = i;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
71 i++;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
73 // Muliply t by 3 in GF(2^8).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
74 t ^= (t << 1) ^ (t & 0x80 ? BPOLY : 0);
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
75 } while( t != 1 ); // Cyclic properties ensure that i < 255.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
76
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
77 powTbl[255] = powTbl[0]; // 255 = '-0', 254 = -1, etc.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
78 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
79
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
80
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
81
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
82 void CalcSBox( byte * sBox )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
83 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
84 byte i, rot;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
85 byte temp;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
86 byte result;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
87
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
88 // Fill all entries of sBox[].
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
89 i = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
90 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
91 // Inverse in GF(2^8).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
92 if( i > 0 ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
93 temp = powTbl[ 255 - logTbl[i] ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
94 } else {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
95 temp = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
96 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
97
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
98 // Affine transformation in GF(2).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
99 result = temp ^ 0x63; // Start with adding a vector in GF(2).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
100 for( rot = 0; rot < 4; rot++ ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
101 // Rotate left.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
102 temp = (temp<<1) | (temp>>7);
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
104 // Add rotated byte in GF(2).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
105 result ^= temp;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
106 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
107
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
108 // Put result in table.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
109 sBox[i] = result;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
110 } while( ++i != 0 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
111 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
112
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
113
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
114
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
115 void CalcSBoxInv( byte * sBox, byte * sBoxInv )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
116 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
117 byte i = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
118 byte j = 0;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
120 // Iterate through all elements in sBoxInv using i.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
121 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
122 // Search through sBox using j.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
123 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
124 // Check if current j is the inverse of current i.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
125 if( sBox[ j ] == i ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
126 // If so, set sBoxInc and indicate search finished.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
127 sBoxInv[ i ] = j;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
128 j = 255;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
129 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
130 } while( ++j != 0 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
131 } while( ++i != 0 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
132 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
133
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
134
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
135
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
136 void CycleLeft( byte * row )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
137 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
138 // Cycle 4 bytes in an array left once.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
139 byte temp = row[0];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
140 row[0] = row[1];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
141 row[1] = row[2];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
142 row[2] = row[3];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
143 row[3] = temp;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
144 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
145
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
148 void InvMixColumn( byte * column )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
149 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
150 byte result0, result1, result2, result3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
151 byte column0, column1, column2, column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
152 byte xor;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
154 // This generates more effective code, at least
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
155 // with the IAR C compiler.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
156 column0 = column[0];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
157 column1 = column[1];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
158 column2 = column[2];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
159 column3 = column[3];
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
161 // Partial sums (modular addition using XOR).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
162 result0 = column1 ^ column2 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
163 result1 = column0 ^ column2 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
164 result2 = column0 ^ column1 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
165 result3 = column0 ^ column1 ^ column2;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
167 // Multiply column bytes by 2 modulo BPOLY.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
168 // This operation is done the following way to ensure cycle count
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
169 // independent from data contents. Take care when changing this code.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
170 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
171 if (column0 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
172 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
173 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
174 column0 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
175 column0 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
176
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
177 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
178 if (column1 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
179 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
180 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
181 column1 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
182 column1 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
183
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
184 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
185 if (column2 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
186 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
187 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
188 column2 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
189 column2 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
190
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
191 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
192 if (column3 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
193 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
194 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
195 column3 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
196 column3 ^= xor;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
198 // More partial sums.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
199 result0 ^= column0 ^ column1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
200 result1 ^= column1 ^ column2;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
201 result2 ^= column2 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
202 result3 ^= column0 ^ column3;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
204 // Multiply column bytes by 2 modulo BPOLY.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
205 // This operation is done the following way to ensure cycle count
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
206 // independent from data contents. Take care when changing this code.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
207 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
208 if (column0 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
209 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
210 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
211 column0 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
212 column0 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
213
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
214 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
215 if (column1 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
216 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
217 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
218 column1 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
219 column1 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
220
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
221 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
222 if (column2 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
223 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
224 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
225 column2 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
226 column2 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
227
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
228 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
229 if (column3 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
230 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
231 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
232 column3 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
233 column3 ^= xor;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
235 // More partial sums.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
236 result0 ^= column0 ^ column2;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
237 result1 ^= column1 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
238 result2 ^= column0 ^ column2;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
239 result3 ^= column1 ^ column3;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
241 // Multiply column bytes by 2 modulo BPOLY.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
242 // This operation is done the following way to ensure cycle count
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
243 // independent from data contents. Take care when changing this code.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
244 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
245 if (column0 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
246 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
247 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
248 column0 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
249 column0 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
250
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
251 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
252 if (column1 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
253 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
254 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
255 column1 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
256 column1 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
257
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
258 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
259 if (column2 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
260 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
261 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
262 column2 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
263 column2 ^= xor;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
264
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
265 xor = 0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
266 if (column3 & 0x80) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
267 xor = BPOLY;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
268 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
269 column3 <<= 1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
270 column3 ^= xor;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
272 // Final partial sum.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
273 column0 ^= column1 ^ column2 ^ column3;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
274
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
275 // Final sums stored into original column bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
276 column[0] = result0 ^ column0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
277 column[1] = result1 ^ column0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
278 column[2] = result2 ^ column0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
279 column[3] = result3 ^ column0;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
280 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
281
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
282
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
284 void SubBytes( byte * bytes, byte count )
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 {
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
286 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
287 *bytes = sBox[ *bytes ]; // Substitute every byte in state.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
288 bytes++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
289 } while( --count );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
290 }
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
294 void InvSubBytesAndXOR( byte * bytes, byte * key, byte count )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
295 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
296 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
297 // *bytes = sBoxInv[ *bytes ] ^ *key; // Inverse substitute every byte in state and add key.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
298 *bytes = block2[ *bytes ] ^ *key; // Use block2 directly. Increases speed.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
299 bytes++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
300 key++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
301 } while( --count );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
304
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
305
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
306 void InvShiftRows( byte * state )
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 {
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
308 byte temp;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
309
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
310 // Note: State is arranged column by column.
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
312 // Cycle second row right one time.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
313 temp = state[ 1 + 3*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
314 state[ 1 + 3*4 ] = state[ 1 + 2*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
315 state[ 1 + 2*4 ] = state[ 1 + 1*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
316 state[ 1 + 1*4 ] = state[ 1 + 0*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
317 state[ 1 + 0*4 ] = temp;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
319 // Cycle third row right two times.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
320 temp = state[ 2 + 0*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
321 state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
322 state[ 2 + 2*4 ] = temp;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
323 temp = state[ 2 + 1*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
324 state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
325 state[ 2 + 3*4 ] = temp;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
327 // Cycle fourth row right three times, ie. left once.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
328 temp = state[ 3 + 0*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
329 state[ 3 + 0*4 ] = state[ 3 + 1*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
330 state[ 3 + 1*4 ] = state[ 3 + 2*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
331 state[ 3 + 2*4 ] = state[ 3 + 3*4 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
332 state[ 3 + 3*4 ] = temp;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
335
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
337 void InvMixColumns( byte * state )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
338 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
339 InvMixColumn( state + 0*4 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
340 InvMixColumn( state + 1*4 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
341 InvMixColumn( state + 2*4 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
342 InvMixColumn( state + 3*4 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
343 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
344
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
345
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
347 void XORBytes( byte * bytes1, byte * bytes2, byte count )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
348 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
349 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
350 *bytes1 ^= *bytes2; // Add in GF(2), ie. XOR.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
351 bytes1++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
352 bytes2++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
353 } while( --count );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
354 }
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
356
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
358 void CopyBytes( byte * to, byte * from, byte count )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
359 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
360 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
361 *to = *from;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
362 to++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
363 from++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
364 } while( --count );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
367
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
368
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
369 void KeyExpansion( byte * key, byte * expandedKey )
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 {
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
371 byte temp[4];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
372 byte i;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
373 byte Rcon[4] = { 0x01, 0x00, 0x00, 0x00 }; // Round constant.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
374
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
375 #if 0
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
376 // matt
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
377 unsigned char BOOTFLASH * key = kTable;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
378 #endif
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
380 // Copy key to start of expanded key.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
381 i = KEYLENGTH;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
382 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
383 *expandedKey = *key;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
384 expandedKey++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
385 key++;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
386 } while( --i );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
387
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
388 // Prepare last 4 bytes of key in temp.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
389 expandedKey -= 4;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
390 temp[0] = *(expandedKey++);
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
391 temp[1] = *(expandedKey++);
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
392 temp[2] = *(expandedKey++);
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
393 temp[3] = *(expandedKey++);
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
395 // Expand key.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
396 i = KEYLENGTH;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
397 while( i < BLOCKSIZE*(ROUNDS+1) ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
398 // Are we at the start of a multiple of the key size?
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
399 if( (i % KEYLENGTH) == 0 ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
400 CycleLeft( temp ); // Cycle left once.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
401 SubBytes( temp, 4 ); // Substitute each byte.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
402 XORBytes( temp, Rcon, 4 ); // Add constant in GF(2).
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
403 *Rcon = (*Rcon << 1) ^ (*Rcon & 0x80 ? BPOLY : 0);
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
404 }
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
406 // Keysize larger than 24 bytes, ie. larger that 192 bits?
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
407 #if KEYLENGTH > 24
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
408 // Are we right past a block size?
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
409 else if( (i % KEYLENGTH) == BLOCKSIZE ) {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
410 SubBytes( temp, 4 ); // Substitute each byte.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
411 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
412 #endif
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
414 // Add bytes in GF(2) one KEYLENGTH away.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
415 XORBytes( temp, expandedKey - KEYLENGTH, 4 );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
417 // Copy result to current 4 bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
418 *(expandedKey++) = temp[ 0 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
419 *(expandedKey++) = temp[ 1 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
420 *(expandedKey++) = temp[ 2 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
421 *(expandedKey++) = temp[ 3 ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
422
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
423 i += 4; // Next 4 bytes.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
424 }
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
427
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
428
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
429 void InvCipher( byte * block, byte * expandedKey )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
430 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
431 byte round = ROUNDS-1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
432 expandedKey += BLOCKSIZE * ROUNDS;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
433
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
434 XORBytes( block, expandedKey, 16 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
435 expandedKey -= BLOCKSIZE;
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
437 do {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
438 InvShiftRows( block );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
439 InvSubBytesAndXOR( block, expandedKey, 16 );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
440 expandedKey -= BLOCKSIZE;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
441 InvMixColumns( block );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
442 } while( --round );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
444 InvShiftRows( block );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
445 InvSubBytesAndXOR( block, expandedKey, 16 );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
448
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
450 void aesInit( unsigned char *key, unsigned char * tempbuf )
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 {
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
452 powTbl = block1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
453 logTbl = block2;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
454 CalcPowLog( powTbl, logTbl );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
456 sBox = tempbuf;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
457 CalcSBox( sBox );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
459 expandedKey = block1;
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
460 KeyExpansion( key, expandedKey );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
461
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
462 sBoxInv = block2; // Must be block2.
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
463 CalcSBoxInv( sBox, sBoxInv );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
464 }
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
465
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
466
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467
10
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
468 void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock )
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
469 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
470 byte temp[ BLOCKSIZE ];
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
471
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
472 CopyBytes( temp, buffer, BLOCKSIZE );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
473 InvCipher( buffer, expandedKey );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
474 if (chainBlock)
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
475 {
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
476 XORBytes( buffer, chainBlock, BLOCKSIZE );
439b7aaaec9e Get aes from avr231 appnote instead
Matt Johnston <matt@ucc.asn.au>
parents: 5
diff changeset
477 CopyBytes( chainBlock, temp, BLOCKSIZE );
3
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 }
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480
ca3a006e292c add aes.c from http://www.pittnerovi.com/jiri/hobby/electronics/crypto/
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 #endif