Skip to content

Commit ad37f76

Browse files
authored
13.1.0 (#31)
* no message * no message * no message * no message * no message * no message * no message * no message
1 parent 893d0a7 commit ad37f76

File tree

10 files changed

+86
-176
lines changed

10 files changed

+86
-176
lines changed

.circleci/config.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ jobs:
77
- checkout
88
- restore_cache:
99
keys:
10-
- source-php7.1-{{ .Branch }}-{{ .Revision }}
11-
- source-php7.1-{{ .Branch }}-
12-
- source-php7.1-
10+
- php7.1-{{ .Branch }}
1311
- run: composer require phpunit/phpunit infection/infection phpmetrics/phpmetrics
1412
- run: mkdir -p coverage
1513
- run: ./vendor/bin/phpunit --coverage-html coverage --coverage-clover=coverage.clover
@@ -19,7 +17,7 @@ jobs:
1917
- run: wget https://scrutinizer-ci.com/ocular.phar
2018
- run: php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2119
- save_cache:
22-
key: source-php7.1-{{ .Branch }}-{{ .Revision }}
20+
key: php7.1-{{ .Branch }}
2321
paths:
2422
- vendor
2523
- store_artifacts:
@@ -36,13 +34,11 @@ jobs:
3634
- checkout
3735
- restore_cache:
3836
keys:
39-
- source-php7.2-{{ .Branch }}-{{ .Revision }}
40-
- source-php7.2-{{ .Branch }}-
41-
- source-php7.2-
37+
- php7.2-{{ .Branch }}
4238
- run: composer require phpunit/phpunit
4339
- run: php vendor/phpunit/phpunit/phpunit
4440
- save_cache:
45-
key: source-php7.2-{{ .Branch }}-{{ .Revision }}
41+
key: php7.2-{{ .Branch }}
4642
paths:
4743
- vendor
4844

@@ -53,13 +49,11 @@ jobs:
5349
- checkout
5450
- restore_cache:
5551
keys:
56-
- source-php7.3-{{ .Branch }}-{{ .Revision }}
57-
- source-php7.3-{{ .Branch }}-
58-
- source-php7.3-
52+
- php7.3-{{ .Branch }}
5953
- run: composer require phpunit/phpunit
6054
- run: php vendor/phpunit/phpunit/phpunit
6155
- save_cache:
62-
key: source-php7.3-{{ .Branch }}-{{ .Revision }}
56+
key: php7.3-{{ .Branch }}
6357
paths:
6458
- vendor
6559
php-74:
@@ -69,13 +63,11 @@ jobs:
6963
- checkout
7064
- restore_cache:
7165
keys:
72-
- source-php7.4-{{ .Branch }}-{{ .Revision }}
73-
- source-php7.4-{{ .Branch }}-
74-
- source-php7.4-
66+
- php7.4-{{ .Branch }}
7567
- run: composer require phpunit/phpunit
7668
- run: php vendor/phpunit/phpunit/phpunit
7769
- save_cache:
78-
key: source-php7.4-{{ .Branch }}-{{ .Revision }}
70+
key: php7.4-{{ .Branch }}
7971
paths:
8072
- vendor
8173

.styleci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ preset: recommended
33
risky: true
44

55
enabled:
6-
- array_indentation
76
- unalign_double_arrow
87
- ternary_to_null_coalescing
9-
- no_alternative_syntax
108
- concat_with_spaces
119

1210
disabled:

README.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ For legacy PHP version support, look [here](https://github.com/mmeyer2k/dcrypt/b
1313
If you need a dcrypt inspired encryption library for .NET, check out [harpocrates](https://github.com/mmeyer2k/harpocrates).
1414

1515
- [Install](#install)
16-
- [Features](#features)
17-
- [Block Ciphers](#block-ciphers)
18-
- [AES-256 GCM Encryption](#aes-256-gcm-encryption)
19-
- [Other AES-256 Modes](#other-aes-256-modes)
20-
- [Custom Encryption Suites](#custom-encryption-suites)
21-
- [Static Wrapper](#static-wrapper)
22-
- [Class Overloading](#class-overloading)
23-
- [Layered Encryption Factory](#layered-encryption-factory)
24-
- [Message Authenticity Checking](#message-authenticity-checking)
25-
- [Stream Ciphers](#stream-ciphers)
26-
- [One Time Pad](#one-time-pad)
16+
- [Block Ciphers](#block-ciphers)
17+
- [AES-256 GCM Encryption](#aes-256-gcm-encryption)
18+
- [Other AES-256 Modes](#other-aes-256-modes)
19+
- [Custom Encryption Suites](#custom-encryption-suites)
20+
- [Static Wrapper](#static-wrapper)
21+
- [Class Overloading](#class-overloading)
22+
- [Layered Encryption Factory](#layered-encryption-factory)
23+
- [Message Authenticity Checking](#message-authenticity-checking)
24+
- [Stream Ciphers](#stream-ciphers)
25+
- [One Time Pad](#one-time-pad)
2726
- [Show me some love](#show-me-some-love-heart_eyes) :heart_eyes::beer:
2827

2928
# Install
@@ -35,38 +34,30 @@ Don't worry, dcrypt does not have any dependencies of its own.
3534
composer require "mmeyer2k/dcrypt=^13.0"
3635
```
3736

38-
# Features
39-
4037
## Block Ciphers
4138

4239
The dcrypt library helps application developers avoid common mistakes in crypto implementations that leave data at risk while still providing flexibility in its options for crypto enthusiasts.
43-
Dcrypt strives to make correct usage simple, but it _is_ possible to use dcrypt incorrectly.
44-
Fully understanding the instructions is important.
45-
46-
Dcrypt's functions __require__ the use of a high entropy __2048 byte__ (minimum) key encoded with base64.
40+
Dcrypt's block cipher functions require the use of a high entropy 256 bit, base64-encoded key.
4741
To generate a new key, execute this on the command line:
4842

4943
```bash
50-
head -c 2048 /dev/urandom | base64 -w 0 | xargs echo
44+
head -c 32 /dev/urandom | base64 -w 0 | xargs echo
5145
```
5246

53-
Storing this key safely is up to you! [Guide to keys](https://github.com/mmeyer2k/dcrypt/blob/master/docs/KEYS.md).
54-
5547
[Specification document](https://github.com/mmeyer2k/dcrypt/blob/master/docs/CRYPTO.md)
5648

57-
5849
### AES-256 GCM Encryption
5950

6051
Since PHP 7.1 supports native AEAD encryption modes, using GCM would be safest option for most applications.
6152
Dcrypt will handle the AEAD authentication tag, SHA3-256 HMAC ([Keccak](https://en.wikipedia.org/wiki/SHA-3)), initialization vector and encrypted message as a single unencoded string.
6253

6354
```php
6455
<?php
65-
$key = "replace this with the output of: head -c 2048 /dev/urandom | base64 -w 0 | xargs echo";
56+
$key = "..............................";
6657

67-
$encrypted = \Dcrypt\Aes256Gcm::encrypt('a secret', $key);
58+
$encrypted = \Dcrypt\Aes::encrypt('a secret', $key);
6859

69-
$plaintext = \Dcrypt\Aes256Gcm::decrypt($encrypted, $key);
60+
$plaintext = \Dcrypt\Aes::decrypt($encrypted, $key);
7061
```
7162

7263
**If in doubt, use this example and don't read any further!**
@@ -77,15 +68,15 @@ If you read to this point then you are an experienced cryptonaut, congrats! :ok_
7768

7869
Several AES-256 encryption modes are supported out of the box via hardcoded classes.
7970

80-
| Class Name | OpenSSL Cipher | Security Rating | Further Reading |
81-
| -------------------- | :--------------: | :---------------: | --------------- |
82-
| `\Dcrypt\Aes256Gcm` | `aes-256-gcm` | :smiley: | [wiki](https://en.wikipedia.org/wiki/Galois/Counter_Mode) |
83-
| `\Dcrypt\Aes256Ctr` | `aes-256-ctr` | :relaxed: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) |
84-
| `\Dcrypt\Aes256Cbc` | `aes-256-cbc` | :expressionless: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) |
85-
| `\Dcrypt\Aes256Ofb` | `aes-256-ofb` | :grimacing: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Output_Feedback_(OFB)) |
86-
| `\Dcrypt\Aes256Cfb` | `aes-256-cfb` | :hushed: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Feedback_(CFB)) |
87-
| `\Dcrypt\Aes256Ccm` | `aes-256-ccm` | :astonished: | [wiki](https://en.wikipedia.org/wiki/CCM_mode) |
88-
| `\Dcrypt\Aes256Ecb` | `aes-256-ecb` | :rage: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#ECB) |
71+
| Class Name | OpenSSL Cipher | Security Rating | Further Reading |
72+
| -------------------- | :--------------: | :---------------: | --------------- |
73+
| `Aes256Gcm` or `Aes` | `aes-256-gcm` | :smiley: | [wiki](https://en.wikipedia.org/wiki/Galois/Counter_Mode) |
74+
| `Aes256Ctr` | `aes-256-ctr` | :relaxed: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) |
75+
| `Aes256Cbc` | `aes-256-cbc` | :expressionless: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) |
76+
| `Aes256Ofb` | `aes-256-ofb` | :grimacing: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Output_Feedback_(OFB)) |
77+
| `Aes256Cfb` | `aes-256-cfb` | :hushed: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Feedback_(CFB)) |
78+
| `Aes256Ccm` | `aes-256-ccm` | :astonished: | [wiki](https://en.wikipedia.org/wiki/CCM_mode) |
79+
| `Aes256Ecb` | `aes-256-ecb` | :rage: | [wiki](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#ECB) |
8980

9081
### Custom Encryption Suites
9182

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changes in Dcrypt
22

3+
## 13.1.0
4+
- Only require 32 byte keys from now on
5+
- Remove key randomness testing in favor of trusting devs
6+
- Add "Aes" as shorthand alias for "Aes256Gcm" to prevent typos
7+
-
8+
39
## 13.0.0
410
- Skip validating key when decrypting
511
- Clean up internal API

docs/KEYS.md

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/Aes.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Aes.php.
7+
*
8+
* PHP version 7
9+
*
10+
* @category Dcrypt
11+
*
12+
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
13+
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
14+
*
15+
* @link https://github.com/mmeyer2k/dcrypt
16+
*/
17+
18+
namespace Dcrypt;
19+
20+
/**
21+
* A shorthand alias for Aes256Gcm to prevent typos and make neater code.
22+
*
23+
* @category Dcrypt
24+
*
25+
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
26+
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
27+
*
28+
* @link https://github.com/mmeyer2k/dcrypt
29+
*/
30+
class Aes extends Aes256Gcm
31+
{
32+
}

src/Exceptions/InvalidKeyException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
*/
3030
class InvalidKeyException extends \Exception
3131
{
32-
const KEYLENGTH = 'Key must be at least 2048 bytes and base64 encoded';
33-
const KEYRANDOM = 'Key does not contain the minimum amount of entropy';
32+
const KEYLENGTH = 'Key must be at least 32 bytes';
3433
}

src/OpensslKey.php

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,22 @@ final class OpensslKey
5555
/**
5656
* OpensslKey constructor.
5757
*
58-
* @param string $algo Algo to use for HKDF
59-
* @param string $key Key
60-
* @param string $ivr Initialization vector
61-
* @param bool $testKey Validate the key
58+
* @param string $algo Algo to use for HKDF
59+
* @param string $key Key to use for encryption
60+
* @param string $ivr Initialization vector
6261
*
6362
* @throws InvalidKeyException
6463
*/
6564
public function __construct(
6665
string $algo,
6766
string $key,
68-
string $ivr = '',
69-
bool $testKey = true
67+
string $ivr = ''
7068
) {
7169
// Store the key as what was supplied
7270
$this->_key = \base64_decode($key);
7371

74-
if ($testKey) {
75-
// Make sure key was properly decoded and meets minimum required length
76-
if (Str::strlen($this->_key) < 2048) {
77-
throw new InvalidKeyException(InvalidKeyException::KEYLENGTH);
78-
}
79-
80-
// Make sure key meets minimum entropy requirement
81-
if (self::_testKeyEntropy($this->_key) === false) {
82-
throw new InvalidKeyException(InvalidKeyException::KEYRANDOM);
83-
}
72+
if (Str::strlen($this->_key) < 32) {
73+
throw new InvalidKeyException(InvalidKeyException::KEYLENGTH);
8474
}
8575

8676
// Store algo in object
@@ -135,24 +125,12 @@ public function deriveKey(string $info): string
135125
*
136126
* @return string
137127
*/
138-
public static function create(int $bytes = 2048): string
128+
public static function create(int $bytes = 32): string
139129
{
140-
if ($bytes < 2048) {
130+
if ($bytes < 32) {
141131
throw new InvalidKeyException(InvalidKeyException::KEYLENGTH);
142132
}
143133

144134
return \base64_encode(\random_bytes($bytes));
145135
}
146-
147-
/**
148-
* Returns true if key has enough entropy.
149-
*
150-
* @param string $key Key string to test
151-
*
152-
* @return bool
153-
*/
154-
private static function _testKeyEntropy(string $key): bool
155-
{
156-
return \count(\array_unique(\str_split($key))) > 250;
157-
}
158136
}

src/OpensslStatic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public static function decrypt(
7070
// Get the encrypted message payload
7171
$msg = Str::substr($data, $isz + $hsz + $tsz);
7272

73-
// Create key derivation object (key testing = false)
74-
$key = new OpensslKey($algo, $key, $ivr, false);
73+
// Create key derivation object
74+
$key = new OpensslKey($algo, $key, $ivr);
7575

7676
// Calculate checksum of message payload for verification
7777
$chk = \hash_hmac($algo, $msg, $key->authenticationKey($cipher), true);

0 commit comments

Comments
 (0)