162306a36Sopenharmony_ci/* gf128mul.h - 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://fp.gladman.plus.com/cryptography_technology/index.htm
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 Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.   All rights reserved.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci LICENSE TERMS
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci The free distribution and use of this software in both source and binary
2262306a36Sopenharmony_ci form is allowed (with or without changes) provided that:
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci   1. distributions of this source code include the above copyright
2562306a36Sopenharmony_ci      notice, this list of conditions and the following disclaimer;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci   2. distributions in binary form include the above copyright
2862306a36Sopenharmony_ci      notice, this list of conditions and the following disclaimer
2962306a36Sopenharmony_ci      in the documentation and/or other associated materials;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci   3. the copyright holder's name is not used to endorse products
3262306a36Sopenharmony_ci      built using this software without specific written permission.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci ALTERNATIVELY, provided that this notice is retained in full, this product
3562306a36Sopenharmony_ci may be distributed under the terms of the GNU General Public License (GPL),
3662306a36Sopenharmony_ci in which case the provisions of the GPL apply INSTEAD OF those given above.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci DISCLAIMER
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci This software is provided 'as is' with no explicit or implied warranties
4162306a36Sopenharmony_ci in respect of its properties, including, but not limited to, correctness
4262306a36Sopenharmony_ci and/or fitness for purpose.
4362306a36Sopenharmony_ci ---------------------------------------------------------------------------
4462306a36Sopenharmony_ci Issue Date: 31/01/2006
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci An implementation of field multiplication in Galois Field GF(2^128)
4762306a36Sopenharmony_ci*/
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#ifndef _CRYPTO_GF128MUL_H
5062306a36Sopenharmony_ci#define _CRYPTO_GF128MUL_H
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#include <asm/byteorder.h>
5362306a36Sopenharmony_ci#include <crypto/b128ops.h>
5462306a36Sopenharmony_ci#include <linux/slab.h>
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* Comment by Rik:
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * For some background on GF(2^128) see for example:
5962306a36Sopenharmony_ci * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * The elements of GF(2^128) := GF(2)[X]/(X^128-X^7-X^2-X^1-1) can
6262306a36Sopenharmony_ci * be mapped to computer memory in a variety of ways. Let's examine
6362306a36Sopenharmony_ci * three common cases.
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * Take a look at the 16 binary octets below in memory order. The msb's
6662306a36Sopenharmony_ci * are left and the lsb's are right. char b[16] is an array and b[0] is
6762306a36Sopenharmony_ci * the first octet.
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * 10000000 00000000 00000000 00000000 .... 00000000 00000000 00000000
7062306a36Sopenharmony_ci *   b[0]     b[1]     b[2]     b[3]          b[13]    b[14]    b[15]
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * Every bit is a coefficient of some power of X. We can store the bits
7362306a36Sopenharmony_ci * in every byte in little-endian order and the bytes themselves also in
7462306a36Sopenharmony_ci * little endian order. I will call this lle (little-little-endian).
7562306a36Sopenharmony_ci * The above buffer represents the polynomial 1, and X^7+X^2+X^1+1 looks
7662306a36Sopenharmony_ci * like 11100001 00000000 .... 00000000 = { 0xE1, 0x00, }.
7762306a36Sopenharmony_ci * This format was originally implemented in gf128mul and is used
7862306a36Sopenharmony_ci * in GCM (Galois/Counter mode) and in ABL (Arbitrary Block Length).
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * Another convention says: store the bits in bigendian order and the
8162306a36Sopenharmony_ci * bytes also. This is bbe (big-big-endian). Now the buffer above
8262306a36Sopenharmony_ci * represents X^127. X^7+X^2+X^1+1 looks like 00000000 .... 10000111,
8362306a36Sopenharmony_ci * b[15] = 0x87 and the rest is 0. LRW uses this convention and bbe
8462306a36Sopenharmony_ci * is partly implemented.
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci * Both of the above formats are easy to implement on big-endian
8762306a36Sopenharmony_ci * machines.
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci * XTS and EME (the latter of which is patent encumbered) use the ble
9062306a36Sopenharmony_ci * format (bits are stored in big endian order and the bytes in little
9162306a36Sopenharmony_ci * endian). The above buffer represents X^7 in this case and the
9262306a36Sopenharmony_ci * primitive polynomial is b[0] = 0x87.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * The common machine word-size is smaller than 128 bits, so to make
9562306a36Sopenharmony_ci * an efficient implementation we must split into machine word sizes.
9662306a36Sopenharmony_ci * This implementation uses 64-bit words for the moment. Machine
9762306a36Sopenharmony_ci * endianness comes into play. The lle format in relation to machine
9862306a36Sopenharmony_ci * endianness is discussed below by the original author of gf128mul Dr
9962306a36Sopenharmony_ci * Brian Gladman.
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci * Let's look at the bbe and ble format on a little endian machine.
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * bbe on a little endian machine u32 x[4]:
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci *  MS            x[0]           LS  MS            x[1]		  LS
10662306a36Sopenharmony_ci *  ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
10762306a36Sopenharmony_ci *  103..96 111.104 119.112 127.120  71...64 79...72 87...80 95...88
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci *  MS            x[2]           LS  MS            x[3]		  LS
11062306a36Sopenharmony_ci *  ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
11162306a36Sopenharmony_ci *  39...32 47...40 55...48 63...56  07...00 15...08 23...16 31...24
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci * ble on a little endian machine
11462306a36Sopenharmony_ci *
11562306a36Sopenharmony_ci *  MS            x[0]           LS  MS            x[1]		  LS
11662306a36Sopenharmony_ci *  ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
11762306a36Sopenharmony_ci *  31...24 23...16 15...08 07...00  63...56 55...48 47...40 39...32
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci *  MS            x[2]           LS  MS            x[3]		  LS
12062306a36Sopenharmony_ci *  ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
12162306a36Sopenharmony_ci *  95...88 87...80 79...72 71...64  127.120 199.112 111.104 103..96
12262306a36Sopenharmony_ci *
12362306a36Sopenharmony_ci * Multiplications in GF(2^128) are mostly bit-shifts, so you see why
12462306a36Sopenharmony_ci * ble (and lbe also) are easier to implement on a little-endian
12562306a36Sopenharmony_ci * machine than on a big-endian machine. The converse holds for bbe
12662306a36Sopenharmony_ci * and lle.
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci * Note: to have good alignment, it seems to me that it is sufficient
12962306a36Sopenharmony_ci * to keep elements of GF(2^128) in type u64[2]. On 32-bit wordsize
13062306a36Sopenharmony_ci * machines this will automatically aligned to wordsize and on a 64-bit
13162306a36Sopenharmony_ci * machine also.
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_ci/*	Multiply a GF(2^128) field element by x. Field elements are
13462306a36Sopenharmony_ci    held in arrays of bytes in which field bits 8n..8n + 7 are held in
13562306a36Sopenharmony_ci    byte[n], with lower indexed bits placed in the more numerically
13662306a36Sopenharmony_ci    significant bit positions within bytes.
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci    On little endian machines the bit indexes translate into the bit
13962306a36Sopenharmony_ci    positions within four 32-bit words in the following way
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci    MS            x[0]           LS  MS            x[1]		  LS
14262306a36Sopenharmony_ci    ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
14362306a36Sopenharmony_ci    24...31 16...23 08...15 00...07  56...63 48...55 40...47 32...39
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci    MS            x[2]           LS  MS            x[3]		  LS
14662306a36Sopenharmony_ci    ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
14762306a36Sopenharmony_ci    88...95 80...87 72...79 64...71  120.127 112.119 104.111 96..103
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci    On big endian machines the bit indexes translate into the bit
15062306a36Sopenharmony_ci    positions within four 32-bit words in the following way
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci    MS            x[0]           LS  MS            x[1]		  LS
15362306a36Sopenharmony_ci    ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
15462306a36Sopenharmony_ci    00...07 08...15 16...23 24...31  32...39 40...47 48...55 56...63
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci    MS            x[2]           LS  MS            x[3]		  LS
15762306a36Sopenharmony_ci    ms   ls ms   ls ms   ls ms   ls  ms   ls ms   ls ms   ls ms   ls
15862306a36Sopenharmony_ci    64...71 72...79 80...87 88...95  96..103 104.111 112.119 120.127
15962306a36Sopenharmony_ci*/
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/*	A slow generic version of gf_mul, implemented for lle and bbe
16262306a36Sopenharmony_ci * 	It multiplies a and b and puts the result in a */
16362306a36Sopenharmony_civoid gf128mul_lle(be128 *a, const be128 *b);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_civoid gf128mul_bbe(be128 *a, const be128 *b);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/*
16862306a36Sopenharmony_ci * The following functions multiply a field element by x in
16962306a36Sopenharmony_ci * the polynomial field representation.  They use 64-bit word operations
17062306a36Sopenharmony_ci * to gain speed but compensate for machine endianness and hence work
17162306a36Sopenharmony_ci * correctly on both styles of machine.
17262306a36Sopenharmony_ci *
17362306a36Sopenharmony_ci * They are defined here for performance.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic inline u64 gf128mul_mask_from_bit(u64 x, int which)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	/* a constant-time version of 'x & ((u64)1 << which) ? (u64)-1 : 0' */
17962306a36Sopenharmony_ci	return ((s64)(x << (63 - which)) >> 63);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic inline void gf128mul_x_lle(be128 *r, const be128 *x)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	u64 a = be64_to_cpu(x->a);
18562306a36Sopenharmony_ci	u64 b = be64_to_cpu(x->b);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/* equivalent to gf128mul_table_le[(b << 7) & 0xff] << 48
18862306a36Sopenharmony_ci	 * (see crypto/gf128mul.c): */
18962306a36Sopenharmony_ci	u64 _tt = gf128mul_mask_from_bit(b, 0) & ((u64)0xe1 << 56);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	r->b = cpu_to_be64((b >> 1) | (a << 63));
19262306a36Sopenharmony_ci	r->a = cpu_to_be64((a >> 1) ^ _tt);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic inline void gf128mul_x_bbe(be128 *r, const be128 *x)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	u64 a = be64_to_cpu(x->a);
19862306a36Sopenharmony_ci	u64 b = be64_to_cpu(x->b);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	/* equivalent to gf128mul_table_be[a >> 63] (see crypto/gf128mul.c): */
20162306a36Sopenharmony_ci	u64 _tt = gf128mul_mask_from_bit(a, 63) & 0x87;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	r->a = cpu_to_be64((a << 1) | (b >> 63));
20462306a36Sopenharmony_ci	r->b = cpu_to_be64((b << 1) ^ _tt);
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/* needed by XTS */
20862306a36Sopenharmony_cistatic inline void gf128mul_x_ble(le128 *r, const le128 *x)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	u64 a = le64_to_cpu(x->a);
21162306a36Sopenharmony_ci	u64 b = le64_to_cpu(x->b);
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	/* equivalent to gf128mul_table_be[b >> 63] (see crypto/gf128mul.c): */
21462306a36Sopenharmony_ci	u64 _tt = gf128mul_mask_from_bit(a, 63) & 0x87;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	r->a = cpu_to_le64((a << 1) | (b >> 63));
21762306a36Sopenharmony_ci	r->b = cpu_to_le64((b << 1) ^ _tt);
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci/* 4k table optimization */
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_cistruct gf128mul_4k {
22362306a36Sopenharmony_ci	be128 t[256];
22462306a36Sopenharmony_ci};
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g);
22762306a36Sopenharmony_cistruct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g);
22862306a36Sopenharmony_civoid gf128mul_4k_lle(be128 *a, const struct gf128mul_4k *t);
22962306a36Sopenharmony_civoid gf128mul_4k_bbe(be128 *a, const struct gf128mul_4k *t);
23062306a36Sopenharmony_civoid gf128mul_x8_ble(le128 *r, const le128 *x);
23162306a36Sopenharmony_cistatic inline void gf128mul_free_4k(struct gf128mul_4k *t)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci	kfree_sensitive(t);
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci/* 64k table optimization, implemented for bbe */
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cistruct gf128mul_64k {
24062306a36Sopenharmony_ci	struct gf128mul_4k *t[16];
24162306a36Sopenharmony_ci};
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci/* First initialize with the constant factor with which you
24462306a36Sopenharmony_ci * want to multiply and then call gf128mul_64k_bbe with the other
24562306a36Sopenharmony_ci * factor in the first argument, and the table in the second.
24662306a36Sopenharmony_ci * Afterwards, the result is stored in *a.
24762306a36Sopenharmony_ci */
24862306a36Sopenharmony_cistruct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g);
24962306a36Sopenharmony_civoid gf128mul_free_64k(struct gf128mul_64k *t);
25062306a36Sopenharmony_civoid gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t);
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci#endif /* _CRYPTO_GF128MUL_H */
253