Skip to content

Commit b2a73d6

Browse files
committed
Crypt: keys of an invalid length weren't being resized
1 parent 7ba5d84 commit b2a73d6

9 files changed

Lines changed: 84 additions & 117 deletions

File tree

src/Crypt/AES.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,21 @@
5959
class AES extends Base
6060
{
6161
/**
62-
* Sets the key length
62+
* Turns key lengths, be they valid or invalid, to valid key lengths
6363
*
64-
* Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
65-
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
66-
*
67-
* @see \phpseclib\Crypt\Rijndael:setKeyLength()
68-
* @access public
6964
* @param int $length
65+
* @access private
66+
* @return int
7067
*/
71-
public function setKeyLength($length)
68+
protected function calculateNewKeyLength($length)
7269
{
7370
switch (true) {
7471
case $length <= 128:
75-
$length = 128;
76-
break;
72+
return 128;
7773
case $length <= 192:
78-
$length = 192;
79-
break;
74+
return 192;
8075
default:
81-
$length = 256;
76+
return 256;
8277
}
83-
parent::setKeyLength($length);
8478
}
8579
}

src/Crypt/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function setKeyLength($length)
263263
{
264264
// algorithms that have a fixed key length should override this with a method that does nothing
265265
$this->changed = true;
266-
$this->key_length = $length;
266+
$this->key_length = static::calculateNewKeyLength($length);
267267
$this->explicit_key_length = true;
268268
}
269269

@@ -307,7 +307,7 @@ public function setKey($key)
307307
{
308308
$this->key = $key;
309309
if (!$this->explicit_key_length) {
310-
$this->key_length = strlen($key) << 3;
310+
$this->key_length = static::calculateNewKeyLength(strlen($key) << 3);
311311
}
312312
$this->changed = true;
313313
}

src/Crypt/Blowfish.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@
4747
class Blowfish extends Base
4848
{
4949
/**
50-
* Sets the key length.
50+
* Turns key lengths, be they valid or invalid, to valid key lengths
5151
*
52-
* Key lengths can be between 32 and 448 bits.
53-
*
54-
* @access public
5552
* @param int $length
53+
* @access private
54+
* @return int
5655
*/
57-
public function setKeyLength($length)
56+
protected function calculateNewKeyLength($length)
5857
{
59-
if ($length < 32) {
60-
$length = 32;
61-
} elseif ($length > 448) {
62-
$length = 448;
58+
switch (true) {
59+
case $length < 32:
60+
return 32;
61+
case $length > 448:
62+
return 448;
6363
}
64-
parent::setKeyLength($length);
64+
return $length;
6565
}
6666
}

src/Crypt/DES.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
class DES extends Base
5353
{
5454
/**
55-
* Dummy method
55+
* Turns key lengths, be they valid or invalid, to valid key lengths
5656
*
57-
* @access public
5857
* @param int $length
58+
* @access private
59+
* @return int
5960
*/
60-
public function setKeyLength($length)
61+
protected function calculateNewKeyLength($length)
6162
{
62-
parent::setKeyLength(64);
63+
return 64;
6364
}
6465
}

src/Crypt/RC2.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,22 @@ public function __construct($mode = self::MODE_CBC)
7373
}
7474

7575
/**
76-
* Sets the key length.
76+
* Turns key lengths, be they valid or invalid, to valid key lengths
7777
*
78-
* Valid key lengths are 8 to 1024.
79-
* Calling this function after setting the key has no effect until the next
80-
* \phpseclib\Crypt\RC2::setKey() call.
81-
*
82-
* @access public
83-
* @param int $length in bits
78+
* @param int $length
79+
* @access private
80+
* @return int
8481
*/
85-
public function setKeyLength($length)
82+
protected function calculateNewKeyLength($length)
8683
{
87-
if ($length < 8) {
88-
$length = 8;
89-
} elseif ($length > 1024) {
90-
$length = 1024;
84+
switch (true) {
85+
case $length < 8:
86+
return 8;
87+
case $length > 1024:
88+
return 1024;
9189
}
92-
parent::setKeyLength($length);
90+
91+
return $length;
9392
}
9493

9594
/**

src/Crypt/RC4.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,21 @@
5454
class RC4 extends Base
5555
{
5656
/**
57-
* Sets the key length
57+
* Turns key lengths, be they valid or invalid, to valid key lengths
5858
*
59-
* Keys can be between 1 and 256 bytes long.
60-
*
61-
* @access public
6259
* @param int $length
60+
* @access private
61+
* @return int
6362
*/
64-
public function setKeyLength($length)
63+
protected function calculateNewKeyLength($length)
6564
{
66-
if ($length < 8) {
67-
$length = 8;
68-
} elseif ($length > 2048) {
69-
$length = 2048;
65+
switch (true) {
66+
case $length < 8:
67+
return 8;
68+
case $length > 2048:
69+
return 2048;
7070
}
71-
parent::setKeyLength($length);
71+
72+
return $length;
7273
}
7374
}

src/Crypt/Rijndael.php

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,47 +63,6 @@
6363
*/
6464
class Rijndael extends Base
6565
{
66-
/**
67-
* Sets the key length.
68-
*
69-
* Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to
70-
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
71-
*
72-
* Note: phpseclib extends Rijndael (and AES) for using 160- and 224-bit keys but they are officially not defined
73-
* and the most (if not all) implementations are not able using 160/224-bit keys but round/pad them up to
74-
* 192/256 bits as, for example, mcrypt will do.
75-
*
76-
* That said, if you want be compatible with other Rijndael and AES implementations,
77-
* you should not setKeyLength(160) or setKeyLength(224).
78-
*
79-
* Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use
80-
* the mcrypt php extension, even if available.
81-
* This results then in slower encryption.
82-
*
83-
* @access public
84-
* @param int $length
85-
*/
86-
public function setKeyLength($length)
87-
{
88-
switch (true) {
89-
case $length <= 128:
90-
$length = 128;
91-
break;
92-
case $length <= 160:
93-
$length = 160;
94-
break;
95-
case $length <= 192:
96-
$length = 192;
97-
break;
98-
case $length <= 224:
99-
$length = 224;
100-
break;
101-
default:
102-
$length = 256;
103-
}
104-
parent::setKeyLength($length);
105-
}
106-
10766
/**
10867
* Sets the block length
10968
*
@@ -123,4 +82,27 @@ public function setBlockLength($length)
12382
}
12483
$this->cipher->setBlockLength($length);
12584
}
85+
86+
/**
87+
* Turns key lengths, be they valid or invalid, to valid key lengths
88+
*
89+
* @param int $length
90+
* @access private
91+
* @return int
92+
*/
93+
protected function calculateNewKeyLength($length)
94+
{
95+
switch (true) {
96+
case $length <= 128:
97+
return 128;
98+
case $length <= 160:
99+
return 160;
100+
case $length <= 192:
101+
return 192;
102+
case $length <= 224:
103+
return 224;
104+
default:
105+
return 256;
106+
}
107+
}
126108
}

src/Crypt/TripleDES.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,21 @@ public function __construct($mode = self::MODE_CBC)
9696
}
9797

9898
/**
99-
* Sets the key length
99+
* Turns key lengths, be they valid or invalid, to valid key lengths
100100
*
101-
* Keys can be between 1 and 256 bytes long.
102-
*
103-
* @access public
104101
* @param int $length
102+
* @access private
103+
* @return int
105104
*/
106-
public function setKeyLength($length)
105+
protected function calculateNewKeyLength($length)
107106
{
108107
switch (true) {
109108
case $length <= 64:
110-
$length = 64;
111-
break;
109+
return 64;
112110
case $length <= 128:
113-
$length = 128;
114-
break;
111+
return 128;
115112
default:
116-
$length = 192;
113+
return 192;
117114
}
118-
119-
parent::setKeyLength($length);
120115
}
121116
}

src/Crypt/Twofish.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,21 @@
4747
class Twofish extends Base
4848
{
4949
/**
50-
* Sets the key length
50+
* Turns key lengths, be they valid or invalid, to valid key lengths
5151
*
52-
* Keys can be between 1 and 256 bytes long.
53-
*
54-
* @access public
5552
* @param int $length
53+
* @access private
54+
* @return int
5655
*/
57-
public function setKeyLength($length)
56+
protected function calculateNewKeyLength($length)
5857
{
5958
switch (true) {
6059
case $length <= 128:
61-
$length = 128;
62-
break;
60+
return 128;
6361
case $length <= 192:
64-
$length = 192;
65-
break;
62+
return 192;
6663
default:
67-
$length = 256;
64+
return 256;
6865
}
69-
70-
parent::setKeyLength($length);
7166
}
7267
}

0 commit comments

Comments
 (0)