2015-05-26 03:30:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2013 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2015-05-26 03:36:10 +00:00
|
|
|
use phpseclib\Crypt\RSA;
|
2015-05-26 03:30:38 +00:00
|
|
|
|
|
|
|
class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase
|
|
|
|
{
|
|
|
|
public function testEncryptionModeNone()
|
|
|
|
{
|
|
|
|
$plaintext = 'a';
|
|
|
|
|
2015-05-26 03:36:10 +00:00
|
|
|
$rsa = new RSA();
|
2015-05-26 03:30:38 +00:00
|
|
|
|
2015-05-31 07:13:11 +00:00
|
|
|
$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-----';
|
2016-04-28 20:34:10 +00:00
|
|
|
$rsa->loadKey($privatekey);
|
|
|
|
$rsa->loadKey($rsa->getPublicKey());
|
2015-05-26 03:30:38 +00:00
|
|
|
|
2016-04-28 20:34:10 +00:00
|
|
|
$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE);
|
2015-05-31 07:13:11 +00:00
|
|
|
$expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' .
|
|
|
|
'5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' .
|
|
|
|
'4d04bd467f6f1e7c';
|
2015-05-31 15:06:13 +00:00
|
|
|
$expected = pack('H*', $expected);
|
2016-04-28 20:34:10 +00:00
|
|
|
$result = $rsa->encrypt($plaintext);
|
2015-05-26 03:30:38 +00:00
|
|
|
|
2015-05-31 07:13:11 +00:00
|
|
|
$this->assertEquals($result, $expected);
|
2015-05-26 03:30:38 +00:00
|
|
|
|
2016-04-28 20:34:10 +00:00
|
|
|
$rsa->loadKey($privatekey);
|
|
|
|
$this->assertEquals(trim($rsa->decrypt($result), "\0"), $plaintext);
|
2016-04-10 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github768
|
|
|
|
*/
|
|
|
|
public function testPSSSigs()
|
|
|
|
{
|
|
|
|
$rsa = new RSA();
|
2016-04-28 20:34:10 +00:00
|
|
|
$rsa->loadKey('-----BEGIN PUBLIC KEY-----
|
2016-04-10 16:30:59 +00:00
|
|
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVx
|
|
|
|
wTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFnc
|
|
|
|
CzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0T
|
|
|
|
p0GbMJDyR4e9T04ZZwIDAQAB
|
|
|
|
-----END PUBLIC KEY-----');
|
|
|
|
|
|
|
|
$sig = pack('H*', '1bd29a1d704a906cd7f726370ce1c63d8fb7b9a620871a05f3141a311c0d6e75fefb5d36dfb50d3ea2d37cd67992471419bfadd35da6e13b494' .
|
|
|
|
'058ddc9b568d4cfea13ddc3c62b86a6256f5f296980d1131d3eaec6089069a3de79983f73eae20198a18721338b4a66e9cfe80e4f8e4fcef7a5bead5cbb' .
|
|
|
|
'b8ac4c76adffbc178c');
|
|
|
|
|
|
|
|
$this->assertTrue($rsa->verify('zzzz', $sig));
|
|
|
|
}
|
2016-06-21 07:56:57 +00:00
|
|
|
|
|
|
|
public function testZeroLengthSalt()
|
|
|
|
{
|
|
|
|
$plaintext = 'a';
|
|
|
|
|
2016-06-22 03:39:33 +00:00
|
|
|
$rsa = new RSA();
|
2016-06-21 07:56:57 +00:00
|
|
|
|
|
|
|
$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->setSaltLength(0);
|
|
|
|
|
|
|
|
// Check we generate the correct signature.
|
|
|
|
$sig = pack('H*', '0ddfc93548e21d015c0a289a640b3b79aecfdfae045f583c5925b91cc5c399bba181616ad6ae20d9662d966f0eb2fddb550f4733268e34d640f4c9dadcaf25b3c82c42130a5081c6ebad7883331c65b25b6a37ffa7c4233a468dae56180787e2718ed87c48d8d50b72f5850e4a40963b4f36710be250ecef6fe0bb91249261a3');
|
|
|
|
$this->assertEquals($sig, $rsa->sign($plaintext));
|
|
|
|
|
|
|
|
// Check we can verify the signature correctly.
|
|
|
|
$rsa->loadKey($rsa->getPublicKey());
|
|
|
|
$this->assertTrue($rsa->verify($plaintext, $sig));
|
|
|
|
}
|
2015-05-26 03:30:38 +00:00
|
|
|
}
|