Replaced some endianness-specific code with endianness-agnostic functions.

This commit is contained in:
LoRd_MuldeR 2020-10-24 21:06:37 +02:00
parent ec7176feba
commit b577afba49
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

View File

@ -9,6 +9,7 @@
#include <slunkcrypt.h> #include <slunkcrypt.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "version.h" #include "version.h"
#ifdef _MSC_VER #ifdef _MSC_VER
@ -30,6 +31,9 @@ const uint16_t SLUNKCRYPT_VERSION_MINOR = MY_VERSION_MINOR;
const uint16_t SLUNKCRYPT_VERSION_PATCH = MY_VERSION_PATCH; const uint16_t SLUNKCRYPT_VERSION_PATCH = MY_VERSION_PATCH;
const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__; const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__;
/* Const */
#define MAGIC_PRIME 0x00000100000001B3ull
// ========================================================================== // ==========================================================================
// Data structures // Data structures
// ========================================================================== // ==========================================================================
@ -74,26 +78,67 @@ volatile int g_slunkcrypt_abort_flag = 0;
} \ } \
while (0) while (0)
// ==========================================================================
// Byte access (endianness agnostic)
// ==========================================================================
static FORCE_INLINE uint32_t lower_u64(const uint64_t value)
{
return (uint32_t)(value & 0xFFFFFFFF);
}
static FORCE_INLINE uint32_t upper_u64(const uint64_t value)
{
return (uint32_t)((value > 32U) & 0xFFFFFFFF);
}
static FORCE_INLINE uint8_t byte_u16(const uint16_t value, const size_t off)
{
assert(index < sizeof(uint16_t));
return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF);
}
static FORCE_INLINE uint8_t byte_u64(const uint64_t value, const size_t off)
{
assert(index < sizeof(uint64_t));
return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF);
}
// ========================================================================== // ==========================================================================
// Hash function // Hash function
// ========================================================================== // ==========================================================================
static FORCE_INLINE void hash_update(uint64_t *const h, const uint8_t* const data, const size_t data_len) static FORCE_INLINE void hash_update_str(uint64_t* const hash, const uint8_t* const data, const size_t data_len)
{ {
for (size_t i = 0U; i < data_len; ++i) for (size_t i = 0U; i < data_len; ++i)
{ {
*h ^= data[i]; *hash = ((*hash) ^ data[i]) * MAGIC_PRIME;
*h *= 0x00000100000001B3ull; }
}
static FORCE_INLINE void hash_update_u64(uint64_t* const hash, const uint64_t value)
{
for (size_t i = 0U; i < sizeof(uint64_t); ++i)
{
*hash = ((*hash) ^ byte_u64(value, i)) * MAGIC_PRIME;
}
}
static FORCE_INLINE void hash_update_u16(uint64_t* const hash, const uint16_t value)
{
for (size_t i = 0U; i < sizeof(uint16_t); ++i)
{
*hash = ((*hash) ^ byte_u16(value, i)) * MAGIC_PRIME;
} }
} }
static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint8_t* const data, const size_t data_len) static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint8_t* const data, const size_t data_len)
{ {
uint64_t h = 0xCBF29CE484222325ull; uint64_t hash = 0xCBF29CE484222325ull;
hash_update(&h, (uint8_t*)&salt, sizeof(uint64_t)); hash_update_u64(&hash, salt);
hash_update(&h, (uint8_t*)&pepper, sizeof(uint16_t)); hash_update_u16(&hash, pepper);
hash_update(&h, data, data_len); hash_update_str(&hash, data, data_len);
return h; return hash;
} }
// ========================================================================== // ==========================================================================
@ -122,10 +167,10 @@ static void generate_key(key_data_t *const key, const uint64_t salt, const uint1
static void random_init(rand_state_t* const state, const uint64_t seed_0, const uint64_t seed_1) static void random_init(rand_state_t* const state, const uint64_t seed_0, const uint64_t seed_1)
{ {
slunkcrypt_bzero(state, sizeof(rand_state_t)); slunkcrypt_bzero(state, sizeof(rand_state_t));
state->a = (uint32_t)( seed_0 & 0xFFFFFFFF); state->a = lower_u64(seed_0);
state->b = (uint32_t)((seed_0 >> 32) & 0xFFFFFFFF); state->b = upper_u64(seed_0);
state->c = (uint32_t)( seed_1 & 0xFFFFFFFF); state->c = lower_u64(seed_1);
state->d = (uint32_t)((seed_1 >> 32) & 0xFFFFFFFF); state->d = upper_u64(seed_1);
} }
static uint32_t random_next(rand_state_t *const state) static uint32_t random_next(rand_state_t *const state)