From 1092f27ca5b314af5fae6b5db8677bb7c526c0ed Mon Sep 17 00:00:00 2001 From: mmeyer2k Date: Tue, 2 Jul 2019 18:27:02 -0500 Subject: [PATCH] 12.0.1 --- .circleci/config.yml | 72 +++++++++--------- README.md | 11 ++- docs/CHANGELOG.md | 17 ++++- docs/KEYS.md | 81 +++++++++++++++++++++ docs/ONETIMEPAD.md | 23 ++++++ docs/TESTING.md | 2 +- examples/{ => classes}/Aes256Base64.php | 0 examples/{ => classes}/TinyFish.php | 0 src/Exceptions/InvalidChecksumException.php | 12 +++ src/Exceptions/InvalidKeyException.php | 12 +++ src/OneTimePad.php | 3 + src/OpensslKey.php | 10 +-- src/OpensslStatic.php | 2 +- 13 files changed, 196 insertions(+), 49 deletions(-) create mode 100644 docs/KEYS.md create mode 100644 docs/ONETIMEPAD.md rename examples/{ => classes}/Aes256Base64.php (100%) rename examples/{ => classes}/TinyFish.php (100%) diff --git a/.circleci/config.yml b/.circleci/config.yml index e1a7742e..d9399b4d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,11 +5,22 @@ jobs: - image: circleci/php:7.1 steps: - checkout + - restore_cache: + keys: + - source-php7.1-{{ .Branch }}-{{ .Revision }} + - source-php7.1-{{ .Branch }}- + - source-php7.1- - run: composer require phpunit/phpunit infection/infection - run: mkdir -p coverage - - run: php vendor/phpunit/phpunit/phpunit --coverage-html coverage + - run: php vendor/phpunit/phpunit/phpunit --coverage-html coverage --coverage-clover=coverage.clover - run: php vendor/infection/infection/bin/infection - run: php examples/support.php + - run: wget https://scrutinizer-ci.com/ocular.phar + - run: php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - save_cache: + key: source-php7.1-{{ .Branch }}-{{ .Revision }} + paths: + - vendor - store_artifacts: path: coverage - store_artifacts: @@ -20,52 +31,39 @@ jobs: - image: circleci/php:7.2 steps: - checkout - - run: mkdir -p coverage - - run: composer require phpunit/phpunit infection/infection - - run: php vendor/phpunit/phpunit/phpunit --coverage-html coverage - - run: php vendor/infection/infection/bin/infection - - run: php examples/support.php - - store_artifacts: - path: coverage - - store_artifacts: - path: infection.log + - restore_cache: + keys: + - source-php7.2-{{ .Branch }}-{{ .Revision }} + - source-php7.2-{{ .Branch }}- + - source-php7.2- + - run: composer require phpunit/phpunit + - run: php vendor/phpunit/phpunit/phpunit + - save_cache: + key: source-php7.2-{{ .Branch }}-{{ .Revision }} + paths: + - vendor build-73: docker: - image: circleci/php:7.3 steps: - checkout - - run: mkdir -p coverage - - run: composer require phpunit/phpunit infection/infection - - run: php vendor/phpunit/phpunit/phpunit --coverage-html coverage - - run: php vendor/infection/infection/bin/infection - - run: php examples/support.php - - store_artifacts: - path: coverage - - store_artifacts: - path: infection.log - - clover: - docker: - - image: circleci/php:7.1 - steps: - - checkout + - restore_cache: + keys: + - source-php7.2-{{ .Branch }}-{{ .Revision }} + - source-php7.2-{{ .Branch }}- + - source-php7.2- - run: composer require phpunit/phpunit - - run: php vendor/phpunit/phpunit/phpunit --coverage-clover=coverage.clover - - run: wget https://scrutinizer-ci.com/ocular.phar - - run: php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - run: php vendor/phpunit/phpunit/phpunit + - save_cache: + key: source-php7.2-{{ .Branch }}-{{ .Revision }} + paths: + - vendor workflows: version: 2 build-test-all: jobs: - build - - build-72: - requires: - - build - - build-73: - requires: - - build-72 - - clover: - requires: - - build-73 \ No newline at end of file + - build-72 + - build-73 \ No newline at end of file diff --git a/README.md b/README.md index 960a9d39..8eca4efe 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,10 @@ To generate a new key, execute this on the command line: head -c 2048 /dev/urandom | base64 -w 0 | xargs echo ``` -Storing this key safely is up to you! +Storing this key safely is up to you! [Guide to keys](https://github.com/mmeyer2k/dcrypt/blob/master/docs/KEYS.md). + +[Specification document](https://github.com/mmeyer2k/dcrypt/blob/master/docs/CRYPTO.md) + ### AES-256 GCM Encryption @@ -163,16 +166,18 @@ try { ## Stream Ciphers Be sure you understand the risks and inherent issues of using a stream cipher before proceeding. -Read the relevant information before using a stream cipher for anything important +Read the relevant information before using a stream cipher for anything important: - [https://en.wikipedia.org/wiki/Stream_cipher_attacks](https://en.wikipedia.org/wiki/Stream_cipher_attacks) - [https://jameshfisher.com/2018/01/01/making-a-stream-cipher/](https://jameshfisher.com/2018/01/01/making-a-stream-cipher/) ### One Time Pad -A fast symmetric stream cipher is quickly accessible with the `OneTimePad` class. +A novel counter-based stream cipher. `OneTimePad` uses SHA3-512 to output a keystream that is ⊕'d with the input in 512 bit chunks. +[Specification document](https://github.com/mmeyer2k/dcrypt/blob/master/docs/ONETIMEPAD.md) + ```php ~/secret.key +``` + +PHP static function: + +```php + + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/mmeyer2k/dcrypt + */ + namespace Dcrypt\Exceptions; class InvalidChecksumException extends \Exception diff --git a/src/Exceptions/InvalidKeyException.php b/src/Exceptions/InvalidKeyException.php index 1898f176..f2cac3a5 100644 --- a/src/Exceptions/InvalidKeyException.php +++ b/src/Exceptions/InvalidKeyException.php @@ -1,5 +1,17 @@ + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/mmeyer2k/dcrypt + */ + namespace Dcrypt\Exceptions; class InvalidKeyException extends \Exception diff --git a/src/OneTimePad.php b/src/OneTimePad.php index c5a5587d..ebd20e87 100644 --- a/src/OneTimePad.php +++ b/src/OneTimePad.php @@ -43,7 +43,10 @@ public static function crypt(string $input, string $key, string $algo = 'sha3-51 $key = new OpensslKey($algo, $key, ''); foreach ($chunks as $i => &$chunk) { + // Create the info key based on counter $info = $length . $i; + + // Xor the derived key with the data chunk $chunk = $chunk ^ $key->deriveKey($info); } diff --git a/src/OpensslKey.php b/src/OpensslKey.php index cd4f8182..09f2f5bf 100644 --- a/src/OpensslKey.php +++ b/src/OpensslKey.php @@ -47,7 +47,7 @@ final class OpensslKey * * @param string $algo Algo to use for HKDF * @param string $key Key - * @param string $ivr Initialization vactor + * @param string $ivr Initialization vector * @throws InvalidKeyException */ public function __construct(string $algo, string $key, string $ivr) @@ -57,7 +57,7 @@ public function __construct(string $algo, string $key, string $ivr) // Make sure key was properly decoded and meets minimum required length if (!is_string($this->key) || Str::strlen($this->key) < 2048) { - throw new InvalidKeyException("Key must be at least 256 bytes and base64 encoded."); + throw new InvalidKeyException("Key must be at least 2048 bytes and base64 encoded."); } // Make sure key meets minimum entropy requirement @@ -95,16 +95,14 @@ public function encryptionKey(string $info): string } /** - * Derive a key with differing authinfo strings + * Derive a key with differing info string parameters * * @param string $info Info parameter to provide to hash_hkdf * @return string */ public function deriveKey(string $info): string { - $key = \hash_hkdf($this->algo, $this->key, 0, $info, $this->ivr); - - return $key; + return \hash_hkdf($this->algo, $this->key, 0, $info, $this->ivr); } /** diff --git a/src/OpensslStatic.php b/src/OpensslStatic.php index 21580190..ca83b94d 100644 --- a/src/OpensslStatic.php +++ b/src/OpensslStatic.php @@ -68,7 +68,7 @@ public static function decrypt(string $data, string $key, string $cipher, string // Compare given checksum against computed checksum using a time-safe function if (!Str::equal($chk, $sum)) { - throw new InvalidChecksumException('Decryption can not proceed due to invalid cyphertext checksum.'); + throw new InvalidChecksumException('Decryption can not proceed due to invalid ciphertext checksum.'); } // Decrypt message and return