2010-03-24 06:21:39 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
2010-03-24 06:21:39 +08: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: |
|
2021-05-06 18:16:35 +08:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
2010-03-24 06:21:39 +08:00
|
|
|
| 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: Michael Maclean <mgdm@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PHP_HASH_FNV_H
|
|
|
|
#define PHP_HASH_FNV_H
|
|
|
|
|
2016-01-29 20:47:47 +08:00
|
|
|
#define PHP_FNV1_32_INIT ((uint32_t)0x811c9dc5)
|
2010-03-24 06:21:39 +08:00
|
|
|
|
2016-01-29 20:47:47 +08:00
|
|
|
#define PHP_FNV_32_PRIME ((uint32_t)0x01000193)
|
2010-03-24 06:21:39 +08:00
|
|
|
|
2016-01-29 20:47:47 +08:00
|
|
|
#define PHP_FNV1_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
|
2010-03-24 06:21:39 +08:00
|
|
|
|
2016-01-29 20:47:47 +08:00
|
|
|
#define PHP_FNV_64_PRIME ((uint64_t)0x100000001b3ULL)
|
2010-03-24 06:21:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hash types
|
|
|
|
*/
|
|
|
|
enum php_fnv_type {
|
|
|
|
PHP_FNV_NONE = 0, /* invalid FNV hash type */
|
|
|
|
PHP_FNV0_32 = 1, /* FNV-0 32 bit hash */
|
|
|
|
PHP_FNV1_32 = 2, /* FNV-1 32 bit hash */
|
|
|
|
PHP_FNV1a_32 = 3, /* FNV-1a 32 bit hash */
|
|
|
|
PHP_FNV0_64 = 4, /* FNV-0 64 bit hash */
|
|
|
|
PHP_FNV1_64 = 5, /* FNV-1 64 bit hash */
|
|
|
|
PHP_FNV1a_64 = 6, /* FNV-1a 64 bit hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2016-01-29 20:47:47 +08:00
|
|
|
uint32_t state;
|
2010-03-24 06:21:39 +08:00
|
|
|
} PHP_FNV132_CTX;
|
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 20:29:42 +08:00
|
|
|
#define PHP_FNV132_SPEC "l."
|
2010-03-24 06:21:39 +08:00
|
|
|
|
|
|
|
typedef struct {
|
2016-01-29 20:47:47 +08:00
|
|
|
uint64_t state;
|
2010-03-24 06:21:39 +08:00
|
|
|
} PHP_FNV164_CTX;
|
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 20:29:42 +08:00
|
|
|
#define PHP_FNV164_SPEC "q."
|
2010-03-24 06:21:39 +08:00
|
|
|
|
|
|
|
|
2020-11-03 03:00:28 +08:00
|
|
|
PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args);
|
2018-10-03 17:32:57 +08:00
|
|
|
PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen);
|
|
|
|
PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen);
|
2021-03-23 04:17:43 +08:00
|
|
|
PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context);
|
2010-03-24 06:21:39 +08:00
|
|
|
|
2020-11-03 03:00:28 +08:00
|
|
|
PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args);
|
2018-10-03 17:32:57 +08:00
|
|
|
PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen);
|
|
|
|
PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen);
|
2021-03-23 04:17:43 +08:00
|
|
|
PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context);
|
2010-03-24 06:21:39 +08:00
|
|
|
|
2016-01-29 20:47:47 +08:00
|
|
|
static uint32_t fnv_32_buf(void *buf, size_t len, uint32_t hval, int alternate);
|
|
|
|
static uint64_t fnv_64_buf(void *buf, size_t len, uint64_t hval, int alternate);
|
2010-03-24 06:21:39 +08:00
|
|
|
|
|
|
|
#endif
|