[PATCH 6/7] cipher: fold AEAD byte-counter helper to 64-bit accumulator
Jussi Kivilinna <[email protected]> Fri, 24 Jul 2026 21:48:05 +0300
| Newsgroups | gmane.comp.encryption.gpg.libgcrypt.devel |
|---|---|
| Message-ID | <[email protected]> |
* cipher/cipher-internal.h (cipher_bytecounter_add): Rewrite using 64-bit accumulator. -- cipher_bytecounter_add split size_t addend into high and low halves with separate carry handling. Load 32-bit counter pair into 64-bit accumulator, add in one step and split back. Signed-off-by: Jussi Kivilinna <[email protected]> --- cipher/cipher-internal.h | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/cipher/cipher-internal.h b/cipher/cipher-internal.h index 8e6cbffc..bc023c76 100644 --- a/cipher/cipher-internal.h +++ b/cipher/cipher-internal.h @@ -812,23 +812,11 @@ static inline unsigned int _gcry_blocksize_shift(gcry_cipher_hd_t c) static inline int cipher_bytecounter_add (u32 ctr[2], size_t add) { - int overflow = 0; - u32 low_add = add; - - if (sizeof(add) > sizeof(u32)) - { - u32 high_add = ((add >> 31) >> 1) & 0xffffffff; - ctr[1] += high_add; - if (ctr[1] < high_add) - overflow = 1; - } - - ctr[0] += low_add; - if (ctr[0] >= low_add) - return overflow; - - ctr[1] += 1; - return (ctr[1] < 1) || overflow; + u64 bytecounter = ((u64)ctr[1] << 32) + ctr[0]; + bytecounter += add; + ctr[0] = bytecounter; + ctr[1] = bytecounter >> 32; + return bytecounter < add; } -- 2.53.0