2016-04-30 21:23:35 +00:00
|
|
|
<?php
|
2016-10-19 12:45:42 +00:00
|
|
|
|
2016-04-30 21:23:35 +00:00
|
|
|
/**
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
namespace phpseclib3\Tests\Unit\Crypt\RSA;
|
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Crypt\RSA;
|
|
|
|
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS1;
|
2022-02-15 01:15:20 +00:00
|
|
|
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS8;
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Crypt\RSA\PrivateKey;
|
|
|
|
use phpseclib3\Crypt\RSA\PublicKey;
|
2022-02-23 02:48:51 +00:00
|
|
|
use phpseclib3\Tests\PhpseclibTestCase;
|
2016-04-30 21:23:35 +00:00
|
|
|
|
2022-02-27 22:35:58 +00:00
|
|
|
class CreateKeyTest extends PhpseclibTestCase
|
2016-04-30 21:23:35 +00:00
|
|
|
{
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testCreateKey(): array
|
2016-04-30 21:23:35 +00:00
|
|
|
{
|
2019-05-19 20:35:29 +00:00
|
|
|
$privatekey = RSA::createKey(768);
|
|
|
|
$publickey = $privatekey->getPublicKey();
|
|
|
|
$this->assertInstanceOf(PrivateKey::class, $privatekey);
|
|
|
|
$this->assertInstanceOf(PublicKey::class, $publickey);
|
2016-04-30 21:23:35 +00:00
|
|
|
$this->assertNotEmpty("$privatekey");
|
|
|
|
$this->assertNotEmpty("$publickey");
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertSame($privatekey->getLength(), 768);
|
|
|
|
$this->assertSame($publickey->getLength(), 768);
|
2016-04-30 21:23:35 +00:00
|
|
|
|
2017-11-27 08:30:14 +00:00
|
|
|
return [$publickey, $privatekey];
|
2016-04-30 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateKey
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testEncryptDecrypt($args): void
|
2016-04-30 21:23:35 +00:00
|
|
|
{
|
2022-07-07 01:43:09 +00:00
|
|
|
[$publickey, $privatekey] = $args;
|
2016-04-30 21:23:35 +00:00
|
|
|
$ciphertext = $publickey->encrypt('zzz');
|
2020-12-13 01:22:36 +00:00
|
|
|
$this->assertIsString($ciphertext);
|
2016-04-30 21:23:35 +00:00
|
|
|
$plaintext = $privatekey->decrypt($ciphertext);
|
|
|
|
$this->assertSame($plaintext, 'zzz');
|
|
|
|
}
|
2016-10-19 12:45:42 +00:00
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testMultiPrime(): void
|
2016-10-19 12:45:42 +00:00
|
|
|
{
|
2018-10-25 01:00:37 +00:00
|
|
|
RSA::useInternalEngine();
|
2016-10-19 12:45:42 +00:00
|
|
|
RSA::setSmallestPrime(256);
|
2019-05-19 20:35:29 +00:00
|
|
|
$privatekey = RSA::createKey(1024);
|
|
|
|
$publickey = $privatekey->getPublicKey();
|
|
|
|
$this->assertInstanceOf(PrivateKey::class, $privatekey);
|
|
|
|
$this->assertInstanceOf(PublicKey::class, $publickey);
|
|
|
|
$this->assertNotEmpty($privatekey->toString('PKCS1'));
|
|
|
|
$this->assertNotEmpty($publickey->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertSame($privatekey->getLength(), 1024);
|
|
|
|
$this->assertSame($publickey->getLength(), 1024);
|
2019-05-19 20:35:29 +00:00
|
|
|
$r = PKCS1::load($privatekey->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertCount(4, $r['primes']);
|
|
|
|
// the last prime number could be slightly over. eg. 99 * 99 == 9801 but 10 * 10 = 100. the more numbers you're
|
|
|
|
// multiplying the less certain you are to have each of them multiply to an n-bit number
|
|
|
|
foreach (array_slice($r['primes'], 0, 3) as $i => $prime) {
|
|
|
|
$this->assertSame($prime->getLength(), 256);
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:35:29 +00:00
|
|
|
$rsa = RSA::load($privatekey->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$signature = $rsa->sign('zzz');
|
2019-05-19 20:35:29 +00:00
|
|
|
$rsa = RSA::load($rsa->getPublicKey()->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertTrue($rsa->verify('zzz', $signature));
|
2016-12-11 15:44:37 +00:00
|
|
|
|
2018-10-25 01:00:37 +00:00
|
|
|
RSA::useBestEngine();
|
2016-10-19 12:45:42 +00:00
|
|
|
}
|
2022-02-15 01:15:20 +00:00
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
public function test3DESPKCS8Encryption(): void
|
2022-02-15 01:15:20 +00:00
|
|
|
{
|
|
|
|
$key = RSA::createKey(768)
|
|
|
|
->withPassword('demo')
|
|
|
|
->toString('PKCS8', ['encryptionAlgorithm' => 'pbeWithSHAAnd3-KeyTripleDES-CBC']);
|
|
|
|
$actual = PKCS8::extractEncryptionAlgorithm($key)['algorithm'];
|
|
|
|
$this->assertSame($actual, 'pbeWithSHAAnd3-KeyTripleDES-CBC');
|
|
|
|
}
|
2016-04-30 21:23:35 +00:00
|
|
|
}
|