Addition to the openssl ext
| From: | Thomas Langås | Date: | Tue, 30 Jul 2002 22:01:52 +0000 |
| Subject: | Addition to the openssl ext | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-86348@lists.php.net to get a copy of this message | ||
Hi!
I've added blowfish cfb64 wrapper to the openssl-ext,
patch is attached.
Feel free to comment on the code, it's my first time
coding in a php ext so I might have done some mistakes.
Would be great if it got included in the std. php so I don't need
to patch the release myself anymore :)
--
Thomas
--- openssl.c.orig Tue Jul 30 15:46:53 2002 +++ openssl.c Tue Jul 30 23:58:28 2002 @@ -41,6 +41,7 @@ #include <openssl/conf.h> #include <openssl/e_os.h> #include <openssl/rand.h> +#include <openssl/blowfish.h> #define DEFAULT_KEY_LENGTH 512 #define MIN_KEY_LENGTH 384 @@ -107,6 +108,10 @@ PHP_FE(openssl_public_encrypt, arg2_force_ref) PHP_FE(openssl_public_decrypt, arg2_force_ref) +/* blowfish funcs */ + PHP_FE(openssl_bf_cfb64_encrypt, NULL) + PHP_FE(openssl_bf_cfb64_decrypt, NULL) + PHP_FE(openssl_error_string, NULL) {NULL, NULL, NULL} }; @@ -605,6 +610,79 @@ return SUCCESS; } /* }}} */ + + + +/* {{{ Blowfish functions */ + +/* {{{ proto mixed openssl_bf_cfb64_encrypt(string plaindata, string key) + Encrypts with blowfish, cfb64, added by Thomas Langås, tlan@stud.ntnu.no */ +PHP_FUNCTION(openssl_bf_cfb64_encrypt) +{ + char *plaindata; + int plaindata_len; + char *key; + int key_len; + unsigned char *cryptdata; + + unsigned char ivec[8]; + BF_KEY bfkey; + int num = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &plaindata, &plaindata_len, &key, &key_len) == FAILURE) + RETURN_FALSE; + + /* Compile the key, save in bfkey */ + BF_set_key(&bfkey, key_len, key); + + /* Allocate enough for encrypted data, and reset it */ + cryptdata = (unsigned char *) malloc(plaindata_len * 2); + memset(cryptdata, 0, plaindata_len*2); + + /* Encrypt data, save in cryptdata */ + memset(ivec, 0, 8); + BF_cfb64_encrypt(plaindata, cryptdata, plaindata_len, &bfkey, ivec, &num, BF_ENCRYPT); + + RETURN_STRING(cryptdata, 1); +} +/* }}} */ + +/* {{{ proto mixed openssl_bf_cfb64_decrypt(string cryptdata, string key) + Decrypts with blowfish, cfb64, added by Thomas Langås, tlan@stud.ntnu.no */ +PHP_FUNCTION(openssl_bf_cfb64_decrypt) +{ + char *cryptdata; + int cryptdata_len; + char *key; + int key_len; + unsigned char *plaindata; + + unsigned char ivec[8]; + BF_KEY bfkey; + int num = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &cryptdata, &cryptdata_len, &key, &key_len) == FAILURE) + RETURN_FALSE; + + /* Compile the key, save in bfkey */ + BF_set_key(&bfkey, key_len, key); + + /* Allocate enough for decrypted data, and reset it */ + plaindata = (unsigned char *) malloc(cryptdata_len+2); + memset(plaindata, 0, cryptdata_len+2); + + /* Decrypt data, save in plaindata */ + memset(ivec, 0, 8); + BF_cfb64_encrypt(cryptdata, plaindata, cryptdata_len, &bfkey, ivec, &num, BF_DECRYPT); + + RETURN_STRING(plaindata, 1); +} +/* }}} */ + +/* }}} */ + + + /* {{{ x509 cert functions */ --- php_openssl.h.orig Tue Dec 11 16:30:02 2001 +++ php_openssl.h Tue Jul 30 19:36:59 2002 @@ -65,6 +65,9 @@ PHP_FUNCTION(openssl_csr_export_to_file); PHP_FUNCTION(openssl_csr_sign); +PHP_FUNCTION(openssl_bf_cfb64_encrypt); +PHP_FUNCTION(openssl_bf_cfb64_decrypt); + #else #define phpext_openssl_ptr NULL
--- openssl.c.orig Tue Jul 30 15:46:53 2002 +++ openssl.c Tue Jul 30 23:58:28 2002 @@ -41,6 +41,7 @@ #include <openssl/conf.h> #include <openssl/e_os.h> #include <openssl/rand.h> +#include <openssl/blowfish.h> #define DEFAULT_KEY_LENGTH 512 #define MIN_KEY_LENGTH 384 @@ -107,6 +108,10 @@ PHP_FE(openssl_public_encrypt, arg2_force_ref) PHP_FE(openssl_public_decrypt, arg2_force_ref) +/* blowfish funcs */ + PHP_FE(openssl_bf_cfb64_encrypt, NULL) + PHP_FE(openssl_bf_cfb64_decrypt, NULL) + PHP_FE(openssl_error_string, NULL) {NULL, NULL, NULL} }; @@ -605,6 +610,79 @@ return SUCCESS; } /* }}} */ + + + +/* {{{ Blowfish functions */ + +/* {{{ proto mixed openssl_bf_cfb64_encrypt(string plaindata, string key) + Encrypts with blowfish, cfb64, added by Thomas Langås, tlan@stud.ntnu.no */ +PHP_FUNCTION(openssl_bf_cfb64_encrypt) +{ + char *plaindata; + int plaindata_len; + char *key; + int key_len; + unsigned char *cryptdata; + + unsigned char ivec[8]; + BF_KEY bfkey; + int num = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &plaindata, &plaindata_len, &key, &key_len) == FAILURE) + RETURN_FALSE; + + /* Compile the key, save in bfkey */ + BF_set_key(&bfkey, key_len, key); + + /* Allocate enough for encrypted data, and reset it */ + cryptdata = (unsigned char *) malloc(plaindata_len * 2); + memset(cryptdata, 0, plaindata_len*2); + + /* Encrypt data, save in cryptdata */ + memset(ivec, 0, 8); + BF_cfb64_encrypt(plaindata, cryptdata, plaindata_len, &bfkey, ivec, &num, BF_ENCRYPT); + + RETURN_STRING(cryptdata, 1); +} +/* }}} */ + +/* {{{ proto mixed openssl_bf_cfb64_decrypt(string cryptdata, string key) + Decrypts with blowfish, cfb64, added by Thomas Langås, tlan@stud.ntnu.no */ +PHP_FUNCTION(openssl_bf_cfb64_decrypt) +{ + char *cryptdata; + int cryptdata_len; + char *key; + int key_len; + unsigned char *plaindata; + + unsigned char ivec[8]; + BF_KEY bfkey; + int num = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &cryptdata, &cryptdata_len, &key, &key_len) == FAILURE) + RETURN_FALSE; + + /* Compile the key, save in bfkey */ + BF_set_key(&bfkey, key_len, key); + + /* Allocate enough for decrypted data, and reset it */ + plaindata = (unsigned char *) malloc(cryptdata_len+2); + memset(plaindata, 0, cryptdata_len+2); + + /* Decrypt data, save in plaindata */ + memset(ivec, 0, 8); + BF_cfb64_encrypt(cryptdata, plaindata, cryptdata_len, &bfkey, ivec, &num, BF_DECRYPT); + + RETURN_STRING(plaindata, 1); +} +/* }}} */ + +/* }}} */ + + + /* {{{ x509 cert functions */ --- php_openssl.h.orig Tue Dec 11 16:30:02 2001 +++ php_openssl.h Tue Jul 30 19:36:59 2002 @@ -65,6 +65,9 @@ PHP_FUNCTION(openssl_csr_export_to_file); PHP_FUNCTION(openssl_csr_sign); +PHP_FUNCTION(openssl_bf_cfb64_encrypt); +PHP_FUNCTION(openssl_bf_cfb64_decrypt); + #else #define phpext_openssl_ptr NULL