[PATCH 03/10] blowfish: reduce CTR bulk counter carry handling to 16 bits
Jussi Kivilinna <[email protected]> Fri, 24 Jul 2026 21:50:10 +0300
| Newsgroups | gmane.comp.encryption.gpg.libgcrypt.devel |
|---|---|
| Message-ID | <[email protected]> |
* cipher/blowfish.c (bf_setkey): Assign ctr16be_enc bulk op. (_gcry_blowfish_ctr_enc): Use cipher_block_add_be16. * cipher/blowfish-arm.S (_gcry_blowfish_arm_ctr_enc): Drop 64-bit counter carry propagation in IV construction. -- Blowfish is 64-bit block cipher, so ARM CTR bulk function built IVs by incrementing 64-bit counter with carry from low to high 32-bit word. Under ctr16be_enc contract low 16 bits never overflow inside bulk call, so low-word add cannot carry into high word. Drop carry propagation. Moves blowfish off transitional ctr_enc alias. Signed-off-by: Jussi Kivilinna <[email protected]> --- cipher/blowfish-arm.S | 16 ++++++++-------- cipher/blowfish.c | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cipher/blowfish-arm.S b/cipher/blowfish-arm.S index a5101b5c..372c3ebc 100644 --- a/cipher/blowfish-arm.S +++ b/cipher/blowfish-arm.S @@ -629,17 +629,17 @@ _gcry_blowfish_arm_ctr_enc: mov lr, r3; - /* Load IV (big => host endian) */ - read_block_aligned(lr, 0, RL0, RR0, be_to_host, RT0); + /* Load IV */ + read_block_aligned(lr, 0, RL0, RR0, host_to_be, RT0); /* Construct IVs */ - adds RR1, RR0, #1; /* +1 */ - adc RL1, RL0, #0; - adds r6, RR1, #1; /* +2 */ - adc r5, RL1, #0; + mov RL1, RL0; + add RR1, RR0, #1; /* +1 */ + add r6, RR0, #2; /* +2 */ - /* Store new IV (host => big-endian) */ - write_block_aligned(lr, 0, r5, r6, host_to_be, RT0); + /* Store new IV */ + be_to_host(r6, RT0); + str r6, [lr, #4]; bl _gcry_blowfish_arm_enc_blk2; /* result in RR0:RL0, RR1:RL1 = r4:r3, r10:r9 */ diff --git a/cipher/blowfish.c b/cipher/blowfish.c index 87abd563..534d994f 100644 --- a/cipher/blowfish.c +++ b/cipher/blowfish.c @@ -678,9 +678,9 @@ _gcry_blowfish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg, cipher_block_cpy (tmpbuf + 0, ctr, BLOWFISH_BLOCKSIZE); cipher_block_cpy (tmpbuf + 8, ctr, BLOWFISH_BLOCKSIZE); cipher_block_cpy (tmpbuf + 16, ctr, BLOWFISH_BLOCKSIZE); - cipher_block_add (tmpbuf + 8, 1, BLOWFISH_BLOCKSIZE); - cipher_block_add (tmpbuf + 16, 2, BLOWFISH_BLOCKSIZE); - cipher_block_add (ctr, 3, BLOWFISH_BLOCKSIZE); + cipher_block_add_be16 (tmpbuf + 8, 1, BLOWFISH_BLOCKSIZE); + cipher_block_add_be16 (tmpbuf + 16, 2, BLOWFISH_BLOCKSIZE); + cipher_block_add_be16 (ctr, 3, BLOWFISH_BLOCKSIZE); /* Encrypt the counter. */ do_encrypt_3(ctx, tmpbuf, tmpbuf); /* XOR the input with the encrypted counter and store in output. */ @@ -699,7 +699,7 @@ _gcry_blowfish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg, outbuf += BLOWFISH_BLOCKSIZE; inbuf += BLOWFISH_BLOCKSIZE; /* Increment the counter. */ - cipher_block_add (ctr, 1, BLOWFISH_BLOCKSIZE); + cipher_block_add_be16 (ctr, 1, BLOWFISH_BLOCKSIZE); } wipememory(tmpbuf, sizeof(tmpbuf)); @@ -1074,7 +1074,7 @@ bf_setkey (void *context, const byte *key, unsigned keylen, memset (bulk_ops, 0, sizeof(*bulk_ops)); bulk_ops->cfb_dec = _gcry_blowfish_cfb_dec; bulk_ops->cbc_dec = _gcry_blowfish_cbc_dec; - bulk_ops->ctr_enc = _gcry_blowfish_ctr_enc; + bulk_ops->ctr16be_enc = _gcry_blowfish_ctr_enc; return rc; } -- 2.53.0