From 1d924cfc7ba2626d811846ec751992f3c9c1a79b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 25 May 2015 22:30:38 -0500 Subject: [PATCH 1/7] RSA: add "none" encryption mode --- phpseclib/Crypt/RSA.php | 33 +++++++++++++++++++++++++++++++ tests/Unit/Crypt/RSA/ModeTest.php | 30 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/Unit/Crypt/RSA/ModeTest.php diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 3146ca14..85477e89 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -107,6 +107,13 @@ define('CRYPT_RSA_ENCRYPTION_OAEP', 1); * compatibility with protocols (like SSH-1) written before OAEP's introduction. */ define('CRYPT_RSA_ENCRYPTION_PKCS1', 2); +/** + * Do not use any padding + * + * Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy + * stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc. + */ +define('CRYPT_RSA_ENCRYPTION_NONE', 3); /**#@-*/ /**#@+ @@ -2444,6 +2451,22 @@ class Crypt_RSA return substr($m, 1); } + /** + * Raw Encryption / Decryption + * + * Doesn't use padding and is not recommended. + * + * @access private + * @param String $m + * @return String + */ + function _raw_encrypt($m) + { + $temp = $this->_os2ip($m); + $temp = $this->_rsaep($temp); + return $this->_i2osp($temp, $this->k); + } + /** * RSAES-PKCS1-V1_5-ENCRYPT * @@ -2890,6 +2913,13 @@ class Crypt_RSA function encrypt($plaintext) { switch ($this->encryptionMode) { + case CRYPT_RSA_ENCRYPTION_NONE: + $plaintext = str_split($plaintext, $this->k); + $ciphertext = ''; + foreach ($plaintext as $m) { + $ciphertext.= $this->_raw_encrypt($m); + } + return $ciphertext; case CRYPT_RSA_ENCRYPTION_PKCS1: $length = $this->k - 11; if ($length <= 0) { @@ -2938,6 +2968,9 @@ class Crypt_RSA $plaintext = ''; switch ($this->encryptionMode) { + case CRYPT_RSA_ENCRYPTION_NONE: + $decrypt = '_raw_encrypt'; + break; case CRYPT_RSA_ENCRYPTION_PKCS1: $decrypt = '_rsaes_pkcs1_v1_5_decrypt'; break; diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php new file mode 100644 index 00000000..113488cf --- /dev/null +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -0,0 +1,30 @@ + + * @copyright 2013 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +require_once 'Crypt/RSA.php' ; + +class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase +{ + public function testEncryptionModeNone() + { + $plaintext = 'a'; + + $rsa = new Crypt_RSA(); + + extract($rsa->createKey()); + $rsa->loadKey($publickey); + + $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); + $a = $rsa->encrypt($plaintext); + $b = $rsa->encrypt($plaintext); + + $this->assertEquals($a, $b); + + $rsa->loadKey($privatekey); + $this->assertEquals(trim($rsa->decrypt($a), "\0"), $plaintext); + } +} From 26d528855a2847b09bba2123a6f6fdd057a1cc2c Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 02:13:11 -0500 Subject: [PATCH 2/7] Tests/RSA: hard-coded key and result --- tests/Unit/Crypt/RSA/ModeTest.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index 113488cf..197a367c 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -13,16 +13,29 @@ class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase { $plaintext = 'a'; - $rsa = new Crypt_RSA(); - - extract($rsa->createKey()); - $rsa->loadKey($publickey); + $privatekey = '-----BEGIN RSA PRIVATE KEY----- +MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp +wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 +1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh +3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2 +pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX +GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il +AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF +L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k +X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl +U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ +37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= +-----END RSA PRIVATE KEY-----'; + $rsa->loadKey($privatekey); + $rsa->loadKey($rsa->getPublicKey()); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); - $a = $rsa->encrypt($plaintext); - $b = $rsa->encrypt($plaintext); + $expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' . + '5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' . + '4d04bd467f6f1e7c'; + $result = $rsa->encrypt($plaintext); - $this->assertEquals($a, $b); + $this->assertEquals($result, $expected); $rsa->loadKey($privatekey); $this->assertEquals(trim($rsa->decrypt($a), "\0"), $plaintext); From 0166d3a7ece7c2d6dbd38ecc0f318907958cf46d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 02:20:16 -0500 Subject: [PATCH 3/7] Tests/RSA: fix error --- tests/Unit/Crypt/RSA/ModeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index 197a367c..b0b84bc3 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -38,6 +38,6 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ $this->assertEquals($result, $expected); $rsa->loadKey($privatekey); - $this->assertEquals(trim($rsa->decrypt($a), "\0"), $plaintext); + $this->assertEquals(trim($rsa->decrypt($result), "\0"), $plaintext); } } From 1316fd45f44d44f7e3b414d804f5210737f199f8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 09:17:53 -0500 Subject: [PATCH 4/7] Tests/RSA: use correct constant --- tests/Unit/Crypt/RSA/ModeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index f0a21dc9..a7ebb45b 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -31,7 +31,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ $rsa->loadKey($privatekey); $rsa->loadKey($rsa->getPublicKey()); - $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); + $rsa->setEncryptionMode(RSA::ENCRYPTION_NONE); $expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' . '5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' . '4d04bd467f6f1e7c'; From 6bce2c79ac2b0abfc526d12d882df788a1be376c Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 09:20:03 -0500 Subject: [PATCH 5/7] Tests/RSA: re-added accidentally removed $rsa initialization --- tests/Unit/Crypt/RSA/ModeTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index b0b84bc3..3216e417 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -13,6 +13,8 @@ class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase { $plaintext = 'a'; + $rsa = new Crypt_RSA(); + $privatekey = '-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 From 2536d224ee6e43e19ba7d3c7b91a7d63e2517ff8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 10:06:13 -0500 Subject: [PATCH 6/7] Tests/RSA: pack expected result --- tests/Unit/Crypt/RSA/ModeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index a7ebb45b..0b90210e 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -35,6 +35,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ $expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' . '5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' . '4d04bd467f6f1e7c'; + $expected = pack('H*', $expected); $result = $rsa->encrypt($plaintext); $this->assertEquals($result, $expected); From ab489801fe1a3c45bad2292d7a22e06914316038 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 31 May 2015 10:07:18 -0500 Subject: [PATCH 7/7] Tests/RSA: pack expected result --- tests/Unit/Crypt/RSA/ModeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index 3216e417..99d2806c 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -35,6 +35,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ $expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' . '5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' . '4d04bd467f6f1e7c'; + $expected = pack('H*', $expected); $result = $rsa->encrypt($plaintext); $this->assertEquals($result, $expected);