From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796328729; b=J9kXSpQ7yA+xMWhHbaUAOGKMQDK5PjZFjlW9zMwGEb7NqGYd5dqxovvqrrmDAjZo9LDI QIR/viHM9WYITZM074WzS/xyOCymV/vuP+qL5X1CXe89TBYG2j1NuVQmVPL7O5prgI9ks 6rSJnOfX4vm01676Uo7SDg8CJflx4NnSfExq7aV7gKXX6FXJyl4S3a6aJYkLGPlaV6jqi owEUoFCxec7Ecwh6FmlYrCxpkc5iwMtXPZ4ZlfXFsZoj13Y5KC9clQYxpeE48VfIC17Os a1O28+j2bwsBRqiyFd3MwpbnuncqlVtynvOSF4x/5nnKBSTc6StbvxdZ/xXlX7WTLtw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796328729; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=HV++uUSM8ibGuQXDBstsZ+Oeizf+KUTcy21dY35zibM=; b=o5yXHmv3iI3lP0A3C7ik5V7A+zoqnDJQxkW01a5O2FKdCQsgMwF2AH4Zjd9y9CmUNrxK Ad/ca0shEINfIa6oDWVAsO2MctZccNU3ewheFQ40wzgCHK9ucVR5i/AhYWQI+PIOd8dTY 8hytu7Rm9WE1Q14J75FpqkopUeHowtIQXKI9KGIsPnsb4faswoNUQzdI4DKEq5DOQMfWY wpuN1M1uWxHS02bzUbdS9KOU6WzpF84fk2ej3ek637ZWZh0Kx5+ftRjs1GLL+S32yIRIv W6/T57DZVTzVMZJJCFucahsXYC7QCOyH7323fUeEU+j6LK4fPuHbpxyWKrVHAvwqnqg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gTHw41rB; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42313606A32D for ; Thu, 3 Dec 2026 20:12:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gTHw41rB Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDEC11F0092B; Thu, 3 Dec 2026 20:12:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796328729; bh=HV++uUSM8ibGuQXDBstsZ+Oeizf+KUTcy21dY35zibM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gTHw41rBjB6ecSqrfhuq5OcAAGxdX4yCE2pSHYBnnwYLJz1DVqyP7I8RRDkZK1R2D iIOiY2xeoQwEueoT+EM6ZicrZwHD9yTKL7OueOgXCW0BcI2wpq2REjus/eOhBnoARb yf/RNdJe7dQdScYhIW/kg91GYt93NqWlLFNleZ2vTLwu0UvfPYEg6Tc6wwkoeNfFFg yOntNvDCMBdTGexvzCWqdCGy+gqaJkicnUAIgj642+p3CqkEmqJJNMMttuc/t2J8VP 7sSp2tCUgcMCu0oLo7EaOiEQGQ8vq9qXn1XzQEREGMOMfl8C1ZnmlQBvQs+DtwJ4eC bzpzB3iwaLWtg== From: Eric Biggers To: linux-crypto@vger.kernel.org Cc: Ard Biesheuvel , "Jason A . Donenfeld" , Herbert Xu , Eric Biggers Subject: [PATCH 1/2] crypto: lrw - Implement field multiplication without gf128mul_64k Date: Thu, 3 Dec 2026 12:11:51 -0800 Message-ID: <20261203201152.76224-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203201152.76224-1-ebiggers@kernel.org> References: <20261203201152.76224-1-ebiggers@kernel.org> X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include +#include #include #include #include #include #include #include +#include #include #include @@ -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