From 27c0f7172dd704cc946789f6b5319ba77bb6355b Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 28 Jan 2020 15:02:04 +0000 Subject: [PATCH] Seed out of bounds check --- composer.json | 4 ++-- src/DrngException.php | 6 ++++++ src/Random.php | 12 +++++++++++- src/SeedSizeOutOfBoundsException.php | 4 ++++ test/phpunit/RandomTest.php | 28 ++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/DrngException.php create mode 100644 src/SeedSizeOutOfBoundsException.php diff --git a/composer.json b/composer.json index 4820ce2..99f383d 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,12 @@ "autoload": { "psr-4": { - "g105b\\DRNG\\": "./src" + "g105b\\drng\\": "./src" } }, "autoload-dev": { "psr-4": { - "g105b\\DRNG\\Test\\": "./test/phpunit" + "g105b\\drng\\Test\\": "./test/phpunit" } } } \ No newline at end of file diff --git a/src/DrngException.php b/src/DrngException.php new file mode 100644 index 0000000..4d035b3 --- /dev/null +++ b/src/DrngException.php @@ -0,0 +1,6 @@ +checkSeedSize($seedBytes); + $this->seedBytes = $seedBytes; // We are using OpenSSL in AES counter method, so need to retain a counter. $this->aesCounter = 0; @@ -32,6 +34,14 @@ public function getBytes(int $size):string { ); } + /** @throws SeedSizeOutOfBoundsException */ + private function checkSeedSize(string $seed):void { + $strlen = strlen($seed); + if($strlen === 0 || $strlen % 16 !== 0) { + throw new SeedSizeOutOfBoundsException(); + } + } + /** * OpenSSL is used to generate random values, according to the * initialisation vector (IV) provided. This function returns an IV diff --git a/src/SeedSizeOutOfBoundsException.php b/src/SeedSizeOutOfBoundsException.php new file mode 100644 index 0000000..1b80a0c --- /dev/null +++ b/src/SeedSizeOutOfBoundsException.php @@ -0,0 +1,4 @@ + 0 && $i % 16 === 0) { + self::assertNull($exception); + } + else { + self::assertNotNull( + $exception, + "Exception should be thrown when byte size is not a multiple of 16" + ); + } + } + } } \ No newline at end of file