diff --git a/lib/isc/include/isc/endian.h b/lib/isc/include/isc/endian.h index be91b1dcfa..6d5e6c2b4f 100644 --- a/lib/isc/include/isc/endian.h +++ b/lib/isc/include/isc/endian.h @@ -164,3 +164,101 @@ #endif /* WORDS_BIGENDIAN */ #endif /* !htobe64 */ + +/* + * Macros to convert uint8_t arrays to integers. + */ + +/* Low-Endian */ + +#define ISC_U16TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); + +#define ISC_U8TO16_LE(p) (((uint16_t)((p)[0])) | ((uint16_t)((p)[1]) << 8)) + +#define ISC_U32TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); + +#define ISC_U8TO32_LE(p) \ + (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \ + ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24)) + +#define ISC_U48TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); \ + (p)[4] = (uint8_t)((v) >> 32); \ + (p)[5] = (uint8_t)((v) >> 40); + +#define ISC_U8TO48_LE(p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ + ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ + ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40)) + +#define ISC_U64TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); \ + (p)[4] = (uint8_t)((v) >> 32); \ + (p)[5] = (uint8_t)((v) >> 40); \ + (p)[6] = (uint8_t)((v) >> 48); \ + (p)[7] = (uint8_t)((v) >> 56); + +#define ISC_U8TO64_LE(p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ + ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ + ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ + ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) + +/* Big-Endian */ + +#define ISC_U16TO8_BE(p, v) \ + (p)[0] = (uint8_t)((v) >> 8); \ + (p)[1] = (uint8_t)((v)); + +#define ISC_U8TO16_BE(p) (((uint16_t)((p)[0]) << 8) | ((uint16_t)((p)[1]))) + +#define ISC_U32TO8_BE(p, v) \ + (p)[0] = (uint8_t)((v) >> 24); \ + (p)[1] = (uint8_t)((v) >> 16); \ + (p)[2] = (uint8_t)((v) >> 8); \ + (p)[3] = (uint8_t)((v)); + +#define ISC_U8TO32_BE(p) \ + (((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \ + ((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3]))) + +#define ISC_U48TO8_BE(p, v) \ + (p)[0] = (uint8_t)((v) >> 40); \ + (p)[1] = (uint8_t)((v) >> 32); \ + (p)[2] = (uint8_t)((v) >> 24); \ + (p)[3] = (uint8_t)((v) >> 16); \ + (p)[4] = (uint8_t)((v) >> 8); \ + (p)[5] = (uint8_t)((v)); + +#define ISC_U8TO48_BE(p) \ + (((uint64_t)((p)[0]) << 40) | ((uint64_t)((p)[1]) << 32) | \ + ((uint64_t)((p)[2]) << 24) | ((uint64_t)((p)[3]) << 16) | \ + ((uint64_t)((p)[4]) << 8) | ((uint64_t)((p)[5]))) + +#define ISC_U64TO8_BE(p, v) \ + (p)[0] = (uint8_t)((v) >> 56); \ + (p)[1] = (uint8_t)((v) >> 48); \ + (p)[2] = (uint8_t)((v) >> 40); \ + (p)[3] = (uint8_t)((v) >> 32); \ + (p)[4] = (uint8_t)((v) >> 24); \ + (p)[5] = (uint8_t)((v) >> 16); \ + (p)[6] = (uint8_t)((v) >> 8); \ + (p)[7] = (uint8_t)((v)); + +#define ISC_U8TO64_BE(p) \ + (((uint64_t)((p)[0]) << 56) | ((uint64_t)((p)[1]) << 48) | \ + ((uint64_t)((p)[2]) << 40) | ((uint64_t)((p)[3]) << 32) | \ + ((uint64_t)((p)[4]) << 24) | ((uint64_t)((p)[5]) << 16) | \ + ((uint64_t)((p)[6]) << 8) | ((uint64_t)((p)[7]))) diff --git a/lib/isc/siphash.c b/lib/isc/siphash.c index 8d13c61243..179e9f31a8 100644 --- a/lib/isc/siphash.c +++ b/lib/isc/siphash.c @@ -67,29 +67,9 @@ #define HALFSIPROUND FULL_ROUND32 -#define U32TO8_LE(p, v) \ - (p)[0] = (uint8_t)((v)); \ - (p)[1] = (uint8_t)((v) >> 8); \ - (p)[2] = (uint8_t)((v) >> 16); \ - (p)[3] = (uint8_t)((v) >> 24); - -#define U8TO32_LE(p) \ - (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \ - ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24)) - #define U8TO32_ONE(case_sensitive, byte) \ (uint32_t)(case_sensitive ? byte : isc__ascii_tolower1(byte)) -#define U64TO8_LE(p, v) \ - U32TO8_LE((p), (uint32_t)((v))); \ - U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); - -#define U8TO64_LE(p) \ - (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ - ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ - ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ - ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) - #define U8TO64_ONE(case_sensitive, byte) \ (uint64_t)(case_sensitive ? byte : isc__ascii_tolower1(byte)) @@ -99,8 +79,8 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, REQUIRE(k != NULL); REQUIRE(out != NULL); - uint64_t k0 = U8TO64_LE(k); - uint64_t k1 = U8TO64_LE(k + 8); + uint64_t k0 = ISC_U8TO64_LE(k); + uint64_t k1 = ISC_U8TO64_LE(k + 8); uint64_t v0 = UINT64_C(0x736f6d6570736575) ^ k0; uint64_t v1 = UINT64_C(0x646f72616e646f6d) ^ k1; @@ -113,8 +93,9 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, const size_t left = inlen & 7; for (; in != end; in += 8) { - uint64_t m = case_sensitive ? U8TO64_LE(in) - : isc_ascii_tolower8(U8TO64_LE(in)); + uint64_t m = case_sensitive + ? ISC_U8TO64_LE(in) + : isc_ascii_tolower8(ISC_U8TO64_LE(in)); v3 ^= m; @@ -169,7 +150,7 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, b = v0 ^ v1 ^ v2 ^ v3; - U64TO8_LE(out, b); + ISC_U64TO8_LE(out, b); } void @@ -178,8 +159,8 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, REQUIRE(k != NULL); REQUIRE(out != NULL); - uint32_t k0 = U8TO32_LE(k); - uint32_t k1 = U8TO32_LE(k + 4); + uint32_t k0 = ISC_U8TO32_LE(k); + uint32_t k1 = ISC_U8TO32_LE(k + 4); uint32_t v0 = UINT32_C(0x00000000) ^ k0; uint32_t v1 = UINT32_C(0x00000000) ^ k1; @@ -192,8 +173,9 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, const int left = inlen & 3; for (; in != end; in += 4) { - uint32_t m = case_sensitive ? U8TO32_LE(in) - : isc_ascii_tolower4(U8TO32_LE(in)); + uint32_t m = case_sensitive + ? ISC_U8TO32_LE(in) + : isc_ascii_tolower4(ISC_U8TO32_LE(in)); v3 ^= m; @@ -235,5 +217,5 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen, } b = v1 ^ v3; - U32TO8_LE(out, b); + ISC_U32TO8_LE(out, b); }