Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions ext/hash/hash_gost.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,12 @@ PHP_HASH_API void PHP_GOSTInitCrypto(PHP_GOST_CTX *context, ZEND_ATTRIBUTE_UNUSE
context->tables = &tables_crypto;
}

static const uint32_t MAX32 = 0xffffffffLU;

PHP_HASH_API void PHP_GOSTUpdate(PHP_GOST_CTX *context, const unsigned char *input, size_t len)
{
if ((MAX32 - context->count[0]) < (len * 8)) {
context->count[1]++;
context->count[0] = MAX32 - context->count[0];
context->count[0] = (len * 8) - context->count[0];
} else {
context->count[0] += len * 8;
}
uint64_t count = (((uint64_t) context->count[1] << 32) | context->count[0]) + (uint64_t) len * 8;

context->count[0] = (uint32_t) count;
context->count[1] = (uint32_t) (count >> 32);

if (context->length + len < 32) {
memcpy(&context->buffer[context->length], input, len);
Expand Down
13 changes: 4 additions & 9 deletions ext/hash/hash_snefru.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,12 @@ PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context, ZEND_ATTRIBUTE_UNUSED
memset(context, 0, sizeof(*context));
}

static const uint32_t MAX32 = 0xffffffffLU;

PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
{
if ((MAX32 - context->count[1]) < (len * 8)) {
context->count[0]++;
context->count[1] = MAX32 - context->count[1];
context->count[1] = ((uint32_t) len * 8) - context->count[1];
} else {
context->count[1] += (uint32_t) len * 8;
}
uint64_t count = (((uint64_t) context->count[0] << 32) | context->count[1]) + (uint64_t) len * 8;

context->count[0] = (uint32_t) (count >> 32);
context->count[1] = (uint32_t) count;

if (context->length + len < 32) {
memcpy(&context->buffer[context->length], input, len);
Expand Down
29 changes: 29 additions & 0 deletions ext/hash/tests/gost_snefru_512mib.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Hash: gost and snefru with 512 MiB of input (bit counter carry)
--SKIPIF--
<?php
if (!getenv('RUN_RESOURCE_HEAVY_TESTS')) die('skip resource-heavy test');
if (getenv('SKIP_SLOW_TESTS')) die('skip slow tests excluded by request');
?>
--INI--
memory_limit=-1
--FILE--
<?php
/* 2^29 bytes are 2^32 bits, so the low word of the bit counter wraps. */
$chunk = str_repeat('a', 1 << 20);
foreach (['gost', 'snefru'] as $algo) {
$ctx = hash_init($algo);
for ($i = 0; $i < 512; $i++) {
hash_update($ctx, $chunk);
}
echo $algo, ': ', hash_final($ctx), "\n";
}
unset($chunk);

/* A single update of 2^29 bytes. */
echo 'snefru: ', hash('snefru', str_repeat('a', 1 << 29)), "\n";
?>
--EXPECT--
gost: 6c30acc180f544a79c22fa763593a812aec721dd61658b1a5e9a6f3a7955146d
snefru: 0842a8e735ab08a30149e7e6e34833f365291978ee68c37a5135253b70afa486
snefru: 0842a8e735ab08a30149e7e6e34833f365291978ee68c37a5135253b70afa486
Loading