Browse code

aes: whitespace cosmetics

Signed-off-by: Mans Rullgard <mans@mansr.com>

Mans Rullgard authored on 2011/06/30 20:29:25
Showing 1 changed files
... ...
@@ -30,13 +30,13 @@ typedef union {
30 30
     uint8_t u8[16];
31 31
 } av_aes_block;
32 32
 
33
-typedef struct AVAES{
33
+typedef struct AVAES {
34 34
     // Note: round_key[16] is accessed in the init code, but this only
35 35
     // overwrites state, which does not matter (see also r7471).
36 36
     av_aes_block round_key[15];
37 37
     av_aes_block state[2];
38 38
     int rounds;
39
-}AVAES;
39
+} AVAES;
40 40
 
41 41
 const int av_aes_size= sizeof(AVAES);
42 42
 
... ...
@@ -54,18 +54,34 @@ static uint32_t enc_multbl[4][256];
54 54
 static uint32_t dec_multbl[4][256];
55 55
 #endif
56 56
 
57
-static inline void addkey(av_aes_block *dst, const av_aes_block *src, const av_aes_block *round_key){
57
+static inline void addkey(av_aes_block *dst, const av_aes_block *src,
58
+                          const av_aes_block *round_key)
59
+{
58 60
     dst->u64[0] = src->u64[0] ^ round_key->u64[0];
59 61
     dst->u64[1] = src->u64[1] ^ round_key->u64[1];
60 62
 }
61 63
 
62
-static void subshift(av_aes_block s0[2], int s, const uint8_t *box){
63
-    av_aes_block *s1= (av_aes_block *)(s0[0].u8 - s);
64
-    av_aes_block *s3= (av_aes_block *)(s0[0].u8 + s);
65
-    s0[0].u8[0]=box[s0[1].u8[ 0]]; s0[0].u8[ 4]=box[s0[1].u8[ 4]]; s0[0].u8[ 8]=box[s0[1].u8[ 8]]; s0[0].u8[12]=box[s0[1].u8[12]];
66
-    s1[0].u8[3]=box[s1[1].u8[ 7]]; s1[0].u8[ 7]=box[s1[1].u8[11]]; s1[0].u8[11]=box[s1[1].u8[15]]; s1[0].u8[15]=box[s1[1].u8[ 3]];
67
-    s0[0].u8[2]=box[s0[1].u8[10]]; s0[0].u8[10]=box[s0[1].u8[ 2]]; s0[0].u8[ 6]=box[s0[1].u8[14]]; s0[0].u8[14]=box[s0[1].u8[ 6]];
68
-    s3[0].u8[1]=box[s3[1].u8[13]]; s3[0].u8[13]=box[s3[1].u8[ 9]]; s3[0].u8[ 9]=box[s3[1].u8[ 5]]; s3[0].u8[ 5]=box[s3[1].u8[ 1]];
64
+static void subshift(av_aes_block s0[2], int s, const uint8_t *box)
65
+{
66
+    av_aes_block *s1 = (av_aes_block *) (s0[0].u8 - s);
67
+    av_aes_block *s3 = (av_aes_block *) (s0[0].u8 + s);
68
+
69
+    s0[0].u8[ 0] = box[s0[1].u8[ 0]];
70
+    s0[0].u8[ 4] = box[s0[1].u8[ 4]];
71
+    s0[0].u8[ 8] = box[s0[1].u8[ 8]];
72
+    s0[0].u8[12] = box[s0[1].u8[12]];
73
+    s1[0].u8[ 3] = box[s1[1].u8[ 7]];
74
+    s1[0].u8[ 7] = box[s1[1].u8[11]];
75
+    s1[0].u8[11] = box[s1[1].u8[15]];
76
+    s1[0].u8[15] = box[s1[1].u8[ 3]];
77
+    s0[0].u8[ 2] = box[s0[1].u8[10]];
78
+    s0[0].u8[10] = box[s0[1].u8[ 2]];
79
+    s0[0].u8[ 6] = box[s0[1].u8[14]];
80
+    s0[0].u8[14] = box[s0[1].u8[ 6]];
81
+    s3[0].u8[ 1] = box[s3[1].u8[13]];
82
+    s3[0].u8[13] = box[s3[1].u8[ 9]];
83
+    s3[0].u8[ 9] = box[s3[1].u8[ 5]];
84
+    s3[0].u8[ 5] = box[s3[1].u8[ 1]];
69 85
 }
70 86
 
71 87
 static inline int mix_core(uint32_t multbl[][256], int a, int b, int c, int d){
... ...
@@ -85,116 +101,134 @@ static inline void mix(av_aes_block state[2], uint32_t multbl[][256], int s1, in
85 85
     state[0].u32[3] = mix_core(multbl, src[3][0], src[s1-1][1], src[1][2], src[s3-1][3]);
86 86
 }
87 87
 
88
-static inline void crypt(AVAES *a, int s, const uint8_t *sbox, uint32_t multbl[][256]){
88
+static inline void crypt(AVAES *a, int s, const uint8_t *sbox,
89
+                         uint32_t multbl[][256])
90
+{
89 91
     int r;
90 92
 
91
-    for(r=a->rounds-1; r>0; r--){
92
-        mix(a->state, multbl, 3-s, 1+s);
93
+    for (r = a->rounds - 1; r > 0; r--) {
94
+        mix(a->state, multbl, 3 - s, 1 + s);
93 95
         addkey(&a->state[1], &a->state[0], &a->round_key[r]);
94 96
     }
97
+
95 98
     subshift(&a->state[0], s, sbox);
96 99
 }
97 100
 
98
-void av_aes_crypt(AVAES *a, uint8_t *dst_, const uint8_t *src_, int count, uint8_t *iv_, int decrypt){
99
-    av_aes_block *dst = (av_aes_block *)dst_;
100
-    const av_aes_block *src = (const av_aes_block *)src_;
101
-    av_aes_block *iv = (av_aes_block *)iv_;
102
-    while(count--){
101
+void av_aes_crypt(AVAES *a, uint8_t *dst_, const uint8_t *src_,
102
+                  int count, uint8_t *iv_, int decrypt)
103
+{
104
+    av_aes_block       *dst = (av_aes_block *) dst_;
105
+    const av_aes_block *src = (const av_aes_block *) src_;
106
+    av_aes_block        *iv = (av_aes_block *) iv_;
107
+
108
+    while (count--) {
103 109
         addkey(&a->state[1], src, &a->round_key[a->rounds]);
104
-        if(decrypt) {
110
+        if (decrypt) {
105 111
             crypt(a, 0, inv_sbox, dec_multbl);
106
-            if(iv){
112
+            if (iv) {
107 113
                 addkey(&a->state[0], &a->state[0], iv);
108 114
                 memcpy(iv, src, 16);
109 115
             }
110 116
             addkey(dst, &a->state[0], &a->round_key[0]);
111
-        }else{
112
-            if(iv) addkey(&a->state[1], &a->state[1], iv);
113
-            crypt(a, 2,     sbox, enc_multbl);
117
+        } else {
118
+            if (iv)
119
+                addkey(&a->state[1], &a->state[1], iv);
120
+            crypt(a, 2, sbox, enc_multbl);
114 121
             addkey(dst, &a->state[0], &a->round_key[0]);
115
-            if(iv) memcpy(iv, dst, 16);
122
+            if (iv)
123
+                memcpy(iv, dst, 16);
116 124
         }
117 125
         src++;
118 126
         dst++;
119 127
     }
120 128
 }
121 129
 
122
-static void init_multbl2(uint8_t tbl[1024], const int c[4], const uint8_t *log8, const uint8_t *alog8, const uint8_t *sbox){
130
+static void init_multbl2(uint8_t tbl[1024], const int c[4],
131
+                         const uint8_t *log8, const uint8_t *alog8,
132
+                         const uint8_t *sbox)
133
+{
123 134
     int i, j;
124
-    for(i=0; i<1024; i++){
125
-        int x= sbox[i>>2];
126
-        if(x) tbl[i]= alog8[ log8[x] + log8[c[i&3]] ];
135
+
136
+    for (i = 0; i < 1024; i++) {
137
+        int x = sbox[i >> 2];
138
+        if (x)
139
+            tbl[i] = alog8[log8[x] + log8[c[i & 3]]];
127 140
     }
128 141
 #if !CONFIG_SMALL
129
-    for(j=256; j<1024; j++)
130
-        for(i=0; i<4; i++)
131
-            tbl[4*j+i]= tbl[4*j + ((i-1)&3) - 1024];
142
+    for (j = 256; j < 1024; j++)
143
+        for (i = 0; i < 4; i++)
144
+            tbl[4*j + i] = tbl[4*j + ((i - 1) & 3) - 1024];
132 145
 #endif
133 146
 }
134 147
 
135 148
 // this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
136
-int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) {
149
+int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
150
+{
137 151
     int i, j, t, rconpointer = 0;
138 152
     uint8_t tk[8][4];
139
-    int KC= key_bits>>5;
140
-    int rounds= KC + 6;
141
-    uint8_t  log8[256];
153
+    int KC = key_bits >> 5;
154
+    int rounds = KC + 6;
155
+    uint8_t log8[256];
142 156
     uint8_t alog8[512];
143 157
 
144
-    if(!enc_multbl[FF_ARRAY_ELEMS(enc_multbl)-1][FF_ARRAY_ELEMS(enc_multbl[0])-1]){
145
-        j=1;
146
-        for(i=0; i<255; i++){
147
-            alog8[i]=
148
-            alog8[i+255]= j;
149
-            log8[j]= i;
150
-            j^= j+j;
151
-            if(j>255) j^= 0x11B;
158
+    if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl)-1][FF_ARRAY_ELEMS(enc_multbl[0])-1]) {
159
+        j = 1;
160
+        for (i = 0; i < 255; i++) {
161
+            alog8[i] = alog8[i + 255] = j;
162
+            log8[j] = i;
163
+            j ^= j + j;
164
+            if (j > 255)
165
+                j ^= 0x11B;
152 166
         }
153
-        for(i=0; i<256; i++){
154
-            j= i ? alog8[255-log8[i]] : 0;
155
-            j ^= (j<<1) ^ (j<<2) ^ (j<<3) ^ (j<<4);
156
-            j = (j ^ (j>>8) ^ 99) & 255;
157
-            inv_sbox[j]= i;
158
-            sbox    [i]= j;
167
+        for (i = 0; i < 256; i++) {
168
+            j = i ? alog8[255 - log8[i]] : 0;
169
+            j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
170
+            j = (j ^ (j >> 8) ^ 99) & 255;
171
+            inv_sbox[j] = i;
172
+            sbox[i] = j;
159 173
         }
160
-        init_multbl2(dec_multbl[0], (const int[4]){0xe, 0x9, 0xd, 0xb}, log8, alog8, inv_sbox);
161
-        init_multbl2(enc_multbl[0], (const int[4]){0x2, 0x1, 0x1, 0x3}, log8, alog8, sbox);
174
+        init_multbl2(dec_multbl[0], (const int[4]) { 0xe, 0x9, 0xd, 0xb },
175
+                     log8, alog8, inv_sbox);
176
+        init_multbl2(enc_multbl[0], (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
177
+                     log8, alog8, sbox);
162 178
     }
163 179
 
164
-    if(key_bits!=128 && key_bits!=192 && key_bits!=256)
180
+    if (key_bits != 128 && key_bits != 192 && key_bits != 256)
165 181
         return -1;
166 182
 
167
-    a->rounds= rounds;
183
+    a->rounds = rounds;
168 184
 
169
-    memcpy(tk, key, KC*4);
185
+    memcpy(tk, key, KC * 4);
170 186
 
171
-    for(t= 0; t < (rounds+1)*16;) {
172
-        memcpy(a->round_key[0].u8+t, tk, KC*4);
173
-        t+= KC*4;
187
+    for (t = 0; t < (rounds + 1) * 16;) {
188
+        memcpy(a->round_key[0].u8 + t, tk, KC * 4);
189
+        t += KC * 4;
174 190
 
175
-        for(i = 0; i < 4; i++)
176
-            tk[0][i] ^= sbox[tk[KC-1][(i+1)&3]];
191
+        for (i = 0; i < 4; i++)
192
+            tk[0][i] ^= sbox[tk[KC - 1][(i + 1) & 3]];
177 193
         tk[0][0] ^= rcon[rconpointer++];
178 194
 
179
-        for(j = 1; j < KC; j++){
180
-            if(KC != 8 || j != KC>>1)
181
-                for(i = 0; i < 4; i++) tk[j][i] ^=      tk[j-1][i];
195
+        for (j = 1; j < KC; j++) {
196
+            if (KC != 8 || j != KC >> 1)
197
+                for (i = 0; i < 4; i++)
198
+                    tk[j][i] ^= tk[j - 1][i];
182 199
             else
183
-                for(i = 0; i < 4; i++) tk[j][i] ^= sbox[tk[j-1][i]];
200
+                for (i = 0; i < 4; i++)
201
+                    tk[j][i] ^= sbox[tk[j - 1][i]];
184 202
         }
185 203
     }
186 204
 
187
-    if(decrypt){
188
-        for(i=1; i<rounds; i++){
205
+    if (decrypt) {
206
+        for (i = 1; i < rounds; i++) {
189 207
             av_aes_block tmp[3];
190 208
             memcpy(&tmp[2], &a->round_key[i], 16);
191 209
             subshift(&tmp[1], 0, sbox);
192 210
             mix(tmp, dec_multbl, 1, 3);
193 211
             memcpy(&a->round_key[i], &tmp[0], 16);
194 212
         }
195
-    }else{
196
-        for(i=0; i<(rounds+1)>>1; i++){
197
-            for(j=0; j<16; j++)
213
+    } else {
214
+        for (i = 0; i < (rounds + 1) >> 1; i++) {
215
+            for (j = 0; j < 16; j++)
198 216
                 FFSWAP(int, a->round_key[i].u8[j], a->round_key[rounds-i].u8[j]);
199 217
         }
200 218
     }
... ...
@@ -209,30 +243,39 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) {
209 209
 
210 210
 int main(int argc, char **argv)
211 211
 {
212
-    int i,j;
212
+    int i, j;
213 213
     AVAES b;
214
-    uint8_t rkey[2][16]= {
215
-        {0},
216
-        {0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59}};
214
+    uint8_t rkey[2][16] = {
215
+        { 0 },
216
+        { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
217
+          0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
218
+    };
217 219
     uint8_t pt[16], rpt[2][16]= {
218
-        {0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3},
219
-        {0}};
220
+        { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
221
+          0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
222
+        { 0 }
223
+    };
220 224
     uint8_t rct[2][16]= {
221
-        {0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf},
222
-        {0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65}};
225
+        { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
226
+          0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
227
+        { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
228
+          0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 }
229
+    };
223 230
     uint8_t temp[16];
224 231
     int err = 0;
225 232
 
226 233
     av_log_set_level(AV_LOG_DEBUG);
227 234
 
228
-    for(i=0; i<2; i++){
235
+    for (i = 0; i < 2; i++) {
229 236
         av_aes_init(&b, rkey[i], 128, 1);
230 237
         av_aes_crypt(&b, temp, rct[i], 1, NULL, 1);
231
-        for(j=0; j<16; j++)
232
-            if(rpt[i][j] != temp[j]) {
233
-                av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], temp[j]);
238
+        for (j = 0; j < 16; j++) {
239
+            if (rpt[i][j] != temp[j]) {
240
+                av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n",
241
+                       j, rpt[i][j], temp[j]);
234 242
                 err = 1;
235 243
             }
244
+        }
236 245
     }
237 246
 
238 247
     if (argc > 1 && !strcmp(argv[1], "-t")) {
... ...
@@ -243,21 +286,23 @@ int main(int argc, char **argv)
243 243
         av_aes_init(&ad, "PI=3.141592654..", 128, 1);
244 244
         av_lfg_init(&prng, 1);
245 245
 
246
-        for(i=0; i<10000; i++){
247
-            for(j=0; j<16; j++){
246
+        for (i = 0; i < 10000; i++) {
247
+            for (j = 0; j < 16; j++) {
248 248
                 pt[j] = av_lfg_get(&prng);
249 249
             }
250 250
             {
251 251
                 START_TIMER;
252 252
                 av_aes_crypt(&ae, temp, pt, 1, NULL, 0);
253
-                if(!(i&(i-1)))
254
-                    av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", temp[0], temp[5], temp[10], temp[15]);
253
+                if (!(i & (i - 1)))
254
+                    av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
255
+                           temp[0], temp[5], temp[10], temp[15]);
255 256
                 av_aes_crypt(&ad, temp, temp, 1, NULL, 1);
256 257
                 STOP_TIMER("aes");
257 258
             }
258
-            for(j=0; j<16; j++){
259
-                if(pt[j] != temp[j]){
260
-                    av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], temp[j]);
259
+            for (j = 0; j < 16; j++) {
260
+                if (pt[j] != temp[j]) {
261
+                    av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n",
262
+                           i, j, pt[j], temp[j]);
261 263
                 }
262 264
             }
263 265
         }