Skip to content

Commit fd3edea

Browse files
committed
Merge pull request #8 from Rican7/enhancement/native-csprng-support
Enhancement - Native CSPRNG support and OpenSSL deprecation
2 parents 664a52b + b4851af commit fd3edea

6 files changed

Lines changed: 93 additions & 6 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ php:
55
- 5.4
66
- 5.5
77
- 5.6
8+
- 7.0
89
- nightly
910
- hhvm
1011

@@ -19,7 +20,7 @@ before_install:
1920

2021
install:
2122
- composer self-update
22-
- composer install --prefer-dist
23+
- make install-deps
2324

2425
script:
2526
- composer validate

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
# Define directories
2+
VENDOR_DIR ?= $(CURDIR)/vendor
3+
4+
5+
# Global/default target
16
all: install test lint check-style
27

3-
install:
4-
composer install --prefer-dist
8+
$(VENDOR_DIR):
9+
composer install --no-interaction --prefer-dist
10+
11+
install-deps: $(VENDOR_DIR)
12+
13+
clean-deps:
14+
rm -rf $(VENDOR_DIR)
515

616
test:
717
./vendor/bin/phpunit
@@ -18,3 +28,5 @@ lint:
1828

1929
check-style:
2030
./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 -p src/ tests/
31+
32+
.PHONY: all install-deps clean-deps test test-with-coverage test-with-coverage-clover lint check-style

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
"ext-openssl": "*",
2828
"phpunit/phpunit": "^4.7",
2929
"phpunit/php-code-coverage": "^2.2",
30-
"squizlabs/php_codesniffer": "^2.3"
30+
"squizlabs/php_codesniffer": "^2.3",
31+
"paragonie/random_compat": "^2.0"
32+
},
33+
"suggest": {
34+
"paragonie/random_compat": "Allows for more cryptographically secure random data generation during the NTLM hashing process"
3135
},
3236
"autoload": {
3337
"psr-4": {"Robin\\Ntlm\\": "src/Robin/Ntlm/"}

src/Robin/Ntlm/Crypt/Random/McryptRandomByteGenerator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Robin NTLM
44
*
5-
* @copyright 2015 Robin Powered, Inc.
5+
* @copyright 2016 Robin Powered, Inc.
66
* @link https://robinpowered.com/
77
*/
88

@@ -16,6 +16,9 @@
1616
* "mcrypt" extension.
1717
*
1818
* @link http://php.net/mcrypt
19+
* @deprectated NOTE! This implementation is deprecated, as the mcrypt library
20+
* is abandoned. More info: https://github.com/robinpowered/php-ntlm/pull/1
21+
* @todo Remove this implementation in a future version.
1922
*/
2023
class McryptRandomByteGenerator implements RandomByteGeneratorInterface
2124
{
@@ -63,9 +66,17 @@ public function __construct($source = self::DEFAULT_SOURCE)
6366

6467
/**
6568
* {@inheritDoc}
69+
*
70+
* @deprectated NOTE! This implementation is deprecated, as the mcrypt
71+
* library is abandoned.
6672
*/
6773
public function generate($size)
6874
{
75+
trigger_error(
76+
'This implementation is deprecated, as the mcrypt library is abandoned',
77+
E_USER_DEPRECATED
78+
);
79+
6980
$generated = mcrypt_create_iv($size, $this->source);
7081

7182
if (false === $generated || strlen($generated) !== $size) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Robin NTLM
4+
*
5+
* @copyright 2016 Robin Powered, Inc.
6+
* @link https://robinpowered.com/
7+
*/
8+
9+
namespace Robin\Ntlm\Crypt\Random;
10+
11+
use Error;
12+
use Exception;
13+
use Robin\Ntlm\Crypt\Exception\CryptographicFailureException;
14+
15+
/**
16+
* A cryptographically secure random byte generator implemented using the native
17+
* PHP CSPRNG functions.
18+
*
19+
* @link http://php.net/csprng
20+
*/
21+
class NativeRandomByteGenerator implements RandomByteGeneratorInterface
22+
{
23+
24+
/**
25+
* Methods
26+
*/
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function generate($size)
32+
{
33+
try {
34+
$generated = random_bytes($size);
35+
} catch (Error $e) {
36+
// PHP 7+ will throw an `Error`. Catch here to make sure that we don't accidentally catch a polyfilled
37+
// `Error` from a polyfill library, such as https://github.com/paragonie/random_compat
38+
throw $e;
39+
} catch (Exception $e) {
40+
throw CryptographicFailureException::forReasonCode(
41+
CryptographicFailureException::CODE_FOR_RANDOM_DATA_GENERATION_FAILURE,
42+
$e
43+
);
44+
}
45+
46+
return $generated;
47+
}
48+
}

src/Robin/Ntlm/Crypt/Random/OpenSslRandomByteGenerator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Robin NTLM
44
*
5-
* @copyright 2015 Robin Powered, Inc.
5+
* @copyright 2016 Robin Powered, Inc.
66
* @link https://robinpowered.com/
77
*/
88

@@ -15,6 +15,9 @@
1515
* "openssl" extension.
1616
*
1717
* @link http://php.net/openssl
18+
* @deprectated NOTE! This implementation is deprecated, as it's been found to
19+
* be insecure. More info: https://github.com/robinpowered/php-ntlm/issues/7
20+
* @todo Remove this implementation in a future version.
1821
*/
1922
class OpenSslRandomByteGenerator implements RandomByteGeneratorInterface
2023
{
@@ -25,9 +28,17 @@ class OpenSslRandomByteGenerator implements RandomByteGeneratorInterface
2528

2629
/**
2730
* {@inheritDoc}
31+
*
32+
* @deprectated NOTE! This implementation is deprecated, as it's been found
33+
* to be insecure.
2834
*/
2935
public function generate($size)
3036
{
37+
trigger_error(
38+
'This implementation is deprecated, as it can be insecure in some circumstances',
39+
E_USER_DEPRECATED
40+
);
41+
3142
$generated = openssl_random_pseudo_bytes($size, $strong);
3243

3344
if (false === $generated || strlen($generated) !== $size || false === $strong) {

0 commit comments

Comments
 (0)