Imported Upstream version 2.4
[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., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #define GHASH_POLYNOMIAL 0xE1
37
38
39 /* When x is shifted out over the block edge, add multiples of the
40    defining polynomial to eliminate each bit. */
41 static unsigned
42 reduce(unsigned x)
43 {
44   unsigned p = GHASH_POLYNOMIAL << 1;
45   unsigned y = 0;
46   for (; x; x >>= 1, p <<= 1)
47     if (x & 1)
48       y ^= p;
49   return y;
50 }
51
52 int
53 main(int argc UNUSED, char **argv UNUSED)
54 {
55   unsigned i;
56   printf("4-bit table:\n");
57   
58   for (i = 0; i<16; i++)
59     {
60       unsigned x;
61       if (i && !(i%8))
62         printf("\n");
63
64       x = reduce(i << 4);
65       printf("W(%02x,%02x),", x >> 8, x & 0xff);
66     }
67   printf("\n\n");
68   printf("8-bit table:\n");
69   for (i = 0; i<256; i++)
70     {
71       unsigned x;
72       if (i && !(i%8))
73         printf("\n");
74
75       x = reduce(i);
76       printf("W(%02x,%02x),", x >> 8, x & 0xff);
77     }
78   printf("\n");
79   return EXIT_SUCCESS;
80 }