2012-04-17 18:56:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-07-24 16:53:12 +00:00
|
|
|
* Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>, and
|
|
|
|
* Robin Appelman <icewind@owncloud.com>
|
2012-04-17 18:56:53 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-15 13:52:03 +00:00
|
|
|
require_once "PHPUnit/Framework/TestCase.php";
|
|
|
|
require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
|
2012-11-14 15:09:12 +00:00
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/keymanager.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/proxy.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/stream.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/util.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../appinfo/app.php' );
|
|
|
|
|
|
|
|
use OCA\Encryption;
|
2012-08-15 13:52:03 +00:00
|
|
|
|
|
|
|
class Test_Crypt extends \PHPUnit_Framework_TestCase {
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
function setUp() {
|
|
|
|
|
|
|
|
// set content for encrypting / decrypting in tests
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
|
|
|
|
$this->dataShort = 'hats';
|
|
|
|
$this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
2012-07-25 11:38:40 +00:00
|
|
|
$this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
|
2012-07-24 16:53:12 +00:00
|
|
|
$this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
|
2012-11-14 15:09:12 +00:00
|
|
|
$this->randomKey = Encryption\Crypt::generateKey();
|
2012-08-23 15:43:10 +00:00
|
|
|
|
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
2012-09-11 12:40:45 +00:00
|
|
|
|
|
|
|
\OC_User::setUserId( 'admin' );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown(){}
|
|
|
|
|
|
|
|
function testGenerateKey() {
|
|
|
|
|
|
|
|
# TODO: use more accurate (larger) string length for test confirmation
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$key = Encryption\Crypt::generateKey();
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-08-14 18:06:56 +00:00
|
|
|
$this->assertTrue( strlen( $key ) > 16 );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:53:12 +00:00
|
|
|
function testGenerateIv() {
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$iv = Encryption\Crypt::generateIv();
|
|
|
|
|
|
|
|
echo $iv;
|
|
|
|
|
|
|
|
$this->assertEquals( 16, strlen( $iv ) );
|
|
|
|
|
|
|
|
return $iv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGenerateIv
|
|
|
|
*/
|
|
|
|
function testConcatIv( $iv ) {
|
|
|
|
|
|
|
|
Encryption\Crypt::concatIv( $this->dataLong, $iv );
|
|
|
|
|
|
|
|
// Fetch encryption metadata from end of file
|
|
|
|
$meta = substr( $catFile, -22 );
|
|
|
|
|
|
|
|
$identifier = substr( $meta, 6 );
|
|
|
|
|
|
|
|
$this->assertEquals( '00iv00', $identifier );
|
|
|
|
|
|
|
|
// Fetch IV from end of file
|
|
|
|
$foundIv = substr( $meta, -16 );
|
|
|
|
|
|
|
|
$this->assertEquals( $iv, $foundIv );
|
|
|
|
|
|
|
|
// Remove IV and IV identifier text to expose encrypted content
|
|
|
|
$data = substr( $catFile, 0, -22 );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $data );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGenerateIv
|
|
|
|
*/
|
|
|
|
function testSplitIv( $iv ) {
|
|
|
|
|
|
|
|
Encryption\Crypt::concatIv( $this->dataLong, $iv );
|
|
|
|
|
|
|
|
// Fetch encryption metadata from end of file
|
|
|
|
$meta = substr( $catFile, -22 );
|
|
|
|
|
|
|
|
$identifier = substr( $meta, 6 );
|
|
|
|
|
|
|
|
$this->assertEquals( '00iv00', $identifier );
|
|
|
|
|
|
|
|
// Fetch IV from end of file
|
|
|
|
$foundIv = substr( $meta, -16 );
|
|
|
|
|
|
|
|
$this->assertEquals( $iv, $foundIv );
|
|
|
|
|
|
|
|
// Remove IV and IV identifier text to expose encrypted content
|
|
|
|
$data = substr( $catFile, 0, -22 );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$this->assertEquals( $this->dataLong, $data );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
function testEncrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDecrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$decrypt = Encryption\Crypt::decrypt( $crypted, $iv, 'hat' );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSymmetricEncryptFileContent() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::symmetricEncryptFileContent( $this->dataShort, 'hat' );
|
2012-08-16 18:18:18 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
$this->assertNotEquals( $this->dataShort, $crypted );
|
2012-08-16 18:18:18 +00:00
|
|
|
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted, 'hat' );
|
2012-08-16 18:18:18 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
2012-08-16 18:18:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-17 15:35:19 +00:00
|
|
|
// These aren't used for now
|
|
|
|
// function testSymmetricBlockEncryptShortFileContent() {
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $crypted = Encryption\Crypt::symmetricBlockEncryptFileContent( $this->dataShort, $this->randomKey );
|
2012-10-17 15:35:19 +00:00
|
|
|
//
|
|
|
|
// $this->assertNotEquals( $this->dataShort, $crypted );
|
|
|
|
//
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $decrypt = Encryption\Crypt::symmetricBlockDecryptFileContent( $crypted, $this->randomKey );
|
2012-10-17 15:35:19 +00:00
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function testSymmetricBlockEncryptLongFileContent() {
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $crypted = Encryption\Crypt::symmetricBlockEncryptFileContent( $this->dataLong, $this->randomKey );
|
2012-10-17 15:35:19 +00:00
|
|
|
//
|
|
|
|
// $this->assertNotEquals( $this->dataLong, $crypted );
|
|
|
|
//
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $decrypt = Encryption\Crypt::symmetricBlockDecryptFileContent( $crypted, $this->randomKey );
|
2012-10-17 15:35:19 +00:00
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
function testSymmetricStreamEncryptShortFileContent() {
|
2012-08-23 18:19:39 +00:00
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
$filename = 'tmp-'.time();
|
2012-08-23 18:19:39 +00:00
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
|
2012-08-23 18:19:39 +00:00
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
|
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
2012-10-17 15:35:19 +00:00
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents( $filename );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-17 15:35:19 +00:00
|
|
|
//echo "\n\n\$retreivedCryptedFile = $retreivedCryptedFile";
|
2012-08-23 18:19:39 +00:00
|
|
|
|
|
|
|
// Check that the file was encrypted before being written to disk
|
|
|
|
$this->assertNotEquals( $this->dataShort, $retreivedCryptedFile );
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2012-08-23 18:19:39 +00:00
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
$key = Keymanager::getFileKey( $filename );
|
2012-08-23 18:19:39 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$manualDecrypt = Encryption\Crypt::symmetricBlockDecryptFileContent( $retreivedCryptedFile, $key );
|
2012-08-23 18:19:39 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $manualDecrypt );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-04-17 18:56:53 +00:00
|
|
|
}
|
2012-08-16 18:18:18 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
/**
|
|
|
|
* @brief Test that data that is written by the crypto stream wrapper
|
|
|
|
* @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
|
|
|
|
*/
|
2012-08-23 18:19:39 +00:00
|
|
|
function testSymmetricStreamEncryptLongFileContent() {
|
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
// Generate a a random filename
|
2012-09-11 12:40:45 +00:00
|
|
|
$filename = 'tmp-'.time();
|
2012-08-23 18:19:39 +00:00
|
|
|
|
2012-09-11 12:40:45 +00:00
|
|
|
echo "\n\n\$filename = $filename\n\n";
|
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong.$this->dataLong );
|
2012-08-23 18:19:39 +00:00
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
2012-10-17 15:35:19 +00:00
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents( $filename );
|
|
|
|
|
|
|
|
// echo "\n\n\$retreivedCryptedFile = $retreivedCryptedFile\n\n";
|
2012-09-11 12:40:45 +00:00
|
|
|
|
|
|
|
// Check that the file was encrypted before being written to disk
|
|
|
|
$this->assertNotEquals( $this->dataLong.$this->dataLong, $retreivedCryptedFile );
|
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
// Manuallly split saved file into separate IVs and encrypted chunks
|
2012-10-17 15:35:19 +00:00
|
|
|
$r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE);
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-17 15:35:19 +00:00
|
|
|
//print_r($r);
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
// Join IVs and their respective data chunks
|
2012-10-17 15:35:19 +00:00
|
|
|
$e = array( $r[0].$r[1], $r[2].$r[3], $r[4].$r[5], $r[6].$r[7], $r[8].$r[9], $r[10].$r[11] );//.$r[11], $r[12].$r[13], $r[14] );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
//print_r($e);
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
// Manually fetch keyfile
|
|
|
|
$keyfile = Keymanager::getFileKey( $filename );
|
|
|
|
|
|
|
|
// Set var for reassembling decrypted content
|
|
|
|
$decrypt = '';
|
|
|
|
|
|
|
|
// Manually decrypt chunk
|
2012-10-17 15:35:19 +00:00
|
|
|
foreach ($e as $e) {
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
// echo "\n\$encryptMe = $f";
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $e, $keyfile );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
// Assemble decrypted chunks
|
|
|
|
$decrypt .= $chunkDecrypt;
|
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
//echo "\n\$chunkDecrypt = $chunkDecrypt";
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong.$this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
// Teadown
|
|
|
|
|
2012-10-17 15:35:19 +00:00
|
|
|
$this->view->unlink( $filename );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
Keymanager::deleteFileKey( $filename );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
/**
|
|
|
|
* @brief Test that data that is read by the crypto stream wrapper
|
|
|
|
* @depends testSymmetricStreamEncryptLongFileContent
|
|
|
|
*/
|
2012-10-10 17:40:59 +00:00
|
|
|
function testSymmetricStreamDecryptShortFileContent() {
|
|
|
|
|
|
|
|
$filename = 'tmp-'.time();
|
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
|
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
2012-10-17 15:35:19 +00:00
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents( $filename );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSymmetricStreamDecryptLongFileContent() {
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
$filename = 'tmp-'.time();
|
2012-10-10 17:40:59 +00:00
|
|
|
|
2012-10-16 14:02:51 +00:00
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
|
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents( '/admin/files/' . $filename );
|
|
|
|
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
2012-10-10 17:40:59 +00:00
|
|
|
|
|
|
|
}
|
2012-08-16 18:18:18 +00:00
|
|
|
|
2012-10-10 17:40:59 +00:00
|
|
|
// Is this test still necessary?
|
2012-08-23 15:43:10 +00:00
|
|
|
// function testSymmetricBlockStreamDecryptFileContent() {
|
|
|
|
//
|
|
|
|
// \OC_User::setUserId( 'admin' );
|
|
|
|
//
|
|
|
|
// // Disable encryption proxy to prevent unwanted en/decryption
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// $cryptedFile = file_put_contents( 'crypt://' . '/blockEncrypt', $this->dataUrl );
|
|
|
|
//
|
|
|
|
// // Disable encryption proxy to prevent unwanted en/decryption
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// echo "\n\n\$cryptedFile = " . $this->view->file_get_contents( '/blockEncrypt' );
|
|
|
|
//
|
|
|
|
// $retreivedCryptedFile = file_get_contents( 'crypt://' . '/blockEncrypt' );
|
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataUrl, $retreivedCryptedFile );
|
|
|
|
//
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// }
|
2012-06-15 21:48:39 +00:00
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
function testSymmetricEncryptFileContentKeyfile() {
|
2012-06-15 21:48:39 +00:00
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->dataUrl );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted['encrypted'] );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted['encrypted'], $crypted['key'] );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:53:12 +00:00
|
|
|
function testIsEncryptedContent() {
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$this->assertFalse( Encryption\Crypt::isEncryptedContent( $this->dataUrl ) );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$this->assertFalse( Encryption\Crypt::isEncryptedContent( $this->legacyEncryptedData ) );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$keyfileContent = Encryption\Crypt::symmetricEncryptFileContent( $this->dataUrl, 'hat' );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$this->assertTrue( Encryption\Crypt::isEncryptedContent( $keyfileContent ) );
|
2012-07-24 16:53:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 17:52:00 +00:00
|
|
|
function testMultiKeyEncrypt() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-15 13:52:03 +00:00
|
|
|
$this->assertEquals( 2, count( $pair1 ) );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['publicKey'] ) > 1 );
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['privateKey'] ) > 1 );
|
|
|
|
|
2012-06-15 21:48:39 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::multiKeyEncrypt( $this->dataUrl, array( $pair1['publicKey'] ) );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted['encrypted'] );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-06-15 21:48:39 +00:00
|
|
|
|
2012-11-14 15:09:12 +00:00
|
|
|
$decrypt = Encryption\Crypt::multiKeyDecrypt( $crypted['encrypted'], $crypted['keys'][0], $pair1['privateKey'] );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-07-18 17:52:00 +00:00
|
|
|
|
2012-06-15 21:48:39 +00:00
|
|
|
}
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
function testKeyEncrypt() {
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-11-14 15:09:12 +00:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Encrypt data
|
2012-11-14 15:09:12 +00:00
|
|
|
$crypted = Encryption\Crypt::keyEncrypt( $this->dataUrl, $pair1['publicKey'] );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Decrypt data
|
2012-11-14 15:09:12 +00:00
|
|
|
$decrypt = Encryption\Crypt::keyDecrypt( $crypted, $pair1['privateKey'] );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testKeyEncryptKeyfile() {
|
|
|
|
|
|
|
|
# TODO: Don't repeat encryption from previous tests, use PHPUnit test interdependency instead
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-11-14 15:09:12 +00:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Encrypt plain data, generate keyfile & encrypted file
|
2012-11-14 15:09:12 +00:00
|
|
|
$cryptedData = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->dataUrl );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Encrypt keyfile
|
2012-11-14 15:09:12 +00:00
|
|
|
$cryptedKey = Encryption\Crypt::keyEncrypt( $cryptedData['key'], $pair1['publicKey'] );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Decrypt keyfile
|
2012-11-14 15:09:12 +00:00
|
|
|
$decryptKey = Encryption\Crypt::keyDecrypt( $cryptedKey, $pair1['privateKey'] );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
// Decrypt encrypted file
|
2012-11-14 15:09:12 +00:00
|
|
|
$decryptData = Encryption\Crypt::symmetricDecryptFileContent( $cryptedData['encrypted'], $decryptKey );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
$this->assertEquals( $this->dataUrl, $decryptData );
|
2012-08-14 18:06:56 +00:00
|
|
|
|
|
|
|
}
|
2012-07-18 17:52:00 +00:00
|
|
|
|
|
|
|
// function testEncryption(){
|
|
|
|
//
|
|
|
|
// $key=uniqid();
|
|
|
|
// $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
// $source=file_get_contents($file); //nice large text file
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2012-08-15 13:52:03 +00:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $chunk=substr($source,0,8192);
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($chunk,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual(strlen($chunk),strlen($encrypted));
|
2012-11-14 15:09:12 +00:00
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$chunk);
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 13:52:03 +00:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $tmpFileEncrypted=OCP\Files::tmpFile();
|
2012-11-14 15:09:12 +00:00
|
|
|
// OC_Encryption\Crypt::encryptfile($file,$tmpFileEncrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $encrypted=file_get_contents($tmpFileEncrypted);
|
2012-11-14 15:09:12 +00:00
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 13:52:03 +00:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $tmpFileDecrypted=OCP\Files::tmpFile();
|
2012-11-14 15:09:12 +00:00
|
|
|
// OC_Encryption\Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $decrypted=file_get_contents($tmpFileDecrypted);
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function testBinary(){
|
|
|
|
// $key=uniqid();
|
|
|
|
//
|
|
|
|
// $file=__DIR__.'/binary';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 17:52:00 +00:00
|
|
|
//
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
2012-11-14 15:09:12 +00:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key,strlen($source));
|
2012-07-18 17:52:00 +00:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
// }
|
|
|
|
|
2012-04-17 18:56:53 +00:00
|
|
|
}
|