2015-05-26 05:30:38 +02: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 05:36:10 +02:00
|
|
|
use phpseclib\Crypt\RSA;
|
2015-05-26 05:30:38 +02:00
|
|
|
|
|
|
|
class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase
|
|
|
|
{
|
|
|
|
public function testEncryptionModeNone()
|
|
|
|
{
|
|
|
|
$plaintext = 'a';
|
|
|
|
|
2015-05-26 05:36:10 +02:00
|
|
|
$rsa = new RSA();
|
2015-05-26 05:30:38 +02:00
|
|
|
|
2015-05-31 09:13:11 +02: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 22:34:10 +02:00
|
|
|
$rsa->loadKey($privatekey);
|
|
|
|
$rsa->loadKey($rsa->getPublicKey());
|
2015-05-26 05:30:38 +02:00
|
|
|
|
2016-04-28 22:34:10 +02:00
|
|
|
$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE);
|
2015-05-31 09:13:11 +02:00
|
|
|
$expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' .
|
|
|
|
'5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' .
|
|
|
|
'4d04bd467f6f1e7c';
|
2015-05-31 17:06:13 +02:00
|
|
|
$expected = pack('H*', $expected);
|
2016-04-28 22:34:10 +02:00
|
|
|
$result = $rsa->encrypt($plaintext);
|
2015-05-26 05:30:38 +02:00
|
|
|
|
2015-05-31 09:13:11 +02:00
|
|
|
$this->assertEquals($result, $expected);
|
2015-05-26 05:30:38 +02:00
|
|
|
|
2016-04-28 22:34:10 +02:00
|
|
|
$rsa->loadKey($privatekey);
|
|
|
|
$this->assertEquals(trim($rsa->decrypt($result), "\0"), $plaintext);
|
2016-04-10 18:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github768
|
|
|
|
*/
|
|
|
|
public function testPSSSigs()
|
|
|
|
{
|
|
|
|
$rsa = new RSA();
|
2016-04-28 22:34:10 +02:00
|
|
|
$rsa->loadKey('-----BEGIN PUBLIC KEY-----
|
2016-04-10 18:30:59 +02: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 09:56:57 +02:00
|
|
|
|
|
|
|
public function testZeroLengthSalt()
|
|
|
|
{
|
|
|
|
$plaintext = 'a';
|
|
|
|
|
2016-06-22 05:39:33 +02:00
|
|
|
$rsa = new RSA();
|
2016-06-21 09:56:57 +02: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));
|
|
|
|
}
|
2019-10-19 15:30:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github1423
|
|
|
|
*/
|
|
|
|
public function testPSSSigsWithNonPowerOf2Key()
|
|
|
|
{
|
|
|
|
$pub = '-----BEGIN PUBLIC KEY-----
|
|
|
|
MF0wDQYJKoZIhvcNAQEBBQADTAAwSQJCAmdYuOvii3I6ya3q/zSeZFoJprgF9fIq
|
|
|
|
k12yS6pCS3c+1wZ9cYFVtgfpSL4XpylLe9EnRT2GRVYCqUkR4AUeTuvnAgMBAAE=
|
|
|
|
-----END PUBLIC KEY-----';
|
|
|
|
|
2019-10-20 20:38:45 +02:00
|
|
|
$rsa = new RSA();
|
2019-10-19 15:30:39 +02:00
|
|
|
$rsa->loadKey($pub);
|
|
|
|
$rsa->setHash('sha256');
|
|
|
|
$rsa->setSaltLength(32);
|
|
|
|
$rsa->setMGFHash('sha256');
|
|
|
|
|
|
|
|
$sig = base64_decode(strtr('Ad022bD-UCmWpBNMtsYJjG0FVxML-FFlN4IKrByP8rwjVzV_D-YqSjc_oW6LrooV7jbtEF5803YLn8lllyzDnw00', '-_', '+/'));
|
|
|
|
$payload = 'eyJraWQiOiJ0RkMyVUloRnBUTV9FYTNxY09kX01xUVQxY0JCbTlrRkxTRGZlSmhzUkc4IiwiYWxnIjoiUFMyNTYifQ.eyJhcHAiOiJhY2NvdW50cG9ydGFsIiwic3ViIjoiNTliOGM4YzA5NTVhNDA5MDg2MGRmYmM3ZGQwMjVjZWEiLCJjbGlkIjoiZTQ5ZTA2N2JiMTFjNDcyMmEzNGIyYjNiOGE2YTYzNTUiLCJhbSI6InBhc3N3b3JkIiwicCI6ImVOcDFrRUZQd3pBTWhmXC9QdEVOYU5kQkc2bUZDNHNpbENNNXU0aTNXMHFSS0hFVDU5V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