Skip to content

Commit

Permalink
Added more tests and implemented empty extension block
Browse files Browse the repository at this point in the history
  • Loading branch information
philios33 committed Dec 5, 2014
1 parent d9926d6 commit e0e5810
Show file tree
Hide file tree
Showing 14 changed files with 519 additions and 25 deletions.
17 changes: 13 additions & 4 deletions AESCryptFileLib.php
@@ -1,8 +1,7 @@
<?php
/**
* Please see https://www.aescrypt.com/aes_file_format.html
* for the file format used. It should theoretically make .aes files which are
* compatible with any AESCrypt software.
* for the file format used.
*
* Sample Usage:
* To come later on
Expand Down Expand Up @@ -451,6 +450,13 @@ private function getBinaryExtensionData($ext_data)
$output .= pack("n", strlen($data));
$output .= $data;
}

//Also insert a 128 byte container
$data = str_repeat(pack("C", 0), 128);
$output .= pack("n", strlen($data));
$output .= $data;

//2 finishing NULL bytes to signify end of extensions
$output .= pack("C", 0);
$output .= pack("C", 0);
return $output;
Expand Down Expand Up @@ -479,7 +485,11 @@ private function validateHMAC($key, $data, $hash, $name)
if ($calculated != $actual) {
$this->debug("CALCULATED", bin2hex($calculated));
$this->debug("ACTUAL", bin2hex($actual));
throw new AESCryptInvalidPassphraseException("{$name} failed to validate. Incorrect password or file corrupted");
if ($name == "HMAC 1") {
throw new AESCryptInvalidPassphraseException("{$name} failed to validate integrity of encryption keys. Incorrect password or file corrupted.");
} else {
throw new AESCryptCorruptedFileException("{$name} failed to validate integrity of encrypted data. The file is corrupted and should not be trusted.");
}
}
}

Expand All @@ -493,7 +503,6 @@ private function debug($name, $msg) {
}

class AESCryptMissingDependencyException extends Exception {} //E.g. missing mcrypt
class AESCryptAuthenticationException extends Exception {} //E.g. when password is wrong
class AESCryptCorruptedFileException extends Exception {} //E.g. when file looks corrupted or wont parse
class AESCryptFileMissingException extends Exception {} //E.g. cant read file to encrypt
class AESCryptFileAccessException extends Exception {} //E.g. read/write error on files
Expand Down
15 changes: 7 additions & 8 deletions README.md
Expand Up @@ -10,7 +10,7 @@ https://www.aescrypt.com/aes_file_format.html
There are many PHP AES implementations available online which offer AES encryption for data streams. It is possible to utilise these low level libraries to encrypt files, but unless you do everything correctly you can end up with an insecure (or broken) library. This library works at a higher level, depending on a low level AES encryption engine (which you can configure), and implementing the open source aes crypt file format.

##Problems
There are many problems to solve when implementing file encryption using a lower level library (such as mycrpt). Many people incorrectly think you can just encrypt data and shove it in a file. Alas, it is not that simple.
There are many problems to solve when implementing file encryption using a low level library such as mycrpt. Many people incorrectly think you can just encrypt data and shove it in a file. Alas, it is not that simple.

The open source file format handles many issues such as null bytes trimming, file integrity and fast password checking. It even comes with file extension identifiers which allows arbitrary data to be tagged within the AES file (unencrypted).

Expand All @@ -21,16 +21,15 @@ This library makes it easier for users who are only interested in encrypting and
2. An AES 256 bit Encryption Implementation (you can use the included mcrypt implementation or some other)
If you don't have mcrypt available, you only need to implement the AES256Implementation interface using whatever library you want.

##Usage (see tests/AESCryptFileLibTest.php)
##Usage (see example_usage.php)
1. Include the AESCryptFileLib.php class
2. Construct an instance of the library using an AES256 implementation
3. Call the public functions

##Compatibility
This library writes version 2 of the aes file structure standard, and is also backwards compatible so it can read the older two versions.
It is fully compatible with any software using the AES Crypt standard file format.
This library writes version 2 of the file specification defined at https://www.aescrypt.com/aes_file_format.html
Backwards compatibility with the older two versions (reading old .aes files) is coming soon.
Output .aes files are fully compatible with any software using the AES Crypt standard file format.

##To fix
1. Add 128 empty bytes in header by default when writing.
2. Add support for reading files stored in version 0 and 1 formats.
3. Add more tests and example files
##To do
1. Add support for reading files stored in version 0 and 1 formats.
File renamed without changes.
45 changes: 45 additions & 0 deletions aes256/MCryptAES256Implementation.php
@@ -0,0 +1,45 @@
<?php

require_once 'AES256Implementation.php';

class MCryptAES256Implementation implements AES256Implementation
{
const BLOCK_SIZE = 16; // 128 bits
const KEY_SIZE = 32; // 256 bits
const MY_MCRYPT_CIPHER = MCRYPT_RIJNDAEL_128;
const MY_MCRYPT_MODE = MCRYPT_MODE_CBC;

public function checkDependencies()
{
$function_list = array(
"mcrypt_create_iv",
"mcrypt_encrypt",
"mcrypt_decrypt",
);
foreach ($function_list as $func) {
if (!function_exists($func)) {
throw new Exception("Missing function dependency: " . $func);
}
}
}

public function createIV()
{
return mcrypt_create_iv( self::BLOCK_SIZE, MCRYPT_RAND );
}

public function createRandomKey()
{
return mcrypt_create_iv( self::KEY_SIZE, MCRYPT_RAND );
}

public function encryptData($the_data, $iv, $enc_key)
{
return mcrypt_encrypt( self::MY_MCRYPT_CIPHER, $enc_key, $the_data , self::MY_MCRYPT_MODE , $iv );
}

public function decryptData($the_data, $iv, $enc_key)
{
return mcrypt_decrypt( self::MY_MCRYPT_CIPHER, $enc_key, $the_data , self::MY_MCRYPT_MODE , $iv );
}
}

0 comments on commit e0e5810

Please sign in to comment.