23#if (defined(__APPLE__) || defined(__APPLE_CC__)) && \
24 (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
25# if defined(__LITTLE_ENDIAN__)
26# undef WORDS_BIGENDIAN
28# if defined(__BIG_ENDIAN__)
29# define WORDS_BIGENDIAN
34static inline uint64_t rol64(uint64_t
v,
unsigned char b) {
35 return (
v << b) | (
v >> (64 - b));
37static inline unsigned char idx(
unsigned char x,
unsigned char y) {
42static inline uint64_t load64(
const unsigned char* x) {
45 for (i = 7; i >= 0; --i) {
51static inline void store64(
unsigned char* x, uint64_t
val) {
53 for (i = 0; i < 8; ++i) {
58static inline void xor64(
unsigned char* x, uint64_t
val) {
60 for (i = 0; i < 8; ++i) {
65# define readLane(x, y) load64(ctx->state+sizeof(uint64_t)*idx(x, y))
66# define writeLane(x, y, v) store64(ctx->state+sizeof(uint64_t)*idx(x, y), v)
67# define XORLane(x, y, v) xor64(ctx->state+sizeof(uint64_t)*idx(x, y), v)
69# define readLane(x, y) (((uint64_t*)ctx->state)[idx(x,y)])
70# define writeLane(x, y, v) (((uint64_t*)ctx->state)[idx(x,y)] = v)
71# define XORLane(x, y, v) (((uint64_t*)ctx->state)[idx(x,y)] ^= v)
74static inline char LFSR86540(
unsigned char* pLFSR)
76 unsigned char LFSR = *pLFSR;
80 LFSR = (LFSR << 1) ^ 0x71;
89 unsigned char LFSRstate = 0x01;
96 for (x = 0; x < 5; ++x) {
97 C[x] = readLane(x, 0) ^ readLane(x, 1) ^
98 readLane(x, 2) ^ readLane(x, 3) ^ readLane(x, 4);
100 for (x = 0; x < 5; ++x) {
101 D =
C[(x+4)%5] ^ rol64(
C[(x+1)%5], 1);
102 for (y = 0; y < 5; ++y) {
109 unsigned char x = 1, y = 0, t;
110 uint64_t
current = readLane(x, y);
111 for (t = 0; t < 24; ++t) {
112 unsigned char r = ((t + 1) * (t + 2) / 2) % 64;
113 unsigned char Y = (2*x + 3*y) % 5;
117 temp = readLane(x, y);
118 writeLane(x, y, rol64(
current, r));
125 for (y = 0; y < 5; ++y) {
127 for (x = 0; x < 5; ++x) {
128 temp[x] = readLane(x, y);
130 for (x = 0; x < 5; ++x) {
131 writeLane(x, y, temp[x] ^((~temp[(x+1)%5]) & temp[(x+2)%5]));
138 for (
j = 0;
j < 7; ++
j) {
139 if (LFSR86540(&LFSRstate)) {
140 uint64_t bitPos = (1<<
j) - 1;
141 XORLane(0, 0, (uint64_t)1 << bitPos);
156 const unsigned char*
buf,
160 size_t len = block_size - ctx->pos;
172 if (ctx->pos >= block_size) {
179static void PHP_SHA3_Final(
unsigned char* digest,
182 size_t digest_size) {
183 size_t len = digest_size;
186 ctx->
state[ctx->pos++] ^= 0x06;
187 ctx->
state[block_size-1] ^= 0x80;
192 int bs = (
len < block_size) ?
len : block_size;
193 digest = zend_mempcpy(digest, ctx->
state, bs);
212 && ctx->pos < block_size) {
215 return r !=
SUCCESS ? r : -2000;
221#define DECLARE_SHA3_OPS(bits) \
222void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \
223 PHP_SHA3_Init(ctx, bits); \
225void PHP_SHA3##bits##Update(PHP_SHA3_##bits##_CTX* ctx, \
226 const unsigned char* input, \
228 PHP_SHA3_Update(ctx, input, inputLen, \
229 (1600 - (2 * bits)) >> 3); \
231void PHP_SHA3##bits##Final(unsigned char* digest, \
232 PHP_SHA3_##bits##_CTX* ctx) { \
233 PHP_SHA3_Final(digest, ctx, \
234 (1600 - (2 * bits)) >> 3, \
237static int php_sha3##bits##_unserialize(php_hashcontext_object *hash, \
240 return php_sha3_unserialize(hash, magic, zv, (1600 - (2 * bits)) >> 3); \
242const php_hash_ops php_hash_sha3_##bits##_ops = { \
244 (php_hash_init_func_t) PHP_SHA3##bits##Init, \
245 (php_hash_update_func_t) PHP_SHA3##bits##Update, \
246 (php_hash_final_func_t) PHP_SHA3##bits##Final, \
248 php_hash_serialize, \
249 php_sha3##bits##_unserialize, \
252 (1600 - (2 * bits)) >> 3, \
253 sizeof(PHP_SHA3_##bits##_CTX), \
261#define SUCCESS SHA3_SUCCESS
262#include "KeccakHash.h"
284#if Keccak_HashInstance_ImplType == 64
286# define PHP_HASH_SERIALIZE_MAGIC_KECCAK 100
287#elif Keccak_HashInstance_ImplType == 32
289# define PHP_HASH_SERIALIZE_MAGIC_KECCAK 101
291# error "Unknown Keccak_HashInstance_ImplType"
293#define PHP_KECCAK_SPEC "b200IiIIB"
297 *
magic = PHP_HASH_SERIALIZE_MAGIC_KECCAK;
305 if (
magic == PHP_HASH_SERIALIZE_MAGIC_KECCAK
307 && ctx->sponge.byteIOIndex < ctx->sponge.rate / 8) {
310 return r !=
SUCCESS ? r : -2000;
316#define DECLARE_SHA3_OPS(bits) \
317void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \
318 ZEND_ASSERT(sizeof(Keccak_HashInstance) <= sizeof(PHP_SHA3_##bits##_CTX)); \
319 Keccak_HashInitialize_SHA3_##bits((Keccak_HashInstance *)ctx); \
321void PHP_SHA3##bits##Update(PHP_SHA3_##bits##_CTX* ctx, \
322 const unsigned char* input, \
324 Keccak_HashUpdate((Keccak_HashInstance *)ctx, input, inputLen * 8); \
326void PHP_SHA3##bits##Final(unsigned char* digest, \
327 PHP_SHA3_##bits##_CTX* ctx) { \
328 Keccak_HashFinal((Keccak_HashInstance *)ctx, digest); \
330const php_hash_ops php_hash_sha3_##bits##_ops = { \
332 (php_hash_init_func_t) PHP_SHA3##bits##Init, \
333 (php_hash_update_func_t) PHP_SHA3##bits##Update, \
334 (php_hash_final_func_t) PHP_SHA3##bits##Final, \
336 php_keccak_serialize, \
337 php_keccak_unserialize, \
340 (1600 - (2 * bits)) >> 3, \
341 sizeof(PHP_SHA3_CTX), \
353#undef DECLARE_SHA3_OPS
count(Countable|array $value, int $mode=COUNT_NORMAL)
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
PHP_HASH_API zend_result php_hash_serialize_spec(const php_hashcontext_object *hash, zval *zv, const char *spec)
PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec)
hash(string $algo, string $data, bool $binary=false, array $options=[])
#define round(tables, k1, k2)
#define DECLARE_SHA3_OPS(bits)
struct _php_hashcontext_object php_hashcontext_object
#define PHP_HASH_SERIALIZE_MAGIC_SPEC
#define ZEND_SECURE_ZERO(var, size)
ZEND_RESULT_CODE zend_result