~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to lib/libgcrypt/cipher/rijndael.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Rijndael (AES) for GnuPG
2
 
 * Copyright (C) 2000, 2001, 2002, 2003, 2007,
3
 
 *               2008 Free Software Foundation, Inc.
4
 
 *
5
 
 * This file is part of Libgcrypt.
6
 
 *
7
 
 * Libgcrypt is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU Lesser General Public License as
9
 
 * published by the Free Software Foundation; either version 2.1 of
10
 
 * the License, or (at your option) any later version.
11
 
 *
12
 
 * Libgcrypt is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
 
 *******************************************************************
20
 
 * The code here is based on the optimized implementation taken from
21
 
 * http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ on Oct 2, 2000,
22
 
 * which carries this notice:
23
 
 *------------------------------------------
24
 
 * rijndael-alg-fst.c   v2.3   April '2000
25
 
 *
26
 
 * Optimised ANSI C code
27
 
 *
28
 
 * authors: v1.0: Antoon Bosselaers
29
 
 *          v2.0: Vincent Rijmen
30
 
 *          v2.3: Paulo Barreto
31
 
 *
32
 
 * This code is placed in the public domain.
33
 
 *------------------------------------------
34
 
 *
35
 
 * The SP800-38a document is available at:
36
 
 *   http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
37
 
 *
38
 
 */
39
 
 
40
 
#include <config.h>
41
 
#include <stdio.h>
42
 
#include <stdlib.h>
43
 
#include <string.h> /* for memcmp() */
44
 
 
45
 
#include "types.h"  /* for byte and u32 typedefs */
46
 
#include "g10lib.h"
47
 
#include "cipher.h"
48
 
 
49
 
#define MAXKC                   (256/32)
50
 
#define MAXROUNDS               14
51
 
#define BLOCKSIZE               (128/8)
52
 
 
53
 
 
54
 
/* USE_PADLOCK indicates whether to compile the padlock specific
55
 
   code.  */
56
 
#undef USE_PADLOCK
57
 
#ifdef ENABLE_PADLOCK_SUPPORT
58
 
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
59
 
# define USE_PADLOCK
60
 
# endif
61
 
#endif /*ENABLE_PADLOCK_SUPPORT*/
62
 
 
63
 
static const char *selftest(void);
64
 
 
65
 
typedef struct 
66
 
{
67
 
  int   ROUNDS;             /* Key-length-dependent number of rounds.  */
68
 
  int decryption_prepared;  /* The decryption key schedule is available.  */
69
 
#ifdef USE_PADLOCK
70
 
  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)));
73
 
#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
 
 
86
 
#define keySched  u1.keyschedule
87
 
#define keySched2 u2.keyschedule
88
 
 
89
 
/* All the numbers.  */
90
 
#include "rijndael-tables.h"
91
 
 
92
 
 
93
 
/* Perform the key setup.  */  
94
 
static gcry_err_code_t
95
 
do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen)
96
 
{
97
 
  static int initialized = 0;
98
 
  static const char *selftest_failed=0;
99
 
  int ROUNDS;
100
 
  int i,j, r, t, rconpointer = 0;
101
 
  int KC;
102
 
  union
103
 
  {
104
 
    PROPERLY_ALIGNED_TYPE dummy;
105
 
    byte k[MAXKC][4];
106
 
  } k;
107
 
#define k k.k
108
 
  union
109
 
  {
110
 
    PROPERLY_ALIGNED_TYPE dummy;
111
 
    byte tk[MAXKC][4];
112
 
  } tk;
113
 
#define tk tk.tk  
114
 
 
115
 
  /* The on-the-fly self tests are only run in non-fips mode. In fips
116
 
     mode explicit self-tests are required.  Actually the on-the-fly
117
 
     self-tests are not fully thread-safe and it might happen that a
118
 
     failed self-test won't get noticed in another thread.  
119
 
 
120
 
     FIXME: We might want to have a central registry of succeeded
121
 
     self-tests. */
122
 
  if (!fips_mode () && !initialized)
123
 
    {
124
 
      initialized = 1;
125
 
      selftest_failed = selftest ();
126
 
      if (selftest_failed)
127
 
        log_error ("%s\n", selftest_failed );
128
 
    }
129
 
  if (selftest_failed)
130
 
    return GPG_ERR_SELFTEST_FAILED;
131
 
 
132
 
  ctx->decryption_prepared = 0;
133
 
#ifdef USE_PADLOCK
134
 
  ctx->use_padlock = 0;
135
 
#endif
136
 
 
137
 
  if( keylen == 128/8 )
138
 
    {
139
 
      ROUNDS = 10;
140
 
      KC = 4;
141
 
#ifdef USE_PADLOCK
142
 
      if ((_gcry_get_hw_features () & HWF_PADLOCK_AES))
143
 
        {
144
 
          ctx->use_padlock = 1;
145
 
          memcpy (ctx->padlock_key, key, keylen);
146
 
        }
147
 
#endif
148
 
    }
149
 
  else if ( keylen == 192/8 )
150
 
    {
151
 
      ROUNDS = 12;
152
 
      KC = 6;
153
 
    }
154
 
  else if ( keylen == 256/8 )
155
 
    {
156
 
      ROUNDS = 14;
157
 
      KC = 8;
158
 
    }
159
 
  else
160
 
    return GPG_ERR_INV_KEYLEN;
161
 
 
162
 
  ctx->ROUNDS = ROUNDS;
163
 
 
164
 
#ifdef USE_PADLOCK
165
 
  if (ctx->use_padlock)
166
 
    {
167
 
      /* Nothing to do as we support only hardware key generation for
168
 
         now.  */
169
 
    }
170
 
  else
171
 
#endif /*USE_PADLOCK*/
172
 
    {
173
 
#define W (ctx->keySched)
174
 
      for (i = 0; i < keylen; i++) 
175
 
        {
176
 
          k[i >> 2][i & 3] = key[i]; 
177
 
        }
178
 
      
179
 
      for (j = KC-1; j >= 0; j--) 
180
 
        {
181
 
          *((u32*)tk[j]) = *((u32*)k[j]);
182
 
        }
183
 
      r = 0;
184
 
      t = 0;
185
 
      /* Copy values into round key array.  */
186
 
      for (j = 0; (j < KC) && (r < ROUNDS + 1); )
187
 
        {
188
 
          for (; (j < KC) && (t < 4); j++, t++)
189
 
            {
190
 
              *((u32*)W[r][t]) = *((u32*)tk[j]);
191
 
            }
192
 
          if (t == 4)
193
 
            {
194
 
              r++;
195
 
              t = 0;
196
 
            }
197
 
        }
198
 
      
199
 
      while (r < ROUNDS + 1)
200
 
        {
201
 
          /* While not enough round key material calculated calculate
202
 
             new values.  */
203
 
          tk[0][0] ^= S[tk[KC-1][1]];
204
 
          tk[0][1] ^= S[tk[KC-1][2]];
205
 
          tk[0][2] ^= S[tk[KC-1][3]];
206
 
          tk[0][3] ^= S[tk[KC-1][0]];
207
 
          tk[0][0] ^= rcon[rconpointer++];
208
 
          
209
 
          if (KC != 8)
210
 
            {
211
 
              for (j = 1; j < KC; j++) 
212
 
                {
213
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
214
 
                }
215
 
            } 
216
 
          else 
217
 
            {
218
 
              for (j = 1; j < KC/2; j++)
219
 
                {
220
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
221
 
                }
222
 
              tk[KC/2][0] ^= S[tk[KC/2 - 1][0]];
223
 
              tk[KC/2][1] ^= S[tk[KC/2 - 1][1]];
224
 
              tk[KC/2][2] ^= S[tk[KC/2 - 1][2]];
225
 
              tk[KC/2][3] ^= S[tk[KC/2 - 1][3]];
226
 
              for (j = KC/2 + 1; j < KC; j++)
227
 
                {
228
 
                  *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
229
 
                }
230
 
            }
231
 
          
232
 
          /* Copy values into round key array.  */
233
 
          for (j = 0; (j < KC) && (r < ROUNDS + 1); )
234
 
            {
235
 
              for (; (j < KC) && (t < 4); j++, t++)
236
 
                {
237
 
                  *((u32*)W[r][t]) = *((u32*)tk[j]);
238
 
                }
239
 
              if (t == 4)
240
 
                {
241
 
                  r++;
242
 
                  t = 0;
243
 
                }
244
 
            }
245
 
        }               
246
 
#undef W    
247
 
    }
248
 
 
249
 
  return 0;
250
 
#undef tk
251
 
#undef k
252
 
}
253
 
 
254
 
 
255
 
static gcry_err_code_t
256
 
rijndael_setkey (void *context, const byte *key, const unsigned keylen)
257
 
{
258
 
  RIJNDAEL_context *ctx = context;
259
 
 
260
 
  int rc = do_setkey (ctx, key, keylen);
261
 
  _gcry_burn_stack ( 100 + 16*sizeof(int));
262
 
  return rc;
263
 
}
264
 
 
265
 
 
266
 
/* Make a decryption key from an encryption key. */
267
 
static void
268
 
prepare_decryption( RIJNDAEL_context *ctx )
269
 
{
270
 
  int r;
271
 
  union
272
 
  {
273
 
    PROPERLY_ALIGNED_TYPE dummy;
274
 
    byte *w;
275
 
  } w;
276
 
#define w w.w
277
 
 
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
 
    }
304
 
#undef W
305
 
#undef w
306
 
}       
307
 
 
308
 
 
309
 
 
310
 
/* Encrypt one block.  A and B need to be aligned on a 4 byte
311
 
   boundary.  A and B may be the same. */
312
 
static void
313
 
do_encrypt_aligned (const RIJNDAEL_context *ctx, 
314
 
                    unsigned char *b, const unsigned char *a)
315
 
{
316
 
#define rk (ctx->keySched)
317
 
  int ROUNDS = ctx->ROUNDS;
318
 
  int r;
319
 
  union
320
 
  {
321
 
    u32  tempu32[4];  /* Force correct alignment. */
322
 
    byte temp[4][4];
323
 
  } u;
324
 
 
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]]));
345
 
 
346
 
  for (r = 1; r < ROUNDS-1; r++)
347
 
    {
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]);
352
 
 
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]]));
369
 
    }
370
 
 
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]);
376
 
  b[ 0] = T1[u.temp[0][0]][1];
377
 
  b[ 1] = T1[u.temp[1][1]][1];
378
 
  b[ 2] = T1[u.temp[2][2]][1];
379
 
  b[ 3] = T1[u.temp[3][3]][1];
380
 
  b[ 4] = T1[u.temp[1][0]][1];
381
 
  b[ 5] = T1[u.temp[2][1]][1];
382
 
  b[ 6] = T1[u.temp[3][2]][1];
383
 
  b[ 7] = T1[u.temp[0][3]][1];
384
 
  b[ 8] = T1[u.temp[2][0]][1];
385
 
  b[ 9] = T1[u.temp[3][1]][1];
386
 
  b[10] = T1[u.temp[0][2]][1];
387
 
  b[11] = T1[u.temp[1][3]][1];
388
 
  b[12] = T1[u.temp[3][0]][1];
389
 
  b[13] = T1[u.temp[0][1]][1];
390
 
  b[14] = T1[u.temp[1][2]][1];
391
 
  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]);
396
 
#undef rk
397
 
}
398
 
 
399
 
 
400
 
static void
401
 
do_encrypt (const RIJNDAEL_context *ctx,
402
 
            unsigned char *bx, const unsigned char *ax)
403
 
{
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;
416
 
 
417
 
  memcpy (a.a, ax, 16);
418
 
  do_encrypt_aligned (ctx, b.b, a.a);
419
 
  memcpy (bx, b.b, 16);
420
 
}
421
 
 
422
 
 
423
 
/* Encrypt or decrypt one block using the padlock engine.  A and B may
424
 
   be the same. */
425
 
#ifdef USE_PADLOCK
426
 
static void
427
 
do_padlock (const RIJNDAEL_context *ctx, int decrypt_flag,
428
 
            unsigned char *bx, const unsigned char *ax)
429
 
{
430
 
  /* BX and AX are not necessary correctly aligned.  Thus we need to
431
 
     copy them here. */
432
 
  unsigned char a[16] __attribute__ ((aligned (16)));
433
 
  unsigned char b[16] __attribute__ ((aligned (16)));
434
 
  unsigned int cword[4] __attribute__ ((aligned (16)));
435
 
 
436
 
  /* The control word fields are:
437
 
      127:12   11:10 9     8     7     6     5     4     3:0
438
 
      RESERVED KSIZE CRYPT INTER KEYGN CIPHR ALIGN DGEST ROUND  */
439
 
  cword[0] = (ctx->ROUNDS & 15);  /* (The mask is just a safeguard.)  */
440
 
  cword[1] = 0;
441
 
  cword[2] = 0;
442
 
  cword[3] = 0;
443
 
  if (decrypt_flag)
444
 
    cword[0] |= 0x00000200;
445
 
 
446
 
  memcpy (a, ax, 16);
447
 
   
448
 
  asm volatile 
449
 
    ("pushfl\n\t"          /* Force key reload.  */            
450
 
     "popfl\n\t"
451
 
     "xchg %3, %%ebx\n\t"  /* Load key.  */
452
 
     "movl $1, %%ecx\n\t"  /* Init counter for just one block.  */
453
 
     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XSTORE ECB. */
454
 
     "xchg %3, %%ebx\n"    /* Restore GOT register.  */
455
 
     : /* No output */
456
 
     : "S" (a), "D" (b), "d" (cword), "r" (ctx->padlock_key)
457
 
     : "%ecx", "cc", "memory"
458
 
     );
459
 
 
460
 
  memcpy (bx, b, 16);
461
 
 
462
 
}
463
 
#endif /*USE_PADLOCK*/
464
 
 
465
 
 
466
 
static void
467
 
rijndael_encrypt (void *context, byte *b, const byte *a)
468
 
{
469
 
  RIJNDAEL_context *ctx = context;
470
 
 
471
 
#ifdef USE_PADLOCK
472
 
  if (ctx->use_padlock)
473
 
    {
474
 
      do_padlock (ctx, 0, b, a);
475
 
      _gcry_burn_stack (48 + 15 /* possible padding for alignment */);
476
 
    }
477
 
  else
478
 
#endif /*USE_PADLOCK*/
479
 
    {
480
 
      do_encrypt (ctx, b, a);
481
 
      _gcry_burn_stack (48 + 2*sizeof(int));
482
 
    }
483
 
}
484
 
 
485
 
 
486
 
/* Bulk encryption of complete blocks in CFB mode.  Caller needs to
487
 
   make sure that IV is aligned on an unsigned long boundary.  This
488
 
   function is only intended for the bulk encryption feature of
489
 
   cipher.c. */
490
 
void
491
 
_gcry_aes_cfb_enc (void *context, unsigned char *iv, 
492
 
                   void *outbuf_arg, const void *inbuf_arg,
493
 
                   unsigned int nblocks)
494
 
{
495
 
  RIJNDAEL_context *ctx = context;
496
 
  unsigned char *outbuf = outbuf_arg;
497
 
  const unsigned char *inbuf = inbuf_arg;
498
 
  unsigned char *ivp;
499
 
  int i;
500
 
 
501
 
#ifdef USE_PADLOCK
502
 
  if (ctx->use_padlock)
503
 
    {
504
 
      /* Fixme: Let Padlock do the CFBing.  */
505
 
      for ( ;nblocks; nblocks-- )
506
 
        {
507
 
          /* Encrypt the IV. */
508
 
          do_padlock (ctx, 0, iv, iv);
509
 
          /* XOR the input with the IV and store input into IV.  */
510
 
          for (ivp=iv,i=0; i < BLOCKSIZE; i++ )
511
 
            *outbuf++ = (*ivp++ ^= *inbuf++);
512
 
        }
513
 
    }
514
 
  else
515
 
#endif /* USE_PADLOCK*/
516
 
    {
517
 
      for ( ;nblocks; nblocks-- )
518
 
        {
519
 
          /* Encrypt the IV. */
520
 
          do_encrypt_aligned (ctx, iv, iv);
521
 
          /* XOR the input with the IV and store input into IV.  */
522
 
          for (ivp=iv,i=0; i < BLOCKSIZE; i++ )
523
 
            *outbuf++ = (*ivp++ ^= *inbuf++);
524
 
        }
525
 
    }
526
 
 
527
 
  _gcry_burn_stack (48 + 2*sizeof(int));
528
 
}
529
 
 
530
 
 
531
 
/* Bulk encryption of complete blocks in CBC mode.  Caller needs to
532
 
   make sure that IV is aligned on an unsigned long boundary.  This
533
 
   function is only intended for the bulk encryption feature of
534
 
   cipher.c. */
535
 
void
536
 
_gcry_aes_cbc_enc (void *context, unsigned char *iv, 
537
 
                   void *outbuf_arg, const void *inbuf_arg,
538
 
                   unsigned int nblocks, int cbc_mac)
539
 
{
540
 
  RIJNDAEL_context *ctx = context;
541
 
  unsigned char *outbuf = outbuf_arg;
542
 
  const unsigned char *inbuf = inbuf_arg;
543
 
  unsigned char *ivp;
544
 
  int i;
545
 
 
546
 
  for ( ;nblocks; nblocks-- )
547
 
    {
548
 
      for (ivp=iv, i=0; i < BLOCKSIZE; i++ )
549
 
        outbuf[i] = inbuf[i] ^ *ivp++;
550
 
 
551
 
#ifdef USE_PADLOCK
552
 
      if (ctx->use_padlock)
553
 
        do_padlock (ctx, 0, outbuf, outbuf);
554
 
      else
555
 
#endif /*USE_PADLOCK*/
556
 
        do_encrypt (ctx, outbuf, outbuf );
557
 
 
558
 
      memcpy (iv, outbuf, BLOCKSIZE);
559
 
      inbuf += BLOCKSIZE;
560
 
      if (!cbc_mac)
561
 
        outbuf += BLOCKSIZE;
562
 
    }
563
 
 
564
 
  _gcry_burn_stack (48 + 2*sizeof(int));
565
 
}
566
 
 
567
 
 
568
 
 
569
 
/* Decrypt one block.  A and B need to be aligned on a 4 byte boundary
570
 
   and the decryption must have been prepared.  A and B may be the
571
 
   same. */
572
 
static void
573
 
do_decrypt_aligned (RIJNDAEL_context *ctx, 
574
 
                    unsigned char *b, const unsigned char *a)
575
 
{
576
 
#define rk  (ctx->keySched2)
577
 
  int ROUNDS = ctx->ROUNDS; 
578
 
  int r;
579
 
  union 
580
 
  {
581
 
    u32  tempu32[4];  /* Force correct alignment. */
582
 
    byte temp[4][4];
583
 
  } u;
584
 
 
585
 
 
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--)
609
 
    {
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]]));
630
 
    }
631
 
 
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]);
637
 
  b[ 0] = S5[u.temp[0][0]];
638
 
  b[ 1] = S5[u.temp[3][1]];
639
 
  b[ 2] = S5[u.temp[2][2]];
640
 
  b[ 3] = S5[u.temp[1][3]];
641
 
  b[ 4] = S5[u.temp[1][0]];
642
 
  b[ 5] = S5[u.temp[0][1]];
643
 
  b[ 6] = S5[u.temp[3][2]];
644
 
  b[ 7] = S5[u.temp[2][3]];
645
 
  b[ 8] = S5[u.temp[2][0]];
646
 
  b[ 9] = S5[u.temp[1][1]];
647
 
  b[10] = S5[u.temp[0][2]];
648
 
  b[11] = S5[u.temp[3][3]];
649
 
  b[12] = S5[u.temp[3][0]];
650
 
  b[13] = S5[u.temp[2][1]];
651
 
  b[14] = S5[u.temp[1][2]];
652
 
  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]);
657
 
#undef rk
658
 
}
659
 
 
660
 
 
661
 
/* Decrypt one block.  AX and BX may be the same. */
662
 
static void
663
 
do_decrypt (RIJNDAEL_context *ctx, byte *bx, const byte *ax)
664
 
{
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
 
  if ( !ctx->decryption_prepared )
679
 
    {
680
 
      prepare_decryption ( ctx );
681
 
      _gcry_burn_stack (64);
682
 
      ctx->decryption_prepared = 1;
683
 
    }
684
 
 
685
 
  memcpy (a.a, ax, 16);
686
 
  do_decrypt_aligned (ctx, b.b, a.a);
687
 
  memcpy (bx, b.b, 16);
688
 
#undef rk
689
 
}
690
 
    
691
 
 
692
 
 
693
 
 
694
 
static void
695
 
rijndael_decrypt (void *context, byte *b, const byte *a)
696
 
{
697
 
  RIJNDAEL_context *ctx = context;
698
 
 
699
 
#ifdef USE_PADLOCK
700
 
  if (ctx->use_padlock)
701
 
    {
702
 
      do_padlock (ctx, 1, b, a);
703
 
      _gcry_burn_stack (48 + 2*sizeof(int) /* FIXME */);
704
 
    }
705
 
  else
706
 
#endif /*USE_PADLOCK*/
707
 
    {
708
 
      do_decrypt (ctx, b, a);
709
 
      _gcry_burn_stack (48+2*sizeof(int));
710
 
    }
711
 
}
712
 
 
713
 
 
714
 
/* Bulk decryption of complete blocks in CFB mode.  Caller needs to
715
 
   make sure that IV is aligned on an unisgned lonhg boundary.  This
716
 
   function is only intended for the bulk encryption feature of
717
 
   cipher.c. */
718
 
void
719
 
_gcry_aes_cfb_dec (void *context, unsigned char *iv, 
720
 
                   void *outbuf_arg, const void *inbuf_arg,
721
 
                   unsigned int nblocks)
722
 
{
723
 
  RIJNDAEL_context *ctx = context;
724
 
  unsigned char *outbuf = outbuf_arg;
725
 
  const unsigned char *inbuf = inbuf_arg;
726
 
  unsigned char *ivp;
727
 
  unsigned char temp;
728
 
  int i;
729
 
 
730
 
#ifdef USE_PADLOCK
731
 
  if (ctx->use_padlock)
732
 
    {
733
 
      /* Fixme:  Let Padlock do the CFBing.  */
734
 
      for ( ;nblocks; nblocks-- )
735
 
        {
736
 
          do_padlock (ctx, 0, iv, iv);
737
 
          for (ivp=iv,i=0; i < BLOCKSIZE; i++ )
738
 
            {
739
 
              temp = *inbuf++;
740
 
              *outbuf++ = *ivp ^ temp;
741
 
              *ivp++ = temp;
742
 
            }
743
 
        }
744
 
    }
745
 
  else
746
 
#endif /*USE_PADLOCK*/
747
 
    {
748
 
      for ( ;nblocks; nblocks-- )
749
 
        {
750
 
          do_encrypt_aligned (ctx, iv, iv);
751
 
          for (ivp=iv,i=0; i < BLOCKSIZE; i++ )
752
 
            {
753
 
              temp = *inbuf++;
754
 
              *outbuf++ = *ivp ^ temp;
755
 
              *ivp++ = temp;
756
 
            }
757
 
        }
758
 
    }
759
 
 
760
 
  _gcry_burn_stack (48 + 2*sizeof(int));
761
 
}
762
 
 
763
 
 
764
 
/* Bulk decryption of complete blocks in CBC mode.  Caller needs to
765
 
   make sure that IV is aligned on an unsigned long boundary.  This
766
 
   function is only intended for the bulk encryption feature of
767
 
   cipher.c. */
768
 
void
769
 
_gcry_aes_cbc_dec (void *context, unsigned char *iv, 
770
 
                   void *outbuf_arg, const void *inbuf_arg,
771
 
                   unsigned int nblocks)
772
 
{
773
 
  RIJNDAEL_context *ctx = context;
774
 
  unsigned char *outbuf = outbuf_arg;
775
 
  const unsigned char *inbuf = inbuf_arg;
776
 
  unsigned char *ivp;
777
 
  int i;
778
 
  unsigned char savebuf[BLOCKSIZE];
779
 
 
780
 
  for ( ;nblocks; nblocks-- )
781
 
    {
782
 
      /* We need to save INBUF away because it may be identical to
783
 
         OUTBUF.  */
784
 
      memcpy (savebuf, inbuf, BLOCKSIZE);
785
 
 
786
 
#ifdef USE_PADLOCK
787
 
      if (ctx->use_padlock)
788
 
        do_padlock (ctx, 1, outbuf, inbuf);
789
 
      else
790
 
#endif /*USE_PADLOCK*/
791
 
        do_decrypt (ctx, outbuf, inbuf);
792
 
 
793
 
      for (ivp=iv, i=0; i < BLOCKSIZE; i++ )
794
 
        outbuf[i] ^= *ivp++;
795
 
      memcpy (iv, savebuf, BLOCKSIZE);
796
 
      inbuf += BLOCKSIZE;
797
 
      outbuf += BLOCKSIZE;
798
 
    }
799
 
 
800
 
  _gcry_burn_stack (48 + 2*sizeof(int) + BLOCKSIZE + 4*sizeof (char*));
801
 
}
802
 
 
803
 
 
804
 
 
805
 
 
806
 
/* Run the self-tests for AES 128.  Returns NULL on success. */
807
 
static const char*
808
 
selftest_basic_128 (void)
809
 
{
810
 
  RIJNDAEL_context ctx;
811
 
  unsigned char scratch[16];       
812
 
 
813
 
  /* The test vectors are from the AES supplied ones; more or less
814
 
     randomly taken from ecb_tbl.txt (I=42,81,14) */
815
 
  static const unsigned char plaintext_128[16] = 
816
 
    {
817
 
      0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,
818
 
      0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A
819
 
    };
820
 
  static const unsigned char key_128[16] =
821
 
    {
822
 
      0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,
823
 
      0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA
824
 
    };
825
 
  static const unsigned char ciphertext_128[16] =
826
 
    {
827
 
      0x67,0x43,0xC3,0xD1,0x51,0x9A,0xB4,0xF2,
828
 
      0xCD,0x9A,0x78,0xAB,0x09,0xA5,0x11,0xBD
829
 
    };
830
 
  
831
 
  rijndael_setkey (&ctx, key_128, sizeof (key_128));
832
 
  rijndael_encrypt (&ctx, scratch, plaintext_128);
833
 
  if (memcmp (scratch, ciphertext_128, sizeof (ciphertext_128)))
834
 
     return "AES-128 test encryption failed.";
835
 
  rijndael_decrypt (&ctx, scratch, scratch);
836
 
  if (memcmp (scratch, plaintext_128, sizeof (plaintext_128)))
837
 
    return "AES-128 test decryption failed.";
838
 
  
839
 
  return NULL;
840
 
}
841
 
 
842
 
/* Run the self-tests for AES 192.  Returns NULL on success. */
843
 
static const char*
844
 
selftest_basic_192 (void)
845
 
{
846
 
  RIJNDAEL_context ctx;
847
 
  unsigned char scratch[16];       
848
 
  
849
 
  static unsigned char plaintext_192[16] = 
850
 
    {
851
 
      0x76,0x77,0x74,0x75,0xF1,0xF2,0xF3,0xF4,
852
 
      0xF8,0xF9,0xE6,0xE7,0x77,0x70,0x71,0x72
853
 
    };
854
 
  static unsigned char key_192[24] = 
855
 
    {
856
 
      0x04,0x05,0x06,0x07,0x09,0x0A,0x0B,0x0C,
857
 
      0x0E,0x0F,0x10,0x11,0x13,0x14,0x15,0x16,
858
 
      0x18,0x19,0x1A,0x1B,0x1D,0x1E,0x1F,0x20
859
 
    };
860
 
  static const unsigned char ciphertext_192[16] =
861
 
    {
862
 
      0x5D,0x1E,0xF2,0x0D,0xCE,0xD6,0xBC,0xBC,
863
 
      0x12,0x13,0x1A,0xC7,0xC5,0x47,0x88,0xAA
864
 
    };
865
 
    
866
 
  rijndael_setkey (&ctx, key_192, sizeof(key_192));
867
 
  rijndael_encrypt (&ctx, scratch, plaintext_192);
868
 
  if (memcmp (scratch, ciphertext_192, sizeof (ciphertext_192)))
869
 
    return "AES-192 test encryption failed.";
870
 
  rijndael_decrypt (&ctx, scratch, scratch);
871
 
  if (memcmp (scratch, plaintext_192, sizeof (plaintext_192)))
872
 
    return "AES-192 test decryption failed.";
873
 
  
874
 
  return NULL;
875
 
}
876
 
 
877
 
 
878
 
/* Run the self-tests for AES 256.  Returns NULL on success. */
879
 
static const char*
880
 
selftest_basic_256 (void)
881
 
{
882
 
  RIJNDAEL_context ctx;
883
 
  unsigned char scratch[16];       
884
 
 
885
 
  static unsigned char plaintext_256[16] = 
886
 
    {
887
 
      0x06,0x9A,0x00,0x7F,0xC7,0x6A,0x45,0x9F,
888
 
      0x98,0xBA,0xF9,0x17,0xFE,0xDF,0x95,0x21
889
 
    };
890
 
  static unsigned char key_256[32] = 
891
 
    {
892
 
      0x08,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,
893
 
      0x12,0x13,0x14,0x15,0x17,0x18,0x19,0x1A,
894
 
      0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x24,
895
 
      0x26,0x27,0x28,0x29,0x2B,0x2C,0x2D,0x2E
896
 
    };
897
 
  static const unsigned char ciphertext_256[16] = 
898
 
    {
899
 
      0x08,0x0E,0x95,0x17,0xEB,0x16,0x77,0x71,
900
 
      0x9A,0xCF,0x72,0x80,0x86,0x04,0x0A,0xE3
901
 
    };
902
 
 
903
 
  rijndael_setkey (&ctx, key_256, sizeof(key_256));
904
 
  rijndael_encrypt (&ctx, scratch, plaintext_256);
905
 
  if (memcmp (scratch, ciphertext_256, sizeof (ciphertext_256)))
906
 
    return "AES-256 test encryption failed.";
907
 
  rijndael_decrypt (&ctx, scratch, scratch);
908
 
  if (memcmp (scratch, plaintext_256, sizeof (plaintext_256)))
909
 
    return "AES-256 test decryption failed.";
910
 
    
911
 
  return NULL;
912
 
}
913
 
 
914
 
/* Run all the self-tests and return NULL on success.  This function
915
 
   is used for the on-the-fly self-tests. */
916
 
static const char *
917
 
selftest (void)
918
 
{
919
 
  const char *r;
920
 
 
921
 
  if ( (r = selftest_basic_128 ())
922
 
       || (r = selftest_basic_192 ())
923
 
       || (r = selftest_basic_256 ()) )
924
 
    return r;
925
 
 
926
 
  return r;
927
 
}
928
 
 
929
 
 
930
 
/* SP800-38a.pdf for AES-128.  */
931
 
static const char *
932
 
selftest_fips_128_38a (int requested_mode)
933
 
{
934
 
  struct tv
935
 
  {
936
 
    int mode;
937
 
    const unsigned char key[16];
938
 
    const unsigned char iv[16];
939
 
    struct 
940
 
    {
941
 
      const unsigned char input[16];
942
 
      const unsigned char output[16];
943
 
    } data[4];
944
 
  } tv[2] =
945
 
    {
946
 
      {
947
 
        GCRY_CIPHER_MODE_CFB,  /* F.3.13, CFB128-AES128 */
948
 
        { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
949
 
          0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
950
 
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
951
 
          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
952
 
        {
953
 
          { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
954
 
              0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
955
 
            { 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20,
956
 
              0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a } },
957
 
          
958
 
          { { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
959
 
              0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
960
 
            { 0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f,
961
 
              0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b } },
962
 
          
963
 
          { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 
964
 
              0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
965
 
            { 0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40,
966
 
              0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf } },
967
 
          
968
 
          { { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
969
 
              0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
970
 
            { 0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e,
971
 
              0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6 } }
972
 
        }
973
 
      },
974
 
      {
975
 
        GCRY_CIPHER_MODE_OFB,
976
 
        { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
977
 
          0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
978
 
        { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
979
 
          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
980
 
        {
981
 
          { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
982
 
              0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
983
 
            { 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20,
984
 
              0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a } },
985
 
 
986
 
          { { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
987
 
              0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
988
 
            { 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
989
 
              0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25 } },
990
 
          
991
 
          { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
992
 
              0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
993
 
            { 0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6,
994
 
              0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc } },
995
 
 
996
 
          { { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
997
 
              0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
998
 
            { 0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78,
999
 
              0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e } },
1000
 
        }
1001
 
      }
1002
 
    };
1003
 
  unsigned char scratch[16];
1004
 
  gpg_error_t err;
1005
 
  int tvi, idx;
1006
 
  gcry_cipher_hd_t hdenc = NULL;
1007
 
  gcry_cipher_hd_t hddec = NULL;
1008
 
 
1009
 
#define Fail(a) do {           \
1010
 
    _gcry_cipher_close (hdenc);  \
1011
 
    _gcry_cipher_close (hddec);  \
1012
 
    return a;                    \
1013
 
  } while (0)
1014
 
 
1015
 
  gcry_assert (sizeof tv[0].data[0].input == sizeof scratch);
1016
 
  gcry_assert (sizeof tv[0].data[0].output == sizeof scratch);
1017
 
 
1018
 
  for (tvi=0; tvi < DIM (tv); tvi++)
1019
 
    if (tv[tvi].mode == requested_mode)
1020
 
      break;
1021
 
  if (tvi == DIM (tv))
1022
 
    Fail ("no test data for this mode");
1023
 
 
1024
 
  err = _gcry_cipher_open (&hdenc, GCRY_CIPHER_AES, tv[tvi].mode, 0);
1025
 
  if (err)
1026
 
    Fail ("open");
1027
 
  err = _gcry_cipher_open (&hddec, GCRY_CIPHER_AES, tv[tvi].mode, 0);
1028
 
  if (err)
1029
 
    Fail ("open");
1030
 
  err = _gcry_cipher_setkey (hdenc, tv[tvi].key,  sizeof tv[tvi].key);
1031
 
  if (!err)
1032
 
    err = _gcry_cipher_setkey (hddec, tv[tvi].key, sizeof tv[tvi].key);
1033
 
  if (err)
1034
 
    Fail ("set key");
1035
 
  err = _gcry_cipher_setiv (hdenc, tv[tvi].iv, sizeof tv[tvi].iv);
1036
 
  if (!err)
1037
 
    err = _gcry_cipher_setiv (hddec, tv[tvi].iv, sizeof tv[tvi].iv);
1038
 
  if (err)
1039
 
    Fail ("set IV");
1040
 
  for (idx=0; idx < DIM (tv[tvi].data); idx++)
1041
 
    {
1042
 
      err = _gcry_cipher_encrypt (hdenc, scratch, sizeof scratch,
1043
 
                                  tv[tvi].data[idx].input,
1044
 
                                  sizeof tv[tvi].data[idx].input);
1045
 
      if (err)
1046
 
        Fail ("encrypt command");
1047
 
      if (memcmp (scratch, tv[tvi].data[idx].output, sizeof scratch))
1048
 
        Fail ("encrypt mismatch");
1049
 
      err = _gcry_cipher_decrypt (hddec, scratch, sizeof scratch,
1050
 
                                  tv[tvi].data[idx].output,
1051
 
                                  sizeof tv[tvi].data[idx].output);
1052
 
      if (err)
1053
 
        Fail ("decrypt command");
1054
 
      if (memcmp (scratch, tv[tvi].data[idx].input, sizeof scratch))
1055
 
        Fail ("decrypt mismatch");
1056
 
    }
1057
 
 
1058
 
#undef Fail
1059
 
  _gcry_cipher_close (hdenc);
1060
 
  _gcry_cipher_close (hddec); 
1061
 
  return NULL;
1062
 
}
1063
 
 
1064
 
 
1065
 
/* Complete selftest for AES-128 with all modes and driver code.  */
1066
 
static gpg_err_code_t
1067
 
selftest_fips_128 (int extended, selftest_report_func_t report)
1068
 
{
1069
 
  const char *what;
1070
 
  const char *errtxt;
1071
 
  
1072
 
  what = "low-level";
1073
 
  errtxt = selftest_basic_128 ();
1074
 
  if (errtxt)
1075
 
    goto failed;
1076
 
 
1077
 
  if (extended)
1078
 
    {
1079
 
      what = "cfb";
1080
 
      errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_CFB);
1081
 
      if (errtxt)
1082
 
        goto failed;
1083
 
      
1084
 
      what = "ofb";
1085
 
      errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_OFB);
1086
 
      if (errtxt)
1087
 
        goto failed;
1088
 
    }
1089
 
 
1090
 
  return 0; /* Succeeded. */
1091
 
 
1092
 
 failed:
1093
 
  if (report)
1094
 
    report ("cipher", GCRY_CIPHER_AES128, what, errtxt);
1095
 
  return GPG_ERR_SELFTEST_FAILED;
1096
 
}
1097
 
 
1098
 
/* Complete selftest for AES-192.  */
1099
 
static gpg_err_code_t
1100
 
selftest_fips_192 (int extended, selftest_report_func_t report)
1101
 
{
1102
 
  const char *what;
1103
 
  const char *errtxt;
1104
 
 
1105
 
  (void)extended; /* No extended tests available.  */
1106
 
 
1107
 
  what = "low-level";
1108
 
  errtxt = selftest_basic_192 ();
1109
 
  if (errtxt)
1110
 
    goto failed;
1111
 
 
1112
 
 
1113
 
  return 0; /* Succeeded. */
1114
 
 
1115
 
 failed:
1116
 
  if (report)
1117
 
    report ("cipher", GCRY_CIPHER_AES192, what, errtxt);
1118
 
  return GPG_ERR_SELFTEST_FAILED;
1119
 
}
1120
 
 
1121
 
 
1122
 
/* Complete selftest for AES-256.  */
1123
 
static gpg_err_code_t
1124
 
selftest_fips_256 (int extended, selftest_report_func_t report)
1125
 
{
1126
 
  const char *what;
1127
 
  const char *errtxt;
1128
 
  
1129
 
  (void)extended; /* No extended tests available.  */
1130
 
 
1131
 
  what = "low-level";
1132
 
  errtxt = selftest_basic_256 ();
1133
 
  if (errtxt)
1134
 
    goto failed;
1135
 
 
1136
 
  return 0; /* Succeeded. */
1137
 
 
1138
 
 failed:
1139
 
  if (report)
1140
 
    report ("cipher", GCRY_CIPHER_AES256, what, errtxt);
1141
 
  return GPG_ERR_SELFTEST_FAILED;
1142
 
}
1143
 
 
1144
 
 
1145
 
 
1146
 
/* Run a full self-test for ALGO and return 0 on success.  */
1147
 
static gpg_err_code_t
1148
 
run_selftests (int algo, int extended, selftest_report_func_t report)
1149
 
{
1150
 
  gpg_err_code_t ec;
1151
 
 
1152
 
  switch (algo)
1153
 
    {
1154
 
    case GCRY_CIPHER_AES128:
1155
 
      ec = selftest_fips_128 (extended, report);
1156
 
      break;
1157
 
    case GCRY_CIPHER_AES192:
1158
 
      ec = selftest_fips_192 (extended, report);
1159
 
      break;
1160
 
    case GCRY_CIPHER_AES256:
1161
 
      ec = selftest_fips_256 (extended, report);
1162
 
      break;
1163
 
    default:
1164
 
      ec = GPG_ERR_CIPHER_ALGO;
1165
 
      break;
1166
 
        
1167
 
    }
1168
 
  return ec;
1169
 
}
1170
 
 
1171
 
 
1172
 
 
1173
 
 
1174
 
static const char *rijndael_names[] =
1175
 
  {
1176
 
    "RIJNDAEL",
1177
 
    "AES128",
1178
 
    "AES-128",
1179
 
    NULL
1180
 
  };
1181
 
 
1182
 
static gcry_cipher_oid_spec_t rijndael_oids[] =
1183
 
  {
1184
 
    { "2.16.840.1.101.3.4.1.1", GCRY_CIPHER_MODE_ECB },
1185
 
    { "2.16.840.1.101.3.4.1.2", GCRY_CIPHER_MODE_CBC },
1186
 
    { "2.16.840.1.101.3.4.1.3", GCRY_CIPHER_MODE_OFB },
1187
 
    { "2.16.840.1.101.3.4.1.4", GCRY_CIPHER_MODE_CFB },
1188
 
    { NULL }
1189
 
  };
1190
 
 
1191
 
gcry_cipher_spec_t _gcry_cipher_spec_aes =
1192
 
  {
1193
 
    "AES", rijndael_names, rijndael_oids, 16, 128, sizeof (RIJNDAEL_context),
1194
 
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1195
 
  };
1196
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes = 
1197
 
  {
1198
 
    run_selftests
1199
 
  };
1200
 
 
1201
 
static const char *rijndael192_names[] =
1202
 
  {
1203
 
    "RIJNDAEL192",
1204
 
    "AES-192",
1205
 
    NULL
1206
 
  };
1207
 
 
1208
 
static gcry_cipher_oid_spec_t rijndael192_oids[] =
1209
 
  {
1210
 
    { "2.16.840.1.101.3.4.1.21", GCRY_CIPHER_MODE_ECB },
1211
 
    { "2.16.840.1.101.3.4.1.22", GCRY_CIPHER_MODE_CBC },
1212
 
    { "2.16.840.1.101.3.4.1.23", GCRY_CIPHER_MODE_OFB },
1213
 
    { "2.16.840.1.101.3.4.1.24", GCRY_CIPHER_MODE_CFB },
1214
 
    { NULL }
1215
 
  };
1216
 
 
1217
 
gcry_cipher_spec_t _gcry_cipher_spec_aes192 =
1218
 
  {
1219
 
    "AES192", rijndael192_names, rijndael192_oids, 16, 192, sizeof (RIJNDAEL_context),
1220
 
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1221
 
  };
1222
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes192 = 
1223
 
  {
1224
 
    run_selftests
1225
 
  };
1226
 
 
1227
 
static const char *rijndael256_names[] =
1228
 
  {
1229
 
    "RIJNDAEL256",
1230
 
    "AES-256",
1231
 
    NULL
1232
 
  };
1233
 
 
1234
 
static gcry_cipher_oid_spec_t rijndael256_oids[] =
1235
 
  {
1236
 
    { "2.16.840.1.101.3.4.1.41", GCRY_CIPHER_MODE_ECB },
1237
 
    { "2.16.840.1.101.3.4.1.42", GCRY_CIPHER_MODE_CBC },
1238
 
    { "2.16.840.1.101.3.4.1.43", GCRY_CIPHER_MODE_OFB },
1239
 
    { "2.16.840.1.101.3.4.1.44", GCRY_CIPHER_MODE_CFB },
1240
 
    { NULL }
1241
 
  };
1242
 
 
1243
 
gcry_cipher_spec_t _gcry_cipher_spec_aes256 =
1244
 
  {
1245
 
    "AES256", rijndael256_names, rijndael256_oids, 16, 256,
1246
 
    sizeof (RIJNDAEL_context),
1247
 
    rijndael_setkey, rijndael_encrypt, rijndael_decrypt
1248
 
  };
1249
 
 
1250
 
cipher_extra_spec_t _gcry_cipher_extraspec_aes256 = 
1251
 
  {
1252
 
    run_selftests
1253
 
  };