Merge branch '3.0'

This commit is contained in:
terrafrost 2021-12-04 22:59:34 -06:00
commit 9c44f493ce
3 changed files with 74 additions and 74 deletions

View File

@ -341,7 +341,9 @@ class Salsa20 extends StreamCipher
} else { } else {
$buffer = &$this->debuffer; $buffer = &$this->debuffer;
} }
if (strlen($buffer['ciphertext'])) { if (!strlen($buffer['ciphertext'])) {
$ciphertext = '';
} else {
$ciphertext = $text ^ Strings::shift($buffer['ciphertext'], strlen($text)); $ciphertext = $text ^ Strings::shift($buffer['ciphertext'], strlen($text));
$text = substr($text, strlen($ciphertext)); $text = substr($text, strlen($ciphertext));
if (!strlen($text)) { if (!strlen($text)) {

View File

@ -93,11 +93,10 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
$expected = pack('H*', $expected); $expected = pack('H*', $expected);
$c = new ChaCha20; $c = new ChaCha20;
$c->setPoly1305Key($key);
$r = new \ReflectionClass(get_class($c)); $r = new \ReflectionClass(get_class($c));
$p = $r->getProperty('poly1305Key'); // this unit test is testing Poly1305 independent of ChaCha20, which phpseclib doesn't
$p->setAccessible(true); // really support, hence this hackish approach
$p->setValue($c, $key);
$m = $r->getMethod('poly1305'); $m = $r->getMethod('poly1305');
$m->setAccessible(true); $m->setAccessible(true);
$result = $m->invokeArgs($c, [$plaintext]); $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 // see https://tools.ietf.org/html/rfc8439#section-2.6.2
public function test262() 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 = '00 00 00 00 00 01 02 03 04 05 06 07';
$nonce = str_replace(' ', '', $nonce); $nonce = str_replace(' ', '', $nonce);
@ -119,25 +118,26 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
$expected = str_replace(' ', '', $expected); $expected = str_replace(' ', '', $expected);
$expected = pack('H*', $expected); $expected = pack('H*', $expected);
$engines = ['PHP', 'OpenSSL', 'libsodium']; $c = new ChaCha20();
foreach ($engines as $engine) { $c->setKey($key);
$c = new ChaCha20(); $c->setNonce($nonce);
$c->setKey($key);
$c->setNonce($nonce); $r = new \ReflectionClass(get_class($c));
//$c->setCounter(0); $m = $r->getMethod('createPoly1305Key');
$c->setPreferredEngine($engine); $m->setAccessible(true);
if ($c->getEngine() != $engine) { $result = $m->invoke($c);
continue;
} $p = $r->getProperty('poly1305Key');
$result = $c->encrypt($plaintext); $p->setAccessible(true);
$this->assertSame($expected, $result, "Failed asserting that ciphertext matches expected value with $engine engine"); $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 // https://tools.ietf.org/html/rfc8439#section-2.8.2
public function test282() 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"; $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); $expected = pack('H*', $expected);
$tag = '1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91'; $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); $tag = pack('H*', $tag);
$engines = ['PHP', 'OpenSSL', 'libsodium']; $engines = ['PHP', 'OpenSSL', 'libsodium'];

View File

@ -22,110 +22,110 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
[ [
'key' => '80000000000000000000000000000000', 'key' => '80000000000000000000000000000000',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => 'F7A274D268316790A67EC058F45C0F2A' . 'result' => '4DFA5E481DA23EA09A31022050859936' .
'067A99FCDE6236C0CEF8E056349FE54C' . 'DA52FCEE218005164F267CB65F5CFD7F' .
'5F13AC74D2539570FD34FEAB06C57205' . '2B4F97E0FF16924A52DF269515110A07' .
'3949B59585742181A5A760223AFA22D4' 'F9E460BC65EF95DA58F740B7D1DBB0AA'
], ],
// set 2 // set 2
[ [
'key' => '00000000000000000000000000000000', 'key' => '00000000000000000000000000000000',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '6D3937FFA13637648E477623277644AD' . 'result' => '6513ADAECFEB124C1CBE6BDAEF690B4F' .
'AD3854E6B2B3E4D68155356F68B30490' . 'FB00B0FCACE33CE806792BB414801998' .
'842B2AEA2E32239BE84E613C6CE1B9BD' . '34BFB1CFDD095802C6E95E251002989A' .
'026094962CB1A6757AF5A13DDAF8252C' 'C22AE588D32AE79320D9BD7732E00338'
], ],
// set 3 // set 3
[ [
'key' => '000102030405060708090A0B0C0D0E0F', 'key' => '000102030405060708090A0B0C0D0E0F',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => 'F3BCF4D6381742839C5627050D4B227F' . 'result' => '2DD5C3F7BA2B20F76802410C68868889' .
'EB1ECCC527BF605C4CB9D6FB0618F419' . '5AD8C1BD4EA6C9B140FB9B90E21049BF' .
'B51846707550BBEEE381E44A50A406D0' . '583F527970EBC1A4C4C5AF117A5940D9' .
'20C8433D08B19C98EFC867ED9897EDBB' '2B98895B1902F02BF6E9BEF8D6B4CCBE'
], ],
// set 4 // set 4
[ [
'key' => '0053A6F94C9FF24598EB3E91E4378ADD', 'key' => '0053A6F94C9FF24598EB3E91E4378ADD',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '196D1A0977F0585B23367497D449E11D' . 'result' => 'BE4EF3D2FAC6C4C3D822CE67436A407C' .
'E328ECD944BC133F786348C9591B35B7' . 'C237981D31A65190B51053D13A19C89F' .
'189CDDD934757ED8F18FBC984DA377A8' . 'C90ACB45C8684058733EDD259869C58E' .
'07147F1A6A9A8759FD2A062FD76D275E' 'EF760862BEFBBCA0F6E675FD1FA25C27'
], ],
// set 5 // set 5
[ [
'key' => '00000000000000000000000000000000', 'key' => '00000000000000000000000000000000',
'iv' => '8000000000000000', 'iv' => '8000000000000000',
'result' => '104639D9F65C879F7DFF8A82A94C130C' . 'result' => 'B66C1E4446DD9557E578E223B0B76801' .
'D6C727B3BC8127943ACDF0AB7AD6D28B' . '7B23B267BB0234AE4626BF443F219776' .
'F2ADF50D81F50C53D0FDFE15803854C7' . '436FB19FD0E8866FCD0DE9A9538F4A09' .
'D67F6C9B4752275696E370A467A4C1F8' 'CA9AC0732E30BCF98E4F13E4B9E201D9'
], ],
// set 6 // set 6
[ [
'key' => '0053A6F94C9FF24598EB3E91E4378ADD', 'key' => '0053A6F94C9FF24598EB3E91E4378ADD',
'iv' => '0D74DB42A91077DE', 'iv' => '0D74DB42A91077DE',
'result' => '620BB4C2ED20F4152F0F86053D3F5595' . 'result' => '05E1E7BEB697D999656BF37C1B978806' .
'8E1FBA48F5D86B25C8F31559F3158072' . '735D0B903A6007BD329927EFBE1B0E2A' .
'6E7ED8525D0B9EA5264BF97750713476' . '8137C1AE291493AA83A821755BEE0B06' .
'1EF65FE195274AFBF000938C03BA59A7' 'CD14855A67E46703EBF8F3114B584CBA'
], ],
// key size: 256 bits // key size: 256 bits
// set 1 // set 1
[ [
'key' => '8000000000000000000000000000000000000000000000000000000000000000', 'key' => '8000000000000000000000000000000000000000000000000000000000000000',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '50EC2485637DB19C6E795E9C73938280' . 'result' => 'E3BE8FDD8BECA2E3EA8EF9475B29A6E7' .
'6F6DB320FE3D0444D56707D7B456457F' . '003951E1097A5C38D23B7A5FAD9F6844' .
'3DB3E8D7065AF375A225A70951C8AB74' . 'B22C97559E2723C7CBBD3FE4FC8D9A07' .
'4EC4D595E85225F08E2BC03FE1C42567' '44652A83E72A9C461876AF4D7EF1A117'
], ],
// set 2 // set 2
[ [
'key' => '0000000000000000000000000000000000000000000000000000000000000000', 'key' => '0000000000000000000000000000000000000000000000000000000000000000',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '7C3A1499A63B507B0BC75824ABEEAA26' . 'result' => '9A97F65B9B4C721B960A672145FCA8D4' .
'109101C5B915F0F554DD9950045D02FA' . 'E32E67F9111EA979CE9C4826806AEEE6' .
'FF815CA8B2C7CFF3625765697B80B026' . '3DE9C0DA2BD7F91EBCB2639BF989C625' .
'7EA87E25412564BD71DD05843A60465E' '1B29BF38D39A9BDCE7C55F4B2AC12A39'
], ],
// set 3 // set 3
[ [
'key' => '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F', 'key' => '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '8C03E9237FEE95D5041C753C204D2B35' . 'result' => 'B580F7671C76E5F7441AF87C146D6B51' .
'764E4A53035A76F9EFBADD7E63E60B69' . '3910DC8B4146EF1B3211CF12AF4A4B49' .
'BF23F7C5FD39B2249B0C628FB654D521' . 'E5C874B3EF4F85E7D7ED539FFEBA73EB' .
'4EB588371E5D2F34BF51396AF3ACB666' '73E0CCA74FBD306D8AA716C7783E89AF'
], ],
// set 4 // set 4
[ [
'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D', 'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D',
'iv' => '0000000000000000', 'iv' => '0000000000000000',
'result' => '2052F9A2853E989133D10938222AC76D' . 'result' => 'F9D2DC274BB55AEFC2A0D9F8A982830F' .
'B8B4CBA135ACB59970DDF9C074C6271A' . '6916122BC0A6870F991C6ED8D00D2F85' .
'5C4E2A7A00D2D697EDFC9B1FF9B365C8' . '94E3151DE4C5A19A9A06FBC191C87BF0' .
'7347B23020663A30711A71E3A02AB00C' '39ADF971314BAF6D02337080F2DAE5CE'
], ],
// set 5 // set 5
[ [
'key' => '0000000000000000000000000000000000000000000000000000000000000000', 'key' => '0000000000000000000000000000000000000000000000000000000000000000',
'iv' => '8000000000000000', 'iv' => '8000000000000000',
'result' => 'FE40F57D1586D7664C2FCA5AB10BD7C7' . 'result' => '2ABA3DC45B4947007B14C851CD694456' .
'9DE3234836E76949F9DC01CBFABC6D6C' . 'B303AD59A465662803006705673D6C3E' .
'42AB27DDC748B4DF7991092972AB4985' . '29F1D3510DFC0405463C03414E0E07E3' .
'CEC19B3E7C2C85D6E25A338DEC288282' '59F1F1816C68B2434A19D3EEE0464873'
], ],
// set 6 // set 6
[ [
'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D', 'key' => '0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D',
'iv' => '0D74DB42A91077DE', 'iv' => '0D74DB42A91077DE',
'result' => 'C349B6A51A3EC9B712EAED3F90D8BCEE' . 'result' => 'F5FAD53F79F9DF58C4AEA0D0ED9A9601' .
'69B7628645F251A996F55260C62EF31F' . 'F278112CA7180D565B420A48019670EA' .
'D6C6B0AEA94E136C9D984AD2DF3578F7' . 'F24CE493A86263F677B46ACE1924773D' .
'8E457527B03A0450580DD874F63B1AB9' '2BB25571E1AA8593758FC382B1280B71'
], ],
]; ];
@ -133,9 +133,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
foreach ($engines as $engine) { foreach ($engines as $engine) {
foreach ($tests as $test) { foreach ($tests as $test) {
foreach ($test['output'] as $output) { $result[] = [$engine, $test['key'], $test['iv'], $test['result']];
$result[] = [$engine, $test['key'], $output['iv'], $output['result']];
}
} }
} }
@ -147,7 +145,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
*/ */
public function testVectors($engine, $key, $iv, $expected) public function testVectors($engine, $key, $iv, $expected)
{ {
$cipher = new Salsa(); $cipher = new Salsa20();
$cipher->setPreferredEngine($engine); $cipher->setPreferredEngine($engine);
$cipher->setKey(pack('H*', $key)); $cipher->setKey(pack('H*', $key));
$cipher->setNonce(pack('H*', $iv)); $cipher->setNonce(pack('H*', $iv));
@ -155,6 +153,6 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
self::markTestSkipped('Unable to initialize ' . $engine . ' engine for ' . (strlen($key) * 8) . '-bit key'); self::markTestSkipped('Unable to initialize ' . $engine . ' engine for ' . (strlen($key) * 8) . '-bit key');
} }
$result = $cipher->encrypt(str_repeat("\0", 64)); $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");
} }
} }