Skip to content

Commit f1054f3

Browse files
committed
- add snefru algo
- fix empty else in config.m4
1 parent d31cd8b commit f1054f3

File tree

10 files changed

+1226
-15
lines changed

10 files changed

+1226
-15
lines changed

ext/hash/config.m4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ PHP_ARG_ENABLE(hash, whether to enable hash support,
66

77
if test "$PHP_HASH" != "no"; then
88
AC_DEFINE(HAVE_HASH_EXT,1,[Have HASH Extension])
9-
PHP_NEW_EXTENSION(hash, hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_whirlpool.c, $ext_shared)
9+
PHP_NEW_EXTENSION(hash, hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_snefru.c hash_whirlpool.c, $ext_shared)
1010
ifdef([PHP_INSTALL_HEADERS], [
11-
PHP_INSTALL_HEADERS(ext/hash, php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h php_hash_haval.h php_hash_tiger.h php_hash_whirlpool.h)
12-
], [ ])
11+
PHP_INSTALL_HEADERS(ext/hash, php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h php_hash_haval.h php_hash_tiger.h php_hash_snefru.h php_hash_whirlpool.h)
12+
])
1313
fi

ext/hash/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ ARG_ENABLE("hash", "enable hash support", "no");
55

66
if (PHP_HASH != "no") {
77
AC_DEFINE('HAVE_HASH_EXT', 1);
8-
EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_whirlpool.c");
8+
EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_snefru.c hash_whirlpool.c");
99
}
1010

ext/hash/hash.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ PHP_MINIT_FUNCTION(hash)
398398
{
399399
php_hash_le_hash = zend_register_list_destructors_ex(php_hash_dtor, NULL, PHP_HASH_RESNAME, module_number);
400400

401-
zend_hash_init(&php_hash_hashtable, 8, NULL, NULL, 1);
401+
zend_hash_init(&php_hash_hashtable, 30, NULL, NULL, 1);
402402

403403
php_hash_register_algo("md5", &php_hash_md5_ops);
404404
php_hash_register_algo("sha1", &php_hash_sha1_ops);
@@ -414,6 +414,7 @@ PHP_MINIT_FUNCTION(hash)
414414
php_hash_register_algo("tiger128,4", &php_hash_4tiger128_ops);
415415
php_hash_register_algo("tiger160,4", &php_hash_4tiger160_ops);
416416
php_hash_register_algo("tiger192,4", &php_hash_4tiger192_ops);
417+
php_hash_register_algo("snefru", &php_hash_snefru_ops);
417418

418419
PHP_HASH_HAVAL_REGISTER(3,128);
419420
PHP_HASH_HAVAL_REGISTER(3,160);

ext/hash/hash_salsa.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ PHP_HASH_API void PHP_SALSA20Init(PHP_SALSA_CTX *context)
154154

155155
PHP_HASH_API void PHP_SALSAUpdate(PHP_SALSA_CTX *context, const unsigned char *input, size_t len)
156156
{
157-
if (context->count + len < 64) {
158-
memcpy(&context->buffer[context->count], input, len);
159-
context->count = len;
157+
if (context->length + len < 64) {
158+
memcpy(&context->buffer[context->length], input, len);
159+
context->length += len;
160160
} else {
161-
size_t i = 0, r = (context->count + len) % 64;
161+
size_t i = 0, r = (context->length + len) % 64;
162162

163-
if (context->count) {
164-
i = 64 - context->count;
165-
memcpy(&context->buffer[context->count], input, i);
163+
if (context->length) {
164+
i = 64 - context->length;
165+
memcpy(&context->buffer[context->length], input, i);
166166
SalsaTransform(context, context->buffer);
167167
memset(context->buffer, 0, 64);
168168
}
@@ -172,15 +172,15 @@ PHP_HASH_API void PHP_SALSAUpdate(PHP_SALSA_CTX *context, const unsigned char *i
172172
}
173173

174174
memcpy(context->buffer, input + i, r);
175-
context->count = r;
175+
context->length = r;
176176
}
177177
}
178178

179179
PHP_HASH_API void PHP_SALSAFinal(unsigned char digest[64], PHP_SALSA_CTX *context)
180180
{
181181
php_uint32 i, j;
182182

183-
if (context->count) {
183+
if (context->length) {
184184
SalsaTransform(context, context->buffer);
185185
}
186186

ext/hash/hash_snefru.c

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2005 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Michael Wallner <mike@php.net> |
16+
| Sara Golemon <pollita@php.net> |
17+
+----------------------------------------------------------------------+
18+
*/
19+
20+
/* $Id$ */
21+
22+
#include "php_hash.h"
23+
#include "php_hash_snefru.h"
24+
#include "php_hash_snefru_tables.h"
25+
26+
#define round(L, C, N, SB) \
27+
SBE = SB[C & 0xff]; \
28+
L ^= SBE; \
29+
N ^= SBE
30+
31+
#ifndef DBG_SNEFRU
32+
#define DBG_SNEFRU 0
33+
#endif
34+
35+
#if DBG_SNEFRU
36+
void ph(php_uint32 h[16])
37+
{
38+
int i;
39+
for (i = 0; i < 16; i++)
40+
printf ("%08lx", h[i]); printf("\n");
41+
}
42+
#endif
43+
44+
static inline void Snefru(php_uint32 input[16])
45+
{
46+
static int shifts[4] = {16, 8, 16, 24};
47+
int b, index, rshift, lshift;
48+
php_uint32 *t0,*t1,SBE,B00,B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15;
49+
50+
B00 = input[0];
51+
B01 = input[1];
52+
B02 = input[2];
53+
B03 = input[3];
54+
B04 = input[4];
55+
B05 = input[5];
56+
B06 = input[6];
57+
B07 = input[7];
58+
B08 = input[8];
59+
B09 = input[9];
60+
B10 = input[10];
61+
B11 = input[11];
62+
B12 = input[12];
63+
B13 = input[13];
64+
B14 = input[14];
65+
B15 = input[15];
66+
67+
for (index = 0; index < 8; index++) {
68+
t0 = tables[2*index+0];
69+
t1 = tables[2*index+1];
70+
for (b = 0; b < 4; b++) {
71+
round(B15, B00, B01, t0);
72+
round(B00, B01, B02, t0);
73+
round(B01, B02, B03, t1);
74+
round(B02, B03, B04, t1);
75+
round(B03, B04, B05, t0);
76+
round(B04, B05, B06, t0);
77+
round(B05, B06, B07, t1);
78+
round(B06, B07, B08, t1);
79+
round(B07, B08, B09, t0);
80+
round(B08, B09, B10, t0);
81+
round(B09, B10, B11, t1);
82+
round(B10, B11, B12, t1);
83+
round(B11, B12, B13, t0);
84+
round(B12, B13, B14, t0);
85+
round(B13, B14, B15, t1);
86+
round(B14, B15, B00, t1);
87+
88+
rshift = shifts[b];
89+
lshift = 32-rshift;
90+
91+
B00 = (B00 >> rshift) | (B00 << lshift);
92+
B01 = (B01 >> rshift) | (B01 << lshift);
93+
B02 = (B02 >> rshift) | (B02 << lshift);
94+
B03 = (B03 >> rshift) | (B03 << lshift);
95+
B04 = (B04 >> rshift) | (B04 << lshift);
96+
B05 = (B05 >> rshift) | (B05 << lshift);
97+
B06 = (B06 >> rshift) | (B06 << lshift);
98+
B07 = (B07 >> rshift) | (B07 << lshift);
99+
B08 = (B08 >> rshift) | (B08 << lshift);
100+
B09 = (B09 >> rshift) | (B09 << lshift);
101+
B10 = (B10 >> rshift) | (B10 << lshift);
102+
B11 = (B11 >> rshift) | (B11 << lshift);
103+
B12 = (B12 >> rshift) | (B12 << lshift);
104+
B13 = (B13 >> rshift) | (B13 << lshift);
105+
B14 = (B14 >> rshift) | (B14 << lshift);
106+
B15 = (B15 >> rshift) | (B15 << lshift);
107+
}
108+
}
109+
input[0] ^= B15;
110+
input[1] ^= B14;
111+
input[2] ^= B13;
112+
input[3] ^= B12;
113+
input[4] ^= B11;
114+
input[5] ^= B10;
115+
input[6] ^= B09;
116+
input[7] ^= B08;
117+
#if DBG_SNEFRU
118+
ph(input);
119+
#endif
120+
}
121+
122+
static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char input[32])
123+
{
124+
int i, j;
125+
126+
for (i = 0, j = 0; i < 32; i += 4, ++j) {
127+
context->state[8+j] = ((input[i] & 0xff) << 24) | ((input[i+1] & 0xff) << 16) |
128+
((input[i+2] & 0xff) << 8) | (input[i+3] & 0xff);
129+
}
130+
Snefru(context->state);
131+
memset(&context->state[8], 0, sizeof(php_uint32) * 8);
132+
}
133+
134+
PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context)
135+
{
136+
memset(context, 0, sizeof(*context));
137+
}
138+
139+
static const php_uint32 MAX32 = 0xffffffffLU;
140+
141+
PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
142+
{
143+
if ((MAX32 - context->count[1]) < (len * 8)) {
144+
context->count[0]++;
145+
context->count[1] = MAX32 - context->count[1];
146+
context->count[1] = (len * 8) - context->count[1];
147+
} else {
148+
context->count[1] += len * 8;
149+
}
150+
151+
if (context->length + len < 32) {
152+
memcpy(&context->buffer[context->length], input, len);
153+
context->length += len;
154+
} else {
155+
size_t i = 0, r = (context->length + len) % 32;
156+
157+
if (context->length) {
158+
i = 32 - context->length;
159+
memcpy(&context->buffer[context->length], input, i);
160+
SnefruTransform(context, context->buffer);
161+
}
162+
163+
for (; i + 32 <= len; i += 32) {
164+
SnefruTransform(context, input + i);
165+
}
166+
167+
memcpy(context->buffer, input + i, r);
168+
memset(&context->buffer[r], 0, 32 - r);
169+
context->length = r;
170+
}
171+
}
172+
173+
PHP_HASH_API void PHP_SNEFRUFinal(unsigned char digest[32], PHP_SNEFRU_CTX *context)
174+
{
175+
php_uint32 i, j;
176+
177+
if (context->length) {
178+
SnefruTransform(context, context->buffer);
179+
}
180+
181+
context->state[14] = context->count[0];
182+
context->state[15] = context->count[1];
183+
Snefru(context->state);
184+
185+
for (i = 0, j = 0; j < 32; i++, j += 4) {
186+
digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
187+
digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
188+
digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
189+
digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
190+
}
191+
192+
memset(context, 0, sizeof(*context));
193+
}
194+
195+
php_hash_ops php_hash_snefru_ops = {
196+
(php_hash_init_func_t) PHP_SNEFRUInit,
197+
(php_hash_update_func_t) PHP_SNEFRUUpdate,
198+
(php_hash_final_func_t) PHP_SNEFRUFinal,
199+
32,
200+
32,
201+
sizeof(PHP_SNEFRU_CTX)
202+
};
203+
204+
/*
205+
* Local variables:
206+
* tab-width: 4
207+
* c-basic-offset: 4
208+
* End:
209+
* vim600: sw=4 ts=4 fdm=marker
210+
* vim<600: sw=4 ts=4
211+
*/

ext/hash/package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<file role="src" name="hash_tiger.c"/>
3939
<file role="src" name="php_hash_tiger.h"/>
4040
<file role="src" name="php_hash_tiger_tables.h"/>
41+
<file role="src" name="hash_snefru.c"/>
42+
<file role="src" name="php_hash_snefru.c"/>
43+
<file role="src" name="php_hash_snefru_tables.h"/>
4144
<file role="doc" name="README"/>
4245
<dir role="test" name="tests">
4346
<file role="test" name="hmac-md5.phpt"/>
@@ -49,6 +52,8 @@
4952
<file role="test" name="ripemd128.phpt"/>
5053
<file role="test" name="ripemd160.phpt"/>
5154
<file role="test" name="haval.phpt"/>
55+
<file role="test" name="tiger.phpt"/>
56+
<file role="test" name="whirlpool.phpt"/>
5257
</dir>
5358
</filelist>
5459

ext/hash/php_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern php_hash_ops php_hash_3tiger192_ops;
6565
extern php_hash_ops php_hash_4tiger128_ops;
6666
extern php_hash_ops php_hash_4tiger160_ops;
6767
extern php_hash_ops php_hash_4tiger192_ops;
68+
extern php_hash_ops php_hash_snefru_ops;
6869

6970
#define PHP_HASH_HAVAL_OPS(p,b) extern php_hash_ops php_hash_##p##haval##b##_ops;
7071

ext/hash/php_hash_salsa.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
typedef struct {
2828
php_uint32 state[16];
2929
unsigned char init:1;
30-
unsigned char count:7;
30+
unsigned char length:7;
3131
unsigned char buffer[64];
3232
void (*Transform)(php_uint32 state[16], php_uint32 data[16]);
3333
} PHP_SALSA_CTX;

ext/hash/php_hash_snefru.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2005 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Michael Wallner <mike@php.net> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef PHP_HASH_SNEFRU_H
22+
#define PHP_HASH_SNEFRU_H
23+
24+
/* SNEFRU-2.5a with 8 passes and 256 bit hash output
25+
* AKA "Xerox Secure Hash Function"
26+
*/
27+
28+
#include "ext/standard/basic_functions.h"
29+
30+
/* SNEFRU context */
31+
typedef struct {
32+
php_uint32 state[16];
33+
php_uint32 count[2];
34+
unsigned char length;
35+
unsigned char buffer[32];
36+
} PHP_SNEFRU_CTX;
37+
38+
PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *);
39+
PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *, const unsigned char *, uint);
40+
PHP_HASH_API void PHP_SNEFRUFinal(unsigned char[32], PHP_SNEFRU_CTX *);
41+
42+
#endif
43+
44+
/*
45+
* Local variables:
46+
* tab-width: 4
47+
* c-basic-offset: 4
48+
* End:
49+
* vim600: sw=4 ts=4 fdm=marker
50+
* vim<600: sw=4 ts=4
51+
*/

0 commit comments

Comments
 (0)