Skip to content

Commit

Permalink
Test stream cipher gcm encryption context filters
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Aug 6, 2014
1 parent 129ca34 commit 6078ede
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/stream_filters_cipher_gcm_enc_read.phpt
@@ -0,0 +1,50 @@
--TEST--
Stream cipher gcm encryption filter for reading
--SKIPIF--
<?php if (!Crypto\Cipher::hasMode(Crypto\Cipher::MODE_GCM)) die("Skip: GCM mode not defined (update OpenSSL version)"); ?>
--FILE--
<?php
$algorithm = 'aes-256-gcm';
$key = str_repeat('x', 32);
$iv = str_repeat('i', 16);
$aad = str_repeat('b', 16);
$data = str_repeat('a', 16);

$filename = (dirname( __FILE__) . "/stream_filters_cipher_gcm_enc_read.tmp");
file_put_contents($filename, $data);

$context = stream_context_create(array(
'crypto' => array(
'filters' => array(
array(
'type' => 'cipher',
'action' => 'encrypt',
'algorithm' => $algorithm,
'key' => $key,
'iv' => $iv,
'aad' => $aad,
)
)
),
));

$stream = fopen("crypto.file://" . $filename, "r", false, $context);
if (!$stream) {
exit;
}
while ($data = fread($stream, strlen($data))) {
echo bin2hex($data) . "\n";
}

$meta_data = stream_get_meta_data($stream);
echo $meta_data['wrapper_data'][0] . "\n";
?>
--CLEAN--
<?php
$filename = (dirname( __FILE__) . "/stream_filters_cipher_gcm_enc_read.tmp");
if (file_exists($filename))
unlink($filename);
?>
--EXPECT--
622070d3bea6f720943d1198a7e6afa5
X-PHP-Crypto-Auth-Tag: f3c2954804f101d3342f6b37ba46ac8e
52 changes: 52 additions & 0 deletions tests/stream_filters_cipher_gcm_enc_write.phpt
@@ -0,0 +1,52 @@
--TEST--
Stream cipher gcm decryption filter for writing
--SKIPIF--
<?php if (!Crypto\Cipher::hasMode(Crypto\Cipher::MODE_GCM)) die("Skip: GCM mode not defined (update OpenSSL version)"); ?>
--FILE--
<?php
$algorithm = 'aes-256-gcm';
$key = str_repeat('x', 32);
$iv = str_repeat('i', 16);
$aad = str_repeat('b', 16);
$data = str_repeat('a', 16);

$context = stream_context_create(array(
'crypto' => array(
'filters' => array(
array(
'type' => 'cipher',
'action' => 'encrypt',
'algorithm' => $algorithm,
'key' => $key,
'iv' => $iv,
'aad' => $aad,
)
)
),
));

$filename = (dirname( __FILE__) . "/stream_filters_cipher_gcm_enc_write.tmp");

$stream = fopen("crypto.file://" . $filename, "w", false, $context);
if (!$stream) {
exit;
}
fwrite($stream, $data, strlen($data));
fflush($stream);

$meta_data = stream_get_meta_data($stream);
echo $meta_data['wrapper_data'][0] . "\n";

fclose($stream);

echo bin2hex(file_get_contents($filename));
?>
--CLEAN--
<?php
$filename = (dirname( __FILE__) . "/stream_filters_cipher_gcm_enc_write.tmp");
if (file_exists($filename))
unlink($filename);
?>
--EXPECT--
X-PHP-Crypto-Auth-Tag: f3c2954804f101d3342f6b37ba46ac8e
622070d3bea6f720943d1198a7e6afa5

0 comments on commit 6078ede

Please sign in to comment.