Skip to content

Commit f33fd64

Browse files
ebiggersherbertx
authored andcommitted
crypto: gf128mul - rename the byte overflow tables
Though the GF(2^128) byte overflow tables were named the "lle" and "bbe" tables, they are not actually tied to these element formats specifically, but rather to particular a "bit endianness". For example, the bbe table is actually used for both bbe and ble multiplication. Therefore, rename the tables to "le" and "be" and update the comment to explain this. Cc: Alex Cope <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 2416e4f commit f33fd64

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

crypto/gf128mul.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,46 @@
8888
q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \
8989
}
9090

91-
/* Given the value i in 0..255 as the byte overflow when a field element
92-
in GHASH is multiplied by x^8, this function will return the values that
93-
are generated in the lo 16-bit word of the field value by applying the
94-
modular polynomial. The values lo_byte and hi_byte are returned via the
95-
macro xp_fun(lo_byte, hi_byte) so that the values can be assembled into
96-
memory as required by a suitable definition of this macro operating on
97-
the table above
98-
*/
91+
/*
92+
* Given a value i in 0..255 as the byte overflow when a field element
93+
* in GF(2^128) is multiplied by x^8, the following macro returns the
94+
* 16-bit value that must be XOR-ed into the low-degree end of the
95+
* product to reduce it modulo the polynomial x^128 + x^7 + x^2 + x + 1.
96+
*
97+
* There are two versions of the macro, and hence two tables: one for
98+
* the "be" convention where the highest-order bit is the coefficient of
99+
* the highest-degree polynomial term, and one for the "le" convention
100+
* where the highest-order bit is the coefficient of the lowest-degree
101+
* polynomial term. In both cases the values are stored in CPU byte
102+
* endianness such that the coefficients are ordered consistently across
103+
* bytes, i.e. in the "be" table bits 15..0 of the stored value
104+
* correspond to the coefficients of x^15..x^0, and in the "le" table
105+
* bits 15..0 correspond to the coefficients of x^0..x^15.
106+
*
107+
* Therefore, provided that the appropriate byte endianness conversions
108+
* are done by the multiplication functions (and these must be in place
109+
* anyway to support both little endian and big endian CPUs), the "be"
110+
* table can be used for multiplications of both "bbe" and "ble"
111+
* elements, and the "le" table can be used for multiplications of both
112+
* "lle" and "lbe" elements.
113+
*/
99114

100-
#define xda_bbe(i) ( \
115+
#define xda_be(i) ( \
101116
(i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \
102117
(i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \
103118
(i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \
104119
(i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \
105120
)
106121

107-
#define xda_lle(i) ( \
122+
#define xda_le(i) ( \
108123
(i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \
109124
(i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \
110125
(i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \
111126
(i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \
112127
)
113128

114-
static const u16 gf128mul_table_lle[256] = gf128mul_dat(xda_lle);
115-
static const u16 gf128mul_table_bbe[256] = gf128mul_dat(xda_bbe);
129+
static const u16 gf128mul_table_le[256] = gf128mul_dat(xda_le);
130+
static const u16 gf128mul_table_be[256] = gf128mul_dat(xda_be);
116131

117132
/*
118133
* The following functions multiply a field element by x or by x^8 in
@@ -125,7 +140,7 @@ static void gf128mul_x_lle(be128 *r, const be128 *x)
125140
{
126141
u64 a = be64_to_cpu(x->a);
127142
u64 b = be64_to_cpu(x->b);
128-
u64 _tt = gf128mul_table_lle[(b << 7) & 0xff];
143+
u64 _tt = gf128mul_table_le[(b << 7) & 0xff];
129144

130145
r->b = cpu_to_be64((b >> 1) | (a << 63));
131146
r->a = cpu_to_be64((a >> 1) ^ (_tt << 48));
@@ -135,7 +150,7 @@ static void gf128mul_x_bbe(be128 *r, const be128 *x)
135150
{
136151
u64 a = be64_to_cpu(x->a);
137152
u64 b = be64_to_cpu(x->b);
138-
u64 _tt = gf128mul_table_bbe[a >> 63];
153+
u64 _tt = gf128mul_table_be[a >> 63];
139154

140155
r->a = cpu_to_be64((a << 1) | (b >> 63));
141156
r->b = cpu_to_be64((b << 1) ^ _tt);
@@ -145,7 +160,7 @@ void gf128mul_x_ble(be128 *r, const be128 *x)
145160
{
146161
u64 a = le64_to_cpu(x->a);
147162
u64 b = le64_to_cpu(x->b);
148-
u64 _tt = gf128mul_table_bbe[b >> 63];
163+
u64 _tt = gf128mul_table_be[b >> 63];
149164

150165
r->a = cpu_to_le64((a << 1) ^ _tt);
151166
r->b = cpu_to_le64((b << 1) | (a >> 63));
@@ -156,7 +171,7 @@ static void gf128mul_x8_lle(be128 *x)
156171
{
157172
u64 a = be64_to_cpu(x->a);
158173
u64 b = be64_to_cpu(x->b);
159-
u64 _tt = gf128mul_table_lle[b & 0xff];
174+
u64 _tt = gf128mul_table_le[b & 0xff];
160175

161176
x->b = cpu_to_be64((b >> 8) | (a << 56));
162177
x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));
@@ -166,7 +181,7 @@ static void gf128mul_x8_bbe(be128 *x)
166181
{
167182
u64 a = be64_to_cpu(x->a);
168183
u64 b = be64_to_cpu(x->b);
169-
u64 _tt = gf128mul_table_bbe[a >> 56];
184+
u64 _tt = gf128mul_table_be[a >> 56];
170185

171186
x->a = cpu_to_be64((a << 8) | (b >> 56));
172187
x->b = cpu_to_be64((b << 8) ^ _tt);

0 commit comments

Comments
 (0)