diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index 3b56270..0090dba 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -31,7 +31,8 @@ const uint16_t SLUNKCRYPT_VERSION_PATCH = MY_VERSION_PATCH; const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__; /* Const */ -#define MAGIC_PRIME 0x00000100000001B3ull +#define HASH_MAGIC_PRIME 0x00000100000001B3ull +#define HASH_OFFSET_BASE 0xCBF29CE484222325ull // ========================================================================== // Data structures @@ -112,7 +113,7 @@ static FORCE_INLINE void hash_update_str(uint64_t* const hash, const uint8_t* co size_t i; for (i = 0U; i < data_len; ++i) { - *hash = ((*hash) ^ data[i]) * MAGIC_PRIME; + *hash = ((*hash) ^ data[i]) * HASH_MAGIC_PRIME; } } @@ -121,7 +122,7 @@ static FORCE_INLINE void hash_update_u64(uint64_t* const hash, const uint64_t va size_t i; for (i = 0U; i < sizeof(uint64_t); ++i) { - *hash = ((*hash) ^ byte_u64(value, i)) * MAGIC_PRIME; + *hash = ((*hash) ^ byte_u64(value, i)) * HASH_MAGIC_PRIME; } } @@ -130,15 +131,23 @@ static FORCE_INLINE void hash_update_u16(uint64_t* const hash, const uint16_t va size_t i; for (i = 0U; i < sizeof(uint16_t); ++i) { - *hash = ((*hash) ^ byte_u16(value, i)) * MAGIC_PRIME; + *hash = ((*hash) ^ byte_u16(value, i)) * HASH_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_init(const uint64_t salt, const uint16_t i, const uint8_t* const data, const size_t data_len) { - uint64_t hash = 0xCBF29CE484222325ull; + uint64_t hash = HASH_OFFSET_BASE; + hash_update_u64(&hash, salt); + hash_update_u16(&hash, i); + hash_update_str(&hash, data, data_len); + return hash; +} + +static uint64_t hash_code_next(const uint64_t salt, const uint8_t* const data, const size_t data_len) +{ + uint64_t hash = HASH_OFFSET_BASE; hash_update_u64(&hash, salt); - hash_update_u16(&hash, pepper); hash_update_str(&hash, data, data_len); return hash; } @@ -147,14 +156,15 @@ static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint // Key derivation // ========================================================================== -static FORCE_INLINE uint64_t keygen_loop(uint64_t value, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len) +static FORCE_INLINE uint64_t keygen_loop(uint64_t salt, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len) { + uint64_t result = salt = hash_code_init(salt, pepper, passwd, passwd_len); size_t i; - for (i = 0U; i < 99971U; ++i) + for (i = 1U; i < 99971U; ++i) { - value ^= hash_code(value, pepper, passwd, passwd_len); + result ^= salt = hash_code_next(salt, passwd, passwd_len); } - return value; + return result; } static void generate_key(key_data_t *const key, const uint64_t salt, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len)