2010-03-24 15:47:40 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 09:03:12 +00:00
|
|
|
| Copyright (c) The PHP Group |
|
2010-03-24 15:47:40 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_01.txt |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Martin Jansen <mj@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Implements Jenkins's one-at-a-time hashing algorithm as presented on
|
|
|
|
* http://www.burtleburtle.net/bob/hash/doobs.html.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "php_hash.h"
|
|
|
|
#include "php_hash_joaat.h"
|
|
|
|
|
|
|
|
const php_hash_ops php_hash_joaat_ops = {
|
Make HashContexts serializable.
* Modify php_hash_ops to contain the algorithm name and
serialize and unserialize methods.
* Implement __serialize and __unserialize magic methods on
HashContext.
Note that serialized HashContexts are not necessarily portable
between PHP versions or from architecture to architecture.
(Most are, though Keccak and slow SHA3s are not.)
An exception is thrown when an unsupported serialization is
attempted.
Because of security concerns, HASH_HMAC contexts are not
currently serializable; attempting to serialize one throws
an exception.
Serialization exposes the state of HashContext memory, so ensure
that memory is zeroed before use by allocating it with a new
php_hash_alloc_context function. Performance impact is
negligible.
Some hash internal states have logical pointers into a buffer,
or sponge, that absorbs input provided in bytes rather than
chunks. The unserialize functions for these hash functions
must validate that the logical pointers are all within bounds,
lest future hash operations cause out-of-bounds memory accesses.
* Adler32, CRC32, FNV, joaat: simple state, no buffer positions
* Gost, MD2, SHA3, Snefru, Tiger, Whirlpool: buffer positions
must be validated
* MD4, MD5, SHA1, SHA2, haval, ripemd: buffer positions encoded
bitwise, forced to within bounds on use; no need to validate
2020-06-08 12:29:42 +00:00
|
|
|
"joaat",
|
2010-03-24 15:47:40 +00:00
|
|
|
(php_hash_init_func_t) PHP_JOAATInit,
|
|
|
|
(php_hash_update_func_t) PHP_JOAATUpdate,
|
|
|
|
(php_hash_final_func_t) PHP_JOAATFinal,
|
Make HashContexts serializable.
* Modify php_hash_ops to contain the algorithm name and
serialize and unserialize methods.
* Implement __serialize and __unserialize magic methods on
HashContext.
Note that serialized HashContexts are not necessarily portable
between PHP versions or from architecture to architecture.
(Most are, though Keccak and slow SHA3s are not.)
An exception is thrown when an unsupported serialization is
attempted.
Because of security concerns, HASH_HMAC contexts are not
currently serializable; attempting to serialize one throws
an exception.
Serialization exposes the state of HashContext memory, so ensure
that memory is zeroed before use by allocating it with a new
php_hash_alloc_context function. Performance impact is
negligible.
Some hash internal states have logical pointers into a buffer,
or sponge, that absorbs input provided in bytes rather than
chunks. The unserialize functions for these hash functions
must validate that the logical pointers are all within bounds,
lest future hash operations cause out-of-bounds memory accesses.
* Adler32, CRC32, FNV, joaat: simple state, no buffer positions
* Gost, MD2, SHA3, Snefru, Tiger, Whirlpool: buffer positions
must be validated
* MD4, MD5, SHA1, SHA2, haval, ripemd: buffer positions encoded
bitwise, forced to within bounds on use; no need to validate
2020-06-08 12:29:42 +00:00
|
|
|
php_hash_copy,
|
|
|
|
php_hash_serialize,
|
|
|
|
php_hash_unserialize,
|
|
|
|
PHP_JOAAT_SPEC,
|
2010-03-24 15:47:40 +00:00
|
|
|
4,
|
|
|
|
4,
|
2017-01-16 13:51:13 +00:00
|
|
|
sizeof(PHP_JOAAT_CTX),
|
|
|
|
0
|
2010-03-24 15:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context)
|
|
|
|
{
|
|
|
|
context->state = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:32:57 +00:00
|
|
|
PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
|
2010-03-24 15:47:40 +00:00
|
|
|
{
|
2010-03-28 10:01:02 +00:00
|
|
|
context->state = joaat_buf((void *)input, inputLen, context->state);
|
2010-03-24 15:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
|
|
|
|
{
|
Correct implementation of joaat hash.
Before this commit, the result produced by a joaat hash depended
on how the input data was chunked. A hash produced by multiple
`hash_update` operations was incorrect. For example, this code,
which should produce three identical lines:
var_dump(hash("joaat", "abcd"));
$hash = hash_init("joaat");
hash_update($hash, "ab");
hash_update($hash, "cd");
var_dump(hash_final($hash));
$hash = hash_init("joaat");
hash_update($hash, "abc");
hash_update($hash, "d");
var_dump(hash_final($hash));
instead produced:
string(8) "cd8b6206"
string(8) "e590d137"
string(8) "2d59d087"
This is because the finalization step, involving shift operations
and adds, was applied on every chunk, rather than once at the end
as is required by the hash definition.
After this commit, the code above produces:
string(8) "cd8b6206"
string(8) "cd8b6206"
string(8) "cd8b6206"
as expected.
Some tests encoded the wrong behavior and were corrected.
Closes GH-5749
2020-06-21 23:54:39 +00:00
|
|
|
uint32_t hval = context->state;
|
|
|
|
hval += (hval << 3);
|
|
|
|
hval ^= (hval >> 11);
|
|
|
|
hval += (hval << 15);
|
|
|
|
|
2010-03-24 15:47:40 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
Correct implementation of joaat hash.
Before this commit, the result produced by a joaat hash depended
on how the input data was chunked. A hash produced by multiple
`hash_update` operations was incorrect. For example, this code,
which should produce three identical lines:
var_dump(hash("joaat", "abcd"));
$hash = hash_init("joaat");
hash_update($hash, "ab");
hash_update($hash, "cd");
var_dump(hash_final($hash));
$hash = hash_init("joaat");
hash_update($hash, "abc");
hash_update($hash, "d");
var_dump(hash_final($hash));
instead produced:
string(8) "cd8b6206"
string(8) "e590d137"
string(8) "2d59d087"
This is because the finalization step, involving shift operations
and adds, was applied on every chunk, rather than once at the end
as is required by the hash definition.
After this commit, the code above produces:
string(8) "cd8b6206"
string(8) "cd8b6206"
string(8) "cd8b6206"
as expected.
Some tests encoded the wrong behavior and were corrected.
Closes GH-5749
2020-06-21 23:54:39 +00:00
|
|
|
memcpy(digest, &hval, 4);
|
2010-03-24 15:47:40 +00:00
|
|
|
#else
|
|
|
|
int i = 0;
|
Correct implementation of joaat hash.
Before this commit, the result produced by a joaat hash depended
on how the input data was chunked. A hash produced by multiple
`hash_update` operations was incorrect. For example, this code,
which should produce three identical lines:
var_dump(hash("joaat", "abcd"));
$hash = hash_init("joaat");
hash_update($hash, "ab");
hash_update($hash, "cd");
var_dump(hash_final($hash));
$hash = hash_init("joaat");
hash_update($hash, "abc");
hash_update($hash, "d");
var_dump(hash_final($hash));
instead produced:
string(8) "cd8b6206"
string(8) "e590d137"
string(8) "2d59d087"
This is because the finalization step, involving shift operations
and adds, was applied on every chunk, rather than once at the end
as is required by the hash definition.
After this commit, the code above produces:
string(8) "cd8b6206"
string(8) "cd8b6206"
string(8) "cd8b6206"
as expected.
Some tests encoded the wrong behavior and were corrected.
Closes GH-5749
2020-06-21 23:54:39 +00:00
|
|
|
unsigned char *c = (unsigned char *) &hval;
|
2010-03-24 15:47:40 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
digest[i] = c[3 - i];
|
|
|
|
}
|
|
|
|
#endif
|
Correct implementation of joaat hash.
Before this commit, the result produced by a joaat hash depended
on how the input data was chunked. A hash produced by multiple
`hash_update` operations was incorrect. For example, this code,
which should produce three identical lines:
var_dump(hash("joaat", "abcd"));
$hash = hash_init("joaat");
hash_update($hash, "ab");
hash_update($hash, "cd");
var_dump(hash_final($hash));
$hash = hash_init("joaat");
hash_update($hash, "abc");
hash_update($hash, "d");
var_dump(hash_final($hash));
instead produced:
string(8) "cd8b6206"
string(8) "e590d137"
string(8) "2d59d087"
This is because the finalization step, involving shift operations
and adds, was applied on every chunk, rather than once at the end
as is required by the hash definition.
After this commit, the code above produces:
string(8) "cd8b6206"
string(8) "cd8b6206"
string(8) "cd8b6206"
as expected.
Some tests encoded the wrong behavior and were corrected.
Closes GH-5749
2020-06-21 23:54:39 +00:00
|
|
|
context->state = 0;
|
2010-03-24 15:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer
|
|
|
|
*
|
|
|
|
* input:
|
|
|
|
* buf - start of buffer to hash
|
|
|
|
* len - length of buffer in octets
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 32 bit hash as a static hash type
|
|
|
|
*/
|
2016-01-29 12:47:47 +00:00
|
|
|
static uint32_t
|
|
|
|
joaat_buf(void *buf, size_t len, uint32_t hval)
|
2010-03-24 15:47:40 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned char *input = (unsigned char *)buf;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2010-03-24 15:47:40 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
hval += input[i];
|
|
|
|
hval += (hval << 10);
|
|
|
|
hval ^= (hval >> 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hval;
|
|
|
|
}
|