]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Let umac and bcrypt share bswap helper function.
authorNiels Möller <nisse@lysator.liu.se>
Fri, 29 Mar 2024 15:04:09 +0000 (16:04 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 29 Mar 2024 15:04:09 +0000 (16:04 +0100)
ChangeLog
blowfish-bcrypt.c
bswap-internal.h
umac-set-key.c

index 0efcfa1329a0ce0816cc9df58923f3a29d4f4bf2..c623daec4f04cedf205e5d54668ac359f411c6f2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-03-29  Niels Möller  <nisse@lysator.liu.se>
+
+       * 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  <nisse@lysator.liu.se>
 
        * sha512-224-meta.c (nettle_sha512_224): Change name to
index ee7c0eb859d891048099754fdf09cab0371c6e3a..b590748eeab690e4d57cbc45dcfde34c934d72d6 100644 (file)
@@ -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);
index 30af52214cc2fbf86aec306f3ac859c3e174d975..148cdc8b58ed7a7f26ea0d650635183517a9a20d 100644 (file)
@@ -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 */
index 9f7bfe60f0641a665514619ee19d13ee239e8fc8..7082f93057ef09c8fbeb8bf8211f444855426d37 100644 (file)
@@ -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);