Req #67304 [NEW]: openssl_decrypt fails with GCM mode

From: Date: Mon, 19 May 2014 17:00:19 +0000
Subject: Req #67304 [NEW]: openssl_decrypt fails with GCM mode
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185864@lists.php.net to get a copy of this message
From:             birki456 at hotmail dot com
Operating system: all
PHP version:      5.5.12
Package:          OpenSSL related
Bug Type:         Feature/Change Request
Bug description:openssl_decrypt fails with GCM mode

Description:
------------
While the gcm mode (which is authenticated encryption) is reported in
openssl_get_cipher_methods() (e.g. [106] => aes-256-gcm), neither
openssl_encrypt nor openssl_decrypt support it. There simply is no code
to supply/return the authentication tag. It should be fairly simple to
add however it possibly requires a change of the return type, because
ciphertext and tag need to be returned in the encryption case and
ciphertext and tag need to be supplied for decryption. It should also be
possible to distinguish between decryption failure due to authentication
failure vs. other failure.

The code below *should*, as last line, print 'recovered: Hello World',
however decryption with gcm always fails.

Test script:
---------------
echo print_r(openssl_get_cipher_methods(), true);

$cipher = 'aes-256-gcm';

$ivlen = openssl_cipher_iv_length($cipher);

echo "iv len: " . $ivlen . "\n";

$iv = openssl_random_pseudo_bytes($ivlen);
$hexiv = bin2hex($iv);

echo "iv: " . $hexiv . "\n";

$plaintext = "Hello World";

echo "plaintext: " . $plaintext . "\n";

$clearpass = 'passphrase';
$pbkdfsalt = openssl_random_pseudo_bytes(16);
$password = hash_pbkdf2('sha256', $clearpass, $pbkdfsalt, 1001, 32,
true);

echo "clearpass: " . $clearpass . "\n";
echo "pbkdfsalt: " . bin2hex($pbkdfsalt) . "\n";
echo "password: " . bin2hex($password) . "\n";

// This is the important part:
$ciphertext = openssl_encrypt($plaintext, $cipher, $password, 0, $iv);

echo "ciphertext: " . print_r($ciphertext, true) . "\n";

$recovered = openssl_decrypt($ciphertext, $cipher, $password, 0, $iv);

echo "recovered: " . $recovered . "\n";



-- 
Edit bug report at https://bugs.php.net/bug.php?id=67304&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=67304&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=67304&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=67304&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=67304&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=67304&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=67304&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=67304&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=67304&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=67304&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=67304&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=67304&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=67304&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=67304&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=67304&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=67304&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=67304&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=67304&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=67304&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=67304&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=67304&r=mysqlcfg



Thread (12 messages)

« previous php.bugs (#185864) next »