Skip to content

Commit

Permalink
Merge pull request #3 from phillipsdata/feat-throw-exception-on-error
Browse files Browse the repository at this point in the history
Feat: throw exception on error
  • Loading branch information
clphillips authored Jan 16, 2018
2 parents 68d1fce + 3858f29 commit ac8bee1
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 69 deletions.
16 changes: 9 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
matrix:
include:
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1

before_script:
- composer install --dev
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/phillipsdata/phpaes.svg?branch=master)](https://travis-ci.org/phillipsdata/phpaes)

FIPS-192 compliant AES cipher.
FIPS-192 compliant AES cipher.

### Supported key lengths:
- 128 bits
Expand Down Expand Up @@ -42,3 +42,19 @@ $x = $aes->decrypt($y);
echo base64_encode($y);
echo $x;
```

## Static Code Analysis

### Running Tests

```sh
vendor/bin/phpunit
```

### Code Style

This project adheres to PSR-2 formatting.

```sh
vendor/bin/phpcs --extensions=php --report=summary --standard=PSR2 ./src ./tests
```
146 changes: 85 additions & 61 deletions src/Aes.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php
namespace PhpAes;

/*
* AES cipher
/**
* AES cipher
*
*/

*/
class Aes
{
// The number of 32-bit words comprising the plaintext and columns comrising the state matrix of an AES cipher.
Expand Down Expand Up @@ -169,21 +168,29 @@ class Aes
private $iv;


/** constructs an AES cipher using a specific key.
*/
/**
* Constructs an AES cipher using a specific key.
*
* @throws LengthException if the initialization vector or key is not the
* appropriate length.
*/
public function __construct($z, $mode = 'ECB', $iv = null)
{
$this->mode = strtoupper($mode);
$this->iv = $iv;
$this->Nk = strlen($z)/4;
$this->Nr = $this->Nk + self::$Nb + 2;

if ($this->mode != "ECB" && strlen($this->iv) != 16) {
die('The initialization vector must be 128 bits (or 16 characters) long.');
if ($this->mode != 'ECB' && strlen($this->iv) != 16) {
throw new \LengthException(
'The initialization vector must be 128 bits (or 16 characters) long.'
);
}

if ($this->Nk != 4 && $this->Nk != 6 && $this->Nk != 8) {
die('Key is ' . ($this->Nk*32) . ' bits long. *not* 128, 192, or 256.');
throw new \LengthException(
'Key is ' . ($this->Nk*32) . ' bits long. *not* 128, 192, or 256.'
);
}

$this->Nr = $this->Nk+self::$Nb+2;
Expand All @@ -197,10 +204,11 @@ public function __destruct()
unset($this->s);
}

/** Encrypts an aribtrary length String.
* @params plaintext string
* @returns ciphertext string
**/
/**
* Encrypts an aribtrary length String.
* @params plaintext string
* @returns ciphertext string
*/
public function encrypt($x)
{
$t = ''; // 16-byte block to hold the temporary input of the cipher
Expand Down Expand Up @@ -268,10 +276,11 @@ public function encrypt($x)
return $y;
}

/** Decrypts an aribtrary length String.
* @params ciphertext string
* @returns plaintext string
**/
/**
* Decrypts an aribtrary length String.
* @params ciphertext string
* @returns plaintext string
*/
public function decrypt($y)
{
$t = array(); // 16-byte block
Expand Down Expand Up @@ -339,10 +348,11 @@ public function decrypt($y)
return rtrim($x, chr(0)); // Remove any buffer residue on return.
}

/** Encrypts the 16-byte plain text.
* @params 16-byte plaintext string
* @returns 16-byte ciphertext string
**/
/**
* Encrypts the 16-byte plain text.
* @params 16-byte plaintext string
* @returns 16-byte ciphertext string
*/
public function encryptBlock($x)
{
$y = ''; // 16-byte string
Expand Down Expand Up @@ -386,10 +396,11 @@ public function encryptBlock($x)
return $y;
}

/** Decrypts the 16-byte cipher text.
* @params 16-byte ciphertext string
* @returns 16-byte plaintext string
**/
/**
* Decrypts the 16-byte cipher text.
* @params 16-byte ciphertext string
* @returns 16-byte plaintext string
*/
public function decryptBlock($y)
{
$x = ''; // 16-byte string
Expand Down Expand Up @@ -434,9 +445,10 @@ public function decryptBlock($y)
return $x;
}

/** makes a big key out of a small one
* @returns void
**/
/**
* Makes a big key out of a small one
* @returns void
*/
private function keyExpansion($z)
{
// Rcon is the round constant
Expand Down Expand Up @@ -492,9 +504,10 @@ private function keyExpansion($z)
}
}

/** adds the key schedule for a round to a state matrix.
* @returns void
**/
/**
* Adds the key schedule for a round to a state matrix.
* @returns void
*/
private function addRoundKey($round)
{
$temp = '';
Expand All @@ -513,9 +526,10 @@ private function addRoundKey($round)
}
}

/** unmixes each column of a state matrix.
* @returns void
**/
/**
* Unmixes each column of a state matrix.
* @returns void
*/
private function invMixColumns()
{
$s0 = $s1 = $s2 = $s3= '';
Expand Down Expand Up @@ -546,9 +560,10 @@ private function invMixColumns()
}
}

/** applies an inverse cyclic shift to the last 3 rows of a state matrix.
* @returns void
**/
/**
* Applies an inverse cyclic shift to the last 3 rows of a state matrix.
* @returns void
*/
private function invShiftRows()
{
$temp = array();
Expand All @@ -562,9 +577,10 @@ private function invShiftRows()
}
}

/** applies inverse S-Box substitution to each byte of a state matrix.
* @returns void
**/
/**
* Applies inverse S-Box substitution to each byte of a state matrix.
* @returns void
*/
private function invSubBytes()
{
for ($i = 0; $i < 4; $i++) {
Expand All @@ -574,9 +590,10 @@ private function invSubBytes()
}
}

/** mixes each column of a state matrix.
* @returns void
**/
/**
* Mixes each column of a state matrix.
* @returns void
*/
private function mixColumns()
{
$s0 = $s1 = $s2 = $s3= '';
Expand Down Expand Up @@ -607,9 +624,10 @@ private function mixColumns()
}
}

/** applies a cyclic shift to the last 3 rows of a state matrix.
* @returns void
**/
/**
* Applies a cyclic shift to the last 3 rows of a state matrix.
* @returns void
*/
private function shiftRows()
{
$temp = array();
Expand All @@ -622,9 +640,11 @@ private function shiftRows()
}
}
}
/** applies S-Box substitution to each byte of a state matrix.
* @returns void
**/

/**
* Applies S-Box substitution to each byte of a state matrix.
* @returns void
*/
private function subBytes()
{

Expand All @@ -635,9 +655,10 @@ private function subBytes()
}
}

/** multiplies two polynomials a(x), b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1
* @returns 8-bit value
**/
/**
* Multiplies two polynomials a(x), b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1
* @returns 8-bit value
*/
private static function mult($a, $b)
{
$sum = self::$ltable[$a] + self::$ltable[$b];
Expand All @@ -647,9 +668,10 @@ private static function mult($a, $b)
return ($a == 0 ? 0 : ($b == 0 ? 0 : $sum));
}

/** applies a cyclic permutation to a 4-byte word.
* @returns 32-bit int
**/
/**
* Applies a cyclic permutation to a 4-byte word.
* @returns 32-bit int
*/
private static function rotWord($w)
{
$temp = $w >> 24; // put the first 8-bits into temp
Expand All @@ -662,9 +684,10 @@ private static function rotWord($w)
return $w;
}

/** applies S-box substitution to each byte of a 4-byte word.
* @returns 32-bit int
**/
/**
* Applies S-box substitution to each byte of a 4-byte word.
* @returns 32-bit int
*/
private static function subWord($w)
{
$temp = 0;
Expand All @@ -683,9 +706,10 @@ private static function subWord($w)
return $w;
}

/** reduces a 64-bit word to a 32-bit word
* @returns void
**/
/**
* Reduces a 64-bit word to a 32-bit word
* @returns void
*/
private static function make32BitWord(&$w)
{
// Reduce this 64-bit word to 32-bits on 64-bit machines
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/AesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class AesTest extends PHPUnit_Framework_TestCase
* @dataProvider cipherProvider
* @covers ::encrypt
* @covers ::decrypt
* @covers ::invMixColumns
* @covers ::invShiftRows
* @covers ::invSubBytes
* @covers ::keyExpansion
* @covers ::make32BitWord
* @covers ::mixColumns
* @covers ::mult
* @covers ::rotWord
* @covers ::shiftRows
* @covers ::subBytes
* @covers ::subWord
*/
public function testCipher($key, $mode, $iv, $input)
{
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/AesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace PhpAes\Aes\Tests\Unit;

use PhpAes\Aes;
use PHPUnit_Framework_TestCase;

/**
* @coversDefaultClass \PhpAes\Aes
*/
class AesTest extends PHPUnit_Framework_TestCase
{

/**
* @expectedException LengthException
* @expectedExceptionMessage The initialization vector must be 128 bits (or 16 characters) long.
* @covers ::__construct
*/
public function testConstructIvLengthException()
{
new Aes('abcdef0123456789', 'CBC');
}

/**
* @expectedException LengthException
* @expectedExceptionMessage Key is 120 bits long. *not* 128, 192, or 256.
* @covers ::__construct
*/
public function testConstructZlengthException()
{
new Aes('abcdef012345678');
}
}

0 comments on commit ac8bee1

Please sign in to comment.