1*61c581a4SArd Biesheuvel /* gf128mul.c - GF(2^128) multiplication functions 2*61c581a4SArd Biesheuvel * 3*61c581a4SArd Biesheuvel * Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. 4*61c581a4SArd Biesheuvel * Copyright (c) 2006, Rik Snel <rsnel@cube.dyndns.org> 5*61c581a4SArd Biesheuvel * 6*61c581a4SArd Biesheuvel * Based on Dr Brian Gladman's (GPL'd) work published at 7*61c581a4SArd Biesheuvel * http://gladman.plushost.co.uk/oldsite/cryptography_technology/index.php 8*61c581a4SArd Biesheuvel * See the original copyright notice below. 9*61c581a4SArd Biesheuvel * 10*61c581a4SArd Biesheuvel * This program is free software; you can redistribute it and/or modify it 11*61c581a4SArd Biesheuvel * under the terms of the GNU General Public License as published by the Free 12*61c581a4SArd Biesheuvel * Software Foundation; either version 2 of the License, or (at your option) 13*61c581a4SArd Biesheuvel * any later version. 14*61c581a4SArd Biesheuvel */ 15*61c581a4SArd Biesheuvel 16*61c581a4SArd Biesheuvel /* 17*61c581a4SArd Biesheuvel --------------------------------------------------------------------------- 18*61c581a4SArd Biesheuvel Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved. 19*61c581a4SArd Biesheuvel 20*61c581a4SArd Biesheuvel LICENSE TERMS 21*61c581a4SArd Biesheuvel 22*61c581a4SArd Biesheuvel The free distribution and use of this software in both source and binary 23*61c581a4SArd Biesheuvel form is allowed (with or without changes) provided that: 24*61c581a4SArd Biesheuvel 25*61c581a4SArd Biesheuvel 1. distributions of this source code include the above copyright 26*61c581a4SArd Biesheuvel notice, this list of conditions and the following disclaimer; 27*61c581a4SArd Biesheuvel 28*61c581a4SArd Biesheuvel 2. distributions in binary form include the above copyright 29*61c581a4SArd Biesheuvel notice, this list of conditions and the following disclaimer 30*61c581a4SArd Biesheuvel in the documentation and/or other associated materials; 31*61c581a4SArd Biesheuvel 32*61c581a4SArd Biesheuvel 3. the copyright holder's name is not used to endorse products 33*61c581a4SArd Biesheuvel built using this software without specific written permission. 34*61c581a4SArd Biesheuvel 35*61c581a4SArd Biesheuvel ALTERNATIVELY, provided that this notice is retained in full, this product 36*61c581a4SArd Biesheuvel may be distributed under the terms of the GNU General Public License (GPL), 37*61c581a4SArd Biesheuvel in which case the provisions of the GPL apply INSTEAD OF those given above. 38*61c581a4SArd Biesheuvel 39*61c581a4SArd Biesheuvel DISCLAIMER 40*61c581a4SArd Biesheuvel 41*61c581a4SArd Biesheuvel This software is provided 'as is' with no explicit or implied warranties 42*61c581a4SArd Biesheuvel in respect of its properties, including, but not limited to, correctness 43*61c581a4SArd Biesheuvel and/or fitness for purpose. 44*61c581a4SArd Biesheuvel --------------------------------------------------------------------------- 45*61c581a4SArd Biesheuvel Issue 31/01/2006 46*61c581a4SArd Biesheuvel 47*61c581a4SArd Biesheuvel This file provides fast multiplication in GF(2^128) as required by several 48*61c581a4SArd Biesheuvel cryptographic authentication modes 49*61c581a4SArd Biesheuvel */ 50*61c581a4SArd Biesheuvel 51*61c581a4SArd Biesheuvel #include <crypto/gf128mul.h> 52*61c581a4SArd Biesheuvel #include <linux/kernel.h> 53*61c581a4SArd Biesheuvel #include <linux/module.h> 54*61c581a4SArd Biesheuvel #include <linux/slab.h> 55*61c581a4SArd Biesheuvel 56*61c581a4SArd Biesheuvel #define gf128mul_dat(q) { \ 57*61c581a4SArd Biesheuvel q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\ 58*61c581a4SArd Biesheuvel q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\ 59*61c581a4SArd Biesheuvel q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\ 60*61c581a4SArd Biesheuvel q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\ 61*61c581a4SArd Biesheuvel q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\ 62*61c581a4SArd Biesheuvel q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\ 63*61c581a4SArd Biesheuvel q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\ 64*61c581a4SArd Biesheuvel q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\ 65*61c581a4SArd Biesheuvel q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\ 66*61c581a4SArd Biesheuvel q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\ 67*61c581a4SArd Biesheuvel q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\ 68*61c581a4SArd Biesheuvel q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\ 69*61c581a4SArd Biesheuvel q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\ 70*61c581a4SArd Biesheuvel q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\ 71*61c581a4SArd Biesheuvel q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\ 72*61c581a4SArd Biesheuvel q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\ 73*61c581a4SArd Biesheuvel q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\ 74*61c581a4SArd Biesheuvel q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\ 75*61c581a4SArd Biesheuvel q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\ 76*61c581a4SArd Biesheuvel q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\ 77*61c581a4SArd Biesheuvel q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\ 78*61c581a4SArd Biesheuvel q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\ 79*61c581a4SArd Biesheuvel q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\ 80*61c581a4SArd Biesheuvel q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\ 81*61c581a4SArd Biesheuvel q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\ 82*61c581a4SArd Biesheuvel q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\ 83*61c581a4SArd Biesheuvel q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\ 84*61c581a4SArd Biesheuvel q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\ 85*61c581a4SArd Biesheuvel q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\ 86*61c581a4SArd Biesheuvel q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\ 87*61c581a4SArd Biesheuvel q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\ 88*61c581a4SArd Biesheuvel q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \ 89*61c581a4SArd Biesheuvel } 90*61c581a4SArd Biesheuvel 91*61c581a4SArd Biesheuvel /* 92*61c581a4SArd Biesheuvel * Given a value i in 0..255 as the byte overflow when a field element 93*61c581a4SArd Biesheuvel * in GF(2^128) is multiplied by x^8, the following macro returns the 94*61c581a4SArd Biesheuvel * 16-bit value that must be XOR-ed into the low-degree end of the 95*61c581a4SArd Biesheuvel * product to reduce it modulo the polynomial x^128 + x^7 + x^2 + x + 1. 96*61c581a4SArd Biesheuvel * 97*61c581a4SArd Biesheuvel * There are two versions of the macro, and hence two tables: one for 98*61c581a4SArd Biesheuvel * the "be" convention where the highest-order bit is the coefficient of 99*61c581a4SArd Biesheuvel * the highest-degree polynomial term, and one for the "le" convention 100*61c581a4SArd Biesheuvel * where the highest-order bit is the coefficient of the lowest-degree 101*61c581a4SArd Biesheuvel * polynomial term. In both cases the values are stored in CPU byte 102*61c581a4SArd Biesheuvel * endianness such that the coefficients are ordered consistently across 103*61c581a4SArd Biesheuvel * bytes, i.e. in the "be" table bits 15..0 of the stored value 104*61c581a4SArd Biesheuvel * correspond to the coefficients of x^15..x^0, and in the "le" table 105*61c581a4SArd Biesheuvel * bits 15..0 correspond to the coefficients of x^0..x^15. 106*61c581a4SArd Biesheuvel * 107*61c581a4SArd Biesheuvel * Therefore, provided that the appropriate byte endianness conversions 108*61c581a4SArd Biesheuvel * are done by the multiplication functions (and these must be in place 109*61c581a4SArd Biesheuvel * anyway to support both little endian and big endian CPUs), the "be" 110*61c581a4SArd Biesheuvel * table can be used for multiplications of both "bbe" and "ble" 111*61c581a4SArd Biesheuvel * elements, and the "le" table can be used for multiplications of both 112*61c581a4SArd Biesheuvel * "lle" and "lbe" elements. 113*61c581a4SArd Biesheuvel */ 114*61c581a4SArd Biesheuvel 115*61c581a4SArd Biesheuvel #define xda_be(i) ( \ 116*61c581a4SArd Biesheuvel (i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \ 117*61c581a4SArd Biesheuvel (i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \ 118*61c581a4SArd Biesheuvel (i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \ 119*61c581a4SArd Biesheuvel (i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \ 120*61c581a4SArd Biesheuvel ) 121*61c581a4SArd Biesheuvel 122*61c581a4SArd Biesheuvel #define xda_le(i) ( \ 123*61c581a4SArd Biesheuvel (i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \ 124*61c581a4SArd Biesheuvel (i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \ 125*61c581a4SArd Biesheuvel (i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \ 126*61c581a4SArd Biesheuvel (i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \ 127*61c581a4SArd Biesheuvel ) 128*61c581a4SArd Biesheuvel 129*61c581a4SArd Biesheuvel static const u16 gf128mul_table_le[256] = gf128mul_dat(xda_le); 130*61c581a4SArd Biesheuvel static const u16 gf128mul_table_be[256] = gf128mul_dat(xda_be); 131*61c581a4SArd Biesheuvel 132*61c581a4SArd Biesheuvel /* 133*61c581a4SArd Biesheuvel * The following functions multiply a field element by x^8 in 134*61c581a4SArd Biesheuvel * the polynomial field representation. They use 64-bit word operations 135*61c581a4SArd Biesheuvel * to gain speed but compensate for machine endianness and hence work 136*61c581a4SArd Biesheuvel * correctly on both styles of machine. 137*61c581a4SArd Biesheuvel */ 138*61c581a4SArd Biesheuvel 139*61c581a4SArd Biesheuvel static void gf128mul_x8_lle(be128 *x) 140*61c581a4SArd Biesheuvel { 141*61c581a4SArd Biesheuvel u64 a = be64_to_cpu(x->a); 142*61c581a4SArd Biesheuvel u64 b = be64_to_cpu(x->b); 143*61c581a4SArd Biesheuvel u64 _tt = gf128mul_table_le[b & 0xff]; 144*61c581a4SArd Biesheuvel 145*61c581a4SArd Biesheuvel x->b = cpu_to_be64((b >> 8) | (a << 56)); 146*61c581a4SArd Biesheuvel x->a = cpu_to_be64((a >> 8) ^ (_tt << 48)); 147*61c581a4SArd Biesheuvel } 148*61c581a4SArd Biesheuvel 149*61c581a4SArd Biesheuvel static void gf128mul_x8_bbe(be128 *x) 150*61c581a4SArd Biesheuvel { 151*61c581a4SArd Biesheuvel u64 a = be64_to_cpu(x->a); 152*61c581a4SArd Biesheuvel u64 b = be64_to_cpu(x->b); 153*61c581a4SArd Biesheuvel u64 _tt = gf128mul_table_be[a >> 56]; 154*61c581a4SArd Biesheuvel 155*61c581a4SArd Biesheuvel x->a = cpu_to_be64((a << 8) | (b >> 56)); 156*61c581a4SArd Biesheuvel x->b = cpu_to_be64((b << 8) ^ _tt); 157*61c581a4SArd Biesheuvel } 158*61c581a4SArd Biesheuvel 159*61c581a4SArd Biesheuvel void gf128mul_x8_ble(le128 *r, const le128 *x) 160*61c581a4SArd Biesheuvel { 161*61c581a4SArd Biesheuvel u64 a = le64_to_cpu(x->a); 162*61c581a4SArd Biesheuvel u64 b = le64_to_cpu(x->b); 163*61c581a4SArd Biesheuvel u64 _tt = gf128mul_table_be[a >> 56]; 164*61c581a4SArd Biesheuvel 165*61c581a4SArd Biesheuvel r->a = cpu_to_le64((a << 8) | (b >> 56)); 166*61c581a4SArd Biesheuvel r->b = cpu_to_le64((b << 8) ^ _tt); 167*61c581a4SArd Biesheuvel } 168*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_x8_ble); 169*61c581a4SArd Biesheuvel 170*61c581a4SArd Biesheuvel void gf128mul_lle(be128 *r, const be128 *b) 171*61c581a4SArd Biesheuvel { 172*61c581a4SArd Biesheuvel be128 p[8]; 173*61c581a4SArd Biesheuvel int i; 174*61c581a4SArd Biesheuvel 175*61c581a4SArd Biesheuvel p[0] = *r; 176*61c581a4SArd Biesheuvel for (i = 0; i < 7; ++i) 177*61c581a4SArd Biesheuvel gf128mul_x_lle(&p[i + 1], &p[i]); 178*61c581a4SArd Biesheuvel 179*61c581a4SArd Biesheuvel memset(r, 0, sizeof(*r)); 180*61c581a4SArd Biesheuvel for (i = 0;;) { 181*61c581a4SArd Biesheuvel u8 ch = ((u8 *)b)[15 - i]; 182*61c581a4SArd Biesheuvel 183*61c581a4SArd Biesheuvel if (ch & 0x80) 184*61c581a4SArd Biesheuvel be128_xor(r, r, &p[0]); 185*61c581a4SArd Biesheuvel if (ch & 0x40) 186*61c581a4SArd Biesheuvel be128_xor(r, r, &p[1]); 187*61c581a4SArd Biesheuvel if (ch & 0x20) 188*61c581a4SArd Biesheuvel be128_xor(r, r, &p[2]); 189*61c581a4SArd Biesheuvel if (ch & 0x10) 190*61c581a4SArd Biesheuvel be128_xor(r, r, &p[3]); 191*61c581a4SArd Biesheuvel if (ch & 0x08) 192*61c581a4SArd Biesheuvel be128_xor(r, r, &p[4]); 193*61c581a4SArd Biesheuvel if (ch & 0x04) 194*61c581a4SArd Biesheuvel be128_xor(r, r, &p[5]); 195*61c581a4SArd Biesheuvel if (ch & 0x02) 196*61c581a4SArd Biesheuvel be128_xor(r, r, &p[6]); 197*61c581a4SArd Biesheuvel if (ch & 0x01) 198*61c581a4SArd Biesheuvel be128_xor(r, r, &p[7]); 199*61c581a4SArd Biesheuvel 200*61c581a4SArd Biesheuvel if (++i >= 16) 201*61c581a4SArd Biesheuvel break; 202*61c581a4SArd Biesheuvel 203*61c581a4SArd Biesheuvel gf128mul_x8_lle(r); 204*61c581a4SArd Biesheuvel } 205*61c581a4SArd Biesheuvel } 206*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_lle); 207*61c581a4SArd Biesheuvel 208*61c581a4SArd Biesheuvel void gf128mul_bbe(be128 *r, const be128 *b) 209*61c581a4SArd Biesheuvel { 210*61c581a4SArd Biesheuvel be128 p[8]; 211*61c581a4SArd Biesheuvel int i; 212*61c581a4SArd Biesheuvel 213*61c581a4SArd Biesheuvel p[0] = *r; 214*61c581a4SArd Biesheuvel for (i = 0; i < 7; ++i) 215*61c581a4SArd Biesheuvel gf128mul_x_bbe(&p[i + 1], &p[i]); 216*61c581a4SArd Biesheuvel 217*61c581a4SArd Biesheuvel memset(r, 0, sizeof(*r)); 218*61c581a4SArd Biesheuvel for (i = 0;;) { 219*61c581a4SArd Biesheuvel u8 ch = ((u8 *)b)[i]; 220*61c581a4SArd Biesheuvel 221*61c581a4SArd Biesheuvel if (ch & 0x80) 222*61c581a4SArd Biesheuvel be128_xor(r, r, &p[7]); 223*61c581a4SArd Biesheuvel if (ch & 0x40) 224*61c581a4SArd Biesheuvel be128_xor(r, r, &p[6]); 225*61c581a4SArd Biesheuvel if (ch & 0x20) 226*61c581a4SArd Biesheuvel be128_xor(r, r, &p[5]); 227*61c581a4SArd Biesheuvel if (ch & 0x10) 228*61c581a4SArd Biesheuvel be128_xor(r, r, &p[4]); 229*61c581a4SArd Biesheuvel if (ch & 0x08) 230*61c581a4SArd Biesheuvel be128_xor(r, r, &p[3]); 231*61c581a4SArd Biesheuvel if (ch & 0x04) 232*61c581a4SArd Biesheuvel be128_xor(r, r, &p[2]); 233*61c581a4SArd Biesheuvel if (ch & 0x02) 234*61c581a4SArd Biesheuvel be128_xor(r, r, &p[1]); 235*61c581a4SArd Biesheuvel if (ch & 0x01) 236*61c581a4SArd Biesheuvel be128_xor(r, r, &p[0]); 237*61c581a4SArd Biesheuvel 238*61c581a4SArd Biesheuvel if (++i >= 16) 239*61c581a4SArd Biesheuvel break; 240*61c581a4SArd Biesheuvel 241*61c581a4SArd Biesheuvel gf128mul_x8_bbe(r); 242*61c581a4SArd Biesheuvel } 243*61c581a4SArd Biesheuvel } 244*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_bbe); 245*61c581a4SArd Biesheuvel 246*61c581a4SArd Biesheuvel /* This version uses 64k bytes of table space. 247*61c581a4SArd Biesheuvel A 16 byte buffer has to be multiplied by a 16 byte key 248*61c581a4SArd Biesheuvel value in GF(2^128). If we consider a GF(2^128) value in 249*61c581a4SArd Biesheuvel the buffer's lowest byte, we can construct a table of 250*61c581a4SArd Biesheuvel the 256 16 byte values that result from the 256 values 251*61c581a4SArd Biesheuvel of this byte. This requires 4096 bytes. But we also 252*61c581a4SArd Biesheuvel need tables for each of the 16 higher bytes in the 253*61c581a4SArd Biesheuvel buffer as well, which makes 64 kbytes in total. 254*61c581a4SArd Biesheuvel */ 255*61c581a4SArd Biesheuvel /* additional explanation 256*61c581a4SArd Biesheuvel * t[0][BYTE] contains g*BYTE 257*61c581a4SArd Biesheuvel * t[1][BYTE] contains g*x^8*BYTE 258*61c581a4SArd Biesheuvel * .. 259*61c581a4SArd Biesheuvel * t[15][BYTE] contains g*x^120*BYTE */ 260*61c581a4SArd Biesheuvel struct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g) 261*61c581a4SArd Biesheuvel { 262*61c581a4SArd Biesheuvel struct gf128mul_64k *t; 263*61c581a4SArd Biesheuvel int i, j, k; 264*61c581a4SArd Biesheuvel 265*61c581a4SArd Biesheuvel t = kzalloc(sizeof(*t), GFP_KERNEL); 266*61c581a4SArd Biesheuvel if (!t) 267*61c581a4SArd Biesheuvel goto out; 268*61c581a4SArd Biesheuvel 269*61c581a4SArd Biesheuvel for (i = 0; i < 16; i++) { 270*61c581a4SArd Biesheuvel t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL); 271*61c581a4SArd Biesheuvel if (!t->t[i]) { 272*61c581a4SArd Biesheuvel gf128mul_free_64k(t); 273*61c581a4SArd Biesheuvel t = NULL; 274*61c581a4SArd Biesheuvel goto out; 275*61c581a4SArd Biesheuvel } 276*61c581a4SArd Biesheuvel } 277*61c581a4SArd Biesheuvel 278*61c581a4SArd Biesheuvel t->t[0]->t[1] = *g; 279*61c581a4SArd Biesheuvel for (j = 1; j <= 64; j <<= 1) 280*61c581a4SArd Biesheuvel gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]); 281*61c581a4SArd Biesheuvel 282*61c581a4SArd Biesheuvel for (i = 0;;) { 283*61c581a4SArd Biesheuvel for (j = 2; j < 256; j += j) 284*61c581a4SArd Biesheuvel for (k = 1; k < j; ++k) 285*61c581a4SArd Biesheuvel be128_xor(&t->t[i]->t[j + k], 286*61c581a4SArd Biesheuvel &t->t[i]->t[j], &t->t[i]->t[k]); 287*61c581a4SArd Biesheuvel 288*61c581a4SArd Biesheuvel if (++i >= 16) 289*61c581a4SArd Biesheuvel break; 290*61c581a4SArd Biesheuvel 291*61c581a4SArd Biesheuvel for (j = 128; j > 0; j >>= 1) { 292*61c581a4SArd Biesheuvel t->t[i]->t[j] = t->t[i - 1]->t[j]; 293*61c581a4SArd Biesheuvel gf128mul_x8_bbe(&t->t[i]->t[j]); 294*61c581a4SArd Biesheuvel } 295*61c581a4SArd Biesheuvel } 296*61c581a4SArd Biesheuvel 297*61c581a4SArd Biesheuvel out: 298*61c581a4SArd Biesheuvel return t; 299*61c581a4SArd Biesheuvel } 300*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_init_64k_bbe); 301*61c581a4SArd Biesheuvel 302*61c581a4SArd Biesheuvel void gf128mul_free_64k(struct gf128mul_64k *t) 303*61c581a4SArd Biesheuvel { 304*61c581a4SArd Biesheuvel int i; 305*61c581a4SArd Biesheuvel 306*61c581a4SArd Biesheuvel for (i = 0; i < 16; i++) 307*61c581a4SArd Biesheuvel kfree_sensitive(t->t[i]); 308*61c581a4SArd Biesheuvel kfree_sensitive(t); 309*61c581a4SArd Biesheuvel } 310*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_free_64k); 311*61c581a4SArd Biesheuvel 312*61c581a4SArd Biesheuvel void gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t) 313*61c581a4SArd Biesheuvel { 314*61c581a4SArd Biesheuvel u8 *ap = (u8 *)a; 315*61c581a4SArd Biesheuvel be128 r[1]; 316*61c581a4SArd Biesheuvel int i; 317*61c581a4SArd Biesheuvel 318*61c581a4SArd Biesheuvel *r = t->t[0]->t[ap[15]]; 319*61c581a4SArd Biesheuvel for (i = 1; i < 16; ++i) 320*61c581a4SArd Biesheuvel be128_xor(r, r, &t->t[i]->t[ap[15 - i]]); 321*61c581a4SArd Biesheuvel *a = *r; 322*61c581a4SArd Biesheuvel } 323*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_64k_bbe); 324*61c581a4SArd Biesheuvel 325*61c581a4SArd Biesheuvel /* This version uses 4k bytes of table space. 326*61c581a4SArd Biesheuvel A 16 byte buffer has to be multiplied by a 16 byte key 327*61c581a4SArd Biesheuvel value in GF(2^128). If we consider a GF(2^128) value in a 328*61c581a4SArd Biesheuvel single byte, we can construct a table of the 256 16 byte 329*61c581a4SArd Biesheuvel values that result from the 256 values of this byte. 330*61c581a4SArd Biesheuvel This requires 4096 bytes. If we take the highest byte in 331*61c581a4SArd Biesheuvel the buffer and use this table to get the result, we then 332*61c581a4SArd Biesheuvel have to multiply by x^120 to get the final value. For the 333*61c581a4SArd Biesheuvel next highest byte the result has to be multiplied by x^112 334*61c581a4SArd Biesheuvel and so on. But we can do this by accumulating the result 335*61c581a4SArd Biesheuvel in an accumulator starting with the result for the top 336*61c581a4SArd Biesheuvel byte. We repeatedly multiply the accumulator value by 337*61c581a4SArd Biesheuvel x^8 and then add in (i.e. xor) the 16 bytes of the next 338*61c581a4SArd Biesheuvel lower byte in the buffer, stopping when we reach the 339*61c581a4SArd Biesheuvel lowest byte. This requires a 4096 byte table. 340*61c581a4SArd Biesheuvel */ 341*61c581a4SArd Biesheuvel struct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g) 342*61c581a4SArd Biesheuvel { 343*61c581a4SArd Biesheuvel struct gf128mul_4k *t; 344*61c581a4SArd Biesheuvel int j, k; 345*61c581a4SArd Biesheuvel 346*61c581a4SArd Biesheuvel t = kzalloc(sizeof(*t), GFP_KERNEL); 347*61c581a4SArd Biesheuvel if (!t) 348*61c581a4SArd Biesheuvel goto out; 349*61c581a4SArd Biesheuvel 350*61c581a4SArd Biesheuvel t->t[128] = *g; 351*61c581a4SArd Biesheuvel for (j = 64; j > 0; j >>= 1) 352*61c581a4SArd Biesheuvel gf128mul_x_lle(&t->t[j], &t->t[j+j]); 353*61c581a4SArd Biesheuvel 354*61c581a4SArd Biesheuvel for (j = 2; j < 256; j += j) 355*61c581a4SArd Biesheuvel for (k = 1; k < j; ++k) 356*61c581a4SArd Biesheuvel be128_xor(&t->t[j + k], &t->t[j], &t->t[k]); 357*61c581a4SArd Biesheuvel 358*61c581a4SArd Biesheuvel out: 359*61c581a4SArd Biesheuvel return t; 360*61c581a4SArd Biesheuvel } 361*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_init_4k_lle); 362*61c581a4SArd Biesheuvel 363*61c581a4SArd Biesheuvel struct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g) 364*61c581a4SArd Biesheuvel { 365*61c581a4SArd Biesheuvel struct gf128mul_4k *t; 366*61c581a4SArd Biesheuvel int j, k; 367*61c581a4SArd Biesheuvel 368*61c581a4SArd Biesheuvel t = kzalloc(sizeof(*t), GFP_KERNEL); 369*61c581a4SArd Biesheuvel if (!t) 370*61c581a4SArd Biesheuvel goto out; 371*61c581a4SArd Biesheuvel 372*61c581a4SArd Biesheuvel t->t[1] = *g; 373*61c581a4SArd Biesheuvel for (j = 1; j <= 64; j <<= 1) 374*61c581a4SArd Biesheuvel gf128mul_x_bbe(&t->t[j + j], &t->t[j]); 375*61c581a4SArd Biesheuvel 376*61c581a4SArd Biesheuvel for (j = 2; j < 256; j += j) 377*61c581a4SArd Biesheuvel for (k = 1; k < j; ++k) 378*61c581a4SArd Biesheuvel be128_xor(&t->t[j + k], &t->t[j], &t->t[k]); 379*61c581a4SArd Biesheuvel 380*61c581a4SArd Biesheuvel out: 381*61c581a4SArd Biesheuvel return t; 382*61c581a4SArd Biesheuvel } 383*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_init_4k_bbe); 384*61c581a4SArd Biesheuvel 385*61c581a4SArd Biesheuvel void gf128mul_4k_lle(be128 *a, const struct gf128mul_4k *t) 386*61c581a4SArd Biesheuvel { 387*61c581a4SArd Biesheuvel u8 *ap = (u8 *)a; 388*61c581a4SArd Biesheuvel be128 r[1]; 389*61c581a4SArd Biesheuvel int i = 15; 390*61c581a4SArd Biesheuvel 391*61c581a4SArd Biesheuvel *r = t->t[ap[15]]; 392*61c581a4SArd Biesheuvel while (i--) { 393*61c581a4SArd Biesheuvel gf128mul_x8_lle(r); 394*61c581a4SArd Biesheuvel be128_xor(r, r, &t->t[ap[i]]); 395*61c581a4SArd Biesheuvel } 396*61c581a4SArd Biesheuvel *a = *r; 397*61c581a4SArd Biesheuvel } 398*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_4k_lle); 399*61c581a4SArd Biesheuvel 400*61c581a4SArd Biesheuvel void gf128mul_4k_bbe(be128 *a, const struct gf128mul_4k *t) 401*61c581a4SArd Biesheuvel { 402*61c581a4SArd Biesheuvel u8 *ap = (u8 *)a; 403*61c581a4SArd Biesheuvel be128 r[1]; 404*61c581a4SArd Biesheuvel int i = 0; 405*61c581a4SArd Biesheuvel 406*61c581a4SArd Biesheuvel *r = t->t[ap[0]]; 407*61c581a4SArd Biesheuvel while (++i < 16) { 408*61c581a4SArd Biesheuvel gf128mul_x8_bbe(r); 409*61c581a4SArd Biesheuvel be128_xor(r, r, &t->t[ap[i]]); 410*61c581a4SArd Biesheuvel } 411*61c581a4SArd Biesheuvel *a = *r; 412*61c581a4SArd Biesheuvel } 413*61c581a4SArd Biesheuvel EXPORT_SYMBOL(gf128mul_4k_bbe); 414*61c581a4SArd Biesheuvel 415*61c581a4SArd Biesheuvel MODULE_LICENSE("GPL"); 416*61c581a4SArd Biesheuvel MODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)"); 417