~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to contrib/pgcrypto/rijndael.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $OpenBSD: rijndael.c,v 1.6 2000/12/09 18:51:34 markus Exp $ */
 
2
 
 
3
/* contrib/pgcrypto/rijndael.c */
 
4
 
 
5
/* This is an independent implementation of the encryption algorithm:   */
 
6
/*                                                                                                                                              */
 
7
/*                 RIJNDAEL by Joan Daemen and Vincent Rijmen                                   */
 
8
/*                                                                                                                                              */
 
9
/* which is a candidate algorithm in the Advanced Encryption Standard   */
 
10
/* programme of the US National Institute of Standards and Technology.  */
 
11
/*                                                                                                                                              */
 
12
/* Copyright in this implementation is held by Dr B R Gladman but I             */
 
13
/* hereby give permission for its free direct or derivative use subject */
 
14
/* to acknowledgment of its origin and compliance with any conditions   */
 
15
/* that the originators of the algorithm place on its exploitation.             */
 
16
/*                                                                                                                                              */
 
17
/* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999             */
 
18
 
 
19
/* Timing data for Rijndael (rijndael.c)
 
20
 
 
21
Algorithm: rijndael (rijndael.c)
 
22
 
 
23
128 bit key:
 
24
Key Setup:        305/1389 cycles (encrypt/decrypt)
 
25
Encrypt:           374 cycles =    68.4 mbits/sec
 
26
Decrypt:           352 cycles =    72.7 mbits/sec
 
27
Mean:              363 cycles =    70.5 mbits/sec
 
28
 
 
29
192 bit key:
 
30
Key Setup:        277/1595 cycles (encrypt/decrypt)
 
31
Encrypt:           439 cycles =    58.3 mbits/sec
 
32
Decrypt:           425 cycles =    60.2 mbits/sec
 
33
Mean:              432 cycles =    59.3 mbits/sec
 
34
 
 
35
256 bit key:
 
36
Key Setup:        374/1960 cycles (encrypt/decrypt)
 
37
Encrypt:           502 cycles =    51.0 mbits/sec
 
38
Decrypt:           498 cycles =    51.4 mbits/sec
 
39
Mean:              500 cycles =    51.2 mbits/sec
 
40
 
 
41
*/
 
42
 
 
43
#include "postgres.h"
 
44
 
 
45
#include <sys/param.h>
 
46
 
 
47
#include "px.h"
 
48
#include "rijndael.h"
 
49
 
 
50
#define PRE_CALC_TABLES
 
51
#define LARGE_TABLES
 
52
 
 
53
static void gen_tabs(void);
 
54
 
 
55
/* 3. Basic macros for speeding up generic operations                           */
 
56
 
 
57
/* Circular rotate of 32 bit values                                                                     */
 
58
 
 
59
#define rotr(x,n)       (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
 
60
#define rotl(x,n)       (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
 
61
 
 
62
/* Invert byte order in a 32 bit variable                                                       */
 
63
 
 
64
#define bswap(x)        ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
 
65
 
 
66
/* Extract byte from a 32 bit quantity (little endian notation)         */
 
67
 
 
68
#define byte(x,n)       ((u1byte)((x) >> (8 * (n))))
 
69
 
 
70
#ifdef WORDS_BIGENDIAN
 
71
#define io_swap(x)      bswap(x)
 
72
#else
 
73
#define io_swap(x)      (x)
 
74
#endif
 
75
 
 
76
#ifdef PRINT_TABS
 
77
#undef PRE_CALC_TABLES
 
78
#endif
 
79
 
 
80
#ifdef PRE_CALC_TABLES
 
81
 
 
82
#include "rijndael.tbl"
 
83
#define tab_gen         1
 
84
#else                                                   /* !PRE_CALC_TABLES */
 
85
 
 
86
static u1byte pow_tab[256];
 
87
static u1byte log_tab[256];
 
88
static u1byte sbx_tab[256];
 
89
static u1byte isb_tab[256];
 
90
static u4byte rco_tab[10];
 
91
static u4byte ft_tab[4][256];
 
92
static u4byte it_tab[4][256];
 
93
 
 
94
#ifdef  LARGE_TABLES
 
95
static u4byte fl_tab[4][256];
 
96
static u4byte il_tab[4][256];
 
97
#endif
 
98
 
 
99
static u4byte tab_gen = 0;
 
100
#endif   /* !PRE_CALC_TABLES */
 
101
 
 
102
#define ff_mult(a,b)    ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
 
103
 
 
104
#define f_rn(bo, bi, n, k)                                                              \
 
105
        (bo)[n] =  ft_tab[0][byte((bi)[n],0)] ^                         \
 
106
                         ft_tab[1][byte((bi)[((n) + 1) & 3],1)] ^       \
 
107
                         ft_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
 
108
                         ft_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
 
109
 
 
110
#define i_rn(bo, bi, n, k)                                                      \
 
111
        (bo)[n] =  it_tab[0][byte((bi)[n],0)] ^                         \
 
112
                         it_tab[1][byte((bi)[((n) + 3) & 3],1)] ^       \
 
113
                         it_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
 
114
                         it_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
 
115
 
 
116
#ifdef LARGE_TABLES
 
117
 
 
118
#define ls_box(x)                                \
 
119
        ( fl_tab[0][byte(x, 0)] ^        \
 
120
          fl_tab[1][byte(x, 1)] ^        \
 
121
          fl_tab[2][byte(x, 2)] ^        \
 
122
          fl_tab[3][byte(x, 3)] )
 
123
 
 
124
#define f_rl(bo, bi, n, k)                                                              \
 
125
        (bo)[n] =  fl_tab[0][byte((bi)[n],0)] ^                         \
 
126
                         fl_tab[1][byte((bi)[((n) + 1) & 3],1)] ^       \
 
127
                         fl_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
 
128
                         fl_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
 
129
 
 
130
#define i_rl(bo, bi, n, k)                                                              \
 
131
        (bo)[n] =  il_tab[0][byte((bi)[n],0)] ^                         \
 
132
                         il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^       \
 
133
                         il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
 
134
                         il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
 
135
#else
 
136
 
 
137
#define ls_box(x)                                                        \
 
138
        ((u4byte)sbx_tab[byte(x, 0)] <<  0) ^    \
 
139
        ((u4byte)sbx_tab[byte(x, 1)] <<  8) ^    \
 
140
        ((u4byte)sbx_tab[byte(x, 2)] << 16) ^    \
 
141
        ((u4byte)sbx_tab[byte(x, 3)] << 24)
 
142
 
 
143
#define f_rl(bo, bi, n, k)                                                                                      \
 
144
        (bo)[n] = (u4byte)sbx_tab[byte((bi)[n],0)] ^                                    \
 
145
                rotl(((u4byte)sbx_tab[byte((bi)[((n) + 1) & 3],1)]),  8) ^      \
 
146
                rotl(((u4byte)sbx_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^      \
 
147
                rotl(((u4byte)sbx_tab[byte((bi)[((n) + 3) & 3],3)]), 24) ^ *((k) + (n))
 
148
 
 
149
#define i_rl(bo, bi, n, k)                                                                                      \
 
150
        (bo)[n] = (u4byte)isb_tab[byte((bi)[n],0)] ^                                    \
 
151
                rotl(((u4byte)isb_tab[byte((bi)[((n) + 3) & 3],1)]),  8) ^      \
 
152
                rotl(((u4byte)isb_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^      \
 
153
                rotl(((u4byte)isb_tab[byte((bi)[((n) + 1) & 3],3)]), 24) ^ *((k) + (n))
 
154
#endif
 
155
 
 
156
static void
 
157
gen_tabs(void)
 
158
{
 
159
#ifndef PRE_CALC_TABLES
 
160
        u4byte          i,
 
161
                                t;
 
162
        u1byte          p,
 
163
                                q;
 
164
 
 
165
        /* log and power tables for GF(2**8) finite field with  */
 
166
        /* 0x11b as modular polynomial - the simplest prmitive  */
 
167
        /* root is 0x11, used here to generate the tables               */
 
168
 
 
169
        for (i = 0, p = 1; i < 256; ++i)
 
170
        {
 
171
                pow_tab[i] = (u1byte) p;
 
172
                log_tab[p] = (u1byte) i;
 
173
 
 
174
                p = p ^ (p << 1) ^ (p & 0x80 ? 0x01b : 0);
 
175
        }
 
176
 
 
177
        log_tab[1] = 0;
 
178
        p = 1;
 
179
 
 
180
        for (i = 0; i < 10; ++i)
 
181
        {
 
182
                rco_tab[i] = p;
 
183
 
 
184
                p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);
 
185
        }
 
186
 
 
187
        /* note that the affine byte transformation matrix in   */
 
188
        /* rijndael specification is in big endian format with  */
 
189
        /* bit 0 as the most significant bit. In the remainder  */
 
190
        /* of the specification the bits are numbered from the  */
 
191
        /* least significant end of a byte.                                             */
 
192
 
 
193
        for (i = 0; i < 256; ++i)
 
194
        {
 
195
                p = (i ? pow_tab[255 - log_tab[i]] : 0);
 
196
                q = p;
 
197
                q = (q >> 7) | (q << 1);
 
198
                p ^= q;
 
199
                q = (q >> 7) | (q << 1);
 
200
                p ^= q;
 
201
                q = (q >> 7) | (q << 1);
 
202
                p ^= q;
 
203
                q = (q >> 7) | (q << 1);
 
204
                p ^= q ^ 0x63;
 
205
                sbx_tab[i] = (u1byte) p;
 
206
                isb_tab[p] = (u1byte) i;
 
207
        }
 
208
 
 
209
        for (i = 0; i < 256; ++i)
 
210
        {
 
211
                p = sbx_tab[i];
 
212
 
 
213
#ifdef  LARGE_TABLES
 
214
 
 
215
                t = p;
 
216
                fl_tab[0][i] = t;
 
217
                fl_tab[1][i] = rotl(t, 8);
 
218
                fl_tab[2][i] = rotl(t, 16);
 
219
                fl_tab[3][i] = rotl(t, 24);
 
220
#endif
 
221
                t = ((u4byte) ff_mult(2, p)) |
 
222
                        ((u4byte) p << 8) |
 
223
                        ((u4byte) p << 16) |
 
224
                        ((u4byte) ff_mult(3, p) << 24);
 
225
 
 
226
                ft_tab[0][i] = t;
 
227
                ft_tab[1][i] = rotl(t, 8);
 
228
                ft_tab[2][i] = rotl(t, 16);
 
229
                ft_tab[3][i] = rotl(t, 24);
 
230
 
 
231
                p = isb_tab[i];
 
232
 
 
233
#ifdef  LARGE_TABLES
 
234
 
 
235
                t = p;
 
236
                il_tab[0][i] = t;
 
237
                il_tab[1][i] = rotl(t, 8);
 
238
                il_tab[2][i] = rotl(t, 16);
 
239
                il_tab[3][i] = rotl(t, 24);
 
240
#endif
 
241
                t = ((u4byte) ff_mult(14, p)) |
 
242
                        ((u4byte) ff_mult(9, p) << 8) |
 
243
                        ((u4byte) ff_mult(13, p) << 16) |
 
244
                        ((u4byte) ff_mult(11, p) << 24);
 
245
 
 
246
                it_tab[0][i] = t;
 
247
                it_tab[1][i] = rotl(t, 8);
 
248
                it_tab[2][i] = rotl(t, 16);
 
249
                it_tab[3][i] = rotl(t, 24);
 
250
        }
 
251
 
 
252
        tab_gen = 1;
 
253
#endif   /* !PRE_CALC_TABLES */
 
254
}
 
255
 
 
256
 
 
257
#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
 
258
 
 
259
#define imix_col(y,x)           \
 
260
do { \
 
261
        u       = star_x(x);            \
 
262
        v       = star_x(u);            \
 
263
        w       = star_x(v);            \
 
264
        t       = w ^ (x);                      \
 
265
   (y)  = u ^ v ^ w;            \
 
266
   (y) ^= rotr(u ^ t,  8) ^ \
 
267
                  rotr(v ^ t, 16) ^ \
 
268
                  rotr(t,24);           \
 
269
} while (0)
 
270
 
 
271
/* initialise the key schedule from the user supplied key       */
 
272
 
 
273
#define loop4(i)                                                                        \
 
274
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];               \
 
275
        t ^= e_key[4 * i];         e_key[4 * i + 4] = t;        \
 
276
        t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t;    \
 
277
        t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t;    \
 
278
        t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t;    \
 
279
} while (0)
 
280
 
 
281
#define loop6(i)                                                                        \
 
282
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];               \
 
283
        t ^= e_key[6 * (i)];       e_key[6 * (i) + 6] = t;      \
 
284
        t ^= e_key[6 * (i) + 1]; e_key[6 * (i) + 7] = t;        \
 
285
        t ^= e_key[6 * (i) + 2]; e_key[6 * (i) + 8] = t;        \
 
286
        t ^= e_key[6 * (i) + 3]; e_key[6 * (i) + 9] = t;        \
 
287
        t ^= e_key[6 * (i) + 4]; e_key[6 * (i) + 10] = t;       \
 
288
        t ^= e_key[6 * (i) + 5]; e_key[6 * (i) + 11] = t;       \
 
289
} while (0)
 
290
 
 
291
#define loop8(i)                                                                        \
 
292
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];               \
 
293
        t ^= e_key[8 * (i)];     e_key[8 * (i) + 8] = t;        \
 
294
        t ^= e_key[8 * (i) + 1]; e_key[8 * (i) + 9] = t;        \
 
295
        t ^= e_key[8 * (i) + 2]; e_key[8 * (i) + 10] = t;       \
 
296
        t ^= e_key[8 * (i) + 3]; e_key[8 * (i) + 11] = t;       \
 
297
        t  = e_key[8 * (i) + 4] ^ ls_box(t);                            \
 
298
        e_key[8 * (i) + 12] = t;                                                        \
 
299
        t ^= e_key[8 * (i) + 5]; e_key[8 * (i) + 13] = t;       \
 
300
        t ^= e_key[8 * (i) + 6]; e_key[8 * (i) + 14] = t;       \
 
301
        t ^= e_key[8 * (i) + 7]; e_key[8 * (i) + 15] = t;       \
 
302
} while (0)
 
303
 
 
304
rijndael_ctx *
 
305
rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len,
 
306
                                 int encrypt)
 
307
{
 
308
        u4byte          i,
 
309
                                t,
 
310
                                u,
 
311
                                v,
 
312
                                w;
 
313
        u4byte     *e_key = ctx->e_key;
 
314
        u4byte     *d_key = ctx->d_key;
 
315
 
 
316
        ctx->decrypt = !encrypt;
 
317
 
 
318
        if (!tab_gen)
 
319
                gen_tabs();
 
320
 
 
321
        ctx->k_len = (key_len + 31) / 32;
 
322
 
 
323
        e_key[0] = io_swap(in_key[0]);
 
324
        e_key[1] = io_swap(in_key[1]);
 
325
        e_key[2] = io_swap(in_key[2]);
 
326
        e_key[3] = io_swap(in_key[3]);
 
327
 
 
328
        switch (ctx->k_len)
 
329
        {
 
330
                case 4:
 
331
                        t = e_key[3];
 
332
                        for (i = 0; i < 10; ++i)
 
333
                                loop4(i);
 
334
                        break;
 
335
 
 
336
                case 6:
 
337
                        e_key[4] = io_swap(in_key[4]);
 
338
                        t = e_key[5] = io_swap(in_key[5]);
 
339
                        for (i = 0; i < 8; ++i)
 
340
                                loop6(i);
 
341
                        break;
 
342
 
 
343
                case 8:
 
344
                        e_key[4] = io_swap(in_key[4]);
 
345
                        e_key[5] = io_swap(in_key[5]);
 
346
                        e_key[6] = io_swap(in_key[6]);
 
347
                        t = e_key[7] = io_swap(in_key[7]);
 
348
                        for (i = 0; i < 7; ++i)
 
349
                                loop8(i);
 
350
                        break;
 
351
        }
 
352
 
 
353
        if (!encrypt)
 
354
        {
 
355
                d_key[0] = e_key[0];
 
356
                d_key[1] = e_key[1];
 
357
                d_key[2] = e_key[2];
 
358
                d_key[3] = e_key[3];
 
359
 
 
360
                for (i = 4; i < 4 * ctx->k_len + 24; ++i)
 
361
                        imix_col(d_key[i], e_key[i]);
 
362
        }
 
363
 
 
364
        return ctx;
 
365
}
 
366
 
 
367
/* encrypt a block of text      */
 
368
 
 
369
#define f_nround(bo, bi, k) \
 
370
do { \
 
371
        f_rn(bo, bi, 0, k);             \
 
372
        f_rn(bo, bi, 1, k);             \
 
373
        f_rn(bo, bi, 2, k);             \
 
374
        f_rn(bo, bi, 3, k);             \
 
375
        k += 4;                                 \
 
376
} while (0)
 
377
 
 
378
#define f_lround(bo, bi, k) \
 
379
do { \
 
380
        f_rl(bo, bi, 0, k);             \
 
381
        f_rl(bo, bi, 1, k);             \
 
382
        f_rl(bo, bi, 2, k);             \
 
383
        f_rl(bo, bi, 3, k);             \
 
384
} while (0)
 
385
 
 
386
void
 
387
rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
 
388
{
 
389
        u4byte          k_len = ctx->k_len;
 
390
        u4byte     *e_key = ctx->e_key;
 
391
        u4byte          b0[4],
 
392
                                b1[4],
 
393
                           *kp;
 
394
 
 
395
        b0[0] = io_swap(in_blk[0]) ^ e_key[0];
 
396
        b0[1] = io_swap(in_blk[1]) ^ e_key[1];
 
397
        b0[2] = io_swap(in_blk[2]) ^ e_key[2];
 
398
        b0[3] = io_swap(in_blk[3]) ^ e_key[3];
 
399
 
 
400
        kp = e_key + 4;
 
401
 
 
402
        if (k_len > 6)
 
403
        {
 
404
                f_nround(b1, b0, kp);
 
405
                f_nround(b0, b1, kp);
 
406
        }
 
407
 
 
408
        if (k_len > 4)
 
409
        {
 
410
                f_nround(b1, b0, kp);
 
411
                f_nround(b0, b1, kp);
 
412
        }
 
413
 
 
414
        f_nround(b1, b0, kp);
 
415
        f_nround(b0, b1, kp);
 
416
        f_nround(b1, b0, kp);
 
417
        f_nround(b0, b1, kp);
 
418
        f_nround(b1, b0, kp);
 
419
        f_nround(b0, b1, kp);
 
420
        f_nround(b1, b0, kp);
 
421
        f_nround(b0, b1, kp);
 
422
        f_nround(b1, b0, kp);
 
423
        f_lround(b0, b1, kp);
 
424
 
 
425
        out_blk[0] = io_swap(b0[0]);
 
426
        out_blk[1] = io_swap(b0[1]);
 
427
        out_blk[2] = io_swap(b0[2]);
 
428
        out_blk[3] = io_swap(b0[3]);
 
429
}
 
430
 
 
431
/* decrypt a block of text      */
 
432
 
 
433
#define i_nround(bo, bi, k) \
 
434
do { \
 
435
        i_rn(bo, bi, 0, k);             \
 
436
        i_rn(bo, bi, 1, k);             \
 
437
        i_rn(bo, bi, 2, k);             \
 
438
        i_rn(bo, bi, 3, k);             \
 
439
        k -= 4;                                 \
 
440
} while (0)
 
441
 
 
442
#define i_lround(bo, bi, k) \
 
443
do { \
 
444
        i_rl(bo, bi, 0, k);             \
 
445
        i_rl(bo, bi, 1, k);             \
 
446
        i_rl(bo, bi, 2, k);             \
 
447
        i_rl(bo, bi, 3, k);             \
 
448
} while (0)
 
449
 
 
450
void
 
451
rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
 
452
{
 
453
        u4byte          b0[4],
 
454
                                b1[4],
 
455
                           *kp;
 
456
        u4byte          k_len = ctx->k_len;
 
457
        u4byte     *e_key = ctx->e_key;
 
458
        u4byte     *d_key = ctx->d_key;
 
459
 
 
460
        b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
 
461
        b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
 
462
        b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
 
463
        b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
 
464
 
 
465
        kp = d_key + 4 * (k_len + 5);
 
466
 
 
467
        if (k_len > 6)
 
468
        {
 
469
                i_nround(b1, b0, kp);
 
470
                i_nround(b0, b1, kp);
 
471
        }
 
472
 
 
473
        if (k_len > 4)
 
474
        {
 
475
                i_nround(b1, b0, kp);
 
476
                i_nround(b0, b1, kp);
 
477
        }
 
478
 
 
479
        i_nround(b1, b0, kp);
 
480
        i_nround(b0, b1, kp);
 
481
        i_nround(b1, b0, kp);
 
482
        i_nround(b0, b1, kp);
 
483
        i_nround(b1, b0, kp);
 
484
        i_nround(b0, b1, kp);
 
485
        i_nround(b1, b0, kp);
 
486
        i_nround(b0, b1, kp);
 
487
        i_nround(b1, b0, kp);
 
488
        i_lround(b0, b1, kp);
 
489
 
 
490
        out_blk[0] = io_swap(b0[0]);
 
491
        out_blk[1] = io_swap(b0[1]);
 
492
        out_blk[2] = io_swap(b0[2]);
 
493
        out_blk[3] = io_swap(b0[3]);
 
494
}
 
495
 
 
496
/*
 
497
 * conventional interface
 
498
 *
 
499
 * ATM it hopes all data is 4-byte aligned - which
 
500
 * should be true for PX.  -marko
 
501
 */
 
502
 
 
503
void
 
504
aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
 
505
{
 
506
        uint32     *k;
 
507
 
 
508
        k = (uint32 *) key;
 
509
        rijndael_set_key(ctx, k, keybits, enc);
 
510
}
 
511
 
 
512
void
 
513
aes_ecb_encrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
 
514
{
 
515
        unsigned        bs = 16;
 
516
        uint32     *d;
 
517
 
 
518
        while (len >= bs)
 
519
        {
 
520
                d = (uint32 *) data;
 
521
                rijndael_encrypt(ctx, d, d);
 
522
 
 
523
                len -= bs;
 
524
                data += bs;
 
525
        }
 
526
}
 
527
 
 
528
void
 
529
aes_ecb_decrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
 
530
{
 
531
        unsigned        bs = 16;
 
532
        uint32     *d;
 
533
 
 
534
        while (len >= bs)
 
535
        {
 
536
                d = (uint32 *) data;
 
537
                rijndael_decrypt(ctx, d, d);
 
538
 
 
539
                len -= bs;
 
540
                data += bs;
 
541
        }
 
542
}
 
543
 
 
544
void
 
545
aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
 
546
{
 
547
        uint32     *iv = (uint32 *) iva;
 
548
        uint32     *d = (uint32 *) data;
 
549
        unsigned        bs = 16;
 
550
 
 
551
        while (len >= bs)
 
552
        {
 
553
                d[0] ^= iv[0];
 
554
                d[1] ^= iv[1];
 
555
                d[2] ^= iv[2];
 
556
                d[3] ^= iv[3];
 
557
 
 
558
                rijndael_encrypt(ctx, d, d);
 
559
 
 
560
                iv = d;
 
561
                d += bs / 4;
 
562
                len -= bs;
 
563
        }
 
564
}
 
565
 
 
566
void
 
567
aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
 
568
{
 
569
        uint32     *d = (uint32 *) data;
 
570
        unsigned        bs = 16;
 
571
        uint32          buf[4],
 
572
                                iv[4];
 
573
 
 
574
        memcpy(iv, iva, bs);
 
575
        while (len >= bs)
 
576
        {
 
577
                buf[0] = d[0];
 
578
                buf[1] = d[1];
 
579
                buf[2] = d[2];
 
580
                buf[3] = d[3];
 
581
 
 
582
                rijndael_decrypt(ctx, buf, d);
 
583
 
 
584
                d[0] ^= iv[0];
 
585
                d[1] ^= iv[1];
 
586
                d[2] ^= iv[2];
 
587
                d[3] ^= iv[3];
 
588
 
 
589
                iv[0] = buf[0];
 
590
                iv[1] = buf[1];
 
591
                iv[2] = buf[2];
 
592
                iv[3] = buf[3];
 
593
                d += 4;
 
594
                len -= bs;
 
595
        }
 
596
}
 
597
 
 
598
/*
 
599
 * pre-calculate tables.
 
600
 *
 
601
 * On i386 lifts 17k from .bss to .rodata
 
602
 * and avoids 1k code and setup time.
 
603
 *        -marko
 
604
 */
 
605
#ifdef PRINT_TABS
 
606
 
 
607
static void
 
608
show256u8(char *name, uint8 *data)
 
609
{
 
610
        int                     i;
 
611
 
 
612
        printf("static const u1byte  %s[256] = {\n  ", name);
 
613
        for (i = 0; i < 256;)
 
614
        {
 
615
                printf("%u", pow_tab[i++]);
 
616
                if (i < 256)
 
617
                        printf(i % 16 ? ", " : ",\n  ");
 
618
        }
 
619
        printf("\n};\n\n");
 
620
}
 
621
 
 
622
 
 
623
static void
 
624
show4x256u32(char *name, uint32 data[4][256])
 
625
{
 
626
        int                     i,
 
627
                                j;
 
628
 
 
629
        printf("static const u4byte  %s[4][256] = {\n{\n  ", name);
 
630
        for (i = 0; i < 4; i++)
 
631
        {
 
632
                for (j = 0; j < 256;)
 
633
                {
 
634
                        printf("0x%08x", data[i][j]);
 
635
                        j++;
 
636
                        if (j < 256)
 
637
                                printf(j % 4 ? ", " : ",\n  ");
 
638
                }
 
639
                printf(i < 3 ? "\n}, {\n  " : "\n}\n");
 
640
        }
 
641
        printf("};\n\n");
 
642
}
 
643
 
 
644
int
 
645
main()
 
646
{
 
647
        int                     i;
 
648
        char       *hdr = "/* Generated by rijndael.c */\n\n";
 
649
 
 
650
        gen_tabs();
 
651
 
 
652
        printf(hdr);
 
653
        show256u8("pow_tab", pow_tab);
 
654
        show256u8("log_tab", log_tab);
 
655
        show256u8("sbx_tab", sbx_tab);
 
656
        show256u8("isb_tab", isb_tab);
 
657
 
 
658
        show4x256u32("ft_tab", ft_tab);
 
659
        show4x256u32("it_tab", it_tab);
 
660
#ifdef LARGE_TABLES
 
661
        show4x256u32("fl_tab", fl_tab);
 
662
        show4x256u32("il_tab", il_tab);
 
663
#endif
 
664
        printf("static const u4byte rco_tab[10] = {\n  ");
 
665
        for (i = 0; i < 10; i++)
 
666
        {
 
667
                printf("0x%08x", rco_tab[i]);
 
668
                if (i < 9)
 
669
                        printf(", ");
 
670
                if (i == 4)
 
671
                        printf("\n  ");
 
672
        }
 
673
        printf("\n};\n\n");
 
674
        return 0;
 
675
}
 
676
 
 
677
#endif