mike            Wed Nov 15 17:16:05 2006 UTC

  Modified files:              
    /php-src/ext/mhash  mhash.c php_mhash.h 
  Log:
  - unicode upgrade
  - use current parameter parsing API
  - added keygen constants
  - Added keygen functions: mhash_keygen_count, mhash_get_keygen_name,
    mhash_keygen_uses_hash, mhash_keygen_uses_salt, mhash_get_keygen_salt_size,
    mhash_keygen
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/mhash/mhash.c?r1=1.52&r2=1.53&diff_format=u
Index: php-src/ext/mhash/mhash.c
diff -u php-src/ext/mhash/mhash.c:1.52 php-src/ext/mhash/mhash.c:1.53
--- php-src/ext/mhash/mhash.c:1.52      Sat Oct  7 22:55:18 2006
+++ php-src/ext/mhash/mhash.c   Wed Nov 15 17:16:04 2006
@@ -16,7 +16,7 @@
    |          Nikos Mavroyanopoulos <[EMAIL PROTECTED]> (HMAC, KEYGEN)       |
    +----------------------------------------------------------------------+
  */
-/* $Id: mhash.c,v 1.52 2006/10/07 22:55:18 bjori Exp $ */
+/* $Id: mhash.c,v 1.53 2006/11/15 17:16:04 mike Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -28,17 +28,23 @@
 
 #include "fcntl.h"
 #include "php_mhash.h"
-#include "mhash.h"
 #include "php_ini.h"
 #include "php_globals.h"
 #include "ext/standard/info.h"
 
 zend_function_entry mhash_functions[] = {
+       PHP_FE(mhash_count, NULL)
        PHP_FE(mhash_get_block_size, NULL)
        PHP_FE(mhash_get_hash_name, NULL)
-       PHP_FE(mhash_keygen_s2k, NULL)
-       PHP_FE(mhash_count, NULL)
+       PHP_FE(mhash_keygen_count, NULL)
+       PHP_FE(mhash_get_keygen_name, NULL)
+       PHP_FE(mhash_keygen_uses_hash, NULL)
+       PHP_FE(mhash_keygen_uses_salt, NULL)
+       PHP_FE(mhash_get_keygen_salt_size, NULL)
+       PHP_FE(mhash_keygen_uses_count, NULL)
        PHP_FE(mhash, NULL)
+       PHP_FE(mhash_keygen, NULL)
+       PHP_FE(mhash_keygen_s2k, NULL)
        {NULL, NULL, NULL}
 };
 
@@ -57,46 +63,151 @@
 ZEND_GET_MODULE(mhash)
 #endif
 
-/* SALTED S2K uses a fixed salt */
-#define SALT_SIZE 8
+#define NO_ARGS() (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, 
""))
+
+extern const unsigned char *mhash_get_hash_name_static(hashid hash);
+extern const unsigned char *mhash_get_keygen_name_static(keygenid keygen);
+
+/* {{{ int php_mhash */
+int php_mhash(hashid hash, const char *input_str, int input_len, const char 
*key_str, int key_len, char **enc, int *len TSRMLS_DC)
+{
+       size_t pbsize;
+       char *result;
+       MHASH mh;
+       
+       if (key_len) {
+               if (!(pbsize = mhash_get_hash_pblock(hash))) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "HMAC 
incompatible hash algorithm");
+                       return FAILURE;
+               }
+               mh = mhash_hmac_init(hash, (char *) key_str, key_len, pbsize);
+       } else {
+               mh = mhash_init(hash);
+       }
+
+       if (mh == MHASH_FAILED) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash 
initialization failed");
+               return FAILURE;
+       }
+
+       mhash(mh, input_str, input_len);
+
+       if (key_len) {
+               result = mhash_hmac_end(mh);
+       } else {
+               result = mhash_end(mh);
+       }
+       
+       if (!result) {
+               return FAILURE;
+       }
+       
+       *len = mhash_get_block_size(hash);
+       *enc = estrndup(result, *len);
+       
+       mhash_free(result);
+       
+       return SUCCESS;
+}
+/* }}} */
+
+/* {{{ int php_mhash_keygen */
+int php_mhash_keygen(keygenid type, hashid hash1, hashid hash2, const char 
*pass_str, int pass_len, const char *salt_str, size_t salt_len, char **key, int 
*len, int max_len, int max_count TSRMLS_DC)
+{
+       KEYGEN keygen;
+       
+       if (type < 0 || type > mhash_keygen_count()) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown keygen 
type %d", type);
+               return FAILURE;
+       }
+       
+       memset(&keygen, 0, sizeof(keygen));
+       
+       if (mhash_keygen_uses_hash_algorithm(type)) {
+               if (hash1 == -1) {
+                       hash1 = hash2;
+               }
+               if (hash2 == -1) {
+                       hash2 = hash1;
+               }
+               keygen.hash_algorithm[0] = hash1;
+               keygen.hash_algorithm[1] = hash2;
+       }
+       
+       if (mhash_keygen_uses_salt(type)) {
+               if (salt_len <= 0) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s 
requires a salt", mhash_get_keygen_name_static(type));
+                       return FAILURE;
+               }
+               keygen.salt = (void *) salt_str;
+               keygen.salt_size = salt_len;
+       }
+       
+       keygen.count = max_count;
+       
+       if (max_len > 0) {
+               *len = max_len;
+       } else {
+               *len = 128;
+       }
+       
+       *key = emalloc(*len + 1);
+       
+       if (mhash_keygen_ext(type, keygen, *key, *len, (void *) pass_str, 
pass_len) < 0) {
+               efree(*key);
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "key generation 
failed");
+               return FAILURE;
+       }
+       
+       (*key)[*len] = '\0';
+       return SUCCESS;
+}
+/* }}} */
 
+/* {{{ PHP_MINIT */
 PHP_MINIT_FUNCTION(mhash)
 {
        int i, n, l;
-       char *name;
+       const char *name;
        char buf[128];
 
-       n = mhash_count() + 1;
-
-       for (i=0; i<n; i++) {
-               if ((name = mhash_get_hash_name(i))) {
-                       l = snprintf(buf, 127, "MHASH_%s", name);
+       for (i = 0, n = mhash_count() + 1; i < n; ++i) {
+               if ((name = (const char *) mhash_get_hash_name_static(i))) {
+                       l = snprintf(buf, sizeof(buf), "MHASH_%s", name);
+                       zend_register_long_constant(buf, l + 1, i, 
CONST_PERSISTENT, module_number TSRMLS_CC);
+               }
+       }
+       for (i = 0, n = mhash_keygen_count() + 1; i < n; ++i) {
+               if ((name = (const char *) mhash_get_keygen_name_static(i))) {
+                       l = snprintf(buf, sizeof(buf), "MHASH_KEYGEN_%s", name);
                        zend_register_long_constant(buf, l + 1, i, 
CONST_PERSISTENT, module_number TSRMLS_CC);
-                       free(name);
                }
        }
 
        return SUCCESS;
 }
+/* }}} */
 
+/* {{{ PHP_MINFO */
 PHP_MINFO_FUNCTION(mhash)
 {
        char version[32];
        
-       sprintf(version,"%d", MHASH_API_VERSION);
+       snprintf(version, sizeof(version), "%d", MHASH_API_VERSION);
        
        php_info_print_table_start();
        php_info_print_table_row(2, "MHASH support", "Enabled");
        php_info_print_table_row(2, "MHASH API Version", version);
        php_info_print_table_end();
 }
+/* }}} */
 
-/* {{{ proto int mhash_count(void)
+/* {{{ proto int mhash_count(void) U
    Gets the number of available hashes */
 PHP_FUNCTION(mhash_count)
 {
-       if (ZEND_NUM_ARGS() != 0) {
-               WRONG_PARAM_COUNT;
+       if (!NO_ARGS()) {
+               return;
        }
 
        RETURN_LONG(mhash_count());
@@ -104,13 +215,13 @@
 
 /* }}} */
 
-/* {{{ proto int mhash_get_block_size(int hash)
+/* {{{ proto int mhash_get_block_size(int hash) U
    Gets the block size of hash */
 PHP_FUNCTION(mhash_get_block_size)
 {
        long hash;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &hash) == 
FAILURE) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&hash)) {
                return;
        }
 
@@ -119,126 +230,164 @@
 
 /* }}} */
 
-/* {{{ proto string mhash_get_hash_name(int hash)
+/* {{{ proto string mhash_get_hash_name(int hash) U
    Gets the name of hash */
 PHP_FUNCTION(mhash_get_hash_name)
 {
-       char *name;
+       const char *name;
        long hash;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &hash) == 
FAILURE) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&hash)) {
                return;
        }
 
-       name = mhash_get_hash_name(hash);
-       if (name) {
-               RETVAL_STRING(name, 1);
-               free(name);
+       if ((name = (const char *) mhash_get_hash_name_static(hash))) {
+               RETVAL_ASCII_STRING((char *) name, 1);
        } else {
                RETVAL_FALSE;
        }
 }
+/* }}} */
 
+/* {{{ int mhash_keygen_count() U
+       Get the number of available keygen algorithms */
+PHP_FUNCTION(mhash_keygen_count)
+{
+       if (!NO_ARGS()) {
+               return;
+       }
+       
+       RETURN_LONG(mhash_keygen_count());
+}
 /* }}} */
 
-/* {{{ proto string mhash(int hash, string data [, string key])
-   Hash data with hash */
-PHP_FUNCTION(mhash)
+/* {{{ string mhash_get_keygen_name(int keygen)
+       Get the name of the keygen algorithm */
+PHP_FUNCTION(mhash_get_keygen_name)
 {
-       MHASH td;
-       int bsize;
-       unsigned char *hash_data;
-       long hash;
-       int data_len, key_len=0;
-       char *data, *key=NULL;
+       const char *name;
+       long keygen;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|s", &hash, 
&data, &data_len, &key, &key_len) == FAILURE) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&keygen)) {
                return;
        }
        
-       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(hash, key, key_len, 
mhash_get_hash_pblock(hash));
+       if ((name = (const char *) mhash_get_keygen_name_static(keygen))) {
+               RETVAL_ASCII_STRING((char *) name, 1);
        } else {
-               td = mhash_init(hash);
+               RETVAL_FALSE;
        }
+}
+/* }}} */
 
-       if (td == MHASH_FAILED) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash 
initialization failed");
-               RETURN_FALSE;
+/* {{{ proto bool mhash_keygen_uses_hash(int keygen) U
+       Whether the keygen algorithm uses a hash algorithm */
+PHP_FUNCTION(mhash_keygen_uses_hash)
+{
+       long keygen;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&keygen)) {
+               return;
        }
+       
+       RETURN_BOOL(mhash_keygen_uses_hash_algorithm(keygen));
+}
+/* }}} */
 
-       mhash(td, data, data_len);
+/* {{{ proto bool mhash_keygen_uses_count
+       Whether the keygen algorithm uses the bytes_count parameter */
+PHP_FUNCTION(mhash_keygen_uses_count)
+{
+       long keygen;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&keygen)) {
+               return;
+       }
+       
+       RETURN_BOOL(mhash_keygen_uses_count(keygen));
+}
+/* }}} */
 
-       if (key_len) {
-               hash_data = (unsigned char *) mhash_hmac_end(td);
-       } else {
-               hash_data = (unsigned char *) mhash_end(td);
+/* {{{ proto bool mhash_keygen uses_salt(int keygen) U
+       Whether the keygen algorithm requires a salt */
+PHP_FUNCTION(mhash_keygen_uses_salt)
+{
+       long keygen;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&keygen)) {
+               return;
        }
        
-       if (hash_data) {
-               RETVAL_STRINGL(hash_data, bsize, 1);
-               mhash_free(hash_data);
-       } else {
+       RETURN_BOOL(mhash_keygen_uses_salt(keygen));
+}
+/* }}} */
+
+/* {{{ proto bool mhash_get_keygen_salt_size(int keygen) U
+       Get the required size of the salt for the keygen algorithm */
+PHP_FUNCTION(mhash_get_keygen_salt_size)
+{
+       long keygen;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", 
&keygen)) {
+               return;
+       }
+       
+       RETURN_LONG(mhash_get_keygen_salt_size(keygen));
+}
+/* }}} */
+
+/* {{{ proto binary mhash(int hash, binary data [, binary key]) U
+   Hash data with hash */
+PHP_FUNCTION(mhash)
+{
+       long hash;
+       char *result, *data, *key = NULL;
+       int result_len, data_len, key_len = 0;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lS|S", 
&hash, &data, &data_len, &key, &key_len)) {
+               return;
+       }
+       if (SUCCESS != php_mhash(hash, data, data_len, key, key_len, &result, 
&result_len TSRMLS_CC)) {
                RETURN_FALSE;
        }
+       RETURN_STRINGL(result, result_len, 0);
 }
 
 /* }}} */
 
-/* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string 
salt, int bytes)
-   Generates a key using hash functions */
-PHP_FUNCTION(mhash_keygen_s2k)
+/* {{{ proto binary mhash_keygen(int type, int hash1, int hash2, binary 
password[, binary salt[, int max_key_size = 128[, int bytes_count = 0]]) U
+       Generate a key */
+PHP_FUNCTION(mhash_keygen)
 {
-       KEYGEN keystruct;
-       char salt[SALT_SIZE], *ret;
-       long hash, bytes;
-       char *password, *in_salt;
-       int password_len, salt_len;
+       long hash1, hash2, type, max_len = 0, bytes_count = 0;
+       char *result_str, *pass_str, *salt_str = NULL;
+       int result_len, pass_len, salt_len = 0;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssl", &hash, 
&password, &password_len, &in_salt, &salt_len, &bytes) == FAILURE) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, 
"lllS|Sll", &type, &hash1, &hash2, &pass_str, &pass_len, &salt_str, &salt_len, 
&max_len, &bytes_count)) {
                return;
        }
-       if (bytes <= 0){
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "the byte parameter 
must be greater than 0");
+       if (SUCCESS != php_mhash_keygen(type, hash1, hash2, pass_str, pass_len, 
salt_str, salt_len, &result_str, &result_len, max_len, bytes_count TSRMLS_CC)) {
                RETURN_FALSE;
        }
-       
-       salt_len = MIN(salt_len, SALT_SIZE);
+       RETURN_STRINGL(result_str, result_len, 0);
+}
+/* }}} */
 
-       if (salt_len > mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED)) {
-               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, in_salt, salt_len);
-       if (salt_len < SALT_SIZE) {
-               memset(salt + salt_len, 0, SALT_SIZE - salt_len);
-       }       
-       salt_len = SALT_SIZE;
-       
-       keystruct.hash_algorithm[0] = hash;
-       keystruct.hash_algorithm[1] = hash;
-       keystruct.count = 0;
-       keystruct.salt = salt;
-       keystruct.salt_size = salt_len;
-
-       ret = emalloc(bytes + 1);
-
-       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_docref(NULL TSRMLS_CC, E_WARNING, "mhash key 
generation failed");
-               efree(ret);
+/* {{{ proto binary mhash_keygen_s2k(int hash, binary input_password, binary 
salt, int bytes)
+   Generates a key using hash functions */
+PHP_FUNCTION(mhash_keygen_s2k)
+{
+       long hash, max_len = 0;
+       char *result_str, *pass_str, *salt_str = NULL;
+       int result_len, pass_len, salt_len = 0;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lSSl", 
&hash, &pass_str, &pass_len, &salt_str, &salt_len, &max_len)) {
+               return;
+       }
+       if (SUCCESS != php_mhash_keygen(KEYGEN_S2K_SALTED, hash, hash, 
pass_str, pass_len, salt_str, salt_len, &result_str, &result_len, max_len, 0 
TSRMLS_CC)) {
                RETURN_FALSE;
        }
+       RETURN_STRINGL(result_str, result_len, 0);
 }
 /* }}} */
 
http://cvs.php.net/viewvc.cgi/php-src/ext/mhash/php_mhash.h?r1=1.13&r2=1.14&diff_format=u
Index: php-src/ext/mhash/php_mhash.h
diff -u php-src/ext/mhash/php_mhash.h:1.13 php-src/ext/mhash/php_mhash.h:1.14
--- php-src/ext/mhash/php_mhash.h:1.13  Sun Jan  1 13:09:51 2006
+++ php-src/ext/mhash/php_mhash.h       Wed Nov 15 17:16:04 2006
@@ -16,7 +16,7 @@
    |          Nikos Mavroyanopoulos <[EMAIL PROTECTED]> (HMAC, KEYGEN)       |
    +----------------------------------------------------------------------+
  */
-/* $Id: php_mhash.h,v 1.13 2006/01/01 13:09:51 sniper Exp $ */
+/* $Id: php_mhash.h,v 1.14 2006/11/15 17:16:04 mike Exp $ */
 
 #ifndef PHP_MHASH_H
 #define PHP_MHASH_H
@@ -29,16 +29,28 @@
 #include "internal_functions.h"
 #endif
 
+#include "mhash.h"
+
 extern zend_module_entry mhash_module_entry;
 #define mhash_module_ptr &mhash_module_entry
 
+int php_mhash(hashid hash, const char *input_str, int input_len, const char 
*key_str, int key_len, char **enc, int *len TSRMLS_DC);
+int php_mhash_keygen(keygenid type, hashid hash1, hashid hash2, const char 
*pass_str, int pass_len, const char *salt_str, size_t salt_len, char **key, int 
*len, int max_len, int max_count TSRMLS_DC);
+
 PHP_MINIT_FUNCTION(mhash);
 PHP_MINFO_FUNCTION(mhash);
+PHP_FUNCTION(mhash_count);
 PHP_FUNCTION(mhash_get_block_size);
 PHP_FUNCTION(mhash_get_hash_name);
-PHP_FUNCTION(mhash_count);
-PHP_FUNCTION(mhash_keygen_s2k);
+PHP_FUNCTION(mhash_keygen_count);
+PHP_FUNCTION(mhash_get_keygen_name);
+PHP_FUNCTION(mhash_keygen_uses_hash);
+PHP_FUNCTION(mhash_keygen_uses_salt);
+PHP_FUNCTION(mhash_get_keygen_salt_size);
+PHP_FUNCTION(mhash_keygen_uses_count);
 PHP_FUNCTION(mhash);
+PHP_FUNCTION(mhash_keygen);
+PHP_FUNCTION(mhash_keygen_s2k);
 
 #else
 #define mhash_module_ptr NULL

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to