Imported Upstream version 2.7.1
[platform/upstream/nettle.git] / 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  */
7
8 /* Generation of fixed multiplication tables. */
9
10 /* nettle, low-level cryptographics library
11  *
12  * Copyright (C) 2011 Niels Möller
13  *
14  * The nettle library is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published by
16  * the Free Software Foundation; either version 2.1 of the License, or (at your
17  * option) any later version.
18  * 
19  * The nettle library is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
22  * License for more details.
23  * 
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with the nettle library; see the file COPYING.LIB.  If not, write to
26  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA 02111-1301, USA.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #define GHASH_POLYNOMIAL 0xE1
34
35
36 /* When x is shifted out over the block edge, add multiples of the
37    defining polynomial to eliminate each bit. */
38 static unsigned
39 reduce(unsigned x)
40 {
41   unsigned p = GHASH_POLYNOMIAL << 1;
42   unsigned y = 0;
43   for (; x; x >>= 1, p <<= 1)
44     if (x & 1)
45       y ^= p;
46   return y;
47 }
48
49 int
50 main(int argc, char **argv)
51 {
52   unsigned i;
53   printf("4-bit table:\n");
54   
55   for (i = 0; i<16; i++)
56     {
57       unsigned x;
58       if (i && !(i%8))
59         printf("\n");
60
61       x = reduce(i << 4);
62       printf("W(%02x,%02x),", x >> 8, x & 0xff);
63     }
64   printf("\n\n");
65   printf("8-bit table:\n");
66   for (i = 0; i<256; i++)
67     {
68       unsigned x;
69       if (i && !(i%8))
70         printf("\n");
71
72       x = reduce(i);
73       printf("W(%02x,%02x),", x >> 8, x & 0xff);
74     }
75   printf("\n");
76   return EXIT_SUCCESS;
77 }