[PATCH 05/10] cast5: reduce CTR bulk counter carry handling to 16 bits
Jussi Kivilinna <[email protected]> Fri, 24 Jul 2026 21:50:12 +0300
| Newsgroups | gmane.comp.encryption.gpg.libgcrypt.devel |
|---|---|
| Message-ID | <[email protected]> |
* cipher/cast5.c (cast_setkey): Assign ctr16be_enc bulk op. (_gcry_cast5_ctr_enc): Use cipher_block_add_be16. * cipher/cast5-arm.S (_gcry_cast5_arm_ctr_enc): Drop 64-bit counter carry propagation in IV construction. -- CAST5 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 cast5 off transitional ctr_enc alias. Signed-off-by: Jussi Kivilinna <[email protected]> --- cipher/cast5-arm.S | 16 ++++++++-------- cipher/cast5.c | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cipher/cast5-arm.S b/cipher/cast5-arm.S index ae53e6b4..75cc4a85 100644 --- a/cipher/cast5-arm.S +++ b/cipher/cast5-arm.S @@ -603,17 +603,17 @@ _gcry_cast5_arm_ctr_enc: mov lr, r3; - /* Load IV (big => host endian) */ - read_block_aligned(lr, 0, RL0, RR0, be_to_host, RT1); + /* 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, RT1); + /* Store new IV */ + be_to_host(r6, RT0); + str r6, [lr, #4]; bl _gcry_cast5_arm_enc_blk2; /* result in RR0:RL0, RR1:RL1 = r4:r3, r10:r9 */ diff --git a/cipher/cast5.c b/cipher/cast5.c index 34cf2e58..19a7f6a6 100644 --- a/cipher/cast5.c +++ b/cipher/cast5.c @@ -814,9 +814,9 @@ _gcry_cast5_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg, cipher_block_cpy (tmpbuf + 0, ctr, CAST5_BLOCKSIZE); cipher_block_cpy (tmpbuf + 8, ctr, CAST5_BLOCKSIZE); cipher_block_cpy (tmpbuf + 16, ctr, CAST5_BLOCKSIZE); - cipher_block_add (tmpbuf + 8, 1, CAST5_BLOCKSIZE); - cipher_block_add (tmpbuf + 16, 2, CAST5_BLOCKSIZE); - cipher_block_add (ctr, 3, CAST5_BLOCKSIZE); + cipher_block_add_be16 (tmpbuf + 8, 1, CAST5_BLOCKSIZE); + cipher_block_add_be16 (tmpbuf + 16, 2, CAST5_BLOCKSIZE); + cipher_block_add_be16 (ctr, 3, CAST5_BLOCKSIZE); /* Encrypt the counter. */ do_encrypt_block_3(ctx, tmpbuf, tmpbuf); /* XOR the input with the encrypted counter and store in output. */ @@ -835,7 +835,7 @@ _gcry_cast5_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg, outbuf += CAST5_BLOCKSIZE; inbuf += CAST5_BLOCKSIZE; /* Increment the counter. */ - cipher_block_add (ctr, 1, CAST5_BLOCKSIZE); + cipher_block_add_be16 (ctr, 1, CAST5_BLOCKSIZE); } wipememory(tmpbuf, sizeof(tmpbuf)); @@ -1171,7 +1171,7 @@ cast_setkey (void *context, const byte *key, unsigned keylen, memset (bulk_ops, 0, sizeof(*bulk_ops)); bulk_ops->cfb_dec = _gcry_cast5_cfb_dec; bulk_ops->cbc_dec = _gcry_cast5_cbc_dec; - bulk_ops->ctr_enc = _gcry_cast5_ctr_enc; + bulk_ops->ctr16be_enc = _gcry_cast5_ctr_enc; return rc; } -- 2.53.0