162306a36Sopenharmony_ci/* gf128mul.c - GF(2^128) multiplication functions 262306a36Sopenharmony_ci * 362306a36Sopenharmony_ci * Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. 462306a36Sopenharmony_ci * Copyright (c) 2006, Rik Snel <rsnel@cube.dyndns.org> 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Based on Dr Brian Gladman's (GPL'd) work published at 762306a36Sopenharmony_ci * http://gladman.plushost.co.uk/oldsite/cryptography_technology/index.php 862306a36Sopenharmony_ci * See the original copyright notice below. 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 1162306a36Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free 1262306a36Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option) 1362306a36Sopenharmony_ci * any later version. 1462306a36Sopenharmony_ci */ 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/* 1762306a36Sopenharmony_ci --------------------------------------------------------------------------- 1862306a36Sopenharmony_ci Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved. 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci LICENSE TERMS 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci The free distribution and use of this software in both source and binary 2362306a36Sopenharmony_ci form is allowed (with or without changes) provided that: 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci 1. distributions of this source code include the above copyright 2662306a36Sopenharmony_ci notice, this list of conditions and the following disclaimer; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci 2. distributions in binary form include the above copyright 2962306a36Sopenharmony_ci notice, this list of conditions and the following disclaimer 3062306a36Sopenharmony_ci in the documentation and/or other associated materials; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci 3. the copyright holder's name is not used to endorse products 3362306a36Sopenharmony_ci built using this software without specific written permission. 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci ALTERNATIVELY, provided that this notice is retained in full, this product 3662306a36Sopenharmony_ci may be distributed under the terms of the GNU General Public License (GPL), 3762306a36Sopenharmony_ci in which case the provisions of the GPL apply INSTEAD OF those given above. 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci DISCLAIMER 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci This software is provided 'as is' with no explicit or implied warranties 4262306a36Sopenharmony_ci in respect of its properties, including, but not limited to, correctness 4362306a36Sopenharmony_ci and/or fitness for purpose. 4462306a36Sopenharmony_ci --------------------------------------------------------------------------- 4562306a36Sopenharmony_ci Issue 31/01/2006 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci This file provides fast multiplication in GF(2^128) as required by several 4862306a36Sopenharmony_ci cryptographic authentication modes 4962306a36Sopenharmony_ci*/ 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci#include <crypto/gf128mul.h> 5262306a36Sopenharmony_ci#include <linux/kernel.h> 5362306a36Sopenharmony_ci#include <linux/module.h> 5462306a36Sopenharmony_ci#include <linux/slab.h> 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci#define gf128mul_dat(q) { \ 5762306a36Sopenharmony_ci q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\ 5862306a36Sopenharmony_ci q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\ 5962306a36Sopenharmony_ci q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\ 6062306a36Sopenharmony_ci q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\ 6162306a36Sopenharmony_ci q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\ 6262306a36Sopenharmony_ci q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\ 6362306a36Sopenharmony_ci q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\ 6462306a36Sopenharmony_ci q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\ 6562306a36Sopenharmony_ci q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\ 6662306a36Sopenharmony_ci q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\ 6762306a36Sopenharmony_ci q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\ 6862306a36Sopenharmony_ci q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\ 6962306a36Sopenharmony_ci q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\ 7062306a36Sopenharmony_ci q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\ 7162306a36Sopenharmony_ci q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\ 7262306a36Sopenharmony_ci q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\ 7362306a36Sopenharmony_ci q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\ 7462306a36Sopenharmony_ci q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\ 7562306a36Sopenharmony_ci q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\ 7662306a36Sopenharmony_ci q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\ 7762306a36Sopenharmony_ci q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\ 7862306a36Sopenharmony_ci q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\ 7962306a36Sopenharmony_ci q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\ 8062306a36Sopenharmony_ci q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\ 8162306a36Sopenharmony_ci q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\ 8262306a36Sopenharmony_ci q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\ 8362306a36Sopenharmony_ci q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\ 8462306a36Sopenharmony_ci q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\ 8562306a36Sopenharmony_ci q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\ 8662306a36Sopenharmony_ci q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\ 8762306a36Sopenharmony_ci q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\ 8862306a36Sopenharmony_ci q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \ 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* 9262306a36Sopenharmony_ci * Given a value i in 0..255 as the byte overflow when a field element 9362306a36Sopenharmony_ci * in GF(2^128) is multiplied by x^8, the following macro returns the 9462306a36Sopenharmony_ci * 16-bit value that must be XOR-ed into the low-degree end of the 9562306a36Sopenharmony_ci * product to reduce it modulo the polynomial x^128 + x^7 + x^2 + x + 1. 9662306a36Sopenharmony_ci * 9762306a36Sopenharmony_ci * There are two versions of the macro, and hence two tables: one for 9862306a36Sopenharmony_ci * the "be" convention where the highest-order bit is the coefficient of 9962306a36Sopenharmony_ci * the highest-degree polynomial term, and one for the "le" convention 10062306a36Sopenharmony_ci * where the highest-order bit is the coefficient of the lowest-degree 10162306a36Sopenharmony_ci * polynomial term. In both cases the values are stored in CPU byte 10262306a36Sopenharmony_ci * endianness such that the coefficients are ordered consistently across 10362306a36Sopenharmony_ci * bytes, i.e. in the "be" table bits 15..0 of the stored value 10462306a36Sopenharmony_ci * correspond to the coefficients of x^15..x^0, and in the "le" table 10562306a36Sopenharmony_ci * bits 15..0 correspond to the coefficients of x^0..x^15. 10662306a36Sopenharmony_ci * 10762306a36Sopenharmony_ci * Therefore, provided that the appropriate byte endianness conversions 10862306a36Sopenharmony_ci * are done by the multiplication functions (and these must be in place 10962306a36Sopenharmony_ci * anyway to support both little endian and big endian CPUs), the "be" 11062306a36Sopenharmony_ci * table can be used for multiplications of both "bbe" and "ble" 11162306a36Sopenharmony_ci * elements, and the "le" table can be used for multiplications of both 11262306a36Sopenharmony_ci * "lle" and "lbe" elements. 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#define xda_be(i) ( \ 11662306a36Sopenharmony_ci (i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \ 11762306a36Sopenharmony_ci (i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \ 11862306a36Sopenharmony_ci (i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \ 11962306a36Sopenharmony_ci (i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \ 12062306a36Sopenharmony_ci) 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci#define xda_le(i) ( \ 12362306a36Sopenharmony_ci (i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \ 12462306a36Sopenharmony_ci (i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \ 12562306a36Sopenharmony_ci (i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \ 12662306a36Sopenharmony_ci (i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \ 12762306a36Sopenharmony_ci) 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic const u16 gf128mul_table_le[256] = gf128mul_dat(xda_le); 13062306a36Sopenharmony_cistatic const u16 gf128mul_table_be[256] = gf128mul_dat(xda_be); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci/* 13362306a36Sopenharmony_ci * The following functions multiply a field element by x^8 in 13462306a36Sopenharmony_ci * the polynomial field representation. They use 64-bit word operations 13562306a36Sopenharmony_ci * to gain speed but compensate for machine endianness and hence work 13662306a36Sopenharmony_ci * correctly on both styles of machine. 13762306a36Sopenharmony_ci */ 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cistatic void gf128mul_x8_lle(be128 *x) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci u64 a = be64_to_cpu(x->a); 14262306a36Sopenharmony_ci u64 b = be64_to_cpu(x->b); 14362306a36Sopenharmony_ci u64 _tt = gf128mul_table_le[b & 0xff]; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci x->b = cpu_to_be64((b >> 8) | (a << 56)); 14662306a36Sopenharmony_ci x->a = cpu_to_be64((a >> 8) ^ (_tt << 48)); 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* time invariant version of gf128mul_x8_lle */ 15062306a36Sopenharmony_cistatic void gf128mul_x8_lle_ti(be128 *x) 15162306a36Sopenharmony_ci{ 15262306a36Sopenharmony_ci u64 a = be64_to_cpu(x->a); 15362306a36Sopenharmony_ci u64 b = be64_to_cpu(x->b); 15462306a36Sopenharmony_ci u64 _tt = xda_le(b & 0xff); /* avoid table lookup */ 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci x->b = cpu_to_be64((b >> 8) | (a << 56)); 15762306a36Sopenharmony_ci x->a = cpu_to_be64((a >> 8) ^ (_tt << 48)); 15862306a36Sopenharmony_ci} 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_cistatic void gf128mul_x8_bbe(be128 *x) 16162306a36Sopenharmony_ci{ 16262306a36Sopenharmony_ci u64 a = be64_to_cpu(x->a); 16362306a36Sopenharmony_ci u64 b = be64_to_cpu(x->b); 16462306a36Sopenharmony_ci u64 _tt = gf128mul_table_be[a >> 56]; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci x->a = cpu_to_be64((a << 8) | (b >> 56)); 16762306a36Sopenharmony_ci x->b = cpu_to_be64((b << 8) ^ _tt); 16862306a36Sopenharmony_ci} 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_civoid gf128mul_x8_ble(le128 *r, const le128 *x) 17162306a36Sopenharmony_ci{ 17262306a36Sopenharmony_ci u64 a = le64_to_cpu(x->a); 17362306a36Sopenharmony_ci u64 b = le64_to_cpu(x->b); 17462306a36Sopenharmony_ci u64 _tt = gf128mul_table_be[a >> 56]; 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci r->a = cpu_to_le64((a << 8) | (b >> 56)); 17762306a36Sopenharmony_ci r->b = cpu_to_le64((b << 8) ^ _tt); 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_x8_ble); 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_civoid gf128mul_lle(be128 *r, const be128 *b) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci /* 18462306a36Sopenharmony_ci * The p array should be aligned to twice the size of its element type, 18562306a36Sopenharmony_ci * so that every even/odd pair is guaranteed to share a cacheline 18662306a36Sopenharmony_ci * (assuming a cacheline size of 32 bytes or more, which is by far the 18762306a36Sopenharmony_ci * most common). This ensures that each be128_xor() call in the loop 18862306a36Sopenharmony_ci * takes the same amount of time regardless of the value of 'ch', which 18962306a36Sopenharmony_ci * is derived from function parameter 'b', which is commonly used as a 19062306a36Sopenharmony_ci * key, e.g., for GHASH. The odd array elements are all set to zero, 19162306a36Sopenharmony_ci * making each be128_xor() a NOP if its associated bit in 'ch' is not 19262306a36Sopenharmony_ci * set, and this is equivalent to calling be128_xor() conditionally. 19362306a36Sopenharmony_ci * This approach aims to avoid leaking information about such keys 19462306a36Sopenharmony_ci * through execution time variances. 19562306a36Sopenharmony_ci * 19662306a36Sopenharmony_ci * Unfortunately, __aligned(16) or higher does not work on x86 for 19762306a36Sopenharmony_ci * variables on the stack so we need to perform the alignment by hand. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ci be128 array[16 + 3] = {}; 20062306a36Sopenharmony_ci be128 *p = PTR_ALIGN(&array[0], 2 * sizeof(be128)); 20162306a36Sopenharmony_ci int i; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci p[0] = *r; 20462306a36Sopenharmony_ci for (i = 0; i < 7; ++i) 20562306a36Sopenharmony_ci gf128mul_x_lle(&p[2 * i + 2], &p[2 * i]); 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci memset(r, 0, sizeof(*r)); 20862306a36Sopenharmony_ci for (i = 0;;) { 20962306a36Sopenharmony_ci u8 ch = ((u8 *)b)[15 - i]; 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci be128_xor(r, r, &p[ 0 + !(ch & 0x80)]); 21262306a36Sopenharmony_ci be128_xor(r, r, &p[ 2 + !(ch & 0x40)]); 21362306a36Sopenharmony_ci be128_xor(r, r, &p[ 4 + !(ch & 0x20)]); 21462306a36Sopenharmony_ci be128_xor(r, r, &p[ 6 + !(ch & 0x10)]); 21562306a36Sopenharmony_ci be128_xor(r, r, &p[ 8 + !(ch & 0x08)]); 21662306a36Sopenharmony_ci be128_xor(r, r, &p[10 + !(ch & 0x04)]); 21762306a36Sopenharmony_ci be128_xor(r, r, &p[12 + !(ch & 0x02)]); 21862306a36Sopenharmony_ci be128_xor(r, r, &p[14 + !(ch & 0x01)]); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci if (++i >= 16) 22162306a36Sopenharmony_ci break; 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci gf128mul_x8_lle_ti(r); /* use the time invariant version */ 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci} 22662306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_lle); 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_civoid gf128mul_bbe(be128 *r, const be128 *b) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci be128 p[8]; 23162306a36Sopenharmony_ci int i; 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci p[0] = *r; 23462306a36Sopenharmony_ci for (i = 0; i < 7; ++i) 23562306a36Sopenharmony_ci gf128mul_x_bbe(&p[i + 1], &p[i]); 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci memset(r, 0, sizeof(*r)); 23862306a36Sopenharmony_ci for (i = 0;;) { 23962306a36Sopenharmony_ci u8 ch = ((u8 *)b)[i]; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci if (ch & 0x80) 24262306a36Sopenharmony_ci be128_xor(r, r, &p[7]); 24362306a36Sopenharmony_ci if (ch & 0x40) 24462306a36Sopenharmony_ci be128_xor(r, r, &p[6]); 24562306a36Sopenharmony_ci if (ch & 0x20) 24662306a36Sopenharmony_ci be128_xor(r, r, &p[5]); 24762306a36Sopenharmony_ci if (ch & 0x10) 24862306a36Sopenharmony_ci be128_xor(r, r, &p[4]); 24962306a36Sopenharmony_ci if (ch & 0x08) 25062306a36Sopenharmony_ci be128_xor(r, r, &p[3]); 25162306a36Sopenharmony_ci if (ch & 0x04) 25262306a36Sopenharmony_ci be128_xor(r, r, &p[2]); 25362306a36Sopenharmony_ci if (ch & 0x02) 25462306a36Sopenharmony_ci be128_xor(r, r, &p[1]); 25562306a36Sopenharmony_ci if (ch & 0x01) 25662306a36Sopenharmony_ci be128_xor(r, r, &p[0]); 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci if (++i >= 16) 25962306a36Sopenharmony_ci break; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci gf128mul_x8_bbe(r); 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci} 26462306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_bbe); 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci/* This version uses 64k bytes of table space. 26762306a36Sopenharmony_ci A 16 byte buffer has to be multiplied by a 16 byte key 26862306a36Sopenharmony_ci value in GF(2^128). If we consider a GF(2^128) value in 26962306a36Sopenharmony_ci the buffer's lowest byte, we can construct a table of 27062306a36Sopenharmony_ci the 256 16 byte values that result from the 256 values 27162306a36Sopenharmony_ci of this byte. This requires 4096 bytes. But we also 27262306a36Sopenharmony_ci need tables for each of the 16 higher bytes in the 27362306a36Sopenharmony_ci buffer as well, which makes 64 kbytes in total. 27462306a36Sopenharmony_ci*/ 27562306a36Sopenharmony_ci/* additional explanation 27662306a36Sopenharmony_ci * t[0][BYTE] contains g*BYTE 27762306a36Sopenharmony_ci * t[1][BYTE] contains g*x^8*BYTE 27862306a36Sopenharmony_ci * .. 27962306a36Sopenharmony_ci * t[15][BYTE] contains g*x^120*BYTE */ 28062306a36Sopenharmony_cistruct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci struct gf128mul_64k *t; 28362306a36Sopenharmony_ci int i, j, k; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci t = kzalloc(sizeof(*t), GFP_KERNEL); 28662306a36Sopenharmony_ci if (!t) 28762306a36Sopenharmony_ci goto out; 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci for (i = 0; i < 16; i++) { 29062306a36Sopenharmony_ci t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL); 29162306a36Sopenharmony_ci if (!t->t[i]) { 29262306a36Sopenharmony_ci gf128mul_free_64k(t); 29362306a36Sopenharmony_ci t = NULL; 29462306a36Sopenharmony_ci goto out; 29562306a36Sopenharmony_ci } 29662306a36Sopenharmony_ci } 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci t->t[0]->t[1] = *g; 29962306a36Sopenharmony_ci for (j = 1; j <= 64; j <<= 1) 30062306a36Sopenharmony_ci gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]); 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci for (i = 0;;) { 30362306a36Sopenharmony_ci for (j = 2; j < 256; j += j) 30462306a36Sopenharmony_ci for (k = 1; k < j; ++k) 30562306a36Sopenharmony_ci be128_xor(&t->t[i]->t[j + k], 30662306a36Sopenharmony_ci &t->t[i]->t[j], &t->t[i]->t[k]); 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci if (++i >= 16) 30962306a36Sopenharmony_ci break; 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci for (j = 128; j > 0; j >>= 1) { 31262306a36Sopenharmony_ci t->t[i]->t[j] = t->t[i - 1]->t[j]; 31362306a36Sopenharmony_ci gf128mul_x8_bbe(&t->t[i]->t[j]); 31462306a36Sopenharmony_ci } 31562306a36Sopenharmony_ci } 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ciout: 31862306a36Sopenharmony_ci return t; 31962306a36Sopenharmony_ci} 32062306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_64k_bbe); 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_civoid gf128mul_free_64k(struct gf128mul_64k *t) 32362306a36Sopenharmony_ci{ 32462306a36Sopenharmony_ci int i; 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci for (i = 0; i < 16; i++) 32762306a36Sopenharmony_ci kfree_sensitive(t->t[i]); 32862306a36Sopenharmony_ci kfree_sensitive(t); 32962306a36Sopenharmony_ci} 33062306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_free_64k); 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_civoid gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t) 33362306a36Sopenharmony_ci{ 33462306a36Sopenharmony_ci u8 *ap = (u8 *)a; 33562306a36Sopenharmony_ci be128 r[1]; 33662306a36Sopenharmony_ci int i; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci *r = t->t[0]->t[ap[15]]; 33962306a36Sopenharmony_ci for (i = 1; i < 16; ++i) 34062306a36Sopenharmony_ci be128_xor(r, r, &t->t[i]->t[ap[15 - i]]); 34162306a36Sopenharmony_ci *a = *r; 34262306a36Sopenharmony_ci} 34362306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_64k_bbe); 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci/* This version uses 4k bytes of table space. 34662306a36Sopenharmony_ci A 16 byte buffer has to be multiplied by a 16 byte key 34762306a36Sopenharmony_ci value in GF(2^128). If we consider a GF(2^128) value in a 34862306a36Sopenharmony_ci single byte, we can construct a table of the 256 16 byte 34962306a36Sopenharmony_ci values that result from the 256 values of this byte. 35062306a36Sopenharmony_ci This requires 4096 bytes. If we take the highest byte in 35162306a36Sopenharmony_ci the buffer and use this table to get the result, we then 35262306a36Sopenharmony_ci have to multiply by x^120 to get the final value. For the 35362306a36Sopenharmony_ci next highest byte the result has to be multiplied by x^112 35462306a36Sopenharmony_ci and so on. But we can do this by accumulating the result 35562306a36Sopenharmony_ci in an accumulator starting with the result for the top 35662306a36Sopenharmony_ci byte. We repeatedly multiply the accumulator value by 35762306a36Sopenharmony_ci x^8 and then add in (i.e. xor) the 16 bytes of the next 35862306a36Sopenharmony_ci lower byte in the buffer, stopping when we reach the 35962306a36Sopenharmony_ci lowest byte. This requires a 4096 byte table. 36062306a36Sopenharmony_ci*/ 36162306a36Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g) 36262306a36Sopenharmony_ci{ 36362306a36Sopenharmony_ci struct gf128mul_4k *t; 36462306a36Sopenharmony_ci int j, k; 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci t = kzalloc(sizeof(*t), GFP_KERNEL); 36762306a36Sopenharmony_ci if (!t) 36862306a36Sopenharmony_ci goto out; 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci t->t[128] = *g; 37162306a36Sopenharmony_ci for (j = 64; j > 0; j >>= 1) 37262306a36Sopenharmony_ci gf128mul_x_lle(&t->t[j], &t->t[j+j]); 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci for (j = 2; j < 256; j += j) 37562306a36Sopenharmony_ci for (k = 1; k < j; ++k) 37662306a36Sopenharmony_ci be128_xor(&t->t[j + k], &t->t[j], &t->t[k]); 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ciout: 37962306a36Sopenharmony_ci return t; 38062306a36Sopenharmony_ci} 38162306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_4k_lle); 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g) 38462306a36Sopenharmony_ci{ 38562306a36Sopenharmony_ci struct gf128mul_4k *t; 38662306a36Sopenharmony_ci int j, k; 38762306a36Sopenharmony_ci 38862306a36Sopenharmony_ci t = kzalloc(sizeof(*t), GFP_KERNEL); 38962306a36Sopenharmony_ci if (!t) 39062306a36Sopenharmony_ci goto out; 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci t->t[1] = *g; 39362306a36Sopenharmony_ci for (j = 1; j <= 64; j <<= 1) 39462306a36Sopenharmony_ci gf128mul_x_bbe(&t->t[j + j], &t->t[j]); 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci for (j = 2; j < 256; j += j) 39762306a36Sopenharmony_ci for (k = 1; k < j; ++k) 39862306a36Sopenharmony_ci be128_xor(&t->t[j + k], &t->t[j], &t->t[k]); 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_ciout: 40162306a36Sopenharmony_ci return t; 40262306a36Sopenharmony_ci} 40362306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_init_4k_bbe); 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_civoid gf128mul_4k_lle(be128 *a, const struct gf128mul_4k *t) 40662306a36Sopenharmony_ci{ 40762306a36Sopenharmony_ci u8 *ap = (u8 *)a; 40862306a36Sopenharmony_ci be128 r[1]; 40962306a36Sopenharmony_ci int i = 15; 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci *r = t->t[ap[15]]; 41262306a36Sopenharmony_ci while (i--) { 41362306a36Sopenharmony_ci gf128mul_x8_lle(r); 41462306a36Sopenharmony_ci be128_xor(r, r, &t->t[ap[i]]); 41562306a36Sopenharmony_ci } 41662306a36Sopenharmony_ci *a = *r; 41762306a36Sopenharmony_ci} 41862306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_4k_lle); 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_civoid gf128mul_4k_bbe(be128 *a, const struct gf128mul_4k *t) 42162306a36Sopenharmony_ci{ 42262306a36Sopenharmony_ci u8 *ap = (u8 *)a; 42362306a36Sopenharmony_ci be128 r[1]; 42462306a36Sopenharmony_ci int i = 0; 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci *r = t->t[ap[0]]; 42762306a36Sopenharmony_ci while (++i < 16) { 42862306a36Sopenharmony_ci gf128mul_x8_bbe(r); 42962306a36Sopenharmony_ci be128_xor(r, r, &t->t[ap[i]]); 43062306a36Sopenharmony_ci } 43162306a36Sopenharmony_ci *a = *r; 43262306a36Sopenharmony_ci} 43362306a36Sopenharmony_ciEXPORT_SYMBOL(gf128mul_4k_bbe); 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 43662306a36Sopenharmony_ciMODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)"); 437