]> granicus.if.org Git - php/commitdiff
Added keygen_s2k function. Function which generates keys from passwords.
authorNikos Mavroyanopoulos <nmav@php.net>
Wed, 25 Oct 2000 18:09:23 +0000 (18:09 +0000)
committerNikos Mavroyanopoulos <nmav@php.net>
Wed, 25 Oct 2000 18:09:23 +0000 (18:09 +0000)
ext/mhash/mhash.c
ext/mhash/php_mhash.h

index 1b4180b7d1b3e01f1d16f6f250863f8b70438847..81303d96383e42fb5cc5c7c407911ea29b34caa6 100644 (file)
@@ -14,7 +14,8 @@
    +----------------------------------------------------------------------+
    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
    |                                                                      |
-   | HMAC functionality added by Nikos Mavroyanopoulos <nmav@hellug.gr>   |
+   | HMAC and KEYGEN functionality added by                               |
+   |  Nikos Mavroyanopoulos <nmav@hellug.gr>                              |
    +----------------------------------------------------------------------+
  */
 
@@ -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
index 60f3955c18b78099fd03705c96d5a4ee79e6486d..ceacfe513003838fbf8b2b90238f8f5518a97513 100644 (file)
@@ -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