~siretart/keepassx/debian

« back to all changes in this revision

Viewing changes to src/crypto/aeskey.c

  • Committer: Reinhard Tartler
  • Date: 2009-04-04 09:16:12 UTC
  • mfrom: (4.1.3 upstream)
  • Revision ID: siretart@tauware.de-20090404091612-0e9uiu3u5xz3da6q
merge new upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 /**************************************************************************
2
 
 *                                                                         *
3
 
 *   Copyright (C) 2007 by Tarek Saidi <tarek.saidi@arcor.de>              *
4
 
 *   Copyright (c) 2003 Dr Brian Gladman, Worcester, UK                    *
5
 
 *                                                                         *
6
 
 *   This program is free software; you can redistribute it and/or modify  *
7
 
 *   it under the terms of the GNU General Public License as published by  *
8
 
 *   the Free Software Foundation; version 2 of the License.               *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
 ***************************************************************************/
 
1
/*
 
2
 ---------------------------------------------------------------------------
 
3
 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
 
4
 
 
5
 LICENSE TERMS
 
6
 
 
7
 The redistribution and use of this software (with or without changes)
 
8
 is allowed without the payment of fees or royalties provided that:
 
9
 
 
10
  1. source code distributions include the above copyright notice, this
 
11
     list of conditions and the following disclaimer;
 
12
 
 
13
  2. binary distributions include the above copyright notice, this list
 
14
     of conditions and the following disclaimer in their documentation;
 
15
 
 
16
  3. the name of the copyright holder is not used to endorse products
 
17
     built using this software without specific written permission.
 
18
 
 
19
 DISCLAIMER
 
20
 
 
21
 This software is provided 'as is' with no explicit or implied warranties
 
22
 in respect of its properties, including, but not limited to, correctness
 
23
 and/or fitness for purpose.
 
24
 ---------------------------------------------------------------------------
 
25
 Issue Date: 20/12/2007
 
26
*/
20
27
 
21
28
#include "aesopt.h"
22
29
#include "aestab.h"
23
30
 
24
31
#ifdef USE_VIA_ACE_IF_PRESENT
25
 
#include "via_ace.h"
 
32
#  include "aes_via_ace.h"
26
33
#endif
27
34
 
28
35
#if defined(__cplusplus)
48
55
    cx->n_col = 8   29 23 19 17 14
49
56
*/
50
57
 
 
58
#if defined( REDUCE_CODE_SIZE )
 
59
#  define ls_box ls_sub
 
60
   uint_32t ls_sub(const uint_32t t, const uint_32t n);
 
61
#  define inv_mcol im_sub
 
62
   uint_32t im_sub(const uint_32t x);
 
63
#  ifdef ENC_KS_UNROLL
 
64
#    undef ENC_KS_UNROLL
 
65
#  endif
 
66
#  ifdef DEC_KS_UNROLL
 
67
#    undef DEC_KS_UNROLL
 
68
#  endif
 
69
#endif
 
70
 
51
71
#if (FUNCS_IN_C & ENC_KEYING_IN_C)
52
72
 
53
 
#if defined(AES_128) || defined(AES_VAR)
 
73
#if defined(AES_128) || defined( AES_VAR )
54
74
 
55
75
#define ke4(k,i) \
56
76
{   k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
59
79
    k[4*(i)+7] = ss[3] ^= ss[2]; \
60
80
}
61
81
 
62
 
aes_rval aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
 
82
AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
63
83
{   uint_32t    ss[4];
64
84
 
65
85
    cx->ks[0] = ss[0] = word_in(key, 0);
67
87
    cx->ks[2] = ss[2] = word_in(key, 2);
68
88
    cx->ks[3] = ss[3] = word_in(key, 3);
69
89
 
70
 
#if ENC_UNROLL == NONE
71
 
    {   uint_32t i;
72
 
        for(i = 0; i < 9; ++i)
73
 
            ke4(cx->ks, i);
74
 
    }
75
 
#else
 
90
#ifdef ENC_KS_UNROLL
76
91
    ke4(cx->ks, 0);  ke4(cx->ks, 1);
77
92
    ke4(cx->ks, 2);  ke4(cx->ks, 3);
78
93
    ke4(cx->ks, 4);  ke4(cx->ks, 5);
79
94
    ke4(cx->ks, 6);  ke4(cx->ks, 7);
80
95
    ke4(cx->ks, 8);
 
96
#else
 
97
    {   uint_32t i;
 
98
        for(i = 0; i < 9; ++i)
 
99
            ke4(cx->ks, i);
 
100
    }
81
101
#endif
82
102
    ke4(cx->ks, 9);
83
103
    cx->inf.l = 0;
87
107
    if(VIA_ACE_AVAILABLE)
88
108
        cx->inf.b[1] = 0xff;
89
109
#endif
90
 
 
91
 
#if defined( AES_ERR_CHK )
92
110
    return EXIT_SUCCESS;
93
 
#endif
94
111
}
95
112
 
96
113
#endif
97
114
 
98
 
#if defined(AES_192) || defined(AES_VAR)
 
115
#if defined(AES_192) || defined( AES_VAR )
99
116
 
100
117
#define kef6(k,i) \
101
118
{   k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
110
127
    k[6*(i)+11] = ss[5] ^= ss[4]; \
111
128
}
112
129
 
113
 
aes_rval aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
 
130
AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
114
131
{   uint_32t    ss[6];
115
132
 
116
133
    cx->ks[0] = ss[0] = word_in(key, 0);
120
137
    cx->ks[4] = ss[4] = word_in(key, 4);
121
138
    cx->ks[5] = ss[5] = word_in(key, 5);
122
139
 
123
 
#if ENC_UNROLL == NONE
124
 
    {   uint_32t i;
125
 
        for(i = 0; i < 7; ++i)
126
 
            ke6(cx->ks, i);
127
 
    }
128
 
#else
 
140
#ifdef ENC_KS_UNROLL
129
141
    ke6(cx->ks, 0);  ke6(cx->ks, 1);
130
142
    ke6(cx->ks, 2);  ke6(cx->ks, 3);
131
143
    ke6(cx->ks, 4);  ke6(cx->ks, 5);
132
144
    ke6(cx->ks, 6);
 
145
#else
 
146
    {   uint_32t i;
 
147
        for(i = 0; i < 7; ++i)
 
148
            ke6(cx->ks, i);
 
149
    }
133
150
#endif
134
151
    kef6(cx->ks, 7);
135
152
    cx->inf.l = 0;
139
156
    if(VIA_ACE_AVAILABLE)
140
157
        cx->inf.b[1] = 0xff;
141
158
#endif
142
 
 
143
 
#if defined( AES_ERR_CHK )
144
159
    return EXIT_SUCCESS;
145
 
#endif
146
160
}
147
161
 
148
162
#endif
149
163
 
150
 
#if defined(AES_256) || defined(AES_VAR)
 
164
#if defined(AES_256) || defined( AES_VAR )
151
165
 
152
166
#define kef8(k,i) \
153
167
{   k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
164
178
    k[8*(i)+15] = ss[7] ^= ss[6]; \
165
179
}
166
180
 
167
 
aes_rval aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
 
181
AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
168
182
{   uint_32t    ss[8];
169
183
 
170
184
    cx->ks[0] = ss[0] = word_in(key, 0);
176
190
    cx->ks[6] = ss[6] = word_in(key, 6);
177
191
    cx->ks[7] = ss[7] = word_in(key, 7);
178
192
 
179
 
#if ENC_UNROLL == NONE
180
 
    {   uint_32t i;
181
 
        for(i = 0; i < 6; ++i)
182
 
            ke8(cx->ks,  i);
183
 
    }
184
 
#else
 
193
#ifdef ENC_KS_UNROLL
185
194
    ke8(cx->ks, 0); ke8(cx->ks, 1);
186
195
    ke8(cx->ks, 2); ke8(cx->ks, 3);
187
196
    ke8(cx->ks, 4); ke8(cx->ks, 5);
 
197
#else
 
198
    {   uint_32t i;
 
199
        for(i = 0; i < 6; ++i)
 
200
            ke8(cx->ks,  i);
 
201
    }
188
202
#endif
189
203
    kef8(cx->ks, 6);
190
204
    cx->inf.l = 0;
194
208
    if(VIA_ACE_AVAILABLE)
195
209
        cx->inf.b[1] = 0xff;
196
210
#endif
197
 
 
198
 
#if defined( AES_ERR_CHK )
199
211
    return EXIT_SUCCESS;
200
 
#endif
201
212
}
202
213
 
203
214
#endif
204
215
 
205
 
#if defined(AES_VAR)
 
216
#if defined( AES_VAR )
206
217
 
207
 
aes_rval aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
208
 
{
 
218
AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
 
219
{   
209
220
    switch(key_len)
210
221
    {
211
 
#if defined( AES_ERR_CHK )
212
222
    case 16: case 128: return aes_encrypt_key128(key, cx);
213
223
    case 24: case 192: return aes_encrypt_key192(key, cx);
214
224
    case 32: case 256: return aes_encrypt_key256(key, cx);
215
225
    default: return EXIT_FAILURE;
216
 
#else
217
 
    case 16: case 128: aes_encrypt_key128(key, cx); return;
218
 
    case 24: case 192: aes_encrypt_key192(key, cx); return;
219
 
    case 32: case 256: aes_encrypt_key256(key, cx); return;
220
 
#endif
221
226
    }
222
227
}
223
228
 
245
250
#endif
246
251
#endif
247
252
 
248
 
#if defined(AES_128) || defined(AES_VAR)
 
253
#if defined(AES_128) || defined( AES_VAR )
249
254
 
250
255
#define k4e(k,i) \
251
256
{   k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
311
316
 
312
317
#endif
313
318
 
314
 
aes_rval aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
 
319
AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
315
320
{   uint_32t    ss[5];
316
321
#if defined( d_vars )
317
322
        d_vars;
321
326
    cx->ks[v(40,(2))] = ss[2] = word_in(key, 2);
322
327
    cx->ks[v(40,(3))] = ss[3] = word_in(key, 3);
323
328
 
324
 
#if DEC_UNROLL == NONE
 
329
#ifdef DEC_KS_UNROLL
 
330
     kdf4(cx->ks, 0); kd4(cx->ks, 1);
 
331
     kd4(cx->ks, 2);  kd4(cx->ks, 3);
 
332
     kd4(cx->ks, 4);  kd4(cx->ks, 5);
 
333
     kd4(cx->ks, 6);  kd4(cx->ks, 7);
 
334
     kd4(cx->ks, 8);  kdl4(cx->ks, 9);
 
335
#else
325
336
    {   uint_32t i;
326
337
        for(i = 0; i < 10; ++i)
327
338
            k4e(cx->ks, i);
330
341
            cx->ks[i] = inv_mcol(cx->ks[i]);
331
342
#endif
332
343
    }
333
 
#else
334
 
    kdf4(cx->ks, 0);  kd4(cx->ks, 1);
335
 
     kd4(cx->ks, 2);  kd4(cx->ks, 3);
336
 
     kd4(cx->ks, 4);  kd4(cx->ks, 5);
337
 
     kd4(cx->ks, 6);  kd4(cx->ks, 7);
338
 
     kd4(cx->ks, 8); kdl4(cx->ks, 9);
339
344
#endif
340
345
    cx->inf.l = 0;
341
346
    cx->inf.b[0] = 10 * 16;
344
349
    if(VIA_ACE_AVAILABLE)
345
350
        cx->inf.b[1] = 0xff;
346
351
#endif
347
 
 
348
 
#if defined( AES_ERR_CHK )
349
352
    return EXIT_SUCCESS;
350
 
#endif
351
353
}
352
354
 
353
355
#endif
354
356
 
355
 
#if defined(AES_192) || defined(AES_VAR)
 
357
#if defined(AES_192) || defined( AES_VAR )
356
358
 
357
359
#define k6ef(k,i) \
358
360
{   k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
393
395
    ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \
394
396
}
395
397
 
396
 
aes_rval aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
 
398
AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
397
399
{   uint_32t    ss[7];
398
400
#if defined( d_vars )
399
401
        d_vars;
403
405
    cx->ks[v(48,(2))] = ss[2] = word_in(key, 2);
404
406
    cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);
405
407
 
406
 
#if DEC_UNROLL == NONE
 
408
#ifdef DEC_KS_UNROLL
 
409
    cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
 
410
    cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
 
411
    kdf6(cx->ks, 0); kd6(cx->ks, 1);
 
412
    kd6(cx->ks, 2);  kd6(cx->ks, 3);
 
413
    kd6(cx->ks, 4);  kd6(cx->ks, 5);
 
414
    kd6(cx->ks, 6);  kdl6(cx->ks, 7);
 
415
#else
407
416
    cx->ks[v(48,(4))] = ss[4] = word_in(key, 4);
408
417
    cx->ks[v(48,(5))] = ss[5] = word_in(key, 5);
409
418
    {   uint_32t i;
416
425
            cx->ks[i] = inv_mcol(cx->ks[i]);
417
426
#endif
418
427
    }
419
 
#else
420
 
    cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
421
 
    cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
422
 
    kdf6(cx->ks, 0); kd6(cx->ks, 1);
423
 
    kd6(cx->ks, 2);  kd6(cx->ks, 3);
424
 
    kd6(cx->ks, 4);  kd6(cx->ks, 5);
425
 
    kd6(cx->ks, 6); kdl6(cx->ks, 7);
426
428
#endif
427
429
    cx->inf.l = 0;
428
430
    cx->inf.b[0] = 12 * 16;
431
433
    if(VIA_ACE_AVAILABLE)
432
434
        cx->inf.b[1] = 0xff;
433
435
#endif
434
 
 
435
 
#if defined( AES_ERR_CHK )
436
436
    return EXIT_SUCCESS;
437
 
#endif
438
437
}
439
438
 
440
439
#endif
441
440
 
442
 
#if defined(AES_256) || defined(AES_VAR)
 
441
#if defined(AES_256) || defined( AES_VAR )
443
442
 
444
443
#define k8ef(k,i) \
445
444
{   k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
487
486
    ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \
488
487
}
489
488
 
490
 
aes_rval aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
 
489
AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
491
490
{   uint_32t    ss[9];
492
491
#if defined( d_vars )
493
492
        d_vars;
497
496
    cx->ks[v(56,(2))] = ss[2] = word_in(key, 2);
498
497
    cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);
499
498
 
500
 
#if DEC_UNROLL == NONE
501
 
    cx->ks[v(56,(4))] = ss[4] = word_in(key, 4);
502
 
    cx->ks[v(56,(5))] = ss[5] = word_in(key, 5);
503
 
    cx->ks[v(56,(6))] = ss[6] = word_in(key, 6);
504
 
    cx->ks[v(56,(7))] = ss[7] = word_in(key, 7);
505
 
    {   uint_32t i;
506
 
 
507
 
        for(i = 0; i < 6; ++i)
508
 
            k8e(cx->ks,  i);
509
 
        k8ef(cx->ks,  6);
510
 
#if !(DEC_ROUND == NO_TABLES)
511
 
        for(i = N_COLS; i < 14 * N_COLS; ++i)
512
 
            cx->ks[i] = inv_mcol(cx->ks[i]);
513
 
 
514
 
#endif
515
 
    }
516
 
#else
 
499
#ifdef DEC_KS_UNROLL
517
500
    cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));
518
501
    cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));
519
502
    cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));
522
505
    kd8(cx->ks, 2);  kd8(cx->ks, 3);
523
506
    kd8(cx->ks, 4);  kd8(cx->ks, 5);
524
507
    kdl8(cx->ks, 6);
 
508
#else
 
509
    cx->ks[v(56,(4))] = ss[4] = word_in(key, 4);
 
510
    cx->ks[v(56,(5))] = ss[5] = word_in(key, 5);
 
511
    cx->ks[v(56,(6))] = ss[6] = word_in(key, 6);
 
512
    cx->ks[v(56,(7))] = ss[7] = word_in(key, 7);
 
513
    {   uint_32t i;
 
514
 
 
515
        for(i = 0; i < 6; ++i)
 
516
            k8e(cx->ks,  i);
 
517
        k8ef(cx->ks,  6);
 
518
#if !(DEC_ROUND == NO_TABLES)
 
519
        for(i = N_COLS; i < 14 * N_COLS; ++i)
 
520
            cx->ks[i] = inv_mcol(cx->ks[i]);
 
521
#endif
 
522
    }
525
523
#endif
526
524
    cx->inf.l = 0;
527
525
    cx->inf.b[0] = 14 * 16;
530
528
    if(VIA_ACE_AVAILABLE)
531
529
        cx->inf.b[1] = 0xff;
532
530
#endif
533
 
 
534
 
#if defined( AES_ERR_CHK )
535
531
    return EXIT_SUCCESS;
536
 
#endif
537
532
}
538
533
 
539
534
#endif
540
535
 
541
 
#if defined(AES_VAR)
 
536
#if defined( AES_VAR )
542
537
 
543
 
aes_rval aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
 
538
AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
544
539
{
545
540
    switch(key_len)
546
541
    {
547
 
#if defined( AES_ERR_CHK )
548
542
    case 16: case 128: return aes_decrypt_key128(key, cx);
549
543
    case 24: case 192: return aes_decrypt_key192(key, cx);
550
544
    case 32: case 256: return aes_decrypt_key256(key, cx);
551
545
    default: return EXIT_FAILURE;
552
 
#else
553
 
    case 16: case 128: aes_decrypt_key128(key, cx); return;
554
 
    case 24: case 192: aes_decrypt_key192(key, cx); return;
555
 
    case 32: case 256: aes_decrypt_key256(key, cx); return;
556
 
#endif
557
546
    }
558
547
}
559
548