~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/lib/libgcrypt/cipher/rijndael.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Rijndael (AES) for GnuPG
2
2
 * Copyright (C) 2000, 2001, 2002, 2003, 2007,
3
 
 *               2008 Free Software Foundation, Inc.
 
3
 *               2008, 2011 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of Libgcrypt.
6
6
 *
51
51
#define BLOCKSIZE               (128/8)
52
52
 
53
53
 
 
54
/* Helper macro to force alignment to 16 bytes.  */
 
55
#ifdef __GNUC__
 
56
# define ATTR_ALIGNED_16  __attribute__ ((aligned (16)))
 
57
#else
 
58
# define ATTR_ALIGNED_16
 
59
#endif
 
60
 
 
61
 
54
62
/* USE_PADLOCK indicates whether to compile the padlock specific
55
63
   code.  */
56
64
#undef USE_PADLOCK
57
65
#ifdef ENABLE_PADLOCK_SUPPORT
58
66
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
59
 
# define USE_PADLOCK
 
67
#  define USE_PADLOCK 1
60
68
# endif
61
69
#endif /*ENABLE_PADLOCK_SUPPORT*/
62
70
 
63
 
static const char *selftest(void);
64
 
 
65
 
typedef struct 
 
71
/* USE_AESNI inidicates whether to compile with Intel AES-NI code.  We
 
72
   need the vector-size attribute which seems to be available since
 
73
   gcc 3.  However, to be on the safe side we require at least gcc 4.  */
 
74
#undef USE_AESNI
 
75
#ifdef ENABLE_AESNI_SUPPORT
 
76
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && __GNUC__ >= 4
 
77
#  define USE_AESNI 1
 
78
# endif
 
79
#endif /* ENABLE_AESNI_SUPPORT */
 
80
 
 
81
#ifdef USE_AESNI
 
82
  typedef int m128i_t __attribute__ ((__vector_size__ (16)));
 
83
#endif /*USE_AESNI*/
 
84
 
 
85
/* Define an u32 variant for the sake of gcc 4.4's strict aliasing.  */
 
86
#if __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 )
 
87
typedef u32           __attribute__ ((__may_alias__)) u32_a_t;
 
88
#else
 
89
typedef u32           u32_a_t;
 
90
#endif
 
91
 
 
92
 
 
93
 
 
94
/* Our context object.  */
 
95
typedef struct
66
96
{
67
 
  int   ROUNDS;             /* Key-length-dependent number of rounds.  */
 
97
  /* The first fields are the keyschedule arrays.  This is so that
 
98
     they are aligned on a 16 byte boundary if using gcc.  This
 
99
     alignment is required for the AES-NI code and a good idea in any
 
100
     case.  The alignment is guaranteed due to the way cipher.c
 
101
     allocates the space for the context.  The PROPERLY_ALIGNED_TYPE
 
102
     hack is used to force a minimal alignment if not using gcc of if
 
103
     the alignment requirement is higher that 16 bytes.  */
 
104
  union
 
105
  {
 
106
    PROPERLY_ALIGNED_TYPE dummy;
 
107
    byte keyschedule[MAXROUNDS+1][4][4];
 
108
#ifdef USE_PADLOCK
 
109
    /* The key as passed to the padlock engine.  It is only used if
 
110
       the padlock engine is used (USE_PADLOCK, below).  */
 
111
    unsigned char padlock_key[16] __attribute__ ((aligned (16)));
 
112
#endif /*USE_PADLOCK*/
 
113
  } u1;
 
114
  union
 
115
  {
 
116
    PROPERLY_ALIGNED_TYPE dummy;
 
117
    byte keyschedule[MAXROUNDS+1][4][4];
 
118
  } u2;
 
119
  int rounds;               /* Key-length-dependent number of rounds.  */
68
120
  int decryption_prepared;  /* The decryption key schedule is available.  */
69
121
#ifdef USE_PADLOCK
70
122
  int use_padlock;          /* Padlock shall be used.  */
71
 
  /* The key as passed to the padlock engine.  */
72
 
  unsigned char padlock_key[16] __attribute__ ((aligned (16)));
 
123
#endif /*USE_PADLOCK*/
 
124
#ifdef USE_AESNI
 
125
  int use_aesni;            /* AES-NI shall be used.  */
 
126
#endif /*USE_AESNI*/
 
127
} RIJNDAEL_context ATTR_ALIGNED_16;
 
128
 
 
129
/* Macros defining alias for the keyschedules.  */
 
130
#define keyschenc  u1.keyschedule
 
131
#define keyschdec  u2.keyschedule
 
132
#define padlockkey u1.padlock_key
 
133
 
 
134
/* Two macros to be called prior and after the use of AESNI
 
135
   instructions.  There should be no external function calls between
 
136
   the use of these macros.  There purpose is to make sure that the
 
137
   SSE regsiters are cleared and won't reveal any information about
 
138
   the key or the data.  */
 
139
#ifdef USE_AESNI
 
140
# define aesni_prepare() do { } while (0)
 
141
# define aesni_cleanup()                                                \
 
142
  do { asm volatile ("pxor %%xmm0, %%xmm0\n\t"                          \
 
143
                     "pxor %%xmm1, %%xmm1\n" :: );                      \
 
144
  } while (0)
 
145
# define aesni_cleanup_2_4()                                            \
 
146
  do { asm volatile ("pxor %%xmm2, %%xmm2\n\t"                          \
 
147
                     "pxor %%xmm3, %%xmm3\n"                            \
 
148
                     "pxor %%xmm4, %%xmm4\n":: );                       \
 
149
  } while (0)
 
150
#else
 
151
# define aesni_prepare() do { } while (0)
 
152
# define aesni_cleanup() do { } while (0)
73
153
#endif
74
 
  union
75
 
  {
76
 
    PROPERLY_ALIGNED_TYPE dummy;
77
 
    byte keyschedule[MAXROUNDS+1][4][4];
78
 
  } u1;
79
 
  union
80
 
  {
81
 
    PROPERLY_ALIGNED_TYPE dummy;
82
 
    byte keyschedule[MAXROUNDS+1][4][4];        
83
 
  } u2;
84
 
} RIJNDAEL_context;
85
154
 
86
 
#define keySched  u1.keyschedule
87
 
#define keySched2 u2.keyschedule
88
155
 
89
156
/* All the numbers.  */
90
157
#include "rijndael-tables.h"
91
158
 
92
159
 
93
 
/* Perform the key setup.  */  
 
160
 
 
161
/* Function prototypes.  */
 
162
#ifdef USE_AESNI
 
163
/* We don't want to inline these functions to help gcc allocate enough
 
164
   registers.  */
 
165
static void do_aesni_ctr (const RIJNDAEL_context *ctx, unsigned char *ctr,
 
166
                          unsigned char *b, const unsigned char *a)
 
167
  __attribute__ ((__noinline__));
 
168
static void do_aesni_ctr_4 (const RIJNDAEL_context *ctx, unsigned char *ctr,
 
169
                            unsigned char *b, const unsigned char *a)
 
170
  __attribute__ ((__noinline__));
 
171
#endif /*USE_AESNI*/
 
172
 
 
173
static const char *selftest(void);
 
174
 
 
175
 
 
176
 
 
177
/* Perform the key setup.  */
94
178
static gcry_err_code_t
95
179
do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen)
96
180
{
97
181
  static int initialized = 0;
98
182
  static const char *selftest_failed=0;
99
 
  int ROUNDS;
 
183
  int rounds;
100
184
  int i,j, r, t, rconpointer = 0;
101
185
  int KC;
102
186
  union
110
194
    PROPERLY_ALIGNED_TYPE dummy;
111
195
    byte tk[MAXKC][4];
112
196
  } tk;
113
 
#define tk tk.tk  
 
197
#define tk tk.tk
114
198
 
115
199
  /* The on-the-fly self tests are only run in non-fips mode. In fips
116
200
     mode explicit self-tests are required.  Actually the on-the-fly
117
201
     self-tests are not fully thread-safe and it might happen that a
118
 
     failed self-test won't get noticed in another thread.  
 
202
     failed self-test won't get noticed in another thread.
119
203
 
120
204
     FIXME: We might want to have a central registry of succeeded
121
205
     self-tests. */
133
217
#ifdef USE_PADLOCK
134
218
  ctx->use_padlock = 0;
135
219
#endif
 
220
#ifdef USE_AESNI
 
221
  ctx->use_aesni = 0;
 
222
#endif
136
223
 
137
224
  if( keylen == 128/8 )
138
225
    {
139
 
      ROUNDS = 10;
 
226
      rounds = 10;
140
227
      KC = 4;
 
228
 
 
229
      if (0)
 
230
        ;
141
231
#ifdef USE_PADLOCK
142
 
      if ((_gcry_get_hw_features () & HWF_PADLOCK_AES))
 
232
      else if ((_gcry_get_hw_features () & HWF_PADLOCK_AES))
143
233
        {
144
234
          ctx->use_padlock = 1;
145
 
          memcpy (ctx->padlock_key, key, keylen);
 
235
          memcpy (ctx->padlockkey, key, keylen);
 
236
        }
 
237
#endif
 
238
#ifdef USE_AESNI
 
239
      else if ((_gcry_get_hw_features () & HWF_INTEL_AESNI))
 
240
        {
 
241
          ctx->use_aesni = 1;
146
242
        }
147
243
#endif
148
244
    }
149
245
  else if ( keylen == 192/8 )
150
246
    {
151
 
      ROUNDS = 12;
 
247
      rounds = 12;
152
248
      KC = 6;
 
249
 
 
250
      if (0)
 
251
        {
 
252
          ;
 
253
        }
 
254
#ifdef USE_AESNI
 
255
      else if ((_gcry_get_hw_features () & HWF_INTEL_AESNI))
 
256
        {
 
257
          ctx->use_aesni = 1;
 
258
        }
 
259
#endif
153
260
    }
154
261
  else if ( keylen == 256/8 )
155
262
    {
156
 
      ROUNDS = 14;
 
263
      rounds = 14;
157
264
      KC = 8;
 
265
 
 
266
      if (0)
 
267
        {
 
268
          ;
 
269
        }
 
270
#ifdef USE_AESNI
 
271
      else if ((_gcry_get_hw_features () & HWF_INTEL_AESNI))
 
272
        {
 
273
          ctx->use_aesni = 1;
 
274
        }
 
275
#endif
158
276
    }
159
277
  else
160
278
    return GPG_ERR_INV_KEYLEN;
161
279
 
162
 
  ctx->ROUNDS = ROUNDS;
163
 
 
164
 
#ifdef USE_PADLOCK
165
 
  if (ctx->use_padlock)
 
280
  ctx->rounds = rounds;
 
281
 
 
282
  /* NB: We don't yet support Padlock hardware key generation.  */
 
283
 
 
284
  if (0)
 
285
    ;
 
286
#ifdef USE_AESNI_is_disabled_here
 
287
  else if (ctx->use_aesni && ctx->rounds == 10)
166
288
    {
167
 
      /* Nothing to do as we support only hardware key generation for
168
 
         now.  */
 
289
      /* Note: This code works for AES-128 but it is not much better
 
290
         than using the standard key schedule.  We disable it for
 
291
         now and don't put any effort into implementing this for
 
292
         AES-192 and AES-256.  */
 
293
      asm volatile ("movl   %[key], %%esi\n\t"
 
294
                    "movdqu (%%esi), %%xmm1\n\t"     /* xmm1 := key   */
 
295
                    "movl   %[ksch], %%esi\n\t"
 
296
                    "movdqa %%xmm1, (%%esi)\n\t"     /* ksch[0] := xmm1  */
 
297
                    "aeskeygenassist $0x01, %%xmm1, %%xmm2\n\t"
 
298
                    "call .Lexpand128_%=\n\t"
 
299
                    "movdqa %%xmm1, 0x10(%%esi)\n\t" /* ksch[1] := xmm1  */
 
300
                    "aeskeygenassist $0x02, %%xmm1, %%xmm2\n\t"
 
301
                    "call .Lexpand128_%=\n\t"
 
302
                    "movdqa %%xmm1, 0x20(%%esi)\n\t" /* ksch[2] := xmm1  */
 
303
                    "aeskeygenassist $0x04, %%xmm1, %%xmm2\n\t"
 
304
                    "call .Lexpand128_%=\n\t"
 
305
                    "movdqa %%xmm1, 0x30(%%esi)\n\t" /* ksch[3] := xmm1  */
 
306
                    "aeskeygenassist $0x08, %%xmm1, %%xmm2\n\t"
 
307
                    "call .Lexpand128_%=\n\t"
 
308
                    "movdqa %%xmm1, 0x40(%%esi)\n\t" /* ksch[4] := xmm1  */
 
309
                    "aeskeygenassist $0x10, %%xmm1, %%xmm2\n\t"
 
310
                    "call .Lexpand128_%=\n\t"
 
311
                    "movdqa %%xmm1, 0x50(%%esi)\n\t" /* ksch[5] := xmm1  */
 
312
                    "aeskeygenassist $0x20, %%xmm1, %%xmm2\n\t"
 
313
                    "call .Lexpand128_%=\n\t"
 
314
                    "movdqa %%xmm1, 0x60(%%esi)\n\t" /* ksch[6] := xmm1  */
 
315
                    "aeskeygenassist $0x40, %%xmm1, %%xmm2\n\t"
 
316
                    "call .Lexpand128_%=\n\t"
 
317
                    "movdqa %%xmm1, 0x70(%%esi)\n\t" /* ksch[7] := xmm1  */
 
318
                    "aeskeygenassist $0x80, %%xmm1, %%xmm2\n\t"
 
319
                    "call .Lexpand128_%=\n\t"
 
320
                    "movdqa %%xmm1, 0x80(%%esi)\n\t" /* ksch[8] := xmm1  */
 
321
                    "aeskeygenassist $0x1b, %%xmm1, %%xmm2\n\t"
 
322
                    "call .Lexpand128_%=\n\t"
 
323
                    "movdqa %%xmm1, 0x90(%%esi)\n\t" /* ksch[9] := xmm1  */
 
324
                    "aeskeygenassist $0x36, %%xmm1, %%xmm2\n\t"
 
325
                    "call .Lexpand128_%=\n\t"
 
326
                    "movdqa %%xmm1, 0xa0(%%esi)\n\t" /* ksch[10] := xmm1  */
 
327
                    "jmp .Lleave%=\n"
 
328
 
 
329
                    ".Lexpand128_%=:\n\t"
 
330
                    "pshufd $0xff, %%xmm2, %%xmm2\n\t"
 
331
                    "movdqa %%xmm1, %%xmm3\n\t"
 
332
                    "pslldq $4, %%xmm3\n\t"
 
333
                    "pxor   %%xmm3, %%xmm1\n\t"
 
334
                    "pslldq $4, %%xmm3\n\t"
 
335
                    "pxor   %%xmm3, %%xmm1\n\t"
 
336
                    "pslldq $4, %%xmm3\n\t"
 
337
                    "pxor   %%xmm3, %%xmm2\n\t"
 
338
                    "pxor   %%xmm2, %%xmm1\n\t"
 
339
                    "ret\n"
 
340
 
 
341
                    ".Lleave%=:\n\t"
 
342
                    "pxor %%xmm1, %%xmm1\n\t"
 
343
                    "pxor %%xmm2, %%xmm2\n\t"
 
344
                    "pxor %%xmm3, %%xmm3\n"
 
345
                    :
 
346
                    : [key] "g" (key), [ksch] "g" (ctx->keyschenc)
 
347
                    : "%esi", "cc", "memory" );
169
348
    }
 
349
#endif /*USE_AESNI*/
170
350
  else
171
 
#endif /*USE_PADLOCK*/
172
351
    {
173
 
#define W (ctx->keySched)
174
 
      for (i = 0; i < keylen; i++) 
 
352
#define W (ctx->keyschenc)
 
353
      for (i = 0; i < keylen; i++)
175
354
        {
176
 
          k[i >> 2][i & 3] = key[i]; 
 
355
          k[i >> 2][i & 3] = key[i];
177
356
        }
178
 
      
179
 
      for (j = KC-1; j >= 0; j--) 
 
357
 
 
358
      for (j = KC-1; j >= 0; j--)
180
359
        {
181
 
          *((u32*)tk[j]) = *((u32*)k[j]);
 
360
          *((u32_a_t*)tk[j]) = *((u32_a_t*)k[j]);
182
361
        }
183
362
      r = 0;
184
363
      t = 0;
185
364
      /* Copy values into round key array.  */
186
 
      for (j = 0; (j < KC) && (r < ROUNDS + 1); )
 
365
      for (j = 0; (j < KC) && (r < rounds + 1); )
187
366
        {
188
367
          for (; (j < KC) && (t < 4); j++, t++)
189
368
            {
190
 
              *((u32*)W[r][t]) = *((u32*)tk[j]);
 
369
              *((u32_a_t*)W[r][t]) = *((u32_a_t*)tk[j]);
191
370
            }
192
371
          if (t == 4)
193
372
            {
195
374
              t = 0;
196
375
            }
197
376
        }
198
 
      
199
 
      while (r < ROUNDS + 1)
 
377
 
 
378
      while (r < rounds + 1)
200
379
        {
201
380
          /* While not enough round key material calculated calculate
202
381
             new values.  */
205
384
          tk[0][2] ^= S[tk[KC-1][3]];
206
385
          tk[0][3] ^= S[tk[KC-1][0]];
207
386
          tk[0][0] ^= rcon[rconpointer++];
208
 
          
 
387
 
209
388
          if (KC != 8)
210
389
            {
211
 
              for (j = 1; j < KC; j++) 
 
390
              for (j = 1; j < KC; j++)
212
391
                {
213
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
 
392
                  *((u32_a_t*)tk[j]) ^= *((u32_a_t*)tk[j-1]);
214
393
                }
215
 
            } 
216
 
          else 
 
394
            }
 
395
          else
217
396
            {
218
397
              for (j = 1; j < KC/2; j++)
219
398
                {
220
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
 
399
                  *((u32_a_t*)tk[j]) ^= *((u32_a_t*)tk[j-1]);
221
400
                }
222
401
              tk[KC/2][0] ^= S[tk[KC/2 - 1][0]];
223
402
              tk[KC/2][1] ^= S[tk[KC/2 - 1][1]];
225
404
              tk[KC/2][3] ^= S[tk[KC/2 - 1][3]];
226
405
              for (j = KC/2 + 1; j < KC; j++)
227
406
                {
228
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
 
407
                  *((u32_a_t*)tk[j]) ^= *((u32_a_t*)tk[j-1]);
229
408
                }
230
409
            }
231
 
          
 
410
 
232
411
          /* Copy values into round key array.  */
233
 
          for (j = 0; (j < KC) && (r < ROUNDS + 1); )
 
412
          for (j = 0; (j < KC) && (r < rounds + 1); )
234
413
            {
235
414
              for (; (j < KC) && (t < 4); j++, t++)
236
415
                {
237
 
                  *((u32*)W[r][t]) = *((u32*)tk[j]);
 
416
                  *((u32_a_t*)W[r][t]) = *((u32_a_t*)tk[j]);
238
417
                }
239
418
              if (t == 4)
240
419
                {
242
421
                  t = 0;
243
422
                }
244
423
            }
245
 
        }               
246
 
#undef W    
 
424
        }
 
425
#undef W
247
426
    }
248
427
 
249
428
  return 0;
268
447
prepare_decryption( RIJNDAEL_context *ctx )
269
448
{
270
449
  int r;
271
 
  union
272
 
  {
273
 
    PROPERLY_ALIGNED_TYPE dummy;
274
 
    byte *w;
275
 
  } w;
 
450
 
 
451
#ifdef USE_AESNI
 
452
  if (ctx->use_aesni)
 
453
    {
 
454
      /* The AES-NI decrypt instructions use the Equivalent Inverse
 
455
         Cipher, thus we can't use the the standard decrypt key
 
456
         preparation.  */
 
457
        m128i_t *ekey = (m128i_t*)ctx->keyschenc;
 
458
        m128i_t *dkey = (m128i_t*)ctx->keyschdec;
 
459
        int rr;
 
460
 
 
461
        dkey[0] = ekey[ctx->rounds];
 
462
        for (r=1, rr=ctx->rounds-1; r < ctx->rounds; r++, rr--)
 
463
          {
 
464
            asm volatile
 
465
              ("movdqu %[ekey], %%xmm1\n\t"
 
466
               /*"aesimc %%xmm1, %%xmm1\n\t"*/
 
467
               ".byte 0x66, 0x0f, 0x38, 0xdb, 0xc9\n\t"
 
468
               "movdqu %%xmm1, %[dkey]"
 
469
               : [dkey] "=m" (dkey[r])
 
470
               : [ekey] "m" (ekey[rr]) );
 
471
          }
 
472
        dkey[r] = ekey[0];
 
473
    }
 
474
  else
 
475
#endif /*USE_AESNI*/
 
476
    {
 
477
      union
 
478
      {
 
479
        PROPERLY_ALIGNED_TYPE dummy;
 
480
        byte *w;
 
481
      } w;
276
482
#define w w.w
277
483
 
278
 
  for (r=0; r < MAXROUNDS+1; r++ )
279
 
    {
280
 
      *((u32*)ctx->keySched2[r][0]) = *((u32*)ctx->keySched[r][0]);
281
 
      *((u32*)ctx->keySched2[r][1]) = *((u32*)ctx->keySched[r][1]);
282
 
      *((u32*)ctx->keySched2[r][2]) = *((u32*)ctx->keySched[r][2]);
283
 
      *((u32*)ctx->keySched2[r][3]) = *((u32*)ctx->keySched[r][3]);
284
 
    }
285
 
#define W (ctx->keySched2)
286
 
  for (r = 1; r < ctx->ROUNDS; r++)
287
 
    {
288
 
      w = W[r][0];
289
 
      *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
290
 
        ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
291
 
       
292
 
      w = W[r][1];
293
 
      *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
294
 
        ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
295
 
        
296
 
      w = W[r][2];
297
 
      *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
298
 
        ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
299
 
        
300
 
      w = W[r][3];
301
 
      *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
302
 
        ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
303
 
    }
 
484
      for (r=0; r < MAXROUNDS+1; r++ )
 
485
        {
 
486
          *((u32_a_t*)ctx->keyschdec[r][0]) = *((u32_a_t*)ctx->keyschenc[r][0]);
 
487
          *((u32_a_t*)ctx->keyschdec[r][1]) = *((u32_a_t*)ctx->keyschenc[r][1]);
 
488
          *((u32_a_t*)ctx->keyschdec[r][2]) = *((u32_a_t*)ctx->keyschenc[r][2]);
 
489
          *((u32_a_t*)ctx->keyschdec[r][3]) = *((u32_a_t*)ctx->keyschenc[r][3]);
 
490
        }
 
491
#define W (ctx->keyschdec)
 
492
      for (r = 1; r < ctx->rounds; r++)
 
493
        {
 
494
          w = W[r][0];
 
495
          *((u32_a_t*)w) = *((u32_a_t*)U1[w[0]]) ^ *((u32_a_t*)U2[w[1]])
 
496
            ^ *((u32_a_t*)U3[w[2]]) ^ *((u32_a_t*)U4[w[3]]);
 
497
 
 
498
          w = W[r][1];
 
499
          *((u32_a_t*)w) = *((u32_a_t*)U1[w[0]]) ^ *((u32_a_t*)U2[w[1]])
 
500
            ^ *((u32_a_t*)U3[w[2]]) ^ *((u32_a_t*)U4[w[3]]);
 
501
 
 
502
          w = W[r][2];
 
503
          *((u32_a_t*)w) = *((u32_a_t*)U1[w[0]]) ^ *((u32_a_t*)U2[w[1]])
 
504
        ^ *((u32_a_t*)U3[w[2]]) ^ *((u32_a_t*)U4[w[3]]);
 
505
 
 
506
          w = W[r][3];
 
507
          *((u32_a_t*)w) = *((u32_a_t*)U1[w[0]]) ^ *((u32_a_t*)U2[w[1]])
 
508
            ^ *((u32_a_t*)U3[w[2]]) ^ *((u32_a_t*)U4[w[3]]);
 
509
        }
304
510
#undef W
305
511
#undef w
306
 
}       
307
 
 
 
512
    }
 
513
}
308
514
 
309
515
 
310
516
/* Encrypt one block.  A and B need to be aligned on a 4 byte
311
517
   boundary.  A and B may be the same. */
312
518
static void
313
 
do_encrypt_aligned (const RIJNDAEL_context *ctx, 
 
519
do_encrypt_aligned (const RIJNDAEL_context *ctx,
314
520
                    unsigned char *b, const unsigned char *a)
315
521
{
316
 
#define rk (ctx->keySched)
317
 
  int ROUNDS = ctx->ROUNDS;
 
522
#define rk (ctx->keyschenc)
 
523
  int rounds = ctx->rounds;
318
524
  int r;
319
525
  union
320
526
  {
322
528
    byte temp[4][4];
323
529
  } u;
324
530
 
325
 
  *((u32*)u.temp[0]) = *((u32*)(a   )) ^ *((u32*)rk[0][0]);
326
 
  *((u32*)u.temp[1]) = *((u32*)(a+ 4)) ^ *((u32*)rk[0][1]);
327
 
  *((u32*)u.temp[2]) = *((u32*)(a+ 8)) ^ *((u32*)rk[0][2]);
328
 
  *((u32*)u.temp[3]) = *((u32*)(a+12)) ^ *((u32*)rk[0][3]);
329
 
  *((u32*)(b    ))   = (*((u32*)T1[u.temp[0][0]])
330
 
                        ^ *((u32*)T2[u.temp[1][1]])
331
 
                        ^ *((u32*)T3[u.temp[2][2]]) 
332
 
                        ^ *((u32*)T4[u.temp[3][3]]));
333
 
  *((u32*)(b + 4))   = (*((u32*)T1[u.temp[1][0]])
334
 
                        ^ *((u32*)T2[u.temp[2][1]])
335
 
                        ^ *((u32*)T3[u.temp[3][2]]) 
336
 
                        ^ *((u32*)T4[u.temp[0][3]]));
337
 
  *((u32*)(b + 8))   = (*((u32*)T1[u.temp[2][0]])
338
 
                        ^ *((u32*)T2[u.temp[3][1]])
339
 
                        ^ *((u32*)T3[u.temp[0][2]]) 
340
 
                        ^ *((u32*)T4[u.temp[1][3]]));
341
 
  *((u32*)(b +12))   = (*((u32*)T1[u.temp[3][0]])
342
 
                        ^ *((u32*)T2[u.temp[0][1]])
343
 
                        ^ *((u32*)T3[u.temp[1][2]]) 
344
 
                        ^ *((u32*)T4[u.temp[2][3]]));
 
531
  *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(a   )) ^ *((u32_a_t*)rk[0][0]);
 
532
  *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(a+ 4)) ^ *((u32_a_t*)rk[0][1]);
 
533
  *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(a+ 8)) ^ *((u32_a_t*)rk[0][2]);
 
534
  *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(a+12)) ^ *((u32_a_t*)rk[0][3]);
 
535
  *((u32_a_t*)(b    ))   = (*((u32_a_t*)T1[u.temp[0][0]])
 
536
                        ^ *((u32_a_t*)T2[u.temp[1][1]])
 
537
                        ^ *((u32_a_t*)T3[u.temp[2][2]])
 
538
                        ^ *((u32_a_t*)T4[u.temp[3][3]]));
 
539
  *((u32_a_t*)(b + 4))   = (*((u32_a_t*)T1[u.temp[1][0]])
 
540
                        ^ *((u32_a_t*)T2[u.temp[2][1]])
 
541
                        ^ *((u32_a_t*)T3[u.temp[3][2]])
 
542
                        ^ *((u32_a_t*)T4[u.temp[0][3]]));
 
543
  *((u32_a_t*)(b + 8))   = (*((u32_a_t*)T1[u.temp[2][0]])
 
544
                        ^ *((u32_a_t*)T2[u.temp[3][1]])
 
545
                        ^ *((u32_a_t*)T3[u.temp[0][2]])
 
546
                        ^ *((u32_a_t*)T4[u.temp[1][3]]));
 
547
  *((u32_a_t*)(b +12))   = (*((u32_a_t*)T1[u.temp[3][0]])
 
548
                        ^ *((u32_a_t*)T2[u.temp[0][1]])
 
549
                        ^ *((u32_a_t*)T3[u.temp[1][2]])
 
550
                        ^ *((u32_a_t*)T4[u.temp[2][3]]));
345
551
 
346
 
  for (r = 1; r < ROUNDS-1; r++)
 
552
  for (r = 1; r < rounds-1; r++)
347
553
    {
348
 
      *((u32*)u.temp[0]) = *((u32*)(b   )) ^ *((u32*)rk[r][0]);
349
 
      *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[r][1]);
350
 
      *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[r][2]);
351
 
      *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[r][3]);
 
554
      *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(b   )) ^ *((u32_a_t*)rk[r][0]);
 
555
      *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(b+ 4)) ^ *((u32_a_t*)rk[r][1]);
 
556
      *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(b+ 8)) ^ *((u32_a_t*)rk[r][2]);
 
557
      *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(b+12)) ^ *((u32_a_t*)rk[r][3]);
352
558
 
353
 
      *((u32*)(b    ))   = (*((u32*)T1[u.temp[0][0]])
354
 
                            ^ *((u32*)T2[u.temp[1][1]])
355
 
                            ^ *((u32*)T3[u.temp[2][2]]) 
356
 
                            ^ *((u32*)T4[u.temp[3][3]]));
357
 
      *((u32*)(b + 4))   = (*((u32*)T1[u.temp[1][0]])
358
 
                            ^ *((u32*)T2[u.temp[2][1]])
359
 
                            ^ *((u32*)T3[u.temp[3][2]]) 
360
 
                            ^ *((u32*)T4[u.temp[0][3]]));
361
 
      *((u32*)(b + 8))   = (*((u32*)T1[u.temp[2][0]])
362
 
                            ^ *((u32*)T2[u.temp[3][1]])
363
 
                            ^ *((u32*)T3[u.temp[0][2]]) 
364
 
                            ^ *((u32*)T4[u.temp[1][3]]));
365
 
      *((u32*)(b +12))   = (*((u32*)T1[u.temp[3][0]])
366
 
                            ^ *((u32*)T2[u.temp[0][1]])
367
 
                            ^ *((u32*)T3[u.temp[1][2]]) 
368
 
                            ^ *((u32*)T4[u.temp[2][3]]));
 
559
      *((u32_a_t*)(b    ))   = (*((u32_a_t*)T1[u.temp[0][0]])
 
560
                            ^ *((u32_a_t*)T2[u.temp[1][1]])
 
561
                            ^ *((u32_a_t*)T3[u.temp[2][2]])
 
562
                            ^ *((u32_a_t*)T4[u.temp[3][3]]));
 
563
      *((u32_a_t*)(b + 4))   = (*((u32_a_t*)T1[u.temp[1][0]])
 
564
                            ^ *((u32_a_t*)T2[u.temp[2][1]])
 
565
                            ^ *((u32_a_t*)T3[u.temp[3][2]])
 
566
                            ^ *((u32_a_t*)T4[u.temp[0][3]]));
 
567
      *((u32_a_t*)(b + 8))   = (*((u32_a_t*)T1[u.temp[2][0]])
 
568
                            ^ *((u32_a_t*)T2[u.temp[3][1]])
 
569
                            ^ *((u32_a_t*)T3[u.temp[0][2]])
 
570
                            ^ *((u32_a_t*)T4[u.temp[1][3]]));
 
571
      *((u32_a_t*)(b +12))   = (*((u32_a_t*)T1[u.temp[3][0]])
 
572
                            ^ *((u32_a_t*)T2[u.temp[0][1]])
 
573
                            ^ *((u32_a_t*)T3[u.temp[1][2]])
 
574
                            ^ *((u32_a_t*)T4[u.temp[2][3]]));
369
575
    }
370
576
 
371
 
  /* Last round is special. */   
372
 
  *((u32*)u.temp[0]) = *((u32*)(b   )) ^ *((u32*)rk[ROUNDS-1][0]);
373
 
  *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[ROUNDS-1][1]);
374
 
  *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[ROUNDS-1][2]);
375
 
  *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[ROUNDS-1][3]);
 
577
  /* Last round is special. */
 
578
  *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(b   )) ^ *((u32_a_t*)rk[rounds-1][0]);
 
579
  *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(b+ 4)) ^ *((u32_a_t*)rk[rounds-1][1]);
 
580
  *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(b+ 8)) ^ *((u32_a_t*)rk[rounds-1][2]);
 
581
  *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(b+12)) ^ *((u32_a_t*)rk[rounds-1][3]);
376
582
  b[ 0] = T1[u.temp[0][0]][1];
377
583
  b[ 1] = T1[u.temp[1][1]][1];
378
584
  b[ 2] = T1[u.temp[2][2]][1];
389
595
  b[13] = T1[u.temp[0][1]][1];
390
596
  b[14] = T1[u.temp[1][2]][1];
391
597
  b[15] = T1[u.temp[2][3]][1];
392
 
  *((u32*)(b   )) ^= *((u32*)rk[ROUNDS][0]);
393
 
  *((u32*)(b+ 4)) ^= *((u32*)rk[ROUNDS][1]);
394
 
  *((u32*)(b+ 8)) ^= *((u32*)rk[ROUNDS][2]);
395
 
  *((u32*)(b+12)) ^= *((u32*)rk[ROUNDS][3]);
 
598
  *((u32_a_t*)(b   )) ^= *((u32_a_t*)rk[rounds][0]);
 
599
  *((u32_a_t*)(b+ 4)) ^= *((u32_a_t*)rk[rounds][1]);
 
600
  *((u32_a_t*)(b+ 8)) ^= *((u32_a_t*)rk[rounds][2]);
 
601
  *((u32_a_t*)(b+12)) ^= *((u32_a_t*)rk[rounds][3]);
396
602
#undef rk
397
603
}
398
604
 
401
607
do_encrypt (const RIJNDAEL_context *ctx,
402
608
            unsigned char *bx, const unsigned char *ax)
403
609
{
404
 
  /* BX and AX are not necessary correctly aligned.  Thus we need to
405
 
     copy them here. */
406
 
  union
407
 
  {
408
 
    u32  dummy[4]; 
409
 
    byte a[16];
410
 
  } a;
411
 
  union
412
 
  {
413
 
    u32  dummy[4]; 
414
 
    byte b[16];
415
 
  } b;
 
610
  /* BX and AX are not necessary correctly aligned.  Thus we might
 
611
     need to copy them here.  We try to align to a 16 bytes.  */
 
612
  if (((size_t)ax & 0x0f) || ((size_t)bx & 0x0f))
 
613
    {
 
614
      union
 
615
      {
 
616
        u32  dummy[4];
 
617
        byte a[16] ATTR_ALIGNED_16;
 
618
      } a;
 
619
      union
 
620
      {
 
621
        u32  dummy[4];
 
622
        byte b[16] ATTR_ALIGNED_16;
 
623
      } b;
416
624
 
417
 
  memcpy (a.a, ax, 16);
418
 
  do_encrypt_aligned (ctx, b.b, a.a);
419
 
  memcpy (bx, b.b, 16);
 
625
      memcpy (a.a, ax, 16);
 
626
      do_encrypt_aligned (ctx, b.b, a.a);
 
627
      memcpy (bx, b.b, 16);
 
628
    }
 
629
  else
 
630
    {
 
631
      do_encrypt_aligned (ctx, bx, ax);
 
632
    }
420
633
}
421
634
 
422
635
 
436
649
  /* The control word fields are:
437
650
      127:12   11:10 9     8     7     6     5     4     3:0
438
651
      RESERVED KSIZE CRYPT INTER KEYGN CIPHR ALIGN DGEST ROUND  */
439
 
  cword[0] = (ctx->ROUNDS & 15);  /* (The mask is just a safeguard.)  */
 
652
  cword[0] = (ctx->rounds & 15);  /* (The mask is just a safeguard.)  */
440
653
  cword[1] = 0;
441
654
  cword[2] = 0;
442
655
  cword[3] = 0;
444
657
    cword[0] |= 0x00000200;
445
658
 
446
659
  memcpy (a, ax, 16);
447
 
   
448
 
  asm volatile 
449
 
    ("pushfl\n\t"          /* Force key reload.  */            
 
660
 
 
661
  asm volatile
 
662
    ("pushfl\n\t"          /* Force key reload.  */
450
663
     "popfl\n\t"
451
664
     "xchg %3, %%ebx\n\t"  /* Load key.  */
452
665
     "movl $1, %%ecx\n\t"  /* Init counter for just one block.  */
453
666
     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XSTORE ECB. */
454
667
     "xchg %3, %%ebx\n"    /* Restore GOT register.  */
455
668
     : /* No output */
456
 
     : "S" (a), "D" (b), "d" (cword), "r" (ctx->padlock_key)
 
669
     : "S" (a), "D" (b), "d" (cword), "r" (ctx->padlockkey)
457
670
     : "%ecx", "cc", "memory"
458
671
     );
459
672
 
463
676
#endif /*USE_PADLOCK*/
464
677
 
465
678
 
 
679
#ifdef USE_AESNI
 
680
/* Encrypt one block using the Intel AES-NI instructions.  A and B may
 
681
   be the same; they need to be properly aligned to 16 bytes.
 
682
 
 
683
   Our problem here is that gcc does not allow the "x" constraint for
 
684
   SSE registers in asm unless you compile with -msse.  The common
 
685
   wisdom is to use a separate file for SSE instructions and build it
 
686
   separately.  This would require a lot of extra build system stuff,
 
687
   similar to what we do in mpi/ for the asm stuff.  What we do
 
688
   instead is to use standard registers and a bit more of plain asm
 
689
   which copies the data and key stuff to the SSE registers and later
 
690
   back.  If we decide to implement some block modes with parallelized
 
691
   AES instructions, it might indeed be better to use plain asm ala
 
692
   mpi/.  */
 
693
static void
 
694
do_aesni_enc_aligned (const RIJNDAEL_context *ctx,
 
695
                      unsigned char *b, const unsigned char *a)
 
696
{
 
697
#define aesenc_xmm1_xmm0      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xc1\n\t"
 
698
#define aesenclast_xmm1_xmm0  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xc1\n\t"
 
699
  /* Note: For now we relax the alignment requirement for A and B: It
 
700
     does not make much difference because in many case we would need
 
701
     to memcpy them to an extra buffer; using the movdqu is much faster
 
702
     that memcpy and movdqa.  For CFB we know that the IV is properly
 
703
     aligned but that is a special case.  We should better implement
 
704
     CFB direct in asm.  */
 
705
  asm volatile ("movdqu %[src], %%xmm0\n\t"     /* xmm0 := *a     */
 
706
                "movl   %[key], %%esi\n\t"      /* esi  := keyschenc */
 
707
                "movdqa (%%esi), %%xmm1\n\t"    /* xmm1 := key[0] */
 
708
                "pxor   %%xmm1, %%xmm0\n\t"     /* xmm0 ^= key[0] */
 
709
                "movdqa 0x10(%%esi), %%xmm1\n\t"
 
710
                aesenc_xmm1_xmm0
 
711
                "movdqa 0x20(%%esi), %%xmm1\n\t"
 
712
                aesenc_xmm1_xmm0
 
713
                "movdqa 0x30(%%esi), %%xmm1\n\t"
 
714
                aesenc_xmm1_xmm0
 
715
                "movdqa 0x40(%%esi), %%xmm1\n\t"
 
716
                aesenc_xmm1_xmm0
 
717
                "movdqa 0x50(%%esi), %%xmm1\n\t"
 
718
                aesenc_xmm1_xmm0
 
719
                "movdqa 0x60(%%esi), %%xmm1\n\t"
 
720
                aesenc_xmm1_xmm0
 
721
                "movdqa 0x70(%%esi), %%xmm1\n\t"
 
722
                aesenc_xmm1_xmm0
 
723
                "movdqa 0x80(%%esi), %%xmm1\n\t"
 
724
                aesenc_xmm1_xmm0
 
725
                "movdqa 0x90(%%esi), %%xmm1\n\t"
 
726
                aesenc_xmm1_xmm0
 
727
                "movdqa 0xa0(%%esi), %%xmm1\n\t"
 
728
                "cmp $10, %[rounds]\n\t"
 
729
                "jz .Lenclast%=\n\t"
 
730
                aesenc_xmm1_xmm0
 
731
                "movdqa 0xb0(%%esi), %%xmm1\n\t"
 
732
                aesenc_xmm1_xmm0
 
733
                "movdqa 0xc0(%%esi), %%xmm1\n\t"
 
734
                "cmp $12, %[rounds]\n\t"
 
735
                "jz .Lenclast%=\n\t"
 
736
                aesenc_xmm1_xmm0
 
737
                "movdqa 0xd0(%%esi), %%xmm1\n\t"
 
738
                aesenc_xmm1_xmm0
 
739
                "movdqa 0xe0(%%esi), %%xmm1\n"
 
740
 
 
741
                ".Lenclast%=:\n\t"
 
742
                aesenclast_xmm1_xmm0
 
743
                "movdqu %%xmm0, %[dst]\n"
 
744
                : [dst] "=m" (*b)
 
745
                : [src] "m" (*a),
 
746
                  [key] "r" (ctx->keyschenc),
 
747
                  [rounds] "r" (ctx->rounds)
 
748
                : "%esi", "cc", "memory");
 
749
#undef aesenc_xmm1_xmm0
 
750
#undef aesenclast_xmm1_xmm0
 
751
}
 
752
 
 
753
 
 
754
static void
 
755
do_aesni_dec_aligned (const RIJNDAEL_context *ctx,
 
756
                      unsigned char *b, const unsigned char *a)
 
757
{
 
758
#define aesdec_xmm1_xmm0      ".byte 0x66, 0x0f, 0x38, 0xde, 0xc1\n\t"
 
759
#define aesdeclast_xmm1_xmm0  ".byte 0x66, 0x0f, 0x38, 0xdf, 0xc1\n\t"
 
760
  asm volatile ("movdqu %[src], %%xmm0\n\t"     /* xmm0 := *a     */
 
761
                "movl   %[key], %%esi\n\t"
 
762
                "movdqa (%%esi), %%xmm1\n\t"
 
763
                "pxor   %%xmm1, %%xmm0\n\t"     /* xmm0 ^= key[0] */
 
764
                "movdqa 0x10(%%esi), %%xmm1\n\t"
 
765
                aesdec_xmm1_xmm0
 
766
                "movdqa 0x20(%%esi), %%xmm1\n\t"
 
767
                aesdec_xmm1_xmm0
 
768
                "movdqa 0x30(%%esi), %%xmm1\n\t"
 
769
                aesdec_xmm1_xmm0
 
770
                "movdqa 0x40(%%esi), %%xmm1\n\t"
 
771
                aesdec_xmm1_xmm0
 
772
                "movdqa 0x50(%%esi), %%xmm1\n\t"
 
773
                aesdec_xmm1_xmm0
 
774
                "movdqa 0x60(%%esi), %%xmm1\n\t"
 
775
                aesdec_xmm1_xmm0
 
776
                "movdqa 0x70(%%esi), %%xmm1\n\t"
 
777
                aesdec_xmm1_xmm0
 
778
                "movdqa 0x80(%%esi), %%xmm1\n\t"
 
779
                aesdec_xmm1_xmm0
 
780
                "movdqa 0x90(%%esi), %%xmm1\n\t"
 
781
                aesdec_xmm1_xmm0
 
782
                "movdqa 0xa0(%%esi), %%xmm1\n\t"
 
783
                "cmp $10, %[rounds]\n\t"
 
784
                "jz .Ldeclast%=\n\t"
 
785
                aesdec_xmm1_xmm0
 
786
                "movdqa 0xb0(%%esi), %%xmm1\n\t"
 
787
                aesdec_xmm1_xmm0
 
788
                "movdqa 0xc0(%%esi), %%xmm1\n\t"
 
789
                "cmp $12, %[rounds]\n\t"
 
790
                "jz .Ldeclast%=\n\t"
 
791
                aesdec_xmm1_xmm0
 
792
                "movdqa 0xd0(%%esi), %%xmm1\n\t"
 
793
                aesdec_xmm1_xmm0
 
794
                "movdqa 0xe0(%%esi), %%xmm1\n"
 
795
 
 
796
                ".Ldeclast%=:\n\t"
 
797
                aesdeclast_xmm1_xmm0
 
798
                "movdqu %%xmm0, %[dst]\n"
 
799
                : [dst] "=m" (*b)
 
800
                : [src] "m" (*a),
 
801
                  [key] "r" (ctx->keyschdec),
 
802
                  [rounds] "r" (ctx->rounds)
 
803
                : "%esi", "cc", "memory");
 
804
#undef aesdec_xmm1_xmm0
 
805
#undef aesdeclast_xmm1_xmm0
 
806
}
 
807
 
 
808
 
 
809
/* Perform a CFB encryption or decryption round using the
 
810
   initialization vector IV and the input block A.  Write the result
 
811
   to the output block B and update IV.  IV needs to be 16 byte
 
812
   aligned.  */
 
813
static void
 
814
do_aesni_cfb (const RIJNDAEL_context *ctx, int decrypt_flag,
 
815
              unsigned char *iv, unsigned char *b, const unsigned char *a)
 
816
{
 
817
#define aesenc_xmm1_xmm0      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xc1\n\t"
 
818
#define aesenclast_xmm1_xmm0  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xc1\n\t"
 
819
  asm volatile ("movdqa %[iv], %%xmm0\n\t"      /* xmm0 := IV     */
 
820
                "movl   %[key], %%esi\n\t"      /* esi  := keyschenc */
 
821
                "movdqa (%%esi), %%xmm1\n\t"    /* xmm1 := key[0] */
 
822
                "pxor   %%xmm1, %%xmm0\n\t"     /* xmm0 ^= key[0] */
 
823
                "movdqa 0x10(%%esi), %%xmm1\n\t"
 
824
                aesenc_xmm1_xmm0
 
825
                "movdqa 0x20(%%esi), %%xmm1\n\t"
 
826
                aesenc_xmm1_xmm0
 
827
                "movdqa 0x30(%%esi), %%xmm1\n\t"
 
828
                aesenc_xmm1_xmm0
 
829
                "movdqa 0x40(%%esi), %%xmm1\n\t"
 
830
                aesenc_xmm1_xmm0
 
831
                "movdqa 0x50(%%esi), %%xmm1\n\t"
 
832
                aesenc_xmm1_xmm0
 
833
                "movdqa 0x60(%%esi), %%xmm1\n\t"
 
834
                aesenc_xmm1_xmm0
 
835
                "movdqa 0x70(%%esi), %%xmm1\n\t"
 
836
                aesenc_xmm1_xmm0
 
837
                "movdqa 0x80(%%esi), %%xmm1\n\t"
 
838
                aesenc_xmm1_xmm0
 
839
                "movdqa 0x90(%%esi), %%xmm1\n\t"
 
840
                aesenc_xmm1_xmm0
 
841
                "movdqa 0xa0(%%esi), %%xmm1\n\t"
 
842
                "cmp $10, %[rounds]\n\t"
 
843
                "jz .Lenclast%=\n\t"
 
844
                aesenc_xmm1_xmm0
 
845
                "movdqa 0xb0(%%esi), %%xmm1\n\t"
 
846
                aesenc_xmm1_xmm0
 
847
                "movdqa 0xc0(%%esi), %%xmm1\n\t"
 
848
                "cmp $12, %[rounds]\n\t"
 
849
                "jz .Lenclast%=\n\t"
 
850
                aesenc_xmm1_xmm0
 
851
                "movdqa 0xd0(%%esi), %%xmm1\n\t"
 
852
                aesenc_xmm1_xmm0
 
853
                "movdqa 0xe0(%%esi), %%xmm1\n"
 
854
 
 
855
                ".Lenclast%=:\n\t"
 
856
                aesenclast_xmm1_xmm0
 
857
                "movdqu %[src], %%xmm1\n\t"      /* Save input.  */
 
858
                "pxor %%xmm1, %%xmm0\n\t"        /* xmm0 = input ^ IV  */
 
859
 
 
860
                "cmp $1, %[decrypt]\n\t"
 
861
                "jz .Ldecrypt_%=\n\t"
 
862
                "movdqa %%xmm0, %[iv]\n\t"       /* [encrypt] Store IV.  */
 
863
                "jmp .Lleave_%=\n"
 
864
                ".Ldecrypt_%=:\n\t"
 
865
                "movdqa %%xmm1, %[iv]\n"         /* [decrypt] Store IV.  */
 
866
                ".Lleave_%=:\n\t"
 
867
                "movdqu %%xmm0, %[dst]\n"        /* Store output.   */
 
868
                : [iv] "+m" (*iv), [dst] "=m" (*b)
 
869
                : [src] "m" (*a),
 
870
                  [key] "g" (ctx->keyschenc),
 
871
                  [rounds] "g" (ctx->rounds),
 
872
                  [decrypt] "m" (decrypt_flag)
 
873
                : "%esi", "cc", "memory");
 
874
#undef aesenc_xmm1_xmm0
 
875
#undef aesenclast_xmm1_xmm0
 
876
}
 
877
 
 
878
/* Perform a CTR encryption round using the counter CTR and the input
 
879
   block A.  Write the result to the output block B and update CTR.
 
880
   CTR needs to be a 16 byte aligned little-endian value.  */
 
881
static void
 
882
do_aesni_ctr (const RIJNDAEL_context *ctx,
 
883
              unsigned char *ctr, unsigned char *b, const unsigned char *a)
 
884
{
 
885
#define aesenc_xmm1_xmm0      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xc1\n\t"
 
886
#define aesenclast_xmm1_xmm0  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xc1\n\t"
 
887
  static unsigned char be_mask[16] __attribute__ ((aligned (16))) =
 
888
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
 
889
 
 
890
  asm volatile ("movdqa %[ctr], %%xmm0\n\t"     /* xmm0, xmm2 := CTR   */
 
891
                "movaps %%xmm0, %%xmm2\n\t"
 
892
                "mov    $1, %%esi\n\t"          /* xmm2++ (big-endian) */
 
893
                "movd   %%esi, %%xmm1\n\t"
 
894
                "pshufb %[mask], %%xmm2\n\t"
 
895
                "paddq  %%xmm1, %%xmm2\n\t"
 
896
                "pshufb %[mask], %%xmm2\n\t"
 
897
                "movdqa %%xmm2, %[ctr]\n"       /* Update CTR.         */
 
898
 
 
899
                "movl   %[key], %%esi\n\t"      /* esi  := keyschenc */
 
900
                "movdqa (%%esi), %%xmm1\n\t"    /* xmm1 := key[0]    */
 
901
                "pxor   %%xmm1, %%xmm0\n\t"     /* xmm0 ^= key[0]    */
 
902
                "movdqa 0x10(%%esi), %%xmm1\n\t"
 
903
                aesenc_xmm1_xmm0
 
904
                "movdqa 0x20(%%esi), %%xmm1\n\t"
 
905
                aesenc_xmm1_xmm0
 
906
                "movdqa 0x30(%%esi), %%xmm1\n\t"
 
907
                aesenc_xmm1_xmm0
 
908
                "movdqa 0x40(%%esi), %%xmm1\n\t"
 
909
                aesenc_xmm1_xmm0
 
910
                "movdqa 0x50(%%esi), %%xmm1\n\t"
 
911
                aesenc_xmm1_xmm0
 
912
                "movdqa 0x60(%%esi), %%xmm1\n\t"
 
913
                aesenc_xmm1_xmm0
 
914
                "movdqa 0x70(%%esi), %%xmm1\n\t"
 
915
                aesenc_xmm1_xmm0
 
916
                "movdqa 0x80(%%esi), %%xmm1\n\t"
 
917
                aesenc_xmm1_xmm0
 
918
                "movdqa 0x90(%%esi), %%xmm1\n\t"
 
919
                aesenc_xmm1_xmm0
 
920
                "movdqa 0xa0(%%esi), %%xmm1\n\t"
 
921
                "cmp $10, %[rounds]\n\t"
 
922
                "jz .Lenclast%=\n\t"
 
923
                aesenc_xmm1_xmm0
 
924
                "movdqa 0xb0(%%esi), %%xmm1\n\t"
 
925
                aesenc_xmm1_xmm0
 
926
                "movdqa 0xc0(%%esi), %%xmm1\n\t"
 
927
                "cmp $12, %[rounds]\n\t"
 
928
                "jz .Lenclast%=\n\t"
 
929
                aesenc_xmm1_xmm0
 
930
                "movdqa 0xd0(%%esi), %%xmm1\n\t"
 
931
                aesenc_xmm1_xmm0
 
932
                "movdqa 0xe0(%%esi), %%xmm1\n"
 
933
 
 
934
                ".Lenclast%=:\n\t"
 
935
                aesenclast_xmm1_xmm0
 
936
                "movdqu %[src], %%xmm1\n\t"      /* xmm1 := input   */
 
937
                "pxor %%xmm1, %%xmm0\n\t"        /* EncCTR ^= input  */
 
938
                "movdqu %%xmm0, %[dst]"          /* Store EncCTR.    */
 
939
 
 
940
                : [ctr] "+m" (*ctr), [dst] "=m" (*b)
 
941
                : [src] "m" (*a),
 
942
                  [key] "g" (ctx->keyschenc),
 
943
                  [rounds] "g" (ctx->rounds),
 
944
                  [mask] "m" (*be_mask)
 
945
                : "%esi", "cc", "memory");
 
946
#undef aesenc_xmm1_xmm0
 
947
#undef aesenclast_xmm1_xmm0
 
948
}
 
949
 
 
950
 
 
951
/* Four blocks at a time variant of do_aesni_ctr.  */
 
952
static void
 
953
do_aesni_ctr_4 (const RIJNDAEL_context *ctx,
 
954
                unsigned char *ctr, unsigned char *b, const unsigned char *a)
 
955
{
 
956
#define aesenc_xmm1_xmm0      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xc1\n\t"
 
957
#define aesenc_xmm1_xmm2      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xd1\n\t"
 
958
#define aesenc_xmm1_xmm3      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xd9\n\t"
 
959
#define aesenc_xmm1_xmm4      ".byte 0x66, 0x0f, 0x38, 0xdc, 0xe1\n\t"
 
960
#define aesenclast_xmm1_xmm0  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xc1\n\t"
 
961
#define aesenclast_xmm1_xmm2  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xd1\n\t"
 
962
#define aesenclast_xmm1_xmm3  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xd9\n\t"
 
963
#define aesenclast_xmm1_xmm4  ".byte 0x66, 0x0f, 0x38, 0xdd, 0xe1\n\t"
 
964
 
 
965
  static unsigned char be_mask[16] __attribute__ ((aligned (16))) =
 
966
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
 
967
 
 
968
  /* Register usage:
 
969
      esi   keyschedule
 
970
      xmm0  CTR-0
 
971
      xmm1  temp / round key
 
972
      xmm2  CTR-1
 
973
      xmm3  CTR-2
 
974
      xmm4  CTR-3
 
975
      xmm5  temp
 
976
   */
 
977
 
 
978
  asm volatile ("movdqa %[ctr], %%xmm0\n\t"     /* xmm0, xmm2 := CTR   */
 
979
                "movaps %%xmm0, %%xmm2\n\t"
 
980
                "mov    $1, %%esi\n\t"          /* xmm1 := 1 */
 
981
                "movd   %%esi, %%xmm1\n\t"
 
982
                "pshufb %[mask], %%xmm2\n\t"    /* xmm2 := le(xmm2) */
 
983
                "paddq  %%xmm1, %%xmm2\n\t"     /* xmm2++           */
 
984
                "movaps %%xmm2, %%xmm3\n\t"     /* xmm3 := xmm2     */
 
985
                "paddq  %%xmm1, %%xmm3\n\t"     /* xmm3++           */
 
986
                "movaps %%xmm3, %%xmm4\n\t"     /* xmm4 := xmm3     */
 
987
                "paddq  %%xmm1, %%xmm4\n\t"     /* xmm4++           */
 
988
                "movaps %%xmm4, %%xmm5\n\t"     /* xmm5 := xmm4     */
 
989
                "paddq  %%xmm1, %%xmm5\n\t"     /* xmm5++           */
 
990
                "pshufb %[mask], %%xmm2\n\t"    /* xmm2 := be(xmm2) */
 
991
                "pshufb %[mask], %%xmm3\n\t"    /* xmm3 := be(xmm3) */
 
992
                "pshufb %[mask], %%xmm4\n\t"    /* xmm4 := be(xmm4) */
 
993
                "pshufb %[mask], %%xmm5\n\t"    /* xmm5 := be(xmm5) */
 
994
                "movdqa %%xmm5, %[ctr]\n"       /* Update CTR.      */
 
995
 
 
996
                "movl   %[key], %%esi\n\t"      /* esi  := keyschenc */
 
997
                "movdqa (%%esi), %%xmm1\n\t"    /* xmm1 := key[0]    */
 
998
                "pxor   %%xmm1, %%xmm0\n\t"     /* xmm0 ^= key[0]    */
 
999
                "pxor   %%xmm1, %%xmm2\n\t"     /* xmm2 ^= key[0]    */
 
1000
                "pxor   %%xmm1, %%xmm3\n\t"     /* xmm3 ^= key[0]    */
 
1001
                "pxor   %%xmm1, %%xmm4\n\t"     /* xmm4 ^= key[0]    */
 
1002
                "movdqa 0x10(%%esi), %%xmm1\n\t"
 
1003
                aesenc_xmm1_xmm0
 
1004
                aesenc_xmm1_xmm2
 
1005
                aesenc_xmm1_xmm3
 
1006
                aesenc_xmm1_xmm4
 
1007
                "movdqa 0x20(%%esi), %%xmm1\n\t"
 
1008
                aesenc_xmm1_xmm0
 
1009
                aesenc_xmm1_xmm2
 
1010
                aesenc_xmm1_xmm3
 
1011
                aesenc_xmm1_xmm4
 
1012
                "movdqa 0x30(%%esi), %%xmm1\n\t"
 
1013
                aesenc_xmm1_xmm0
 
1014
                aesenc_xmm1_xmm2
 
1015
                aesenc_xmm1_xmm3
 
1016
                aesenc_xmm1_xmm4
 
1017
                "movdqa 0x40(%%esi), %%xmm1\n\t"
 
1018
                aesenc_xmm1_xmm0
 
1019
                aesenc_xmm1_xmm2
 
1020
                aesenc_xmm1_xmm3
 
1021
                aesenc_xmm1_xmm4
 
1022
                "movdqa 0x50(%%esi), %%xmm1\n\t"
 
1023
                aesenc_xmm1_xmm0
 
1024
                aesenc_xmm1_xmm2
 
1025
                aesenc_xmm1_xmm3
 
1026
                aesenc_xmm1_xmm4
 
1027
                "movdqa 0x60(%%esi), %%xmm1\n\t"
 
1028
                aesenc_xmm1_xmm0
 
1029
                aesenc_xmm1_xmm2
 
1030
                aesenc_xmm1_xmm3
 
1031
                aesenc_xmm1_xmm4
 
1032
                "movdqa 0x70(%%esi), %%xmm1\n\t"
 
1033
                aesenc_xmm1_xmm0
 
1034
                aesenc_xmm1_xmm2
 
1035
                aesenc_xmm1_xmm3
 
1036
                aesenc_xmm1_xmm4
 
1037
                "movdqa 0x80(%%esi), %%xmm1\n\t"
 
1038
                aesenc_xmm1_xmm0
 
1039
                aesenc_xmm1_xmm2
 
1040
                aesenc_xmm1_xmm3
 
1041
                aesenc_xmm1_xmm4
 
1042
                "movdqa 0x90(%%esi), %%xmm1\n\t"
 
1043
                aesenc_xmm1_xmm0
 
1044
                aesenc_xmm1_xmm2
 
1045
                aesenc_xmm1_xmm3
 
1046
                aesenc_xmm1_xmm4
 
1047
                "movdqa 0xa0(%%esi), %%xmm1\n\t"
 
1048
                "cmp $10, %[rounds]\n\t"
 
1049
                "jz .Lenclast%=\n\t"
 
1050
                aesenc_xmm1_xmm0
 
1051
                aesenc_xmm1_xmm2
 
1052
                aesenc_xmm1_xmm3
 
1053
                aesenc_xmm1_xmm4
 
1054
                "movdqa 0xb0(%%esi), %%xmm1\n\t"
 
1055
                aesenc_xmm1_xmm0
 
1056
                aesenc_xmm1_xmm2
 
1057
                aesenc_xmm1_xmm3
 
1058
                aesenc_xmm1_xmm4
 
1059
                "movdqa 0xc0(%%esi), %%xmm1\n\t"
 
1060
                "cmp $12, %[rounds]\n\t"
 
1061
                "jz .Lenclast%=\n\t"
 
1062
                aesenc_xmm1_xmm0
 
1063
                aesenc_xmm1_xmm2
 
1064
                aesenc_xmm1_xmm3
 
1065
                aesenc_xmm1_xmm4
 
1066
                "movdqa 0xd0(%%esi), %%xmm1\n\t"
 
1067
                aesenc_xmm1_xmm0
 
1068
                aesenc_xmm1_xmm2
 
1069
                aesenc_xmm1_xmm3
 
1070
                aesenc_xmm1_xmm4
 
1071
                "movdqa 0xe0(%%esi), %%xmm1\n"
 
1072
 
 
1073
                ".Lenclast%=:\n\t"
 
1074
                aesenclast_xmm1_xmm0
 
1075
                aesenclast_xmm1_xmm2
 
1076
                aesenclast_xmm1_xmm3
 
1077
                aesenclast_xmm1_xmm4
 
1078
 
 
1079
                "movdqu %[src], %%xmm1\n\t"      /* Get block 1.      */
 
1080
                "pxor %%xmm1, %%xmm0\n\t"        /* EncCTR-1 ^= input */
 
1081
                "movdqu %%xmm0, %[dst]\n\t"      /* Store block 1     */
 
1082
 
 
1083
                "movdqu (16)%[src], %%xmm1\n\t"  /* Get block 2.      */
 
1084
                "pxor %%xmm1, %%xmm2\n\t"        /* EncCTR-2 ^= input */
 
1085
                "movdqu %%xmm2, (16)%[dst]\n\t"  /* Store block 2.    */
 
1086
 
 
1087
                "movdqu (32)%[src], %%xmm1\n\t"  /* Get block 3.      */
 
1088
                "pxor %%xmm1, %%xmm3\n\t"        /* EncCTR-3 ^= input */
 
1089
                "movdqu %%xmm3, (32)%[dst]\n\t"  /* Store block 3.    */
 
1090
 
 
1091
                "movdqu (48)%[src], %%xmm1\n\t"  /* Get block 4.      */
 
1092
                "pxor %%xmm1, %%xmm4\n\t"        /* EncCTR-4 ^= input */
 
1093
                "movdqu %%xmm4, (48)%[dst]"      /* Store block 4.   */
 
1094
 
 
1095
                : [ctr] "+m" (*ctr), [dst] "=m" (*b)
 
1096
                : [src] "m" (*a),
 
1097
                  [key] "g" (ctx->keyschenc),
 
1098
                  [rounds] "g" (ctx->rounds),
 
1099
                  [mask] "m" (*be_mask)
 
1100
                : "%esi", "cc", "memory");
 
1101
#undef aesenc_xmm1_xmm0
 
1102
#undef aesenc_xmm1_xmm2
 
1103
#undef aesenc_xmm1_xmm3
 
1104
#undef aesenc_xmm1_xmm4
 
1105
#undef aesenclast_xmm1_xmm0
 
1106
#undef aesenclast_xmm1_xmm2
 
1107
#undef aesenclast_xmm1_xmm3
 
1108
#undef aesenclast_xmm1_xmm4
 
1109
}
 
1110
 
 
1111
 
 
1112
static void
 
1113
do_aesni (RIJNDAEL_context *ctx, int decrypt_flag,
 
1114
          unsigned char *bx, const unsigned char *ax)
 
1115
{
 
1116
 
 
1117
  if (decrypt_flag)
 
1118
    {
 
1119
      if (!ctx->decryption_prepared )
 
1120
        {
 
1121
          prepare_decryption ( ctx );
 
1122
          ctx->decryption_prepared = 1;
 
1123
        }
 
1124
      do_aesni_dec_aligned (ctx, bx, ax);
 
1125
    }
 
1126
  else
 
1127
    do_aesni_enc_aligned (ctx, bx, ax);
 
1128
}
 
1129
#endif /*USE_AESNI*/
 
1130
 
 
1131
 
466
1132
static void
467
1133
rijndael_encrypt (void *context, byte *b, const byte *a)
468
1134
{
469
1135
  RIJNDAEL_context *ctx = context;
470
1136
 
 
1137
  if (0)
 
1138
    ;
471
1139
#ifdef USE_PADLOCK
472
 
  if (ctx->use_padlock)
 
1140
  else if (ctx->use_padlock)
473
1141
    {
474
1142
      do_padlock (ctx, 0, b, a);
475
1143
      _gcry_burn_stack (48 + 15 /* possible padding for alignment */);
476
1144
    }
 
1145
#endif /*USE_PADLOCK*/
 
1146
#ifdef USE_AESNI
 
1147
  else if (ctx->use_aesni)
 
1148
    {
 
1149
      aesni_prepare ();
 
1150
      do_aesni (ctx, 0, b, a);
 
1151
      aesni_cleanup ();
 
1152
    }
 
1153
#endif /*USE_AESNI*/
477
1154
  else
478
 
#endif /*USE_PADLOCK*/
479
1155
    {
480
1156
      do_encrypt (ctx, b, a);
481
 
      _gcry_burn_stack (48 + 2*sizeof(int));
 
1157
      _gcry_burn_stack (56 + 2*sizeof(int));
482
1158
    }
483
1159
}
484
1160
 
488
1164
   function is only intended for the bulk encryption feature of
489
1165
   cipher.c. */
490
1166
void
491
 
_gcry_aes_cfb_enc (void *context, unsigned char *iv, 
 
1167
_gcry_aes_cfb_enc (void *context, unsigned char *iv,
492
1168
                   void *outbuf_arg, const void *inbuf_arg,
493
1169
                   unsigned int nblocks)
494
1170
{
498
1174
  unsigned char *ivp;
499
1175
  int i;
500
1176
 
 
1177
  if (0)
 
1178
    ;
501
1179
#ifdef USE_PADLOCK
502
 
  if (ctx->use_padlock)
 
1180
  else if (ctx->use_padlock)
503
1181
    {
504
1182
      /* Fixme: Let Padlock do the CFBing.  */
505
1183
      for ( ;nblocks; nblocks-- )
511
1189
            *outbuf++ = (*ivp++ ^= *inbuf++);
512
1190
        }
513
1191
    }
 
1192
#endif /*USE_PADLOCK*/
 
1193
#ifdef USE_AESNI
 
1194
  else if (ctx->use_aesni)
 
1195
    {
 
1196
      aesni_prepare ();
 
1197
      for ( ;nblocks; nblocks-- )
 
1198
        {
 
1199
          do_aesni_cfb (ctx, 0, iv, outbuf, inbuf);
 
1200
          outbuf += BLOCKSIZE;
 
1201
          inbuf  += BLOCKSIZE;
 
1202
        }
 
1203
      aesni_cleanup ();
 
1204
    }
 
1205
#endif /*USE_AESNI*/
514
1206
  else
515
 
#endif /* USE_PADLOCK*/
516
1207
    {
517
1208
      for ( ;nblocks; nblocks-- )
518
1209
        {
533
1224
   function is only intended for the bulk encryption feature of
534
1225
   cipher.c. */
535
1226
void
536
 
_gcry_aes_cbc_enc (void *context, unsigned char *iv, 
 
1227
_gcry_aes_cbc_enc (void *context, unsigned char *iv,
537
1228
                   void *outbuf_arg, const void *inbuf_arg,
538
1229
                   unsigned int nblocks, int cbc_mac)
539
1230
{
543
1234
  unsigned char *ivp;
544
1235
  int i;
545
1236
 
 
1237
#ifdef USE_AESNI
 
1238
  if (ctx->use_aesni)
 
1239
    aesni_prepare ();
 
1240
#endif /*USE_AESNI*/
 
1241
 
546
1242
  for ( ;nblocks; nblocks-- )
547
1243
    {
548
1244
      for (ivp=iv, i=0; i < BLOCKSIZE; i++ )
549
1245
        outbuf[i] = inbuf[i] ^ *ivp++;
550
1246
 
 
1247
      if (0)
 
1248
        ;
551
1249
#ifdef USE_PADLOCK
552
 
      if (ctx->use_padlock)
 
1250
      else if (ctx->use_padlock)
553
1251
        do_padlock (ctx, 0, outbuf, outbuf);
 
1252
#endif /*USE_PADLOCK*/
 
1253
#ifdef USE_AESNI
 
1254
      else if (ctx->use_aesni)
 
1255
        do_aesni (ctx, 0, outbuf, outbuf);
 
1256
#endif /*USE_AESNI*/
554
1257
      else
555
 
#endif /*USE_PADLOCK*/
556
1258
        do_encrypt (ctx, outbuf, outbuf );
557
1259
 
558
1260
      memcpy (iv, outbuf, BLOCKSIZE);
561
1263
        outbuf += BLOCKSIZE;
562
1264
    }
563
1265
 
 
1266
#ifdef USE_AESNI
 
1267
  if (ctx->use_aesni)
 
1268
    aesni_cleanup ();
 
1269
#endif /*USE_AESNI*/
 
1270
 
 
1271
  _gcry_burn_stack (48 + 2*sizeof(int));
 
1272
}
 
1273
 
 
1274
 
 
1275
/* Bulk encryption of complete blocks in CTR mode.  Caller needs to
 
1276
   make sure that CTR is aligned on a 16 byte boundary if AESNI; the
 
1277
   minimum alignment is for an u32.  This function is only intended
 
1278
   for the bulk encryption feature of cipher.c.  CTR is expected to be
 
1279
   of size BLOCKSIZE. */
 
1280
void
 
1281
_gcry_aes_ctr_enc (void *context, unsigned char *ctr,
 
1282
                   void *outbuf_arg, const void *inbuf_arg,
 
1283
                   unsigned int nblocks)
 
1284
{
 
1285
  RIJNDAEL_context *ctx = context;
 
1286
  unsigned char *outbuf = outbuf_arg;
 
1287
  const unsigned char *inbuf = inbuf_arg;
 
1288
  unsigned char *p;
 
1289
  int i;
 
1290
 
 
1291
  if (0)
 
1292
    ;
 
1293
#ifdef USE_AESNI
 
1294
  else if (ctx->use_aesni)
 
1295
    {
 
1296
      aesni_prepare ();
 
1297
      for ( ;nblocks > 3 ; nblocks -= 4 )
 
1298
        {
 
1299
          do_aesni_ctr_4 (ctx, ctr, outbuf, inbuf);
 
1300
          outbuf += 4*BLOCKSIZE;
 
1301
          inbuf  += 4*BLOCKSIZE;
 
1302
        }
 
1303
      for ( ;nblocks; nblocks-- )
 
1304
        {
 
1305
          do_aesni_ctr (ctx, ctr, outbuf, inbuf);
 
1306
          outbuf += BLOCKSIZE;
 
1307
          inbuf  += BLOCKSIZE;
 
1308
        }
 
1309
      aesni_cleanup ();
 
1310
      aesni_cleanup_2_4 ();
 
1311
    }
 
1312
#endif /*USE_AESNI*/
 
1313
  else
 
1314
    {
 
1315
      union { unsigned char x1[16]; u32 x32[4]; } tmp;
 
1316
 
 
1317
      for ( ;nblocks; nblocks-- )
 
1318
        {
 
1319
          /* Encrypt the counter. */
 
1320
          do_encrypt_aligned (ctx, tmp.x1, ctr);
 
1321
          /* XOR the input with the encrypted counter and store in output.  */
 
1322
          for (p=tmp.x1, i=0; i < BLOCKSIZE; i++)
 
1323
            *outbuf++ = (*p++ ^= *inbuf++);
 
1324
          /* Increment the counter.  */
 
1325
          for (i = BLOCKSIZE; i > 0; i--)
 
1326
            {
 
1327
              ctr[i-1]++;
 
1328
              if (ctr[i-1])
 
1329
                break;
 
1330
            }
 
1331
        }
 
1332
    }
 
1333
 
564
1334
  _gcry_burn_stack (48 + 2*sizeof(int));
565
1335
}
566
1336
 
570
1340
   and the decryption must have been prepared.  A and B may be the
571
1341
   same. */
572
1342
static void
573
 
do_decrypt_aligned (RIJNDAEL_context *ctx, 
 
1343
do_decrypt_aligned (RIJNDAEL_context *ctx,
574
1344
                    unsigned char *b, const unsigned char *a)
575
1345
{
576
 
#define rk  (ctx->keySched2)
577
 
  int ROUNDS = ctx->ROUNDS; 
 
1346
#define rk  (ctx->keyschdec)
 
1347
  int rounds = ctx->rounds;
578
1348
  int r;
579
 
  union 
 
1349
  union
580
1350
  {
581
1351
    u32  tempu32[4];  /* Force correct alignment. */
582
1352
    byte temp[4][4];
583
1353
  } u;
584
1354
 
585
1355
 
586
 
  *((u32*)u.temp[0]) = *((u32*)(a   )) ^ *((u32*)rk[ROUNDS][0]);
587
 
  *((u32*)u.temp[1]) = *((u32*)(a+ 4)) ^ *((u32*)rk[ROUNDS][1]);
588
 
  *((u32*)u.temp[2]) = *((u32*)(a+ 8)) ^ *((u32*)rk[ROUNDS][2]);
589
 
  *((u32*)u.temp[3]) = *((u32*)(a+12)) ^ *((u32*)rk[ROUNDS][3]);
590
 
  
591
 
  *((u32*)(b   ))    = (*((u32*)T5[u.temp[0][0]])
592
 
                        ^ *((u32*)T6[u.temp[3][1]])
593
 
                        ^ *((u32*)T7[u.temp[2][2]]) 
594
 
                        ^ *((u32*)T8[u.temp[1][3]]));
595
 
  *((u32*)(b+ 4))    = (*((u32*)T5[u.temp[1][0]])
596
 
                        ^ *((u32*)T6[u.temp[0][1]])
597
 
                        ^ *((u32*)T7[u.temp[3][2]]) 
598
 
                        ^ *((u32*)T8[u.temp[2][3]]));
599
 
  *((u32*)(b+ 8))    = (*((u32*)T5[u.temp[2][0]])
600
 
                        ^ *((u32*)T6[u.temp[1][1]])
601
 
                        ^ *((u32*)T7[u.temp[0][2]]) 
602
 
                        ^ *((u32*)T8[u.temp[3][3]]));
603
 
  *((u32*)(b+12))    = (*((u32*)T5[u.temp[3][0]])
604
 
                        ^ *((u32*)T6[u.temp[2][1]])
605
 
                        ^ *((u32*)T7[u.temp[1][2]]) 
606
 
                        ^ *((u32*)T8[u.temp[0][3]]));
607
 
 
608
 
  for (r = ROUNDS-1; r > 1; r--)
 
1356
  *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(a   )) ^ *((u32_a_t*)rk[rounds][0]);
 
1357
  *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(a+ 4)) ^ *((u32_a_t*)rk[rounds][1]);
 
1358
  *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(a+ 8)) ^ *((u32_a_t*)rk[rounds][2]);
 
1359
  *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(a+12)) ^ *((u32_a_t*)rk[rounds][3]);
 
1360
 
 
1361
  *((u32_a_t*)(b   ))    = (*((u32_a_t*)T5[u.temp[0][0]])
 
1362
                        ^ *((u32_a_t*)T6[u.temp[3][1]])
 
1363
                        ^ *((u32_a_t*)T7[u.temp[2][2]])
 
1364
                        ^ *((u32_a_t*)T8[u.temp[1][3]]));
 
1365
  *((u32_a_t*)(b+ 4))    = (*((u32_a_t*)T5[u.temp[1][0]])
 
1366
                        ^ *((u32_a_t*)T6[u.temp[0][1]])
 
1367
                        ^ *((u32_a_t*)T7[u.temp[3][2]])
 
1368
                        ^ *((u32_a_t*)T8[u.temp[2][3]]));
 
1369
  *((u32_a_t*)(b+ 8))    = (*((u32_a_t*)T5[u.temp[2][0]])
 
1370
                        ^ *((u32_a_t*)T6[u.temp[1][1]])
 
1371
                        ^ *((u32_a_t*)T7[u.temp[0][2]])
 
1372
                        ^ *((u32_a_t*)T8[u.temp[3][3]]));
 
1373
  *((u32_a_t*)(b+12))    = (*((u32_a_t*)T5[u.temp[3][0]])
 
1374
                        ^ *((u32_a_t*)T6[u.temp[2][1]])
 
1375
                        ^ *((u32_a_t*)T7[u.temp[1][2]])
 
1376
                        ^ *((u32_a_t*)T8[u.temp[0][3]]));
 
1377
 
 
1378
  for (r = rounds-1; r > 1; r--)
609
1379
    {
610
 
      *((u32*)u.temp[0]) = *((u32*)(b   )) ^ *((u32*)rk[r][0]);
611
 
      *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[r][1]);
612
 
      *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[r][2]);
613
 
      *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[r][3]);
614
 
      *((u32*)(b   ))    = (*((u32*)T5[u.temp[0][0]])
615
 
                            ^ *((u32*)T6[u.temp[3][1]])
616
 
                            ^ *((u32*)T7[u.temp[2][2]]) 
617
 
                            ^ *((u32*)T8[u.temp[1][3]]));
618
 
      *((u32*)(b+ 4))    = (*((u32*)T5[u.temp[1][0]])
619
 
                            ^ *((u32*)T6[u.temp[0][1]])
620
 
                            ^ *((u32*)T7[u.temp[3][2]]) 
621
 
                            ^ *((u32*)T8[u.temp[2][3]]));
622
 
      *((u32*)(b+ 8))    = (*((u32*)T5[u.temp[2][0]])
623
 
                            ^ *((u32*)T6[u.temp[1][1]])
624
 
                            ^ *((u32*)T7[u.temp[0][2]]) 
625
 
                            ^ *((u32*)T8[u.temp[3][3]]));
626
 
      *((u32*)(b+12))    = (*((u32*)T5[u.temp[3][0]])
627
 
                            ^ *((u32*)T6[u.temp[2][1]])
628
 
                            ^ *((u32*)T7[u.temp[1][2]]) 
629
 
                            ^ *((u32*)T8[u.temp[0][3]]));
 
1380
      *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(b   )) ^ *((u32_a_t*)rk[r][0]);
 
1381
      *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(b+ 4)) ^ *((u32_a_t*)rk[r][1]);
 
1382
      *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(b+ 8)) ^ *((u32_a_t*)rk[r][2]);
 
1383
      *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(b+12)) ^ *((u32_a_t*)rk[r][3]);
 
1384
      *((u32_a_t*)(b   ))    = (*((u32_a_t*)T5[u.temp[0][0]])
 
1385
                            ^ *((u32_a_t*)T6[u.temp[3][1]])
 
1386
                            ^ *((u32_a_t*)T7[u.temp[2][2]])
 
1387
                            ^ *((u32_a_t*)T8[u.temp[1][3]]));
 
1388
      *((u32_a_t*)(b+ 4))    = (*((u32_a_t*)T5[u.temp[1][0]])
 
1389
                            ^ *((u32_a_t*)T6[u.temp[0][1]])
 
1390
                            ^ *((u32_a_t*)T7[u.temp[3][2]])
 
1391
                            ^ *((u32_a_t*)T8[u.temp[2][3]]));
 
1392
      *((u32_a_t*)(b+ 8))    = (*((u32_a_t*)T5[u.temp[2][0]])
 
1393
                            ^ *((u32_a_t*)T6[u.temp[1][1]])
 
1394
                            ^ *((u32_a_t*)T7[u.temp[0][2]])
 
1395
                            ^ *((u32_a_t*)T8[u.temp[3][3]]));
 
1396
      *((u32_a_t*)(b+12))    = (*((u32_a_t*)T5[u.temp[3][0]])
 
1397
                            ^ *((u32_a_t*)T6[u.temp[2][1]])
 
1398
                            ^ *((u32_a_t*)T7[u.temp[1][2]])
 
1399
                            ^ *((u32_a_t*)T8[u.temp[0][3]]));
630
1400
    }
631
1401
 
632
 
  /* Last round is special. */   
633
 
  *((u32*)u.temp[0]) = *((u32*)(b   )) ^ *((u32*)rk[1][0]);
634
 
  *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[1][1]);
635
 
  *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[1][2]);
636
 
  *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[1][3]);
 
1402
  /* Last round is special. */
 
1403
  *((u32_a_t*)u.temp[0]) = *((u32_a_t*)(b   )) ^ *((u32_a_t*)rk[1][0]);
 
1404
  *((u32_a_t*)u.temp[1]) = *((u32_a_t*)(b+ 4)) ^ *((u32_a_t*)rk[1][1]);
 
1405
  *((u32_a_t*)u.temp[2]) = *((u32_a_t*)(b+ 8)) ^ *((u32_a_t*)rk[1][2]);
 
1406
  *((u32_a_t*)u.temp[3]) = *((u32_a_t*)(b+12)) ^ *((u32_a_t*)rk[1][3]);
637
1407
  b[ 0] = S5[u.temp[0][0]];
638
1408
  b[ 1] = S5[u.temp[3][1]];
639
1409
  b[ 2] = S5[u.temp[2][2]];
650
1420
  b[13] = S5[u.temp[2][1]];
651
1421
  b[14] = S5[u.temp[1][2]];
652
1422
  b[15] = S5[u.temp[0][3]];
653
 
  *((u32*)(b   )) ^= *((u32*)rk[0][0]);
654
 
  *((u32*)(b+ 4)) ^= *((u32*)rk[0][1]);
655
 
  *((u32*)(b+ 8)) ^= *((u32*)rk[0][2]);
656
 
  *((u32*)(b+12)) ^= *((u32*)rk[0][3]);
 
1423
  *((u32_a_t*)(b   )) ^= *((u32_a_t*)rk[0][0]);
 
1424
  *((u32_a_t*)(b+ 4)) ^= *((u32_a_t*)rk[0][1]);
 
1425
  *((u32_a_t*)(b+ 8)) ^= *((u32_a_t*)rk[0][2]);
 
1426
  *((u32_a_t*)(b+12)) ^= *((u32_a_t*)rk[0][3]);
657
1427
#undef rk
658
1428
}
659
1429
 
662
1432
static void
663
1433
do_decrypt (RIJNDAEL_context *ctx, byte *bx, const byte *ax)
664
1434
{
665
 
  /* BX and AX are not necessary correctly aligned.  Thus we need to
666
 
     copy them here. */
667
 
  union
668
 
  {
669
 
    u32  dummy[4]; 
670
 
    byte a[16];
671
 
  } a;
672
 
  union
673
 
  {
674
 
    u32  dummy[4]; 
675
 
    byte b[16];
676
 
  } b;
677
 
 
678
1435
  if ( !ctx->decryption_prepared )
679
1436
    {
680
1437
      prepare_decryption ( ctx );
682
1439
      ctx->decryption_prepared = 1;
683
1440
    }
684
1441
 
685
 
  memcpy (a.a, ax, 16);
686
 
  do_decrypt_aligned (ctx, b.b, a.a);
687
 
  memcpy (bx, b.b, 16);
688
 
#undef rk
 
1442
  /* BX and AX are not necessary correctly aligned.  Thus we might
 
1443
     need to copy them here.  We try to align to a 16 bytes. */
 
1444
  if (((size_t)ax & 0x0f) || ((size_t)bx & 0x0f))
 
1445
    {
 
1446
      union
 
1447
      {
 
1448
        u32  dummy[4];
 
1449
        byte a[16] ATTR_ALIGNED_16;
 
1450
      } a;
 
1451
      union
 
1452
      {
 
1453
        u32  dummy[4];
 
1454
        byte b[16] ATTR_ALIGNED_16;
 
1455
      } b;
 
1456
 
 
1457
      memcpy (a.a, ax, 16);
 
1458
      do_decrypt_aligned (ctx, b.b, a.a);
 
1459
      memcpy (bx, b.b, 16);
 
1460
    }
 
1461
  else
 
1462
    {
 
1463
      do_decrypt_aligned (ctx, bx, ax);
 
1464
    }
689
1465
}
690
 
    
 
1466
 
691
1467
 
692
1468
 
693
1469
 
696
1472
{
697
1473
  RIJNDAEL_context *ctx = context;
698
1474
 
 
1475
  if (0)
 
1476
    ;
699
1477
#ifdef USE_PADLOCK
700
 
  if (ctx->use_padlock)
 
1478
  else if (ctx->use_padlock)
701
1479
    {
702
1480
      do_padlock (ctx, 1, b, a);
703
1481
      _gcry_burn_stack (48 + 2*sizeof(int) /* FIXME */);
704
1482
    }
 
1483
#endif /*USE_PADLOCK*/
 
1484
#ifdef USE_AESNI
 
1485
  else if (ctx->use_aesni)
 
1486
    {
 
1487
      aesni_prepare ();
 
1488
      do_aesni (ctx, 1, b, a);
 
1489
      aesni_cleanup ();
 
1490
    }
 
1491
#endif /*USE_AESNI*/
705
1492
  else
706
 
#endif /*USE_PADLOCK*/
707
1493
    {
708
1494
      do_decrypt (ctx, b, a);
709
 
      _gcry_burn_stack (48+2*sizeof(int));
 
1495
      _gcry_burn_stack (56+2*sizeof(int));
710
1496
    }
711
1497
}
712
1498
 
716
1502
   function is only intended for the bulk encryption feature of
717
1503
   cipher.c. */
718
1504
void
719
 
_gcry_aes_cfb_dec (void *context, unsigned char *iv, 
 
1505
_gcry_aes_cfb_dec (void *context, unsigned char *iv,
720
1506
                   void *outbuf_arg, const void *inbuf_arg,
721
1507
                   unsigned int nblocks)
722
1508
{
727
1513
  unsigned char temp;
728
1514
  int i;
729
1515
 
 
1516
  if (0)
 
1517
    ;
730
1518
#ifdef USE_PADLOCK
731
 
  if (ctx->use_padlock)
 
1519
  else if (ctx->use_padlock)
732
1520
    {
733
1521
      /* Fixme:  Let Padlock do the CFBing.  */
734
1522
      for ( ;nblocks; nblocks-- )
742
1530
            }
743
1531
        }
744
1532
    }
 
1533
#endif /*USE_PADLOCK*/
 
1534
#ifdef USE_AESNI
 
1535
  else if (ctx->use_aesni)
 
1536
    {
 
1537
      aesni_prepare ();
 
1538
      for ( ;nblocks; nblocks-- )
 
1539
        {
 
1540
          do_aesni_cfb (ctx, 1, iv, outbuf, inbuf);
 
1541
          outbuf += BLOCKSIZE;
 
1542
          inbuf  += BLOCKSIZE;
 
1543
        }
 
1544
      aesni_cleanup ();
 
1545
    }
 
1546
#endif /*USE_AESNI*/
745
1547
  else
746
 
#endif /*USE_PADLOCK*/
747
1548
    {
748
1549
      for ( ;nblocks; nblocks-- )
749
1550
        {
766
1567
   function is only intended for the bulk encryption feature of
767
1568
   cipher.c. */
768
1569
void
769
 
_gcry_aes_cbc_dec (void *context, unsigned char *iv, 
 
1570
_gcry_aes_cbc_dec (void *context, unsigned char *iv,
770
1571
                   void *outbuf_arg, const void *inbuf_arg,
771
1572
                   unsigned int nblocks)
772
1573
{
777
1578
  int i;
778
1579
  unsigned char savebuf[BLOCKSIZE];
779
1580
 
 
1581
#ifdef USE_AESNI
 
1582
  if (ctx->use_aesni)
 
1583
    aesni_prepare ();
 
1584
#endif /*USE_AESNI*/
 
1585
 
780
1586
  for ( ;nblocks; nblocks-- )
781
1587
    {
782
1588
      /* We need to save INBUF away because it may be identical to
783
1589
         OUTBUF.  */
784
1590
      memcpy (savebuf, inbuf, BLOCKSIZE);
785
1591
 
 
1592
      if (0)
 
1593
        ;
786
1594
#ifdef USE_PADLOCK
787
 
      if (ctx->use_padlock)
 
1595
      else if (ctx->use_padlock)
788
1596
        do_padlock (ctx, 1, outbuf, inbuf);
 
1597
#endif /*USE_PADLOCK*/
 
1598
#ifdef USE_AESNI
 
1599
      else if (ctx->use_aesni)
 
1600
        do_aesni (ctx, 1, outbuf, inbuf);
 
1601
#endif /*USE_AESNI*/
789
1602
      else
790
 
#endif /*USE_PADLOCK*/
791
1603
        do_decrypt (ctx, outbuf, inbuf);
792
1604
 
793
1605
      for (ivp=iv, i=0; i < BLOCKSIZE; i++ )
797
1609
      outbuf += BLOCKSIZE;
798
1610
    }
799
1611
 
 
1612
#ifdef USE_AESNI
 
1613
  if (ctx->use_aesni)
 
1614
    aesni_cleanup ();
 
1615
#endif /*USE_AESNI*/
 
1616
 
800
1617
  _gcry_burn_stack (48 + 2*sizeof(int) + BLOCKSIZE + 4*sizeof (char*));
801
1618
}
802
1619
 
808
1625
selftest_basic_128 (void)
809
1626
{
810
1627
  RIJNDAEL_context ctx;
811
 
  unsigned char scratch[16];       
 
1628
  unsigned char scratch[16];
812
1629
 
813
1630
  /* The test vectors are from the AES supplied ones; more or less
814
1631
     randomly taken from ecb_tbl.txt (I=42,81,14) */
815
 
  static const unsigned char plaintext_128[16] = 
 
1632
#if 1
 
1633
  static const unsigned char plaintext_128[16] =
816
1634
    {
817
1635
      0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,
818
1636
      0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A
827
1645
      0x67,0x43,0xC3,0xD1,0x51,0x9A,0xB4,0xF2,
828
1646
      0xCD,0x9A,0x78,0xAB,0x09,0xA5,0x11,0xBD
829
1647
    };
830
 
  
 
1648
#else
 
1649
  /* Test vectors from fips-197, appendix C. */
 
1650
# warning debug test vectors in use
 
1651
  static const unsigned char plaintext_128[16] =
 
1652
    {
 
1653
      0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
 
1654
      0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
 
1655
    };
 
1656
  static const unsigned char key_128[16] =
 
1657
    {
 
1658
      0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
 
1659
      0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
 
1660
      /* 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, */
 
1661
      /* 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c */
 
1662
    };
 
1663
  static const unsigned char ciphertext_128[16] =
 
1664
    {
 
1665
      0x69,0xc4,0xe0,0xd8,0x6a,0x7b,0x04,0x30,
 
1666
      0xd8,0xcd,0xb7,0x80,0x70,0xb4,0xc5,0x5a
 
1667
    };
 
1668
#endif
 
1669
 
831
1670
  rijndael_setkey (&ctx, key_128, sizeof (key_128));
832
1671
  rijndael_encrypt (&ctx, scratch, plaintext_128);
833
1672
  if (memcmp (scratch, ciphertext_128, sizeof (ciphertext_128)))
835
1674
  rijndael_decrypt (&ctx, scratch, scratch);
836
1675
  if (memcmp (scratch, plaintext_128, sizeof (plaintext_128)))
837
1676
    return "AES-128 test decryption failed.";
838
 
  
 
1677
 
839
1678
  return NULL;
840
1679
}
841
1680
 
844
1683
selftest_basic_192 (void)
845
1684
{
846
1685
  RIJNDAEL_context ctx;
847
 
  unsigned char scratch[16];       
848
 
  
849
 
  static unsigned char plaintext_192[16] = 
 
1686
  unsigned char scratch[16];
 
1687
 
 
1688
  static unsigned char plaintext_192[16] =
850
1689
    {
851
1690
      0x76,0x77,0x74,0x75,0xF1,0xF2,0xF3,0xF4,
852
1691
      0xF8,0xF9,0xE6,0xE7,0x77,0x70,0x71,0x72
853
1692
    };
854
 
  static unsigned char key_192[24] = 
 
1693
  static unsigned char key_192[24] =
855
1694
    {
856
1695
      0x04,0x05,0x06,0x07,0x09,0x0A,0x0B,0x0C,
857
1696
      0x0E,0x0F,0x10,0x11,0x13,0x14,0x15,0x16,
862
1701
      0x5D,0x1E,0xF2,0x0D,0xCE,0xD6,0xBC,0xBC,
863
1702
      0x12,0x13,0x1A,0xC7,0xC5,0x47,0x88,0xAA
864
1703
    };
865
 
    
 
1704
 
866
1705
  rijndael_setkey (&ctx, key_192, sizeof(key_192));
867
1706
  rijndael_encrypt (&ctx, scratch, plaintext_192);
868
1707
  if (memcmp (scratch, ciphertext_192, sizeof (ciphertext_192)))
870
1709
  rijndael_decrypt (&ctx, scratch, scratch);
871
1710
  if (memcmp (scratch, plaintext_192, sizeof (plaintext_192)))
872
1711
    return "AES-192 test decryption failed.";
873
 
  
 
1712
 
874
1713
  return NULL;
875
1714
}
876
1715
 
880
1719
selftest_basic_256 (void)
881
1720
{
882
1721
  RIJNDAEL_context ctx;
883
 
  unsigned char scratch[16];       
 
1722
  unsigned char scratch[16];
884
1723
 
885
 
  static unsigned char plaintext_256[16] = 
 
1724
  static unsigned char plaintext_256[16] =
886
1725
    {
887
1726
      0x06,0x9A,0x00,0x7F,0xC7,0x6A,0x45,0x9F,
888
1727
      0x98,0xBA,0xF9,0x17,0xFE,0xDF,0x95,0x21
889
1728
    };
890
 
  static unsigned char key_256[32] = 
 
1729
  static unsigned char key_256[32] =
891
1730
    {
892
1731
      0x08,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,
893
1732
      0x12,0x13,0x14,0x15,0x17,0x18,0x19,0x1A,
894
1733
      0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x24,
895
1734
      0x26,0x27,0x28,0x29,0x2B,0x2C,0x2D,0x2E
896
1735
    };
897
 
  static const unsigned char ciphertext_256[16] = 
 
1736
  static const unsigned char ciphertext_256[16] =
898
1737
    {
899
1738
      0x08,0x0E,0x95,0x17,0xEB,0x16,0x77,0x71,
900
1739
      0x9A,0xCF,0x72,0x80,0x86,0x04,0x0A,0xE3
907
1746
  rijndael_decrypt (&ctx, scratch, scratch);
908
1747
  if (memcmp (scratch, plaintext_256, sizeof (plaintext_256)))
909
1748
    return "AES-256 test decryption failed.";
910
 
    
 
1749
 
911
1750
  return NULL;
912
1751
}
913
1752
 
936
1775
    int mode;
937
1776
    const unsigned char key[16];
938
1777
    const unsigned char iv[16];
939
 
    struct 
 
1778
    struct
940
1779
    {
941
1780
      const unsigned char input[16];
942
1781
      const unsigned char output[16];
947
1786
        GCRY_CIPHER_MODE_CFB,  /* F.3.13, CFB128-AES128 */
948
1787
        { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
949
1788
          0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
950
 
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
 
1789
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
951
1790
          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
952
1791
        {
953
1792
          { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
954
1793
              0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
955
1794
            { 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20,
956
1795
              0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a } },
957
 
          
 
1796
 
958
1797
          { { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
959
1798
              0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
960
1799
            { 0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f,
961
1800
              0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b } },
962
 
          
963
 
          { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 
 
1801
 
 
1802
          { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
964
1803
              0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
965
1804
            { 0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40,
966
1805
              0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf } },
967
 
          
 
1806
 
968
1807
          { { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
969
1808
              0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
970
1809
            { 0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e,
975
1814
        GCRY_CIPHER_MODE_OFB,
976
1815
        { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
977
1816
          0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
978
 
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
 
1817
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
979
1818
          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
980
1819
        {
981
1820
          { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
987
1826
              0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
988
1827
            { 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
989
1828
              0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25 } },
990
 
          
 
1829
 
991
1830
          { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
992
1831
              0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
993
1832
            { 0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6,
1057
1896
 
1058
1897
#undef Fail
1059
1898
  _gcry_cipher_close (hdenc);
1060
 
  _gcry_cipher_close (hddec); 
 
1899
  _gcry_cipher_close (hddec);
1061
1900
  return NULL;
1062
1901
}
1063
1902
 
1068
1907
{
1069
1908
  const char *what;
1070
1909
  const char *errtxt;
1071
 
  
 
1910
 
1072
1911
  what = "low-level";
1073
1912
  errtxt = selftest_basic_128 ();
1074
1913
  if (errtxt)
1080
1919
      errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_CFB);
1081
1920
      if (errtxt)
1082
1921
        goto failed;
1083
 
      
 
1922
 
1084
1923
      what = "ofb";
1085
1924
      errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_OFB);
1086
1925
      if (errtxt)
1125
1964
{
1126
1965
  const char *what;
1127
1966
  const char *errtxt;
1128
 
  
 
1967
 
1129
1968
  (void)extended; /* No extended tests available.  */
1130
1969
 
1131
1970
  what = "low-level";
1163
2002
    default:
1164
2003
      ec = GPG_ERR_CIPHER_ALGO;
1165
2004
      break;
1166
 
        
 
2005
 
1167
2006
    }
1168
2007
  return ec;
1169
2008
}
1193
2032
    "AES", rijndael_names, rijndael_oids, 16, 128, sizeof (RIJNDAEL_context),
1194
2033
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1195
2034
  };
1196
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes = 
 
2035
cipher_extra_spec_t _gcry_cipher_extraspec_aes =
1197
2036
  {
1198
2037
    run_selftests
1199
2038
  };
1219
2058
    "AES192", rijndael192_names, rijndael192_oids, 16, 192, sizeof (RIJNDAEL_context),
1220
2059
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1221
2060
  };
1222
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes192 = 
 
2061
cipher_extra_spec_t _gcry_cipher_extraspec_aes192 =
1223
2062
  {
1224
2063
    run_selftests
1225
2064
  };
1247
2086
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1248
2087
  };
1249
2088
 
1250
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes256 = 
 
2089
cipher_extra_spec_t _gcry_cipher_extraspec_aes256 =
1251
2090
  {
1252
2091
    run_selftests
1253
2092
  };