Fix accidental use of C99 for loop.
[gd/nettle] / gcmdata.c
1 /* gcmdata.c
2
3    Galois counter mode, specified by NIST,
4    http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
5
6    Generation of fixed multiplication tables.
7
8    Copyright (C) 2011 Niels Möller
9
10    This file is part of GNU Nettle.
11
12    GNU Nettle is free software: you can redistribute it and/or
13    modify it under the terms of either:
14
15      * the GNU Lesser General Public License as published by the Free
16        Software Foundation; either version 3 of the License, or (at your
17        option) any later version.
18
19    or
20
21      * the GNU General Public License as published by the Free
22        Software Foundation; either version 2 of the License, or (at your
23        option) any later version.
24
25    or both in parallel, as here.
26
27    GNU Nettle is distributed in the hope that it will be useful,
28    but WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30    General Public License for more details.
31
32    You should have received copies of the GNU General Public License and
33    the GNU Lesser General Public License along with this program.  If
34    not, see http://www.gnu.org/licenses/.
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #define GHASH_POLYNOMIAL 0xE1
41
42
43 /* When x is shifted out over the block edge, add multiples of the
44    defining polynomial to eliminate each bit. */
45 static unsigned
46 reduce(unsigned x)
47 {
48   unsigned p = GHASH_POLYNOMIAL << 1;
49   unsigned y = 0;
50   for (; x; x >>= 1, p <<= 1)
51     if (x & 1)
52       y ^= p;
53   return y;
54 }
55
56 int
57 main(int argc, char **argv)
58 {
59   unsigned i;
60   printf("4-bit table:\n");
61   
62   for (i = 0; i<16; i++)
63     {
64       unsigned x;
65       if (i && !(i%8))
66         printf("\n");
67
68       x = reduce(i << 4);
69       printf("W(%02x,%02x),", x >> 8, x & 0xff);
70     }
71   printf("\n\n");
72   printf("8-bit table:\n");
73   for (i = 0; i<256; i++)
74     {
75       unsigned x;
76       if (i && !(i%8))
77         printf("\n");
78
79       x = reduce(i);
80       printf("W(%02x,%02x),", x >> 8, x & 0xff);
81     }
82   printf("\n");
83   return EXIT_SUCCESS;
84 }