From: Nikos Mavroyanopoulos Date: Wed, 25 Oct 2000 18:09:23 +0000 (+0000) Subject: Added keygen_s2k function. Function which generates keys from passwords. X-Git-Tag: php-4.0.4RC3~534 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=93232c70640938c55912b52278d1bb22c040ec3f;p=php Added keygen_s2k function. Function which generates keys from passwords. --- diff --git a/ext/mhash/mhash.c b/ext/mhash/mhash.c index 1b4180b7d1..81303d9638 100644 --- a/ext/mhash/mhash.c +++ b/ext/mhash/mhash.c @@ -14,7 +14,8 @@ +----------------------------------------------------------------------+ | Authors: Sascha Schumann | | | - | HMAC functionality added by Nikos Mavroyanopoulos | + | HMAC and KEYGEN functionality added by | + | Nikos Mavroyanopoulos | +----------------------------------------------------------------------+ */ @@ -32,6 +33,7 @@ function_entry mhash_functions[] = { 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, NULL) {0} , @@ -52,6 +54,7 @@ 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" static PHP_MINIT_FUNCTION(mhash) { int i; @@ -192,5 +195,78 @@ PHP_FUNCTION(mhash) /* }}} */ +/* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes) + generate 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; + unsigned char *key; + int password_len, salt_len; + int hashid, size=0, val; + KEYGEN keystruct; + char salt[SALT_SIZE], *ret; + char* password, error[128]; + + if (ZEND_NUM_ARGS() != 4) { + WRONG_PARAM_COUNT; + } + if (zend_get_parameters_ex(4, &hash, &input_password, &input_salt, &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 = (*input_password)->value.str.val; + password_len = (*input_password)->value.str.len; + + salt_len = (*input_salt)->value.str.len; + + 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); + } + + memset( salt, 0, SALT_SIZE); + memcpy( salt, (*input_salt)->value.str.val, salt_len); + salt_len=SALT_SIZE; + +/* if (salt_len==0) { + * php_error(E_WARNING, "Not using salt is really not recommended); + * } + */ + + hashid = (*hash)->value.lval; + size = (*bytes)->value.lval; + + keystruct.hash_algorithm[0]=hashid; + keystruct.hash_algorithm[1]=hashid; + keystruct.count=0; + keystruct.salt = salt; + keystruct.salt_size = salt_len; + + ret = malloc(size); + if (ret==NULL) { + php_error(E_WARNING, MHASH_KEYGEN_FAILED_MSG); + RETURN_FALSE; + } + + val = mhash_keygen_ext( KEYGEN_S2K_SALTED, keystruct, ret, size, password, password_len); + if ( val >= 0) { + RETVAL_STRINGL(ret, size, 1); + free(ret); + } else { + php_error(E_WARNING, MHASH_KEYGEN_FAILED_MSG); + free(ret); + RETURN_FALSE; + } +} + +/* }}} */ + #endif diff --git a/ext/mhash/php_mhash.h b/ext/mhash/php_mhash.h index 60f3955c18..ceacfe5130 100644 --- a/ext/mhash/php_mhash.h +++ b/ext/mhash/php_mhash.h @@ -15,6 +15,7 @@ extern zend_module_entry mhash_module_entry; PHP_FUNCTION(mhash_get_block_size); PHP_FUNCTION(mhash_get_hash_name); PHP_FUNCTION(mhash_count); +PHP_FUNCTION(mhash_keygen_s2k); PHP_FUNCTION(mhash); #else