2023-10-04 Niels Möller <nisse@lysator.liu.se>
+ * bswap-internal.h (bswap32_if_be, bswap32_if_le): New macros.
+ * blowfish-bcrypt.c (bswap32_if_le_n): Rename, to not collide with
+ new macro.
+ (bswap32_if_le): ... old name, deleted.
+ * umac-set-key.c (bswap32_if_le_n): Define in the same way as for
+ bcrypt, replacing...
+ (BE_SWAP32_N): ...deleted macro.
+ * umac-l3.c (_nettle_umac_l3_init): Use bswap64_if_le.
+ * umac-l2.c (_nettle_umac_l2_init): Use bswap32_if_le.
+ * chacha-core-internal.c (_nettle_chacha_core): Use bswap32_if_be.
+ * salsa20-core-internal.c (_nettle_salsa20_core): Likewise
+
* umac-l2.c (_nettle_umac_l2_final): Delete redundant assignment.
2023-10-03 Niels Möller <nisse@lysator.liu.se>
};
#if WORDS_BIGENDIAN
-#define bswap32_if_le(x, n)
+#define bswap32_if_le_n(n, x)
#else
-static void bswap32_if_le (uint32_t *x, unsigned n)
+static void
+bswap32_if_le_n (unsigned n, uint32_t *x)
{
unsigned i;
for (i = 0; i < n; i++)
else if (lenscheme < HASHOFFSET)
return 0;
memcpy(psalt, data.binary.salt, BLOWFISH_BCRYPT_BINSALT_SIZE);
- bswap32_if_le (data.binary.salt, 4);
+ bswap32_if_le_n (4, data.binary.salt);
if (log2rounds < minlog2rounds || log2rounds > 31)
return 0;
dst = (uint8_t*)
encode_radix64((char*) dst, BLOWFISH_BCRYPT_BINSALT_SIZE, psalt) - 1;
- bswap32_if_le (data.binary.output, 6);
+ bswap32_if_le_n (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);
}
#endif
-#if WORDS_BIGENDIAN
-#define bswap64_if_le(x) (x)
-#else
-#define bswap64_if_le nettle_bswap64
-#endif
-
#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)
#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
#endif
#endif /* NETTLE_BSWAP_INTERNAL_H_INCLUDED */
#include "chacha.h"
#include "chacha-internal.h"
+#include "bswap-internal.h"
#include "macros.h"
/* For fat builds */
# define DEBUG(i)
#endif
-#ifdef WORDS_BIGENDIAN
-#define LE_SWAP32(v) \
- ((ROTL32(8, v) & 0x00FF00FFUL) | \
- (ROTL32(24, v) & 0xFF00FF00UL))
-#else
-#define LE_SWAP32(v) (v)
-#endif
-
#define QROUND(x0, x1, x2, x3) do { \
x0 = x0 + x1; x3 = ROTL32(16, (x0 ^ x3)); \
x2 = x2 + x3; x1 = ROTL32(12, (x1 ^ x2)); \
for (i = 0; i < _CHACHA_STATE_LENGTH; i++)
{
uint32_t t = x[i] + src[i];
- dst[i] = LE_SWAP32 (t);
+ dst[i] = bswap32_if_be (t);
}
}
#include "salsa20.h"
#include "salsa20-internal.h"
+#include "bswap-internal.h"
#include "macros.h"
/* For fat builds */
# define DEBUG(i)
#endif
-#ifdef WORDS_BIGENDIAN
-#define LE_SWAP32(v) \
- ((ROTL32(8, v) & 0x00FF00FFUL) | \
- (ROTL32(24, v) & 0xFF00FF00UL))
-#else
-#define LE_SWAP32(v) (v)
-#endif
-
#define QROUND(x0, x1, x2, x3) do { \
x1 ^= ROTL32(7, x0 + x3); \
x2 ^= ROTL32(9, x1 + x0); \
for (i = 0; i < _SALSA20_INPUT_LENGTH; i++)
{
uint32_t t = x[i] + src[i];
- dst[i] = LE_SWAP32 (t);
+ dst[i] = bswap32_if_be (t);
}
}
#include "umac.h"
#include "umac-internal.h"
-#include "macros.h"
+#include "bswap-internal.h"
/* Same mask applied to low and high halves */
#define KEY_MASK 0x01ffffffUL
-#if WORDS_BIGENDIAN
-#define BE_SWAP32(x) x
-#else
-#define BE_SWAP32(x) \
- ((ROTL32(8, x) & 0x00FF00FFUL) | \
- (ROTL32(24, x) & 0xFF00FF00UL))
-#endif
-
void
_nettle_umac_l2_init (unsigned size, uint32_t *k)
{
for (i = 0; i < size; i++)
{
uint32_t w = k[i];
- w = BE_SWAP32 (w);
+ w = bswap32_if_le (w);
k[i] = w & KEY_MASK;
}
}
#include "umac.h"
#include "umac-internal.h"
-#include "macros.h"
+#include "bswap-internal.h"
/* 2^36 - 5 */
#define P 0x0000000FFFFFFFFBULL
-#if WORDS_BIGENDIAN
-#define BE_SWAP64(x) x
-#else
-#define BE_SWAP64(x) \
- (((x & 0xff) << 56) \
- | ((x & 0xff00) << 40) \
- | ((x & 0xff0000) << 24) \
- | ((x & 0xff000000) << 8) \
- | ((x >> 8) & 0xff000000) \
- | ((x >> 24) & 0xff0000) \
- | ((x >> 40) & 0xff00) \
- | (x >> 56) )
-#endif
-
void
_nettle_umac_l3_init (unsigned size, uint64_t *k)
{
for (i = 0; i < size; i++)
{
uint64_t w = k[i];
- w = BE_SWAP64 (w);
+ w = bswap64_if_le (w);
k[i] = w % P;
}
}
uint32_t y = (umac_l3_word (key, m[0])
+ umac_l3_word (key + 4, m[1])) % P;
-#if !WORDS_BIGENDIAN
- y = ((ROTL32(8, y) & 0x00FF00FFUL)
- | (ROTL32(24, y) & 0xFF00FF00UL));
-#endif
- return y;
+ return bswap32_if_le (y);
}
#include "umac-internal.h"
#include "macros.h"
+#include "bswap-internal.h"
static void
umac_kdf (struct aes128_ctx *aes, unsigned index, unsigned length, uint8_t *dst)
}
#if WORDS_BIGENDIAN
-#define BE_SWAP32(x) x
-#define BE_SWAP32_N(n, x)
+/* FIXME: Duplicated with blowfish-bcrypt.c. */
+#define bswap32_if_le_n(n, x)
#else
-#define BE_SWAP32(x) \
- ((ROTL32(8, x) & 0x00FF00FFUL) | \
- (ROTL32(24, x) & 0xFF00FF00UL))
-#define BE_SWAP32_N(n, x) do { \
- unsigned be_i; \
- for (be_i = 0; be_i < n; be_i++) \
- { \
- uint32_t be_x = (x)[be_i]; \
- (x)[be_i] = BE_SWAP32 (be_x); \
- } \
- } while (0)
+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
size = UMAC_BLOCK_SIZE / 4 + 4*(n-1);
umac_kdf (aes, 1, size * sizeof(uint32_t), (uint8_t *) l1_key);
- BE_SWAP32_N (size, l1_key);
+ bswap32_if_le_n (size, l1_key);
size = 6*n;
umac_kdf (aes, 2, size * sizeof(uint32_t), (uint8_t *) l2_key);