2012-04-17 22:56:53 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2012-07-24 20:53:12 +04:00
|
|
|
* Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>, and
|
|
|
|
* Robin Appelman <icewind@owncloud.com>
|
2012-04-17 22:56:53 +04:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
namespace OCA_Encryption;
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
require_once "PHPUnit/Framework/TestCase.php";
|
|
|
|
require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
|
|
|
|
|
|
|
|
class Test_Crypt extends \PHPUnit_Framework_TestCase {
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function setUp() {
|
|
|
|
|
|
|
|
// set content for encrypting / decrypting in tests
|
|
|
|
$this->data = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
2012-07-25 15:38:40 +04:00
|
|
|
$this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
|
2012-07-24 20:53:12 +04:00
|
|
|
$this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown(){}
|
|
|
|
|
|
|
|
function testGenerateKey() {
|
|
|
|
|
|
|
|
# TODO: use more accurate (larger) string length for test confirmation
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$key = Crypt::generateKey();
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-08-14 22:06:56 +04:00
|
|
|
$this->assertTrue( strlen( $key ) > 16 );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:53:12 +04:00
|
|
|
function testGenerateIv() {
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$iv = Crypt::generateIv();
|
2012-07-24 20:53:12 +04:00
|
|
|
|
|
|
|
$this->assertTrue( strlen( $iv ) == 16 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testEncrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$crypted = Crypt::encrypt( $this->data, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertNotEquals( $this->data, $crypted );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDecrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$crypted = Crypt::encrypt( $this->data, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$decrypt = Crypt::decrypt( $crypted, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSymmetricEncryptFileContent() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$keyfileContent = Crypt::symmetricEncryptFileContent( $this->data, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertNotEquals( $this->data, $keyfileContent );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$decrypt = Crypt::symmetricDecryptFileContent( $keyfileContent, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-04-17 22:56:53 +04:00
|
|
|
}
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testSymmetricEncryptFileContentKeyfile() {
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$crypted = Crypt::symmetricEncryptFileContentKeyfile( $this->data );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertNotEquals( $this->data, $crypted['encrypted'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$decrypt = Crypt::symmetricDecryptFileContent( $crypted['encrypted'], $crypted['key'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:53:12 +04:00
|
|
|
function testIsEncryptedContent() {
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertFalse( Crypt::isEncryptedContent( $this->data ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertFalse( Crypt::isEncryptedContent( $this->legacyEncryptedData ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$keyfileContent = Crypt::symmetricEncryptFileContent( $this->data, 'hat' );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertTrue( Crypt::isEncryptedContent( $keyfileContent ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testMultiKeyEncrypt() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$pair1 = Crypt::createKeypair();
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( 2, count( $pair1 ) );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['publicKey'] ) > 1 );
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['privateKey'] ) > 1 );
|
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$crypted = Crypt::multiKeyEncrypt( $this->data, array( $pair1['publicKey'] ) );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertNotEquals( $this->data, $crypted['encrypted'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$decrypt = Crypt::multiKeyDecrypt( $crypted['encrypted'], $crypted['keys'][0], $pair1['privateKey'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
}
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
function testKeyEncrypt() {
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-08-15 17:52:03 +04:00
|
|
|
$pair1 = Crypt::createKeypair();
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt data
|
2012-08-15 17:52:03 +04:00
|
|
|
$crypted = Crypt::keyEncrypt( $this->data, $pair1['publicKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertNotEquals( $this->data, $crypted );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt data
|
2012-08-15 17:52:03 +04:00
|
|
|
$decrypt = Crypt::keyDecrypt( $crypted, $pair1['privateKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decrypt );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testKeyEncryptKeyfile() {
|
|
|
|
|
|
|
|
# TODO: Don't repeat encryption from previous tests, use PHPUnit test interdependency instead
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-08-15 17:52:03 +04:00
|
|
|
$pair1 = Crypt::createKeypair();
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt plain data, generate keyfile & encrypted file
|
2012-08-15 17:52:03 +04:00
|
|
|
$cryptedData = Crypt::symmetricEncryptFileContentKeyfile( $this->data );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt keyfile
|
2012-08-15 17:52:03 +04:00
|
|
|
$cryptedKey = Crypt::keyEncrypt( $cryptedData['key'], $pair1['publicKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt keyfile
|
2012-08-15 17:52:03 +04:00
|
|
|
$decryptKey = Crypt::keyDecrypt( $cryptedKey, $pair1['privateKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt encrypted file
|
2012-08-15 17:52:03 +04:00
|
|
|
$decryptData = Crypt::symmetricDecryptFileContent( $cryptedData['encrypted'], $decryptKey );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( $this->data, $decryptData );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
}
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
// function testEncryption(){
|
|
|
|
//
|
|
|
|
// $key=uniqid();
|
|
|
|
// $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
// $source=file_get_contents($file); //nice large text file
|
|
|
|
// $encrypted=OC_Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::decrypt($encrypted,$key);
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $chunk=substr($source,0,8192);
|
|
|
|
// $encrypted=OC_Crypt::encrypt($chunk,$key);
|
|
|
|
// $this->assertEqual(strlen($chunk),strlen($encrypted));
|
|
|
|
// $decrypted=OC_Crypt::decrypt($encrypted,$key);
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$chunk);
|
|
|
|
//
|
|
|
|
// $encrypted=OC_Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $tmpFileEncrypted=OCP\Files::tmpFile();
|
|
|
|
// OC_Crypt::encryptfile($file,$tmpFileEncrypted,$key);
|
|
|
|
// $encrypted=file_get_contents($tmpFileEncrypted);
|
|
|
|
// $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $tmpFileDecrypted=OCP\Files::tmpFile();
|
|
|
|
// OC_Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
|
|
|
|
// $decrypted=file_get_contents($tmpFileDecrypted);
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
|
|
|
// $encrypted=OC_Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::decrypt($encrypted,$key);
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $encrypted=OC_Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function testBinary(){
|
|
|
|
// $key=uniqid();
|
|
|
|
//
|
|
|
|
// $file=__DIR__.'/binary';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
|
|
|
// $encrypted=OC_Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::decrypt($encrypted,$key);
|
|
|
|
//
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
//
|
|
|
|
// $encrypted=OC_Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Crypt::blockDecrypt($encrypted,$key,strlen($source));
|
|
|
|
// $this->assertEqual($decrypted,$source);
|
|
|
|
// }
|
|
|
|
|
2012-04-17 22:56:53 +04:00
|
|
|
}
|