18c2ecf20Sopenharmony_ci/* gf128mul.c - GF(2^128) multiplication functions
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.
48c2ecf20Sopenharmony_ci * Copyright (c) 2006, Rik Snel <rsnel@cube.dyndns.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Based on Dr Brian Gladman's (GPL'd) work published at
78c2ecf20Sopenharmony_ci * http://gladman.plushost.co.uk/oldsite/cryptography_technology/index.php
88c2ecf20Sopenharmony_ci * See the original copyright notice below.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
118c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free
128c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option)
138c2ecf20Sopenharmony_ci * any later version.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci ---------------------------------------------------------------------------
188c2ecf20Sopenharmony_ci Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.   All rights reserved.
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci LICENSE TERMS
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci The free distribution and use of this software in both source and binary
238c2ecf20Sopenharmony_ci form is allowed (with or without changes) provided that:
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci   1. distributions of this source code include the above copyright
268c2ecf20Sopenharmony_ci      notice, this list of conditions and the following disclaimer;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci   2. distributions in binary form include the above copyright
298c2ecf20Sopenharmony_ci      notice, this list of conditions and the following disclaimer
308c2ecf20Sopenharmony_ci      in the documentation and/or other associated materials;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci   3. the copyright holder's name is not used to endorse products
338c2ecf20Sopenharmony_ci      built using this software without specific written permission.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci ALTERNATIVELY, provided that this notice is retained in full, this product
368c2ecf20Sopenharmony_ci may be distributed under the terms of the GNU General Public License (GPL),
378c2ecf20Sopenharmony_ci in which case the provisions of the GPL apply INSTEAD OF those given above.
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci DISCLAIMER
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci This software is provided 'as is' with no explicit or implied warranties
428c2ecf20Sopenharmony_ci in respect of its properties, including, but not limited to, correctness
438c2ecf20Sopenharmony_ci and/or fitness for purpose.
448c2ecf20Sopenharmony_ci ---------------------------------------------------------------------------
458c2ecf20Sopenharmony_ci Issue 31/01/2006
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci This file provides fast multiplication in GF(2^128) as required by several
488c2ecf20Sopenharmony_ci cryptographic authentication modes
498c2ecf20Sopenharmony_ci*/
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include <crypto/gf128mul.h>
528c2ecf20Sopenharmony_ci#include <linux/kernel.h>
538c2ecf20Sopenharmony_ci#include <linux/module.h>
548c2ecf20Sopenharmony_ci#include <linux/slab.h>
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define gf128mul_dat(q) { \
578c2ecf20Sopenharmony_ci	q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\
588c2ecf20Sopenharmony_ci	q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\
598c2ecf20Sopenharmony_ci	q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\
608c2ecf20Sopenharmony_ci	q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\
618c2ecf20Sopenharmony_ci	q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\
628c2ecf20Sopenharmony_ci	q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\
638c2ecf20Sopenharmony_ci	q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\
648c2ecf20Sopenharmony_ci	q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\
658c2ecf20Sopenharmony_ci	q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\
668c2ecf20Sopenharmony_ci	q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\
678c2ecf20Sopenharmony_ci	q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\
688c2ecf20Sopenharmony_ci	q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\
698c2ecf20Sopenharmony_ci	q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\
708c2ecf20Sopenharmony_ci	q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\
718c2ecf20Sopenharmony_ci	q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\
728c2ecf20Sopenharmony_ci	q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\
738c2ecf20Sopenharmony_ci	q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\
748c2ecf20Sopenharmony_ci	q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\
758c2ecf20Sopenharmony_ci	q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\
768c2ecf20Sopenharmony_ci	q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\
778c2ecf20Sopenharmony_ci	q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\
788c2ecf20Sopenharmony_ci	q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\
798c2ecf20Sopenharmony_ci	q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\
808c2ecf20Sopenharmony_ci	q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\
818c2ecf20Sopenharmony_ci	q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\
828c2ecf20Sopenharmony_ci	q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\
838c2ecf20Sopenharmony_ci	q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\
848c2ecf20Sopenharmony_ci	q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\
858c2ecf20Sopenharmony_ci	q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\
868c2ecf20Sopenharmony_ci	q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\
878c2ecf20Sopenharmony_ci	q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\
888c2ecf20Sopenharmony_ci	q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*
928c2ecf20Sopenharmony_ci * Given a value i in 0..255 as the byte overflow when a field element
938c2ecf20Sopenharmony_ci * in GF(2^128) is multiplied by x^8, the following macro returns the
948c2ecf20Sopenharmony_ci * 16-bit value that must be XOR-ed into the low-degree end of the
958c2ecf20Sopenharmony_ci * product to reduce it modulo the polynomial x^128 + x^7 + x^2 + x + 1.
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * There are two versions of the macro, and hence two tables: one for
988c2ecf20Sopenharmony_ci * the "be" convention where the highest-order bit is the coefficient of
998c2ecf20Sopenharmony_ci * the highest-degree polynomial term, and one for the "le" convention
1008c2ecf20Sopenharmony_ci * where the highest-order bit is the coefficient of the lowest-degree
1018c2ecf20Sopenharmony_ci * polynomial term.  In both cases the values are stored in CPU byte
1028c2ecf20Sopenharmony_ci * endianness such that the coefficients are ordered consistently across
1038c2ecf20Sopenharmony_ci * bytes, i.e. in the "be" table bits 15..0 of the stored value
1048c2ecf20Sopenharmony_ci * correspond to the coefficients of x^15..x^0, and in the "le" table
1058c2ecf20Sopenharmony_ci * bits 15..0 correspond to the coefficients of x^0..x^15.
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * Therefore, provided that the appropriate byte endianness conversions
1088c2ecf20Sopenharmony_ci * are done by the multiplication functions (and these must be in place
1098c2ecf20Sopenharmony_ci * anyway to support both little endian and big endian CPUs), the "be"
1108c2ecf20Sopenharmony_ci * table can be used for multiplications of both "bbe" and "ble"
1118c2ecf20Sopenharmony_ci * elements, and the "le" table can be used for multiplications of both
1128c2ecf20Sopenharmony_ci * "lle" and "lbe" elements.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define xda_be(i) ( \
1168c2ecf20Sopenharmony_ci	(i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \
1178c2ecf20Sopenharmony_ci	(i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \
1188c2ecf20Sopenharmony_ci	(i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \
1198c2ecf20Sopenharmony_ci	(i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \
1208c2ecf20Sopenharmony_ci)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define xda_le(i) ( \
1238c2ecf20Sopenharmony_ci	(i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \
1248c2ecf20Sopenharmony_ci	(i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \
1258c2ecf20Sopenharmony_ci	(i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \
1268c2ecf20Sopenharmony_ci	(i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \
1278c2ecf20Sopenharmony_ci)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic const u16 gf128mul_table_le[256] = gf128mul_dat(xda_le);
1308c2ecf20Sopenharmony_cistatic const u16 gf128mul_table_be[256] = gf128mul_dat(xda_be);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci * The following functions multiply a field element by x^8 in
1348c2ecf20Sopenharmony_ci * the polynomial field representation.  They use 64-bit word operations
1358c2ecf20Sopenharmony_ci * to gain speed but compensate for machine endianness and hence work
1368c2ecf20Sopenharmony_ci * correctly on both styles of machine.
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic void gf128mul_x8_lle(be128 *x)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	u64 a = be64_to_cpu(x->a);
1428c2ecf20Sopenharmony_ci	u64 b = be64_to_cpu(x->b);
1438c2ecf20Sopenharmony_ci	u64 _tt = gf128mul_table_le[b & 0xff];
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	x->b = cpu_to_be64((b >> 8) | (a << 56));
1468c2ecf20Sopenharmony_ci	x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void gf128mul_x8_bbe(be128 *x)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	u64 a = be64_to_cpu(x->a);
1528c2ecf20Sopenharmony_ci	u64 b = be64_to_cpu(x->b);
1538c2ecf20Sopenharmony_ci	u64 _tt = gf128mul_table_be[a >> 56];
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	x->a = cpu_to_be64((a << 8) | (b >> 56));
1568c2ecf20Sopenharmony_ci	x->b = cpu_to_be64((b << 8) ^ _tt);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_civoid gf128mul_x8_ble(le128 *r, const le128 *x)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	u64 a = le64_to_cpu(x->a);
1628c2ecf20Sopenharmony_ci	u64 b = le64_to_cpu(x->b);
1638c2ecf20Sopenharmony_ci	u64 _tt = gf128mul_table_be[a >> 56];
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	r->a = cpu_to_le64((a << 8) | (b >> 56));
1668c2ecf20Sopenharmony_ci	r->b = cpu_to_le64((b << 8) ^ _tt);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_x8_ble);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_civoid gf128mul_lle(be128 *r, const be128 *b)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	be128 p[8];
1738c2ecf20Sopenharmony_ci	int i;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	p[0] = *r;
1768c2ecf20Sopenharmony_ci	for (i = 0; i < 7; ++i)
1778c2ecf20Sopenharmony_ci		gf128mul_x_lle(&p[i + 1], &p[i]);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	memset(r, 0, sizeof(*r));
1808c2ecf20Sopenharmony_ci	for (i = 0;;) {
1818c2ecf20Sopenharmony_ci		u8 ch = ((u8 *)b)[15 - i];
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		if (ch & 0x80)
1848c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[0]);
1858c2ecf20Sopenharmony_ci		if (ch & 0x40)
1868c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[1]);
1878c2ecf20Sopenharmony_ci		if (ch & 0x20)
1888c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[2]);
1898c2ecf20Sopenharmony_ci		if (ch & 0x10)
1908c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[3]);
1918c2ecf20Sopenharmony_ci		if (ch & 0x08)
1928c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[4]);
1938c2ecf20Sopenharmony_ci		if (ch & 0x04)
1948c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[5]);
1958c2ecf20Sopenharmony_ci		if (ch & 0x02)
1968c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[6]);
1978c2ecf20Sopenharmony_ci		if (ch & 0x01)
1988c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[7]);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci		if (++i >= 16)
2018c2ecf20Sopenharmony_ci			break;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		gf128mul_x8_lle(r);
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_lle);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_civoid gf128mul_bbe(be128 *r, const be128 *b)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	be128 p[8];
2118c2ecf20Sopenharmony_ci	int i;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	p[0] = *r;
2148c2ecf20Sopenharmony_ci	for (i = 0; i < 7; ++i)
2158c2ecf20Sopenharmony_ci		gf128mul_x_bbe(&p[i + 1], &p[i]);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	memset(r, 0, sizeof(*r));
2188c2ecf20Sopenharmony_ci	for (i = 0;;) {
2198c2ecf20Sopenharmony_ci		u8 ch = ((u8 *)b)[i];
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		if (ch & 0x80)
2228c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[7]);
2238c2ecf20Sopenharmony_ci		if (ch & 0x40)
2248c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[6]);
2258c2ecf20Sopenharmony_ci		if (ch & 0x20)
2268c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[5]);
2278c2ecf20Sopenharmony_ci		if (ch & 0x10)
2288c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[4]);
2298c2ecf20Sopenharmony_ci		if (ch & 0x08)
2308c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[3]);
2318c2ecf20Sopenharmony_ci		if (ch & 0x04)
2328c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[2]);
2338c2ecf20Sopenharmony_ci		if (ch & 0x02)
2348c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[1]);
2358c2ecf20Sopenharmony_ci		if (ch & 0x01)
2368c2ecf20Sopenharmony_ci			be128_xor(r, r, &p[0]);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		if (++i >= 16)
2398c2ecf20Sopenharmony_ci			break;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		gf128mul_x8_bbe(r);
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_bbe);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*      This version uses 64k bytes of table space.
2478c2ecf20Sopenharmony_ci    A 16 byte buffer has to be multiplied by a 16 byte key
2488c2ecf20Sopenharmony_ci    value in GF(2^128).  If we consider a GF(2^128) value in
2498c2ecf20Sopenharmony_ci    the buffer's lowest byte, we can construct a table of
2508c2ecf20Sopenharmony_ci    the 256 16 byte values that result from the 256 values
2518c2ecf20Sopenharmony_ci    of this byte.  This requires 4096 bytes. But we also
2528c2ecf20Sopenharmony_ci    need tables for each of the 16 higher bytes in the
2538c2ecf20Sopenharmony_ci    buffer as well, which makes 64 kbytes in total.
2548c2ecf20Sopenharmony_ci*/
2558c2ecf20Sopenharmony_ci/* additional explanation
2568c2ecf20Sopenharmony_ci * t[0][BYTE] contains g*BYTE
2578c2ecf20Sopenharmony_ci * t[1][BYTE] contains g*x^8*BYTE
2588c2ecf20Sopenharmony_ci *  ..
2598c2ecf20Sopenharmony_ci * t[15][BYTE] contains g*x^120*BYTE */
2608c2ecf20Sopenharmony_cistruct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct gf128mul_64k *t;
2638c2ecf20Sopenharmony_ci	int i, j, k;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	t = kzalloc(sizeof(*t), GFP_KERNEL);
2668c2ecf20Sopenharmony_ci	if (!t)
2678c2ecf20Sopenharmony_ci		goto out;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++) {
2708c2ecf20Sopenharmony_ci		t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL);
2718c2ecf20Sopenharmony_ci		if (!t->t[i]) {
2728c2ecf20Sopenharmony_ci			gf128mul_free_64k(t);
2738c2ecf20Sopenharmony_ci			t = NULL;
2748c2ecf20Sopenharmony_ci			goto out;
2758c2ecf20Sopenharmony_ci		}
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	t->t[0]->t[1] = *g;
2798c2ecf20Sopenharmony_ci	for (j = 1; j <= 64; j <<= 1)
2808c2ecf20Sopenharmony_ci		gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	for (i = 0;;) {
2838c2ecf20Sopenharmony_ci		for (j = 2; j < 256; j += j)
2848c2ecf20Sopenharmony_ci			for (k = 1; k < j; ++k)
2858c2ecf20Sopenharmony_ci				be128_xor(&t->t[i]->t[j + k],
2868c2ecf20Sopenharmony_ci					  &t->t[i]->t[j], &t->t[i]->t[k]);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		if (++i >= 16)
2898c2ecf20Sopenharmony_ci			break;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		for (j = 128; j > 0; j >>= 1) {
2928c2ecf20Sopenharmony_ci			t->t[i]->t[j] = t->t[i - 1]->t[j];
2938c2ecf20Sopenharmony_ci			gf128mul_x8_bbe(&t->t[i]->t[j]);
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciout:
2988c2ecf20Sopenharmony_ci	return t;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_64k_bbe);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_civoid gf128mul_free_64k(struct gf128mul_64k *t)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	int i;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++)
3078c2ecf20Sopenharmony_ci		kfree_sensitive(t->t[i]);
3088c2ecf20Sopenharmony_ci	kfree_sensitive(t);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_free_64k);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_civoid gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	u8 *ap = (u8 *)a;
3158c2ecf20Sopenharmony_ci	be128 r[1];
3168c2ecf20Sopenharmony_ci	int i;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	*r = t->t[0]->t[ap[15]];
3198c2ecf20Sopenharmony_ci	for (i = 1; i < 16; ++i)
3208c2ecf20Sopenharmony_ci		be128_xor(r, r, &t->t[i]->t[ap[15 - i]]);
3218c2ecf20Sopenharmony_ci	*a = *r;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_64k_bbe);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci/*      This version uses 4k bytes of table space.
3268c2ecf20Sopenharmony_ci    A 16 byte buffer has to be multiplied by a 16 byte key
3278c2ecf20Sopenharmony_ci    value in GF(2^128).  If we consider a GF(2^128) value in a
3288c2ecf20Sopenharmony_ci    single byte, we can construct a table of the 256 16 byte
3298c2ecf20Sopenharmony_ci    values that result from the 256 values of this byte.
3308c2ecf20Sopenharmony_ci    This requires 4096 bytes. If we take the highest byte in
3318c2ecf20Sopenharmony_ci    the buffer and use this table to get the result, we then
3328c2ecf20Sopenharmony_ci    have to multiply by x^120 to get the final value. For the
3338c2ecf20Sopenharmony_ci    next highest byte the result has to be multiplied by x^112
3348c2ecf20Sopenharmony_ci    and so on. But we can do this by accumulating the result
3358c2ecf20Sopenharmony_ci    in an accumulator starting with the result for the top
3368c2ecf20Sopenharmony_ci    byte.  We repeatedly multiply the accumulator value by
3378c2ecf20Sopenharmony_ci    x^8 and then add in (i.e. xor) the 16 bytes of the next
3388c2ecf20Sopenharmony_ci    lower byte in the buffer, stopping when we reach the
3398c2ecf20Sopenharmony_ci    lowest byte. This requires a 4096 byte table.
3408c2ecf20Sopenharmony_ci*/
3418c2ecf20Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct gf128mul_4k *t;
3448c2ecf20Sopenharmony_ci	int j, k;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	t = kzalloc(sizeof(*t), GFP_KERNEL);
3478c2ecf20Sopenharmony_ci	if (!t)
3488c2ecf20Sopenharmony_ci		goto out;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	t->t[128] = *g;
3518c2ecf20Sopenharmony_ci	for (j = 64; j > 0; j >>= 1)
3528c2ecf20Sopenharmony_ci		gf128mul_x_lle(&t->t[j], &t->t[j+j]);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	for (j = 2; j < 256; j += j)
3558c2ecf20Sopenharmony_ci		for (k = 1; k < j; ++k)
3568c2ecf20Sopenharmony_ci			be128_xor(&t->t[j + k], &t->t[j], &t->t[k]);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ciout:
3598c2ecf20Sopenharmony_ci	return t;
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_4k_lle);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct gf128mul_4k *t;
3668c2ecf20Sopenharmony_ci	int j, k;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	t = kzalloc(sizeof(*t), GFP_KERNEL);
3698c2ecf20Sopenharmony_ci	if (!t)
3708c2ecf20Sopenharmony_ci		goto out;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	t->t[1] = *g;
3738c2ecf20Sopenharmony_ci	for (j = 1; j <= 64; j <<= 1)
3748c2ecf20Sopenharmony_ci		gf128mul_x_bbe(&t->t[j + j], &t->t[j]);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	for (j = 2; j < 256; j += j)
3778c2ecf20Sopenharmony_ci		for (k = 1; k < j; ++k)
3788c2ecf20Sopenharmony_ci			be128_xor(&t->t[j + k], &t->t[j], &t->t[k]);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ciout:
3818c2ecf20Sopenharmony_ci	return t;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_4k_bbe);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_civoid gf128mul_4k_lle(be128 *a, const struct gf128mul_4k *t)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	u8 *ap = (u8 *)a;
3888c2ecf20Sopenharmony_ci	be128 r[1];
3898c2ecf20Sopenharmony_ci	int i = 15;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	*r = t->t[ap[15]];
3928c2ecf20Sopenharmony_ci	while (i--) {
3938c2ecf20Sopenharmony_ci		gf128mul_x8_lle(r);
3948c2ecf20Sopenharmony_ci		be128_xor(r, r, &t->t[ap[i]]);
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci	*a = *r;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_4k_lle);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_civoid gf128mul_4k_bbe(be128 *a, const struct gf128mul_4k *t)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	u8 *ap = (u8 *)a;
4038c2ecf20Sopenharmony_ci	be128 r[1];
4048c2ecf20Sopenharmony_ci	int i = 0;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	*r = t->t[ap[0]];
4078c2ecf20Sopenharmony_ci	while (++i < 16) {
4088c2ecf20Sopenharmony_ci		gf128mul_x8_bbe(r);
4098c2ecf20Sopenharmony_ci		be128_xor(r, r, &t->t[ap[i]]);
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	*a = *r;
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gf128mul_4k_bbe);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)");
417