* [PATCH 0/2] lib/crypto: gf128mul: Remove 64 KiB table support
@ 2026-12-03 20:11 Eric Biggers
2026-12-03 20:11 ` [PATCH 1/2] crypto: lrw - Implement field multiplication without gf128mul_64k Eric Biggers
2026-12-03 20:11 ` [PATCH 2/2] lib/crypto: gf128mul: Remove unused 64k bbe table functions Eric Biggers
0 siblings, 2 replies; 3+ messages in thread
From: Eric Biggers @ 2026-12-03 20:11 UTC (permalink / raw)
To: linux-crypto
Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers
This series is targeting libcrypto-next.
This series removes the support for massive 64 KiB multiplication tables
from the gf128mul module. To do so, it updates lrw.c to use a more
lightweight (and faster in practice) field multiplication method.
Eric Biggers (2):
crypto: lrw - Implement field multiplication without gf128mul_64k
lib/crypto: gf128mul: Remove unused 64k bbe table functions
crypto/Kconfig | 1 -
crypto/lrw.c | 90 +++++++++++++++++++--------------------
include/crypto/gf128mul.h | 17 --------
lib/crypto/gf128mul.c | 90 ---------------------------------------
4 files changed, 44 insertions(+), 154 deletions(-)
base-commit: 7845a3cc6e006f849ff882737afbd46584168eac
--
2.56.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] crypto: lrw - Implement field multiplication without gf128mul_64k
2026-12-03 20:11 [PATCH 0/2] lib/crypto: gf128mul: Remove 64 KiB table support Eric Biggers
@ 2026-12-03 20:11 ` Eric Biggers
2026-12-03 20:11 ` [PATCH 2/2] lib/crypto: gf128mul: Remove unused 64k bbe table functions Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-12-03 20:11 UTC (permalink / raw)
To: linux-crypto
Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers
The lrw template is one of the only remaining users of the gf128mul
module, since GHASH and POLYVAL no longer use it. It's also the only
user of the "bbe" and "64k" functions in it.
The 64 KiB multiplication table, of which a separate instance is created
for each LRW key, is quite enormous. It's very bad on the CPU's dcache
for anyone still using this legacy disk encryption mode. Moreover,
initializing it requires key-dependent table lookups.
The 64 KiB table is used to do GF(2^128) multiplications (using the
"bbe" convention) by the field element 'Key2' in two places:
1. When setting a new key, compute and cache
'mulinc[i] = Key2 * (x^i + ... + 1)' for i in 0..127.
This creates a 2 KiB table (different from the big 64 KiB table).
2. When en/decrypting each message, compute the initial tweak
T = Key2 * IV.
The mulinc initialization is easy to do differently, using the formula:
mulinc[i] = mulinc[i - 1] + (Key2 * x^i)
Maintain Key2 * x^i as a loop variable that's multiplied by x once per
iteration using gf128mul_x_bbe(). That doesn't require a table.
'T = Key2 * IV' is a bit harder to reimplement. The naive solution of
iterating over all 128 coefficients would be slow.
Instead, we can use the 'mulinc' table, which contains prefix sums.
mulinc[i] is Key2 times a contiguous run of 1 bits starting at the
low-order bit -- used by lrw_next_index() to compute Key2 * (T + 1) from
Key2 * T. However, by adding two entries we can compute Key2 times
*any* contiguous run of 1 bits. E.g., considering 0b1100:
Key2 * 0b1100
= Key2 * (x^3 + x^2)
= (Key2 * (x^3 + x^2 + x + 1)) + (Key2 * (x + 1))
= mulinc[3] + mulinc[1]
If there are other runs of 1 bits, we add those entries too. The
indices of the mulinc entries to add are the bit positions in IV where
the next higher bit is different: equal to IV ^ (IV >> 1).
Adding (XOR'ing) all such mulinc entries gives Key2 * IV, in linear time
with respect to the number of runs of 1 bits in the IV.
The IV is public, so the IV-dependent execution time is tolerable.
For dm-crypt use, the average number of loop iterations,
popcount(IV ^ (IV >> 1)), also tends to be no more than the number of
iterations (16) done with the gf128mul_64k table. It also eliminates
the massive 64 KiB table. So in practice it is faster, despite the
branches being less predictable. E.g., a benchmark with en/decrypting
512-byte messages and an incrementing IV starting at 4 random bytes and
12 zero bytes showed about 5% better performance.
Setting a key is now much faster too.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
crypto/Kconfig | 1 -
crypto/lrw.c | 90 ++++++++++++++++++++++++--------------------------
2 files changed, 44 insertions(+), 47 deletions(-)
diff --git a/crypto/Kconfig b/crypto/Kconfig
index 981d5dc422d4c..b203f20b7ca42 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -699,7 +699,6 @@ config CRYPTO_HCTR2
config CRYPTO_LRW
tristate "LRW (Liskov Rivest Wagner)"
- select CRYPTO_LIB_GF128MUL
select CRYPTO_SKCIPHER
select CRYPTO_MANAGER
select CRYPTO_ECB
diff --git a/crypto/lrw.c b/crypto/lrw.c
index e306e85d7cedc..6baae2971e432 100644
--- a/crypto/lrw.c
+++ b/crypto/lrw.c
@@ -15,12 +15,14 @@
#include <crypto/internal/skcipher.h>
#include <crypto/scatterwalk.h>
+#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
+#include <linux/unaligned.h>
#include <crypto/b128ops.h>
#include <crypto/gf128mul.h>
@@ -31,20 +33,9 @@ struct lrw_tfm_ctx {
struct crypto_skcipher *child;
/*
- * optimizes multiplying a random (non incrementing, as at the
- * start of a new sector) value with key2, we could also have
- * used 4k optimization tables or no optimization at all. In the
- * latter case we would have to store key2 here
- */
- struct gf128mul_64k *table;
-
- /*
- * stores:
- * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
- * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
- * key2*{ 0,0,...1,1,1,1,1 }, etc
- * needed for optimized multiplication of incrementing values
- * with key2
+ * mulinc[i] = Key2 * (x^i + ... + 1) in GF(2^128), i.e. the prefix XOR
+ * sums of Key2 * x^j. Used to compute the initial tweak Key2 * IV and
+ * to update the tweak when incrementing the block counter.
*/
be128 mulinc[128];
};
@@ -54,15 +45,34 @@ struct lrw_request_ctx {
struct skcipher_request subreq;
};
-static inline void lrw_setbit128_bbe(void *b, int bit)
+/*
+ * Compute T = Key2 * IV in GF(2^128) using mulinc[].
+ *
+ * Because mulinc[i] stores the prefix XOR sums of Key2 * x^0 through
+ * Key2 * x^i, any contiguous run of 1 bits in IV from bit a through bit b
+ * contributes mulinc[b] ^ mulinc[a - 1] (or just mulinc[b] if a == 0).
+ * Those endpoints are the bit positions in IV where the next higher bit is
+ * different, i.e. the set bits of IV ^ (IV >> 1).
+ *
+ * This has IV-dependent execution time, which is fine since the IV is public.
+ */
+static void lrw_mul_tweak(be128 *t, const u8 *iv, const be128 mulinc[128])
{
- __set_bit(bit ^ (0x80 -
-#ifdef __BIG_ENDIAN
- BITS_PER_LONG
-#else
- BITS_PER_BYTE
-#endif
- ), b);
+ u64 hi = get_unaligned_be64(&iv[0]);
+ u64 lo = get_unaligned_be64(&iv[8]);
+ u64 diff_hi = hi ^ (hi >> 1);
+ u64 diff_lo = lo ^ (lo >> 1) ^ (hi << 63);
+ be128 res = { 0 };
+
+ while (diff_lo) {
+ be128_xor(&res, &res, &mulinc[__ffs64(diff_lo)]);
+ diff_lo &= diff_lo - 1;
+ }
+ while (diff_hi) {
+ be128_xor(&res, &res, &mulinc[64 + __ffs64(diff_hi)]);
+ diff_hi &= diff_hi - 1;
+ }
+ *t = res;
}
static int lrw_setkey(struct crypto_skcipher *parent, const u8 *key,
@@ -72,7 +82,7 @@ static int lrw_setkey(struct crypto_skcipher *parent, const u8 *key,
struct crypto_skcipher *child = ctx->child;
int err, bsize = LRW_BLOCK_SIZE;
const u8 *tweak = key + keylen - bsize;
- be128 tmp = { 0 };
+ be128 power;
int i;
crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
@@ -82,21 +92,14 @@ static int lrw_setkey(struct crypto_skcipher *parent, const u8 *key,
if (err)
return err;
- if (ctx->table)
- gf128mul_free_64k(ctx->table);
-
- /* initialize multiplication table for Key2 */
- ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
- if (!ctx->table)
- return -ENOMEM;
-
- /* initialize optimization table */
- for (i = 0; i < 128; i++) {
- lrw_setbit128_bbe(&tmp, i);
- ctx->mulinc[i] = tmp;
- gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
+ /* Initialize mulinc[i] = Key2 * (x^i + ... + 1) */
+ memcpy(&power, tweak, sizeof(power));
+ ctx->mulinc[0] = power;
+ for (i = 1; i < 128; i++) {
+ gf128mul_x_bbe(&power, &power);
+ be128_xor(&ctx->mulinc[i], &ctx->mulinc[i - 1], &power);
}
-
+ memzero_explicit(&power, sizeof(power));
return 0;
}
@@ -124,9 +127,9 @@ static int lrw_next_index(u32 *counter)
}
/*
- * If we get here, then x == 128 and we are incrementing the counter
+ * If we get here, then res == 128 and we are incrementing the counter
* from all ones to all zeros. This means we must return index 127, i.e.
- * the one corresponding to key2*{ 1,...,1 }.
+ * the index of mulinc[] that contains Key2 * (x^127 + ... + 1).
*/
return 127;
}
@@ -233,11 +236,8 @@ static void lrw_init_crypt(struct skcipher_request *req)
skcipher_request_set_crypt(subreq, req->dst, req->dst,
req->cryptlen, req->iv);
- /* calculate first value of T */
- memcpy(&rctx->t, req->iv, sizeof(rctx->t));
-
- /* T <- I*Key2 */
- gf128mul_64k_bbe(&rctx->t, ctx->table);
+ /* Calculate initial tweak: T = Key2 * IV */
+ lrw_mul_tweak(&rctx->t, req->iv, ctx->mulinc);
}
static int lrw_encrypt(struct skcipher_request *req)
@@ -285,8 +285,6 @@ static void lrw_exit_tfm(struct crypto_skcipher *tfm)
{
struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- if (ctx->table)
- gf128mul_free_64k(ctx->table);
crypto_free_skcipher(ctx->child);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] lib/crypto: gf128mul: Remove unused 64k bbe table functions
2026-12-03 20:11 [PATCH 0/2] lib/crypto: gf128mul: Remove 64 KiB table support Eric Biggers
2026-12-03 20:11 ` [PATCH 1/2] crypto: lrw - Implement field multiplication without gf128mul_64k Eric Biggers
@ 2026-12-03 20:11 ` Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-12-03 20:11 UTC (permalink / raw)
To: linux-crypto
Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers
Now that lrw.c no longer uses them, remove the unused
'struct gf128mul_64k', gf128mul_init_64k_bbe(), gf128mul_free_64k(), and
gf128mul_64k_bbe().
Then remove gf128mul_x8_bbe() (which was called only by
gf128mul_init_64k_bbe()) and the now-unneeded <linux/slab.h> include.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
include/crypto/gf128mul.h | 17 --------
lib/crypto/gf128mul.c | 90 ---------------------------------------
2 files changed, 107 deletions(-)
diff --git a/include/crypto/gf128mul.h b/include/crypto/gf128mul.h
index 8fb6f8dbbfa9e..2bdd0ed63d05e 100644
--- a/include/crypto/gf128mul.h
+++ b/include/crypto/gf128mul.h
@@ -210,21 +210,4 @@ static inline void gf128mul_x_ble(le128 *r, const le128 *x)
void gf128mul_x8_ble(le128 *r, const le128 *x);
-/* 64k table optimization, implemented for bbe */
-
-struct gf128mul_64k {
- struct {
- be128 t[256];
- } *t[16];
-};
-
-/* First initialize with the constant factor with which you
- * want to multiply and then call gf128mul_64k_bbe with the other
- * factor in the first argument, and the table in the second.
- * Afterwards, the result is stored in *a.
- */
-struct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g);
-void gf128mul_free_64k(struct gf128mul_64k *t);
-void gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t);
-
#endif /* _CRYPTO_GF128MUL_H */
diff --git a/lib/crypto/gf128mul.c b/lib/crypto/gf128mul.c
index 7ebf07ce11683..bd57d31791256 100644
--- a/lib/crypto/gf128mul.c
+++ b/lib/crypto/gf128mul.c
@@ -52,7 +52,6 @@
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/module.h>
-#include <linux/slab.h>
#define gf128mul_dat(q) { \
q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\
@@ -140,16 +139,6 @@ static void gf128mul_x8_lle_ti(be128 *x)
x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));
}
-static void gf128mul_x8_bbe(be128 *x)
-{
- u64 a = be64_to_cpu(x->a);
- u64 b = be64_to_cpu(x->b);
- u64 _tt = gf128mul_table_be[a >> 56];
-
- x->a = cpu_to_be64((a << 8) | (b >> 56));
- x->b = cpu_to_be64((b << 8) ^ _tt);
-}
-
void gf128mul_x8_ble(le128 *r, const le128 *x)
{
u64 a = le64_to_cpu(x->a);
@@ -208,84 +197,5 @@ void gf128mul_lle(be128 *r, const be128 *b)
}
EXPORT_SYMBOL(gf128mul_lle);
-/* This version uses 64k bytes of table space.
- A 16 byte buffer has to be multiplied by a 16 byte key
- value in GF(2^128). If we consider a GF(2^128) value in
- the buffer's lowest byte, we can construct a table of
- the 256 16 byte values that result from the 256 values
- of this byte. This requires 4096 bytes. But we also
- need tables for each of the 16 higher bytes in the
- buffer as well, which makes 64 kbytes in total.
-*/
-/* additional explanation
- * t[0][BYTE] contains g*BYTE
- * t[1][BYTE] contains g*x^8*BYTE
- * ..
- * t[15][BYTE] contains g*x^120*BYTE */
-struct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g)
-{
- struct gf128mul_64k *t;
- int i, j, k;
-
- t = kzalloc_obj(*t);
- if (!t)
- goto out;
-
- for (i = 0; i < 16; i++) {
- t->t[i] = kzalloc_obj(*t->t[i]);
- if (!t->t[i]) {
- gf128mul_free_64k(t);
- t = NULL;
- goto out;
- }
- }
-
- t->t[0]->t[1] = *g;
- for (j = 1; j <= 64; j <<= 1)
- gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]);
-
- for (i = 0;;) {
- for (j = 2; j < 256; j += j)
- for (k = 1; k < j; ++k)
- be128_xor(&t->t[i]->t[j + k],
- &t->t[i]->t[j], &t->t[i]->t[k]);
-
- if (++i >= 16)
- break;
-
- for (j = 128; j > 0; j >>= 1) {
- t->t[i]->t[j] = t->t[i - 1]->t[j];
- gf128mul_x8_bbe(&t->t[i]->t[j]);
- }
- }
-
-out:
- return t;
-}
-EXPORT_SYMBOL(gf128mul_init_64k_bbe);
-
-void gf128mul_free_64k(struct gf128mul_64k *t)
-{
- int i;
-
- for (i = 0; i < 16; i++)
- kfree_sensitive(t->t[i]);
- kfree_sensitive(t);
-}
-EXPORT_SYMBOL(gf128mul_free_64k);
-
-void gf128mul_64k_bbe(be128 *a, const struct gf128mul_64k *t)
-{
- u8 *ap = (u8 *)a;
- be128 r[1];
- int i;
-
- *r = t->t[0]->t[ap[15]];
- for (i = 1; i < 16; ++i)
- be128_xor(r, r, &t->t[i]->t[ap[15 - i]]);
- *a = *r;
-}
-EXPORT_SYMBOL(gf128mul_64k_bbe);
-
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)");
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 20:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 20:11 [PATCH 0/2] lib/crypto: gf128mul: Remove 64 KiB table support Eric Biggers
2026-12-03 20:11 ` [PATCH 1/2] crypto: lrw - Implement field multiplication without gf128mul_64k Eric Biggers
2026-12-03 20:11 ` [PATCH 2/2] lib/crypto: gf128mul: Remove unused 64k bbe table functions Eric Biggers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox