/* gf128mul.c - GF(2^128) multiplication functions1*2* Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.3* Copyright (c) 2006, Rik Snel <[email protected]>4*5* Based on Dr Brian Gladman's (GPL'd) work published at6* http://gladman.plushost.co.uk/oldsite/cryptography_technology/index.php7* See the original copyright notice below.8*9* This program is free software; you can redistribute it and/or modify it10* under the terms of the GNU General Public License as published by the Free11* Software Foundation; either version 2 of the License, or (at your option)12* any later version.13*/1415/*16---------------------------------------------------------------------------17Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved.1819LICENSE TERMS2021The free distribution and use of this software in both source and binary22form is allowed (with or without changes) provided that:23241. distributions of this source code include the above copyright25notice, this list of conditions and the following disclaimer;26272. distributions in binary form include the above copyright28notice, this list of conditions and the following disclaimer29in the documentation and/or other associated materials;30313. the copyright holder's name is not used to endorse products32built using this software without specific written permission.3334ALTERNATIVELY, provided that this notice is retained in full, this product35may be distributed under the terms of the GNU General Public License (GPL),36in which case the provisions of the GPL apply INSTEAD OF those given above.3738DISCLAIMER3940This software is provided 'as is' with no explicit or implied warranties41in respect of its properties, including, but not limited to, correctness42and/or fitness for purpose.43---------------------------------------------------------------------------44Issue 31/01/20064546This file provides fast multiplication in GF(2^128) as required by several47cryptographic authentication modes48*/4950#include <crypto/gf128mul.h>51#include <linux/export.h>52#include <linux/kernel.h>53#include <linux/module.h>54#include <linux/slab.h>5556#define gf128mul_dat(q) { \57q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\58q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\59q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\60q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\61q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\62q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\63q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\64q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\65q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\66q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\67q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\68q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\69q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\70q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\71q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\72q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\73q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\74q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\75q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\76q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\77q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\78q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\79q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\80q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\81q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\82q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\83q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\84q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\85q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\86q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\87q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\88q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \89}9091/*92* Given a value i in 0..255 as the byte overflow when a field element93* in GF(2^128) is multiplied by x^8, the following macro returns the94* 16-bit value that must be XOR-ed into the low-degree end of the95* 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 for98* the "be" convention where the highest-order bit is the coefficient of99* the highest-degree polynomial term, and one for the "le" convention100* where the highest-order bit is the coefficient of the lowest-degree101* polynomial term. In both cases the values are stored in CPU byte102* endianness such that the coefficients are ordered consistently across103* bytes, i.e. in the "be" table bits 15..0 of the stored value104* correspond to the coefficients of x^15..x^0, and in the "le" table105* bits 15..0 correspond to the coefficients of x^0..x^15.106*107* Therefore, provided that the appropriate byte endianness conversions108* are done by the multiplication functions (and these must be in place109* 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 both112* "lle" and "lbe" elements.113*/114115#define xda_be(i) ( \116(i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \117(i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \118(i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \119(i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \120)121122#define xda_le(i) ( \123(i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \124(i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \125(i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \126(i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \127)128129static const u16 gf128mul_table_le[256] = gf128mul_dat(xda_le);130static const u16 gf128mul_table_be[256] = gf128mul_dat(xda_be);131132/*133* The following functions multiply a field element by x^8 in134* the polynomial field representation. They use 64-bit word operations135* to gain speed but compensate for machine endianness and hence work136* correctly on both styles of machine.137*/138139static void gf128mul_x8_lle(be128 *x)140{141u64 a = be64_to_cpu(x->a);142u64 b = be64_to_cpu(x->b);143u64 _tt = gf128mul_table_le[b & 0xff];144145x->b = cpu_to_be64((b >> 8) | (a << 56));146x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));147}148149/* time invariant version of gf128mul_x8_lle */150static void gf128mul_x8_lle_ti(be128 *x)151{152u64 a = be64_to_cpu(x->a);153u64 b = be64_to_cpu(x->b);154u64 _tt = xda_le(b & 0xff); /* avoid table lookup */155156x->b = cpu_to_be64((b >> 8) | (a << 56));157x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));158}159160static void gf128mul_x8_bbe(be128 *x)161{162u64 a = be64_to_cpu(x->a);163u64 b = be64_to_cpu(x->b);164u64 _tt = gf128mul_table_be[a >> 56];165166x->a = cpu_to_be64((a << 8) | (b >> 56));167x->b = cpu_to_be64((b << 8) ^ _tt);168}169170void gf128mul_x8_ble(le128 *r, const le128 *x)171{172u64 a = le64_to_cpu(x->a);173u64 b = le64_to_cpu(x->b);174u64 _tt = gf128mul_table_be[a >> 56];175176r->a = cpu_to_le64((a << 8) | (b >> 56));177r->b = cpu_to_le64((b << 8) ^ _tt);178}179EXPORT_SYMBOL(gf128mul_x8_ble);180181void gf128mul_lle(be128 *r, const be128 *b)182{183/*184* The p array should be aligned to twice the size of its element type,185* so that every even/odd pair is guaranteed to share a cacheline186* (assuming a cacheline size of 32 bytes or more, which is by far the187* most common). This ensures that each be128_xor() call in the loop188* takes the same amount of time regardless of the value of 'ch', which189* is derived from function parameter 'b', which is commonly used as a190* key, e.g., for GHASH. The odd array elements are all set to zero,191* making each be128_xor() a NOP if its associated bit in 'ch' is not192* set, and this is equivalent to calling be128_xor() conditionally.193* This approach aims to avoid leaking information about such keys194* through execution time variances.195*196* Unfortunately, __aligned(16) or higher does not work on x86 for197* variables on the stack so we need to perform the alignment by hand.198*/199be128 array[16 + 3] = {};200be128 *p = PTR_ALIGN(&array[0], 2 * sizeof(be128));201int i;202203p[0] = *r;204for (i = 0; i < 7; ++i)205gf128mul_x_lle(&p[2 * i + 2], &p[2 * i]);206207memset(r, 0, sizeof(*r));208for (i = 0;;) {209u8 ch = ((u8 *)b)[15 - i];210211be128_xor(r, r, &p[ 0 + !(ch & 0x80)]);212be128_xor(r, r, &p[ 2 + !(ch & 0x40)]);213be128_xor(r, r, &p[ 4 + !(ch & 0x20)]);214be128_xor(r, r, &p[ 6 + !(ch & 0x10)]);215be128_xor(r, r, &p[ 8 + !(ch & 0x08)]);216be128_xor(r, r, &p[10 + !(ch & 0x04)]);217be128_xor(r, r, &p[12 + !(ch & 0x02)]);218be128_xor(r, r, &p[14 + !(ch & 0x01)]);219220if (++i >= 16)221break;222223gf128mul_x8_lle_ti(r); /* use the time invariant version */224}225}226EXPORT_SYMBOL(gf128mul_lle);227228/* This version uses 64k bytes of table space.229A 16 byte buffer has to be multiplied by a 16 byte key230value in GF(2^128). If we consider a GF(2^128) value in231the buffer's lowest byte, we can construct a table of232the 256 16 byte values that result from the 256 values233of this byte. This requires 4096 bytes. But we also234need tables for each of the 16 higher bytes in the235buffer as well, which makes 64 kbytes in total.236*/237/* additional explanation238* t[0][BYTE] contains g*BYTE239* t[1][BYTE] contains g*x^8*BYTE240* ..241* t[15][BYTE] contains g*x^120*BYTE */242struct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g)243{244struct gf128mul_64k *t;245int i, j, k;246247t = kzalloc(sizeof(*t), GFP_KERNEL);248if (!t)249goto out;250251for (i = 0; i < 16; i++) {252t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL);253if (!t->t[i]) {254gf128mul_free_64k(t);255t = NULL;256goto out;257}258}259260t->t[0]->t[1] = *g;261for (j = 1; j <= 64; j <<= 1)262gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]);263264for (i = 0;;) {265for (j = 2; j < 256; j += j)266for (k = 1; k < j; ++k)267be128_xor(&t->t[i]->t[j + k],268&t->t[i]->t[j], &t->t[i]->t[k]);269270if (++i >= 16)271break;272273for (j = 128; j > 0; j >>= 1) {274t->t[i]->t[j] = t->t[i - 1]->t[j];275gf128mul_x8_bbe(&t->t[i]->t[j]);276}277}278279out:280return t;281}282EXPORT_SYMBOL(gf128mul_init_64k_bbe);283284void gf128mul_free_64k(struct gf128mul_64k *t)285{286int i;287288for (i = 0; i < 16; i++)289kfree_sensitive(t->t[i]);290kfree_sensitive(t);291}292EXPORT_SYMBOL(gf128mul_free_64k);293294void gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t)295{296u8 *ap = (u8 *)a;297be128 r[1];298int i;299300*r = t->t[0]->t[ap[15]];301for (i = 1; i < 16; ++i)302be128_xor(r, r, &t->t[i]->t[ap[15 - i]]);303*a = *r;304}305EXPORT_SYMBOL(gf128mul_64k_bbe);306307/* This version uses 4k bytes of table space.308A 16 byte buffer has to be multiplied by a 16 byte key309value in GF(2^128). If we consider a GF(2^128) value in a310single byte, we can construct a table of the 256 16 byte311values that result from the 256 values of this byte.312This requires 4096 bytes. If we take the highest byte in313the buffer and use this table to get the result, we then314have to multiply by x^120 to get the final value. For the315next highest byte the result has to be multiplied by x^112316and so on. But we can do this by accumulating the result317in an accumulator starting with the result for the top318byte. We repeatedly multiply the accumulator value by319x^8 and then add in (i.e. xor) the 16 bytes of the next320lower byte in the buffer, stopping when we reach the321lowest byte. This requires a 4096 byte table.322*/323struct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g)324{325struct gf128mul_4k *t;326int j, k;327328t = kzalloc(sizeof(*t), GFP_KERNEL);329if (!t)330goto out;331332t->t[128] = *g;333for (j = 64; j > 0; j >>= 1)334gf128mul_x_lle(&t->t[j], &t->t[j+j]);335336for (j = 2; j < 256; j += j)337for (k = 1; k < j; ++k)338be128_xor(&t->t[j + k], &t->t[j], &t->t[k]);339340out:341return t;342}343EXPORT_SYMBOL(gf128mul_init_4k_lle);344345void gf128mul_4k_lle(be128 *a, const struct gf128mul_4k *t)346{347u8 *ap = (u8 *)a;348be128 r[1];349int i = 15;350351*r = t->t[ap[15]];352while (i--) {353gf128mul_x8_lle(r);354be128_xor(r, r, &t->t[ap[i]]);355}356*a = *r;357}358EXPORT_SYMBOL(gf128mul_4k_lle);359360MODULE_LICENSE("GPL");361MODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)");362363364