cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
gf128mul.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The redistribution and use of this software (with or without changes)
8  is allowed without the payment of fees or royalties provided that:
9 
10  1. source code distributions include the above copyright notice, this
11  list of conditions and the following disclaimer;
12 
13  2. binary distributions include the above copyright notice, this list
14  of conditions and the following disclaimer in their documentation;
15 
16  3. the name of the copyright holder is not used to endorse products
17  built using this software without specific written permission.
18 
19  DISCLAIMER
20 
21  This software is provided 'as is' with no explicit or implied warranties
22  in respect of its properties, including, but not limited to, correctness
23  and/or fitness for purpose.
24  ---------------------------------------------------------------------------
25  Issue Date: 07/10/2010
26 
27  An implementation of field multiplication in the Galois Field GF(2^128)
28 
29  A polynomial representation is used for the field with the coefficients
30  held in bit sequences in which the bit numbers are the powers of x that
31  a bit represents. The field polynomial used is (x^128+x^7+x^2+x+1).
32 
33  The obvious way of representing field elements in a computer system is
34  to map 'x' in the field to the binary integer '2'. But this was way
35  too obvious for cryptographers!
36 
37  Here bytes are numbered in their memory order and bits within bytes are
38  numbered according to their integer numeric significance (that is as is
39  now normal with bit 0 representing unity) . The term 'little endian'
40  will then used to describe mappings where numeric (power of 2) or field
41  (power of x) significance increases with increasing bit or byte numbers
42  with 'big endian' being used to describe the inverse situation.
43 
44  The GF bit sequence can then be mapped onto 8-bit bytes in computer
45  memory in one of four simple ways:
46 
47  A mapping in which x maps to the integer 2 in little endian
48  form for both bytes and bits within bytes:
49 
50  LL: bit for x^n ==> bit for 2^(n % 8) in byte[n / 8]
51 
52  A mapping in which x maps to the integer 2 in big endian form
53  for both bytes and bits within bytes:
54 
55  BL: bit for x^n ==> bit for 2^(n % 8) in byte[15 - n / 8]
56 
57  A little endian mapping for bytes but with the bits within
58  bytes in reverse order (big endian bytes):
59 
60  LB: bit for x^n ==> bit for 2^(7 - n % 8) in byte[n / 8]
61 
62  A big endian mapping for bytes but with the bits within
63  bytes in reverse order (big endian bytes):
64 
65  BB: bit for x^n ==> bit for 2^(7 - n % 8) in byte[15 - n / 8]
66 
67  128-bit field elements are represented by 16 byte buffers but for
68  processing efficiency reasons it is often desirable to process arrays of
69  bytes using longer types such as, for example, unsigned long values.
70  The type used for representing these buffers will be called a 'gf_unit'
71  and the buffer itself will be referred to as a 'gf_t' type.
72 
73  THe field multiplier is based on the assumption that one of the two
74  field elements involved in multiplication will change only relatively
75  infrequently, making it worthwhile to precompute tables to speed up
76  multiplication by this value.
77 */
78 
79 #ifndef _GF128MUL_H
80 #define _GF128MUL_H
81 
82 #include <stdlib.h>
83 #include <string.h>
84 
85 #if defined( INC_ALL ) /* pcg */
86  #include "brg_endian.h"
87 #else
88  #include "crypt/brg_endian.h"
89 #endif /* Compiler-specific includes */
90 
91 /* The processing unit used within 16 byte buffers. This can
92  provide significant advantages if acess to larger types
93  is available without causing memory access exceptions
94 */
95 
96 #if !defined( UNIT_BITS )
97 # if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
98 # define UNIT_BITS 8
99 # elif defined( _WIN64 )
100 # define UNIT_BITS 64
101 # else
102 # define UNIT_BITS 32
103 # endif
104 #endif
105 
106 #if UNIT_BITS == 64 && !defined( NEED_UINT_64T )
107 # define NEED_UINT_64T
108 #endif
109 
110 #if defined( INC_ALL ) /* pcg */
111  #include "brg_types.h"
112 #else
113  #include "crypt/brg_types.h"
114 #endif /* Compiler-specific includes */
115 
116 /* Choose the Galois Field representation to use (see above) */
117 #if 0
118 # define GF_MODE_LL
119 #elif 0
120 # define GF_MODE_BL
121 #elif 1
122 # define GF_MODE_LB /* the representation used by GCM */
123 #elif 0
124 # define GF_MODE_BB
125 #else
126 # error mode is not defined
127 #endif
128 
129 /* Table sizes for GF(128) Multiply. Normally larger tables give
130  higher speed but cache loading might change this. Normally only
131  one table size (or none at all) will be specified here
132 */
133 #if 0
134 # define TABLES_64K
135 #endif
136 #if 0
137 # define TABLES_8K
138 #endif
139 #if 1
140 # define TABLES_4K
141 #endif
142 #if 0
143 # define TABLES_256
144 #endif
145 
146 #if !(defined( TABLES_64K ) || defined( TABLES_8K ) ||defined( TABLES_4K ) ||defined( TABLES_256 ))
147 # define NO_TABLES
148 #endif
149 
150 #if defined(__cplusplus)
151 extern "C"
152 {
153 #endif
154 
155 #define GF_BYTE_LEN 16
156 #define GF_UNIT_LEN (GF_BYTE_LEN / (UNIT_BITS >> 3))
157 
158 UNIT_TYPEDEF(gf_unit_t, UNIT_BITS);
160 
161 /* Code for conversion between the four different galois field representations
162  is optionally available using gf_convert.c
163 */
164 
165 typedef enum { REVERSE_NONE = 0, REVERSE_BITS = 1, REVERSE_BYTES = 2 } transform;
166 
167 void convert_representation(gf_t dest, const gf_t source, transform rev);
168 
169 void gf_mul(gf_t a, const gf_t b); /* slow field multiply */
170 
171 /* types and calls for 64k table driven field multiplier */
172 
173 typedef gf_t gf_t64k_a[16][256];
174 typedef gf_t (*gf_t64k_t)[256];
175 
176 void init_64k_table(const gf_t g, gf_t64k_t t);
177 void gf_mul_64k(gf_t a, const gf_t64k_t t, void *r);
178 
179 /* types and calls for 8k table driven field multiplier */
180 
181 typedef gf_t gf_t8k_a[32][16];
182 typedef gf_t (*gf_t8k_t)[16];
183 
184 void init_8k_table(const gf_t g, gf_t8k_t t);
185 void gf_mul_8k(gf_t a, const gf_t8k_t t, gf_t r);
186 
187 /* types and calls for 8k table driven field multiplier */
188 
189 typedef gf_t gf_t4k_a[256];
190 typedef gf_t (*gf_t4k_t);
191 
192 void init_4k_table(const gf_t g, gf_t4k_t t);
193 void gf_mul_4k(gf_t a, const gf_t4k_t t, gf_t r);
194 
195 /* types and calls for 8k table driven field multiplier */
196 
197 typedef gf_t gf_t256_a[16];
198 typedef gf_t (*gf_t256_t);
199 
200 void init_256_table(const gf_t g, gf_t256_t t);
201 void gf_mul_256(gf_t a, const gf_t256_t t, gf_t r);
202 
203 #if defined(__cplusplus)
204 }
205 #endif
206 
207 #endif