From d7d2b6ef500c92d0e44d80a195f079f3d5d87f4f Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Wed, 20 Nov 2002 06:48:39 +0000 Subject: [PATCH] Switched to a new parameter parsing API, which simplifies the code a great deal. Made error reporting use php_error_docref() Fixed a bug in mhash_keygen_s2k() that caused non \0 terminated data to be returned. Added tests of all mhash functions. --- ext/mhash/mhash.c | 155 ++++++++++++++------------------------- ext/mhash/tests/001.phpt | 67 +++++++++++++++++ ext/mhash/tests/002.phpt | 28 +++++++ ext/mhash/tests/003.phpt | Bin 0 -> 1756 bytes ext/mhash/tests/skip.inc | 5 ++ 5 files changed, 154 insertions(+), 101 deletions(-) create mode 100644 ext/mhash/tests/001.phpt create mode 100644 ext/mhash/tests/002.phpt create mode 100644 ext/mhash/tests/003.phpt create mode 100644 ext/mhash/tests/skip.inc diff --git a/ext/mhash/mhash.c b/ext/mhash/mhash.c index c2f22818a3..fc3a957ea6 100644 --- a/ext/mhash/mhash.c +++ b/ext/mhash/mhash.c @@ -56,22 +56,21 @@ zend_module_entry mhash_module_entry = { ZEND_GET_MODULE(mhash) #endif -#define MHASH_FAILED_MSG "mhash initialization failed" -#define MHASH_KEYGEN_FAILED_MSG "mhash key generation failed" +/* SALTED S2K uses a fixed salt */ +#define SALT_SIZE 8 static PHP_MINIT_FUNCTION(mhash) { - int i; + int i, n, l; char *name; char buf[128]; - for (i = 0; i <= mhash_count(); i++) { - name = mhash_get_hash_name(i); - if (name) { - snprintf(buf, 127, "MHASH_%s", name); - zend_register_long_constant(buf, strlen(buf) + 1, - i, CONST_PERSISTENT, - module_number TSRMLS_CC); + n = mhash_count() + 1; + + for (i=0; i 3) { + int hash; + int data_len, key_len=0; + char *data, *key=NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|s", &hash, &data, &data_len, &key, &key_len) == FAILURE) { WRONG_PARAM_COUNT; } - if (num_args == 2) { /* 2 arguments, just hash */ - if (zend_get_parameters_ex(2, &hash, &data) == FAILURE) { - WRONG_PARAM_COUNT; - } - } else { /* 3 arguments, do HMAC hash (keyed hash) */ - if (zend_get_parameters_ex(3, &hash, &data, &key) == - FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(key); - } - - convert_to_long_ex(hash); - convert_to_string_ex(data); - - bsize = mhash_get_block_size(Z_LVAL_PP(hash)); - - if (num_args == 3) { - if (mhash_get_hash_pblock(Z_LVAL_PP(hash)) == 0) { - php_error(E_WARNING, MHASH_FAILED_MSG); + + bsize = mhash_get_block_size(hash); + + if (key_len) { + if (mhash_get_hash_pblock(hash) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash initialization failed"); RETURN_FALSE; } - td = mhash_hmac_init(Z_LVAL_PP(hash), - Z_STRVAL_PP(key), - Z_STRLEN_PP(key), - mhash_get_hash_pblock(Z_LVAL_PP(hash))); + td = mhash_hmac_init(hash, key, key_len, mhash_get_hash_pblock(hash)); } else { - td = mhash_init(Z_LVAL_PP(hash)); + td = mhash_init(hash); } + if (td == MHASH_FAILED) { - php_error(E_WARNING, MHASH_FAILED_MSG); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash initialization failed"); RETURN_FALSE; } - mhash(td, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); + mhash(td, data, data_len); - if (num_args == 3) { + if (key_len) { hash_data = (unsigned char *) mhash_hmac_end(td); } else { hash_data = (unsigned char *) mhash_end(td); } + if (hash_data) { RETVAL_STRINGL(hash_data, bsize, 1); mhash_free(hash_data); @@ -215,69 +192,45 @@ PHP_FUNCTION(mhash) /* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes) Generates a key using hash functions */ -/* SALTED S2K uses a fixed salt */ -#define SALT_SIZE 8 PHP_FUNCTION(mhash_keygen_s2k) { - pval **hash, **input_password, **bytes, **input_salt; - int password_len, salt_len; - int hashid, size=0, val; KEYGEN keystruct; char salt[SALT_SIZE], *ret; - char* password, error[128]; + int hash, bytes; + char *password, *in_salt; + int password_len, salt_len; - if (ZEND_NUM_ARGS() != 4) { - WRONG_PARAM_COUNT; - } - if (zend_get_parameters_ex(4, &hash, &input_password, &input_salt, &bytes) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssl", &hash, &password, &password_len, &in_salt, &salt_len, &bytes) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long_ex(hash); - convert_to_string_ex(input_password); - convert_to_string_ex(input_salt); - convert_to_long_ex(bytes); - - password = Z_STRVAL_PP(input_password); - password_len = Z_STRLEN_PP(input_password); - - salt_len = MIN(Z_STRLEN_PP(input_salt), SALT_SIZE); + + salt_len = MIN(salt_len, SALT_SIZE); if (salt_len > mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED)) { - sprintf( error, "The specified salt [%d] is more bytes than the required by the algorithm [%d]\n", salt_len, mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED)); - - php_error(E_WARNING, error); + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "The specified salt [%d] is more bytes than the required by the algorithm [%d]\n", + salt_len, mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED)); } - memcpy(salt, Z_STRVAL_PP(input_salt), salt_len); - if (salt_len < SALT_SIZE) + memcpy(salt, in_salt, salt_len); + if (salt_len < SALT_SIZE) { memset(salt + salt_len, 0, SALT_SIZE - salt_len); - salt_len=SALT_SIZE; - -/* if (salt_len==0) { - * php_error(E_WARNING, "Not using salt is really not recommended); - * } - */ + } + salt_len = SALT_SIZE; - hashid = Z_LVAL_PP(hash); - size = Z_LVAL_PP(bytes); - - keystruct.hash_algorithm[0]=hashid; - keystruct.hash_algorithm[1]=hashid; - keystruct.count=0; + keystruct.hash_algorithm[0] = hash; + keystruct.hash_algorithm[1] = hash; + keystruct.count = 0; keystruct.salt = salt; keystruct.salt_size = salt_len; - ret = emalloc(size); - if (ret==NULL) { - php_error(E_WARNING, MHASH_KEYGEN_FAILED_MSG); - RETURN_FALSE; - } + ret = emalloc(bytes + 1); - val = mhash_keygen_ext( KEYGEN_S2K_SALTED, keystruct, ret, size, password, password_len); - if (val >= 0) { - RETVAL_STRINGL(ret, size, 0); + if (mhash_keygen_ext(KEYGEN_S2K_SALTED, keystruct, ret, bytes, password, password_len) >= 0) { + ret[bytes] = '\0'; + RETVAL_STRINGL(ret, bytes, 0); } else { - php_error(E_WARNING, MHASH_KEYGEN_FAILED_MSG); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash key generation failed"); efree(ret); RETURN_FALSE; } diff --git a/ext/mhash/tests/001.phpt b/ext/mhash/tests/001.phpt new file mode 100644 index 0000000000..57f50c0deb --- /dev/null +++ b/ext/mhash/tests/001.phpt @@ -0,0 +1,67 @@ +--TEST-- +mhash() test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +MHASH_MD5 +string(16) "-›Û‘ùN–ÙÄâ®S*̓j" + +MHASH_SHA1 +string(20) "/“AåZƒíõI{ø;£Û*}à£" + +MHASH_HAVAL256 +string(32) "²Uþÿ­d'5Ç›Æ•¡ü¥;Ýýúñ ²u’‡“¯" + +MHASH_HAVAL192 +string(24) "Lè7ÞH0 *²Æp”Ɉß×ÛÍ" + +MHASH_HAVAL224 +string(28) "SbхgR¿,›²Öý×r¹ÅÈÎ^È&•&K…á" + +MHASH_HAVAL160 +string(20) "Ƴo‡u Wi¼´ò"q”{ùË" + +MHASH_RIPEMD160 +string(20) "lGCZ¡ÓYķƯF4Ÿ >XX=" + +MHASH_GOST +string(32) " +%Rνõ|­ñQGòU¶C)5»œ,Ç⍋-ž" + +MHASH_TIGER +string(24) "•:Ãyš¹ý둮«— ~g9\»T0 à +" + +MHASH_CRC32 +string(4) "ƒ¸" + +MHASH_CRC32B +string(4) "¤·Zß" + diff --git a/ext/mhash/tests/002.phpt b/ext/mhash/tests/002.phpt new file mode 100644 index 0000000000..3dcdab2d91 --- /dev/null +++ b/ext/mhash/tests/002.phpt @@ -0,0 +1,28 @@ +--TEST-- +mhash_get_block_size() & mhash_get_hash_name() test +--SKIPIF-- + +--FILE-- +\t" . mhash_get_block_size($i) . "\n"; + } + } +?> +--EXPECT-- +CRC32 -> 4 +MD5 -> 16 +SHA1 -> 20 +HAVAL256 -> 32 +RIPEMD160 -> 20 +TIGER -> 24 +GOST -> 32 +CRC32B -> 4 +HAVAL224 -> 28 +HAVAL192 -> 24 +HAVAL160 -> 20 diff --git a/ext/mhash/tests/003.phpt b/ext/mhash/tests/003.phpt new file mode 100644 index 0000000000000000000000000000000000000000..677142e4c30f38434c715b4eba8d4f6cfa6d9bee GIT binary patch literal 1756 zcmdPZ4RH+)(beV3%}6ZHh|f-~Oi#^=FE+~7&{QZ%EiU2G)eZLc4DfUVDzULI$SC0A z%*;#9DNRXLP%6&OEYJh;l&rbz?SKm1Jbhf@%D7aDOA89}i%L>c;=x8I<|x=IBo-AV zR%&o5`Fc18d&K*?m@4VOIKdu{h6tXAW0<3lk*OJ)h@qtsnuw8+37UwZnE^s1$TPsz z*9Aq$-9I=4p&-Q5-8Bfobq;biMzR9TbyDKewC3XCOv^7yO-#;E(7@ug#9{@N#GG_Z zg=#KNPL+bh;^OiYpnr=?isFk>3sMtHG(gfi3Wf$iqkt-k6LU(i$s2*Cfhx)pi{evC za|<*uA|^RMuec;J4`QOGjsny+AOmD5$O;2ZkSVp0SaFRAaCHX7FwiZTdFdJ;X$7UW z38H@Mrx=}mX;QT?V9L$|Zm-#%EttX+Qn{)sIelHm)&~zt;=(!q-P4`_vUu*SwT|YI zzm{8_dD|pVzUA(wiiVWOg|CA*ue=&}sd?6Fwa^nf|8BpqQfNs$KJ(`Fswa(XGL6Br z8I@42ylWU!z$VM?8XJ7*H#f(9p2SsYF;W|sGyW2o8ltSyy~1ee+9zzAXICV1?Xnf? zc&6m(#kWp9G{f=((Gx%Zf3-hyw@~4c8w({T z?C@Tn>xpJ1-#vGR`R!LhHRPbU{Dwt;j8;XxyL|Ov=w%bLTF=NNom=<*IjdQ z%D?Z`A1CdLu0XR=>A|5wH71)@y`PTkyY=NB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