Skip to content

Commit 5f546fa

Browse files
committed
Fixed unit testing, simplified parameters
1 parent b688b47 commit 5f546fa

6 files changed

Lines changed: 136 additions & 96 deletions

File tree

application/Config/Encryption.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Encryption extends BaseConfig
3030
| Encryption driver to use
3131
|--------------------------------------------------------------------------
3232
|
33-
| One of the supported drivers, eg 'openssl' or 'mcrypt'.
34-
| The default driver, if you don't specify one, is 'openssl'.
33+
| One of the supported drivers, eg 'OpenSSL' or 'Sodium'.
34+
| The default driver, if you don't specify one, is 'OpenSSL'.
3535
*/
3636
public $driver = 'OpenSSL';
3737

@@ -40,35 +40,30 @@ class Encryption extends BaseConfig
4040
| Encryption Cipher
4141
|--------------------------------------------------------------------------
4242
|
43-
| Name of the encryption cipher to use, eg 'aes-256' or 'blowfish'
43+
| Name of the encryption cipher to use, eg 'aes-256' or 'blowfish'.
44+
| The cipher must be supported by your designated driver.
4445
*/
4546
public $cipher = 'AES-256-CBC';
4647

4748
/*
4849
|--------------------------------------------------------------------------
49-
| Authentication
50+
| Authentication digest
5051
|--------------------------------------------------------------------------
5152
|
52-
| Use HMAC message authentication (true/false)
53-
*/
54-
public $hmac = 'HMAC';
55-
56-
/*
57-
|--------------------------------------------------------------------------
58-
| HMAC digest
59-
|--------------------------------------------------------------------------
60-
|
61-
| HMAC digest algorithm to use
53+
| HMAC digest algorithm to use, empty for none.
54+
| Values: SHA512, SHA384, SHA256, or SHA224.
6255
*/
6356
public $digest = 'SHA512';
6457

6558
/*
6659
|--------------------------------------------------------------------------
67-
| Base64 encoding?
60+
| Result encoding
6861
|--------------------------------------------------------------------------
6962
|
70-
| If true, base64 encode results, and expect base64-encoded ciphertext.
63+
| Which, if any, encoding to apply to encrypted results and to assume
64+
| provided ciphertext.
65+
| Values; empty (for no encoding), base64 or hex.
7166
*/
72-
public $base64 = 'base64';
67+
public $encoding = 'base64';
7368

7469
}

system/Encryption/Encryption.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ class Encryption
8080
* Our default configuration
8181
*/
8282
protected $default = [
83-
'driver' => 'OpenSSL', // The PHP extension we plan to use
84-
'key' => '', // no starting key material
85-
'cipher' => 'AES-256-CBC', // Encryption cipher
86-
'hmac' => 'HMAC', // Use HMAC message authentication (true/false)
87-
'digest' => 'SHA512', // HMAC digest algorithm to use
88-
'base64' => 'base64', // Base64 encoding?
83+
'driver' => 'OpenSSL', // The PHP extension we plan to use
84+
'key' => '', // no starting key material
85+
'cipher' => 'AES-256-CBC', // Encryption cipher
86+
'digest' => 'SHA512', // HMAC digest algorithm to use
87+
'encoding' => 'base64', // Base64 encoding
8988
];
90-
protected $driver, $key, $cipher, $hmac, $digest, $base64;
89+
protected $driver, $key, $cipher, $digest, $base64;
9190

9291
/**
9392
* Map of drivers to handler classes, in preference order
@@ -112,6 +111,11 @@ class Encryption
112111
'SHA512' => 64
113112
];
114113

114+
/**
115+
* List of acceptable encodings
116+
*/
117+
protected $encodings = ['base64', 'hex'];
118+
115119
// --------------------------------------------------------------------
116120

117121
/**
@@ -169,11 +173,17 @@ public function initialize(array $params = [])
169173
throw new EncryptionException("Driver '" . $params['driver'] . "' is not available.");
170174

171175
// Check for a bad digest
172-
if ( ! isset($this->digests[$params['digest']]))
173-
throw new EncryptionException("Unknown digest '" . $params['digest'] . "' specified.");
176+
if ( ! empty($params['digest']))
177+
if ( ! isset($this->digests[$params['digest']]))
178+
throw new EncryptionException("Unknown digest '" . $params['digest'] . "' specified.");
179+
180+
// Check for valid encoding
181+
if (!empty($param['encoding']))
182+
if (! in_array($params['encoding'],$this->encodings))
183+
throw new EncryptionException("Unknown encoding '" . $params['encoding'] . "' specified.");
174184

175185
// Derive a secret key for the encrypter
176-
$params['secret'] = bin2hex(hash_hkdf($this->digest, $params['key']));
186+
$params['secret'] = bin2hex(\hash_hkdf($this->digest, $params['key']));
177187

178188
$handlerName = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler';
179189
$this->encrypter = new $handlerName($params);
@@ -225,7 +235,7 @@ protected function properParams($params = null)
225235
*/
226236
public static function createKey($length = 32)
227237
{
228-
return openssl_random_pseudo_bytes($length);
238+
return \openssl_random_pseudo_bytes($length);
229239
}
230240

231241
// --------------------------------------------------------------------
@@ -238,7 +248,7 @@ public static function createKey($length = 32)
238248
*/
239249
public function __get($key)
240250
{
241-
if (in_array($key, ['config', 'cipher', 'key', 'driver', 'drivers', 'digest', 'digests', 'default', 'hmac', 'base64'], true))
251+
if (in_array($key, ['config', 'cipher', 'key', 'driver', 'drivers', 'digest', 'digests', 'default', 'encoding'], true))
242252
{
243253
return $this->{$key};
244254
}

system/Encryption/Handlers/OpenSSLHandler.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ public function __construct($params = [])
5656

5757
if (empty($this->key))
5858
throw new \CodeIgniter\Encryption\EncryptionException("OpenSSL handler configuration missing key.");
59-
if (empty($this->hmac))
60-
throw new \CodeIgniter\Encryption\EncryptionException("OpenSSL handler configuration missing HMAC control.");
61-
if (empty($this->digest))
62-
throw new \CodeIgniter\Encryption\EncryptionException("OpenSSL handler configuration missing HMAC digest.");
63-
if (empty($this->base64))
64-
throw new \CodeIgniter\Encryption\EncryptionException("OpenSSL handler configuration missing base64 control.");
6559

6660
$this->logger->info('OpenSSL handler initialized with cipher ' . $this->cipher . '.');
6761
}
@@ -75,24 +69,27 @@ public function __construct($params = [])
7569
public function encrypt($data)
7670
{
7771
// basic encryption
78-
$iv = ($iv_size = openssl_cipher_iv_length($this->cipher)) ? openssl_random_pseudo_bytes($iv_size) : null;
72+
$iv = ($iv_size = \openssl_cipher_iv_length($this->cipher)) ? \openssl_random_pseudo_bytes($iv_size) : null;
7973

80-
$data = openssl_encrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
74+
$data = \openssl_encrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
8175

8276
if ($data === false)
8377
return false;
8478

8579
$result = $iv . $data;
8680

8781
// HMAC?
88-
if ($this->hmac == 'hmac')
82+
if ( ! empty($this->digest))
8983
{
90-
$hmacKey = hash_hmac($this->digest, $result, $this->secret,true);
84+
$hmacKey = \hash_hmac($this->digest, $result, $this->secret, true);
9185
$result = $hmacKey . $result;
9286
}
9387

94-
if ($this->base64 == 'base64')
95-
$result = base64_encode($result);
88+
if ( ! empty($this->encoding))
89+
if ($this->encoding == 'base64')
90+
$result = \base64_encode($result);
91+
elseif ($this->encoding == 'hex')
92+
$result = \bin2hex($result);
9693

9794
return $result;
9895
}
@@ -107,21 +104,24 @@ public function encrypt($data)
107104
*/
108105
public function decrypt($data)
109106
{
110-
if ($this->base64 == 'base64')
111-
$data = base64_decode($data);
107+
if ( ! empty($this->encoding))
108+
if ($this->encoding == 'base64')
109+
$data = \base64_decode($data);
110+
elseif ($this->encoding == 'hex')
111+
$data = \hex2bin($data);
112112

113113
// HMAC?
114-
if ($this->hmac == 'hmac')
114+
if ( ! empty($this->digest))
115115
{
116-
$hmacLength = self::substr($this->digest,3) / 8;
117-
$hmacKey = self::substr($data,0,$hmacLength);
118-
$data = self::substr($data,$hmacLength);
119-
$hmacCalc = hash_hmac($this->digest, $data, $this->secret,true);
116+
$hmacLength = self::substr($this->digest, 3) / 8;
117+
$hmacKey = self::substr($data, 0, $hmacLength);
118+
$data = self::substr($data, $hmacLength);
119+
$hmacCalc = \hash_hmac($this->digest, $data, $this->secret, true);
120120
if ($hmacKey != $hmacCalc)
121121
throw new \CodeIgniter\Encryption\EncryptionException("Message authentication failed.");
122122
}
123-
124-
if ($iv_size = openssl_cipher_iv_length($this->cipher))
123+
124+
if ($iv_size = \openssl_cipher_iv_length($this->cipher))
125125
{
126126
$iv = self::substr($data, 0, $iv_size);
127127
$data = self::substr($data, $iv_size);
@@ -131,7 +131,7 @@ public function decrypt($data)
131131
$iv = null;
132132
}
133133

134-
return openssl_decrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
134+
return \openssl_decrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
135135
}
136136

137137
}

tests/system/Encryption/EncryptionTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,11 @@ public function testParameters()
8484
// make sure we can over-ride any parameter
8585
// change the driver once we have more than 1
8686
$expected = [
87-
'driver' => 'OpenSSL', // The PHP extension we plan to use
88-
'key' => 'Top banana', // no starting key material
89-
'cipher' => 'AES-128-CBC', // Encryption cipher
90-
'hmac' => false, // Use HMAC message authentication (true/false)
91-
'digest' => 'SHA128', // HMAC digest algorithm to use
92-
'base64' => false, // Base64 encoding?
87+
'driver' => 'OpenSSL', // The PHP extension we plan to use
88+
'key' => 'Top banana', // no starting key material
89+
'cipher' => 'AES-128-CBC', // Encryption cipher
90+
'digest' => '', // HMAC digest algorithm to use
91+
'encoding' => '', // Base64 encoding?
9392
];
9493
$this->encrypt = new \CodeIgniter\Encryption\Encryption($expected);
9594
foreach ($expected as $key => $value)
@@ -115,12 +114,11 @@ public function testInitialization()
115114
// make sure we can over-ride any parameter
116115
// change the driver once we have more than 1
117116
$expected = [
118-
'driver' => 'OpenSSL', // The PHP extension we plan to use
119-
'key' => 'Top banana', // no starting key material
120-
'cipher' => 'AES-256-CBC', // Encryption cipher
121-
'hmac' => 'HMAC', // Use HMAC message authentication (true/false)
122-
'digest' => 'SHA512', // HMAC digest algorithm to use
123-
'base64' => 'base64', // Base64 encoding?
117+
'driver' => 'OpenSSL', // The PHP extension we plan to use
118+
'key' => 'Top banana', // no starting key material
119+
'cipher' => 'AES-256-CBC', // Encryption cipher
120+
'digest' => 'SHA512', // HMAC digest algorithm to use
121+
'encoding' => 'base64', // Base64 encoding?
124122
];
125123
$this->encrypt = $this->encryption->initialize($expected);
126124
foreach ($expected as $key => $value)

tests/system/Encryption/OpenSSLHandlerTest.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public function testWithDES()
8181
/**
8282
* test with & without HMAC
8383
*/
84-
public function testAuthentication()
84+
public function testWithAuthentication()
8585
{
8686
$params = [
8787
'driver' => 'OpenSSL',
88-
'hmac' => 'hmac',
89-
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
88+
'digest' => 'SHA512',
89+
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
9090
];
9191

9292
$encrypter = $this->encryption->initialize($params);
@@ -99,13 +99,13 @@ public function testAuthentication()
9999
}
100100

101101
/**
102-
* test with & without encoding
102+
* test with & without HMAC
103103
*/
104-
public function testWithoutEncoding()
104+
public function testWithoutAuthentication()
105105
{
106106
$params = [
107107
'driver' => 'OpenSSL',
108-
'base64' => 'none',
108+
'digest' => '',
109109
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
110110
];
111111

@@ -118,15 +118,35 @@ public function testWithoutEncoding()
118118
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
119119
}
120120

121+
/**
122+
* test with & without encoding
123+
*/
124+
public function testWithoutEncoding()
125+
{
126+
$params = [
127+
'driver' => 'OpenSSL',
128+
'encoding' => '',
129+
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
130+
];
131+
132+
$encrypter = $this->encryption->initialize($params);
133+
134+
// simple encrypt/decrypt, default parameters
135+
$message = 'This is a plain-text message.';
136+
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
137+
$message = 'This is a different plain-text message.';
138+
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
139+
}
140+
121141
/**
122142
* test with & without encoding
123143
*/
124144
public function testWithEncoding()
125145
{
126146
$params = [
127-
'driver' => 'OpenSSL',
128-
'base64' => 'base64',
129-
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
147+
'driver' => 'OpenSSL',
148+
'encoding' => 'base64',
149+
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
130150
];
131151

132152
$encrypter = $this->encryption->initialize($params);
@@ -138,5 +158,24 @@ public function testWithEncoding()
138158
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
139159
}
140160

161+
/**
162+
* test with & without encoding
163+
*/
164+
public function testWithHexEncoding()
165+
{
166+
$params = [
167+
'driver' => 'OpenSSL',
168+
'encoding' => 'hex',
169+
'key' => '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c'
170+
];
171+
172+
$encrypter = $this->encryption->initialize($params);
173+
174+
// simple encrypt/decrypt, default parameters
175+
$message = 'This is a plain-text message.';
176+
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
177+
$message = 'This is a different plain-text message.';
178+
$this->assertEquals($message, $encrypter->decrypt($encrypter->encrypt($message)));
179+
}
141180

142181
}

0 commit comments

Comments
 (0)