From 35d8974ac1a478ba4c29e0a228688f526e49dbdd Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 4 Dec 2021 16:27:52 -0600 Subject: [PATCH 1/3] Tests: ChaCha20 and Salsa20 unit tests weren't being ran --- tests/Unit/Crypt/{ChaCha20.php => ChaCha20Test.php} | 0 tests/Unit/Crypt/{Salsa20.php => Salsa20Test.php} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/Unit/Crypt/{ChaCha20.php => ChaCha20Test.php} (100%) rename tests/Unit/Crypt/{Salsa20.php => Salsa20Test.php} (100%) diff --git a/tests/Unit/Crypt/ChaCha20.php b/tests/Unit/Crypt/ChaCha20Test.php similarity index 100% rename from tests/Unit/Crypt/ChaCha20.php rename to tests/Unit/Crypt/ChaCha20Test.php diff --git a/tests/Unit/Crypt/Salsa20.php b/tests/Unit/Crypt/Salsa20Test.php similarity index 100% rename from tests/Unit/Crypt/Salsa20.php rename to tests/Unit/Crypt/Salsa20Test.php From 4141799c02c0fb99a2ede952a6aeb59b1907382d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 4 Dec 2021 16:32:07 -0600 Subject: [PATCH 2/3] Tests: fix issues with Salsa20 / ChaCha20 unit tests --- phpseclib/Crypt/Salsa20.php | 4 +++- tests/Unit/Crypt/ChaCha20Test.php | 40 +++++++++++++++---------------- tests/Unit/Crypt/Salsa20Test.php | 6 ++--- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/phpseclib/Crypt/Salsa20.php b/phpseclib/Crypt/Salsa20.php index d76af6af..6c3faeca 100644 --- a/phpseclib/Crypt/Salsa20.php +++ b/phpseclib/Crypt/Salsa20.php @@ -341,7 +341,9 @@ class Salsa20 extends StreamCipher } else { $buffer = &$this->debuffer; } - if (strlen($buffer['ciphertext'])) { + if (!strlen($buffer['ciphertext'])) { + $ciphertext = ''; + } else { $ciphertext = $text ^ Strings::shift($buffer['ciphertext'], strlen($text)); $text = substr($text, strlen($ciphertext)); if (!strlen($text)) { diff --git a/tests/Unit/Crypt/ChaCha20Test.php b/tests/Unit/Crypt/ChaCha20Test.php index 67acaae7..ab5c784b 100644 --- a/tests/Unit/Crypt/ChaCha20Test.php +++ b/tests/Unit/Crypt/ChaCha20Test.php @@ -93,11 +93,10 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase $expected = pack('H*', $expected); $c = new ChaCha20; + $c->setPoly1305Key($key); $r = new \ReflectionClass(get_class($c)); - $p = $r->getProperty('poly1305Key'); - $p->setAccessible(true); - $p->setValue($c, $key); - + // this unit test is testing Poly1305 independent of ChaCha20, which phpseclib doesn't + // really support, hence this hackish approach $m = $r->getMethod('poly1305'); $m->setAccessible(true); $result = $m->invokeArgs($c, [$plaintext]); @@ -108,7 +107,7 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase // see https://tools.ietf.org/html/rfc8439#section-2.6.2 public function test262() { - $key = implode('', range("\80", "\x9f")); + $key = implode('', range("\x80", "\x9f")); $nonce = '00 00 00 00 00 01 02 03 04 05 06 07'; $nonce = str_replace(' ', '', $nonce); @@ -119,25 +118,26 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase $expected = str_replace(' ', '', $expected); $expected = pack('H*', $expected); - $engines = ['PHP', 'OpenSSL', 'libsodium']; - foreach ($engines as $engine) { - $c = new ChaCha20(); - $c->setKey($key); - $c->setNonce($nonce); - //$c->setCounter(0); - $c->setPreferredEngine($engine); - if ($c->getEngine() != $engine) { - continue; - } - $result = $c->encrypt($plaintext); - $this->assertSame($expected, $result, "Failed asserting that ciphertext matches expected value with $engine engine"); - } + $c = new ChaCha20(); + $c->setKey($key); + $c->setNonce($nonce); + + $r = new \ReflectionClass(get_class($c)); + $m = $r->getMethod('createPoly1305Key'); + $m->setAccessible(true); + $result = $m->invoke($c); + + $p = $r->getProperty('poly1305Key'); + $p->setAccessible(true); + $actual = $p->getValue($c); + + $this->assertSame($expected, $actual, 'Failed asserting that the poly1305 key is what it ought to be'); } // https://tools.ietf.org/html/rfc8439#section-2.8.2 public function test282() { - $key = implode('', range("\80", "\x9f")); + $key = implode('', range("\x80", "\x9f")); $nonce = "\x07\0\0\0" . "\x40\x41\x42\x43\x44\x45\x46\x47"; @@ -160,7 +160,7 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase $expected = pack('H*', $expected); $tag = '1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91'; - $tag = str_replace(' ', '', $tag); + $tag = str_replace(':', '', $tag); $tag = pack('H*', $tag); $engines = ['PHP', 'OpenSSL', 'libsodium']; diff --git a/tests/Unit/Crypt/Salsa20Test.php b/tests/Unit/Crypt/Salsa20Test.php index 8d2f8a3f..678d82ba 100644 --- a/tests/Unit/Crypt/Salsa20Test.php +++ b/tests/Unit/Crypt/Salsa20Test.php @@ -133,9 +133,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase foreach ($engines as $engine) { foreach ($tests as $test) { - foreach ($test['output'] as $output) { - $result[] = [$engine, $test['key'], $output['iv'], $output['result']]; - } + $result[] = [$engine, $test['key'], $test['iv'], $test['result']]; } } @@ -147,7 +145,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase */ public function testVectors($engine, $key, $iv, $expected) { - $cipher = new Salsa(); + $cipher = new Salsa20(); $cipher->setPreferredEngine($engine); $cipher->setKey(pack('H*', $key)); $cipher->setNonce(pack('H*', $iv)); From 8c137a19e494e4399a44b52ea6adc1e9a5b4053b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 4 Dec 2021 22:21:33 -0600 Subject: [PATCH 3/3] Tests/Salsa20: use stream[0..63] instead of xor-digest --- tests/Unit/Crypt/Salsa20Test.php | 98 ++++++++++++++++---------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/tests/Unit/Crypt/Salsa20Test.php b/tests/Unit/Crypt/Salsa20Test.php index 678d82ba..81d7570c 100644 --- a/tests/Unit/Crypt/Salsa20Test.php +++ b/tests/Unit/Crypt/Salsa20Test.php @@ -22,110 +22,110 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase [ 'key' => '80000000000000000000000000000000', 'iv' => '0000000000000000', - 'result' => 'F7A274D268316790A67EC058F45C0F2A' . - '067A99FCDE6236C0CEF8E056349FE54C' . - '5F13AC74D2539570FD34FEAB06C57205' . - '3949B59585742181A5A760223AFA22D4' + 'result' => '4DFA5E481DA23EA09A31022050859936' . + 'DA52FCEE218005164F267CB65F5CFD7F' . + '2B4F97E0FF16924A52DF269515110A07' . + 'F9E460BC65EF95DA58F740B7D1DBB0AA' ], // set 2 [ 'key' => '00000000000000000000000000000000', 'iv' => '0000000000000000', - 'result' => '6D3937FFA13637648E477623277644AD' . - 'AD3854E6B2B3E4D68155356F68B30490' . - '842B2AEA2E32239BE84E613C6CE1B9BD' . - '026094962CB1A6757AF5A13DDAF8252C' + 'result' => '6513ADAECFEB124C1CBE6BDAEF690B4F' . + 'FB00B0FCACE33CE806792BB414801998' . + '34BFB1CFDD095802C6E95E251002989A' . + 'C22AE588D32AE79320D9BD7732E00338' ], // set 3 [ 'key' => '000102030405060708090A0B0C0D0E0F', 'iv' => '0000000000000000', - 'result' => 'F3BCF4D6381742839C5627050D4B227F' . - 'EB1ECCC527BF605C4CB9D6FB0618F419' . - 'B51846707550BBEEE381E44A50A406D0' . - '20C8433D08B19C98EFC867ED9897EDBB' + 'result' => '2DD5C3F7BA2B20F76802410C68868889' . + '5AD8C1BD4EA6C9B140FB9B90E21049BF' . + '583F527970EBC1A4C4C5AF117A5940D9' . + '2B98895B1902F02BF6E9BEF8D6B4CCBE' ], // set 4 [ 'key' => '0053A6F94C9FF24598EB3E91E4378ADD', 'iv' => '0000000000000000', - 'result' => '196D1A0977F0585B23367497D449E11D' . - 'E328ECD944BC133F786348C9591B35B7' . - '189CDDD934757ED8F18FBC984DA377A8' . - '07147F1A6A9A8759FD2A062FD76D275E' + 'result' => 'BE4EF3D2FAC6C4C3D822CE67436A407C' . + 'C237981D31A65190B51053D13A19C89F' . + 'C90ACB45C8684058733EDD259869C58E' . + 'EF760862BEFBBCA0F6E675FD1FA25C27' ], // set 5 [ 'key' => '00000000000000000000000000000000', 'iv' => '8000000000000000', - 'result' => '104639D9F65C879F7DFF8A82A94C130C' . - 'D6C727B3BC8127943ACDF0AB7AD6D28B' . - 'F2ADF50D81F50C53D0FDFE15803854C7' . - 'D67F6C9B4752275696E370A467A4C1F8' + 'result' => 'B66C1E4446DD9557E578E223B0B76801' . + '7B23B267BB0234AE4626BF443F219776' . + '436FB19FD0E8866FCD0DE9A9538F4A09' . + 'CA9AC0732E30BCF98E4F13E4B9E201D9' ], // set 6 [ 'key' => '0053A6F94C9FF24598EB3E91E4378ADD', 'iv' => '0D74DB42A91077DE', - 'result' => '620BB4C2ED20F4152F0F86053D3F5595' . - '8E1FBA48F5D86B25C8F31559F3158072' . - '6E7ED8525D0B9EA5264BF97750713476' . - '1EF65FE195274AFBF000938C03BA59A7' + 'result' => '05E1E7BEB697D999656BF37C1B978806' . + '735D0B903A6007BD329927EFBE1B0E2A' . + '8137C1AE291493AA83A821755BEE0B06' . + 'CD14855A67E46703EBF8F3114B584CBA' ], // key size: 256 bits // set 1 [ 'key' => '8000000000000000000000000000000000000000000000000000000000000000', 'iv' => '0000000000000000', - 'result' => '50EC2485637DB19C6E795E9C73938280' . - '6F6DB320FE3D0444D56707D7B456457F' . - '3DB3E8D7065AF375A225A70951C8AB74' . - '4EC4D595E85225F08E2BC03FE1C42567' + 'result' => 'E3BE8FDD8BECA2E3EA8EF9475B29A6E7' . + '003951E1097A5C38D23B7A5FAD9F6844' . + 'B22C97559E2723C7CBBD3FE4FC8D9A07' . + '44652A83E72A9C461876AF4D7EF1A117' ], // set 2 [ 'key' => '0000000000000000000000000000000000000000000000000000000000000000', 'iv' => '0000000000000000', - 'result' => '7C3A1499A63B507B0BC75824ABEEAA26' . - '109101C5B915F0F554DD9950045D02FA' . - 'FF815CA8B2C7CFF3625765697B80B026' . - '7EA87E25412564BD71DD05843A60465E' + 'result' => '9A97F65B9B4C721B960A672145FCA8D4' . + 'E32E67F9111EA979CE9C4826806AEEE6' . + '3DE9C0DA2BD7F91EBCB2639BF989C625' . + '1B29BF38D39A9BDCE7C55F4B2AC12A39' ], // set 3 [ 'key' => '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F', 'iv' => '0000000000000000', - 'result' => '8C03E9237FEE95D5041C753C204D2B35' . - '764E4A53035A76F9EFBADD7E63E60B69' . - 'BF23F7C5FD39B2249B0C628FB654D521' . - '4EB588371E5D2F34BF51396AF3ACB666' + 'result' => 'B580F7671C76E5F7441AF87C146D6B51' . + '3910DC8B4146EF1B3211CF12AF4A4B49' . + 'E5C874B3EF4F85E7D7ED539FFEBA73EB' . + '73E0CCA74FBD306D8AA716C7783E89AF' ], // set 4 [ 'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D', 'iv' => '0000000000000000', - 'result' => '2052F9A2853E989133D10938222AC76D' . - 'B8B4CBA135ACB59970DDF9C074C6271A' . - '5C4E2A7A00D2D697EDFC9B1FF9B365C8' . - '7347B23020663A30711A71E3A02AB00C' + 'result' => 'F9D2DC274BB55AEFC2A0D9F8A982830F' . + '6916122BC0A6870F991C6ED8D00D2F85' . + '94E3151DE4C5A19A9A06FBC191C87BF0' . + '39ADF971314BAF6D02337080F2DAE5CE' ], // set 5 [ 'key' => '0000000000000000000000000000000000000000000000000000000000000000', 'iv' => '8000000000000000', - 'result' => 'FE40F57D1586D7664C2FCA5AB10BD7C7' . - '9DE3234836E76949F9DC01CBFABC6D6C' . - '42AB27DDC748B4DF7991092972AB4985' . - 'CEC19B3E7C2C85D6E25A338DEC288282' + 'result' => '2ABA3DC45B4947007B14C851CD694456' . + 'B303AD59A465662803006705673D6C3E' . + '29F1D3510DFC0405463C03414E0E07E3' . + '59F1F1816C68B2434A19D3EEE0464873' ], // set 6 [ 'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D', 'iv' => '0D74DB42A91077DE', - 'result' => 'C349B6A51A3EC9B712EAED3F90D8BCEE' . - '69B7628645F251A996F55260C62EF31F' . - 'D6C6B0AEA94E136C9D984AD2DF3578F7' . - '8E457527B03A0450580DD874F63B1AB9' + 'result' => 'F5FAD53F79F9DF58C4AEA0D0ED9A9601' . + 'F278112CA7180D565B420A48019670EA' . + 'F24CE493A86263F677B46ACE1924773D' . + '2BB25571E1AA8593758FC382B1280B71' ], ]; @@ -153,6 +153,6 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase self::markTestSkipped('Unable to initialize ' . $engine . ' engine for ' . (strlen($key) * 8) . '-bit key'); } $result = $cipher->encrypt(str_repeat("\0", 64)); - $this->assertEquals(bin2hex($result), $expected, "Failed asserting that key $key / $iv yielded expected output in $engine engine"); + $this->assertEquals(strtoupper(bin2hex($result)), $expected, "Failed asserting that key $key / $iv yielded expected output in $engine engine"); } }