From: Niels Möller Date: Fri, 29 Mar 2024 15:04:09 +0000 (+0100) Subject: Let umac and bcrypt share bswap helper function. X-Git-Tag: nettle_3.10rc1~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5a00e63a235d8920388ffbb32e45a48b24f3828b;p=thirdparty%2Fnettle.git Let umac and bcrypt share bswap helper function. --- diff --git a/ChangeLog b/ChangeLog index 0efcfa13..c623daec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2024-03-29 Niels Möller + + * bswap-internal.h (nettle_bswap32_n): New inline function. + (bswap32_n_if_le): New macro, to reduce code duplication. + * blowfish-bcrypt.c (bswap32_if_le_n): Deleted, usage replaced + with shared bswap32_n_if_le. + * umac-set-key.c (bswap32_if_le_n): Likewise. + 2024-03-28 Niels Möller * sha512-224-meta.c (nettle_sha512_224): Change name to diff --git a/blowfish-bcrypt.c b/blowfish-bcrypt.c index ee7c0eb8..b590748e 100644 --- a/blowfish-bcrypt.c +++ b/blowfish-bcrypt.c @@ -149,18 +149,6 @@ static uint32_t magic_w[6] = { 0x64657253, 0x63727944, 0x6F756274 }; -#if WORDS_BIGENDIAN -#define bswap32_if_le_n(n, x) -#else -static void -bswap32_if_le_n (unsigned n, uint32_t *x) -{ - unsigned i; - for (i = 0; i < n; i++) - x[i] = nettle_bswap32 (x[i]); -} -#endif - static void set_xkey(size_t lenkey, const uint8_t *key, bf_key expanded, bf_key initial, unsigned bug, uint32_t safety) @@ -341,7 +329,7 @@ static int ibcrypt(uint8_t *dst, else if (lenscheme < HASHOFFSET) return 0; memcpy(psalt, data.binary.salt, BLOWFISH_BCRYPT_BINSALT_SIZE); - bswap32_if_le_n (4, data.binary.salt); + bswap32_n_if_le (4, data.binary.salt); if (log2rounds < minlog2rounds || log2rounds > 31) return 0; @@ -446,7 +434,7 @@ static int ibcrypt(uint8_t *dst, dst = (uint8_t*) encode_radix64((char*) dst, BLOWFISH_BCRYPT_BINSALT_SIZE, psalt) - 1; - bswap32_if_le_n (6, data.binary.output); + bswap32_n_if_le (6, data.binary.output); /* This has to be bug-compatible with the original implementation, so only encode 23 of the 24 bytes. */ encode_radix64((char*) dst, 23, (uint8_t *) data.binary.output); diff --git a/bswap-internal.h b/bswap-internal.h index 30af5221..148cdc8b 100644 --- a/bswap-internal.h +++ b/bswap-internal.h @@ -62,16 +62,26 @@ nettle_bswap32 (uint32_t x) } #endif +static inline void +nettle_bswap32_n (unsigned n, uint32_t *x) +{ + unsigned i; + for (i = 0; i < n; i++) + x[i] = nettle_bswap32 (x[i]); +} + #if WORDS_BIGENDIAN #define bswap64_if_be nettle_bswap64 #define bswap32_if_be nettle_bswap32 #define bswap64_if_le(x) (x) #define bswap32_if_le(x) (x) +#define bswap32_n_if_le(n, x) #else #define bswap64_if_be(x) (x) #define bswap32_if_be(x) (x) #define bswap64_if_le nettle_bswap64 #define bswap32_if_le nettle_bswap32 +#define bswap32_n_if_le nettle_bswap32_n #endif #endif /* NETTLE_BSWAP_INTERNAL_H_INCLUDED */ diff --git a/umac-set-key.c b/umac-set-key.c index 9f7bfe60..7082f930 100644 --- a/umac-set-key.c +++ b/umac-set-key.c @@ -61,19 +61,6 @@ umac_kdf (struct aes128_ctx *aes, unsigned index, unsigned length, uint8_t *dst) } } -#if WORDS_BIGENDIAN -/* FIXME: Duplicated with blowfish-bcrypt.c. */ -#define bswap32_if_le_n(n, x) -#else -static void -bswap32_if_le_n (unsigned n, uint32_t *x) -{ - unsigned i; - for (i = 0; i < n; i++) - x[i] = nettle_bswap32 (x[i]); -} -#endif - void _nettle_umac_set_key (uint32_t *l1_key, uint32_t *l2_key, uint64_t *l3_key1, uint32_t *l3_key2, @@ -86,7 +73,7 @@ _nettle_umac_set_key (uint32_t *l1_key, uint32_t *l2_key, size = UMAC_BLOCK_SIZE / 4 + 4*(n-1); umac_kdf (aes, 1, size * sizeof(uint32_t), (uint8_t *) l1_key); - bswap32_if_le_n (size, l1_key); + bswap32_n_if_le (size, l1_key); size = 6*n; umac_kdf (aes, 2, size * sizeof(uint32_t), (uint8_t *) l2_key);