Skip to content

Commit 96513e8

Browse files
authored
Preparation 3.0 (#323)
* Preparation 3.0 * Update with ECS and Rector * Bugs fixed
1 parent 2aab79c commit 96513e8

File tree

6 files changed

+34
-96
lines changed

6 files changed

+34
-96
lines changed

RSA.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* The MIT License (MIT)
7-
*
8-
* Copyright (c) 2014-2020 Spomky-Labs
9-
*
10-
* This software may be modified and distributed under the terms
11-
* of the MIT license. See the LICENSE file for details.
12-
*/
13-
145
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
156

167
use function in_array;
@@ -34,13 +25,10 @@ public function encryptKey(JWK $key, string $cek, array $completeHeader, array &
3425
return RSACrypt::encrypt($pub, $cek, $this->getEncryptionMode(), $this->getHashAlgorithm());
3526
}
3627

37-
/**
38-
* @throws InvalidArgumentException if the key is not private
39-
*/
4028
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string
4129
{
4230
$this->checkKey($key);
43-
if (!$key->has('d')) {
31+
if (! $key->has('d')) {
4432
throw new InvalidArgumentException('The key is not a private key');
4533
}
4634
$priv = RSAKey::createFromJWK($key);
@@ -53,12 +41,9 @@ public function getKeyManagementMode(): string
5341
return self::MODE_ENCRYPT;
5442
}
5543

56-
/**
57-
* @throws InvalidArgumentException if the key type is not allowed
58-
*/
5944
protected function checkKey(JWK $key): void
6045
{
61-
if (!in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
46+
if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
6247
throw new InvalidArgumentException('Wrong key type.');
6348
}
6449
}

RSA15.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* The MIT License (MIT)
7-
*
8-
* Copyright (c) 2014-2020 Spomky-Labs
9-
*
10-
* This software may be modified and distributed under the terms
11-
* of the MIT license. See the LICENSE file for details.
12-
*/
13-
145
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
156

167
use Jose\Component\Encryption\Algorithm\KeyEncryption\Util\RSACrypt;

RSAOAEP.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* The MIT License (MIT)
7-
*
8-
* Copyright (c) 2014-2020 Spomky-Labs
9-
*
10-
* This software may be modified and distributed under the terms
11-
* of the MIT license. See the LICENSE file for details.
12-
*/
13-
145
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
156

167
use Jose\Component\Encryption\Algorithm\KeyEncryption\Util\RSACrypt;

RSAOAEP256.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* The MIT License (MIT)
7-
*
8-
* Copyright (c) 2014-2020 Spomky-Labs
9-
*
10-
* This software may be modified and distributed under the terms
11-
* of the MIT license. See the LICENSE file for details.
12-
*/
13-
145
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
156

167
use Jose\Component\Encryption\Algorithm\KeyEncryption\Util\RSACrypt;

Util/RSACrypt.php

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* The MIT License (MIT)
7-
*
8-
* Copyright (c) 2014-2020 Spomky-Labs
9-
*
10-
* This software may be modified and distributed under the terms
11-
* of the MIT license. See the LICENSE file for details.
12-
*/
13-
145
namespace Jose\Component\Encryption\Algorithm\KeyEncryption\Util;
156

167
use function chr;
178
use function count;
189
use InvalidArgumentException;
19-
use function is_array;
2010
use Jose\Component\Core\Util\BigInteger;
2111
use Jose\Component\Core\Util\Hash;
2212
use Jose\Component\Core\Util\RSAKey;
2313
use function ord;
2414
use RuntimeException;
15+
use const STR_PAD_LEFT;
2516

2617
/**
2718
* @internal
2819
*/
29-
class RSACrypt
20+
final class RSACrypt
3021
{
3122
/**
3223
* Optimal Asymmetric Encryption Padding (OAEP).
@@ -40,30 +31,20 @@ class RSACrypt
4031

4132
public static function encrypt(RSAKey $key, string $data, int $mode, ?string $hash = null): string
4233
{
43-
switch ($mode) {
44-
case self::ENCRYPTION_OAEP:
45-
return self::encryptWithRSAOAEP($key, $data, $hash);
46-
47-
case self::ENCRYPTION_PKCS1:
48-
return self::encryptWithRSA15($key, $data);
49-
50-
default:
51-
throw new InvalidArgumentException('Unsupported mode.');
52-
}
34+
return match ($mode) {
35+
self::ENCRYPTION_OAEP => self::encryptWithRSAOAEP($key, $data, $hash),
36+
self::ENCRYPTION_PKCS1 => self::encryptWithRSA15($key, $data),
37+
default => throw new InvalidArgumentException('Unsupported mode.'),
38+
};
5339
}
5440

5541
public static function decrypt(RSAKey $key, string $plaintext, int $mode, ?string $hash = null): string
5642
{
57-
switch ($mode) {
58-
case self::ENCRYPTION_OAEP:
59-
return self::decryptWithRSAOAEP($key, $plaintext, $hash);
60-
61-
case self::ENCRYPTION_PKCS1:
62-
return self::decryptWithRSA15($key, $plaintext);
63-
64-
default:
65-
throw new InvalidArgumentException('Unsupported mode.');
66-
}
43+
return match ($mode) {
44+
self::ENCRYPTION_OAEP => self::decryptWithRSAOAEP($key, $plaintext, $hash),
45+
self::ENCRYPTION_PKCS1 => self::decryptWithRSA15($key, $plaintext),
46+
default => throw new InvalidArgumentException('Unsupported mode.'),
47+
};
6748
}
6849

6950
public static function encryptWithRSA15(RSAKey $key, string $data): string
@@ -81,7 +62,7 @@ public static function encryptWithRSA15(RSAKey $key, string $data): string
8162
$ps .= $temp;
8263
}
8364
$type = 2;
84-
$data = chr(0).chr($type).$ps.chr(0).$data;
65+
$data = chr(0) . chr($type) . $ps . chr(0) . $data;
8566

8667
$data = BigInteger::createFromBinaryString($data);
8768
$c = self::getRSAEP($key, $data);
@@ -97,7 +78,7 @@ public static function decryptWithRSA15(RSAKey $key, string $c): string
9778
$c = BigInteger::createFromBinaryString($c);
9879
$m = self::getRSADP($key, $c);
9980
$em = self::convertIntegerToOctetString($m, $key->getModulusLength());
100-
if (0 !== ord($em[0]) || ord($em[1]) > 2) {
81+
if (ord($em[0]) !== 0 || ord($em[1]) > 2) {
10182
throw new InvalidArgumentException('Unable to decrypt');
10283
}
10384
$ps = mb_substr($em, 2, (int) mb_strpos($em, chr(0), 2, '8bit') - 2, '8bit');
@@ -117,15 +98,12 @@ public static function encryptWithRSAOAEP(RSAKey $key, string $plaintext, string
11798
/** @var Hash $hash */
11899
$hash = Hash::$hash_algorithm();
119100
$length = $key->getModulusLength() - 2 * $hash->getLength() - 2;
120-
if (0 >= $length) {
101+
if ($length <= 0) {
121102
throw new RuntimeException();
122103
}
123-
$plaintext = mb_str_split($plaintext, $length, '8bit');
124-
if (!is_array($plaintext)) {
125-
throw new RuntimeException('Invalid payload');
126-
}
104+
$splitPlaintext = mb_str_split($plaintext, $length, '8bit');
127105
$ciphertext = '';
128-
foreach ($plaintext as $m) {
106+
foreach ($splitPlaintext as $m) {
129107
$ciphertext .= self::encryptRSAESOAEP($key, $m, $hash);
130108
}
131109

@@ -137,17 +115,19 @@ public static function encryptWithRSAOAEP(RSAKey $key, string $plaintext, string
137115
*/
138116
public static function decryptWithRSAOAEP(RSAKey $key, string $ciphertext, string $hash_algorithm): string
139117
{
140-
if (0 >= $key->getModulusLength()) {
118+
if ($key->getModulusLength() <= 0) {
141119
throw new RuntimeException('Invalid modulus length');
142120
}
143121
$hash = Hash::$hash_algorithm();
144-
$ciphertext = mb_str_split($ciphertext, $key->getModulusLength(), '8bit');
145-
if (!is_array($ciphertext)) {
146-
throw new RuntimeException('Invalid ciphertext');
147-
}
148-
$ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $key->getModulusLength(), chr(0), STR_PAD_LEFT);
122+
$splitCiphertext = mb_str_split($ciphertext, $key->getModulusLength(), '8bit');
123+
$splitCiphertext[count($splitCiphertext) - 1] = str_pad(
124+
$splitCiphertext[count($splitCiphertext) - 1],
125+
$key->getModulusLength(),
126+
chr(0),
127+
STR_PAD_LEFT
128+
);
149129
$plaintext = '';
150-
foreach ($ciphertext as $c) {
130+
foreach ($splitCiphertext as $c) {
151131
$temp = self::getRSAESOAEP($key, $c, $hash);
152132
$plaintext .= $temp;
153133
}
@@ -206,7 +186,7 @@ private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): s
206186
$count = ceil($maskLen / $mgfHash->getLength());
207187
for ($i = 0; $i < $count; ++$i) {
208188
$c = pack('N', $i);
209-
$t .= $mgfHash->hash($mgfSeed.$c);
189+
$t .= $mgfHash->hash($mgfSeed . $c);
210190
}
211191

212192
return mb_substr($t, 0, $maskLen, '8bit');
@@ -220,13 +200,13 @@ private static function encryptRSAESOAEP(RSAKey $key, string $m, Hash $hash): st
220200
$mLen = mb_strlen($m, '8bit');
221201
$lHash = $hash->hash('');
222202
$ps = str_repeat(chr(0), $key->getModulusLength() - $mLen - 2 * $hash->getLength() - 2);
223-
$db = $lHash.$ps.chr(1).$m;
203+
$db = $lHash . $ps . chr(1) . $m;
224204
$seed = random_bytes($hash->getLength());
225205
$dbMask = self::getMGF1($seed, $key->getModulusLength() - $hash->getLength() - 1, $hash/*MGF*/);
226206
$maskedDB = $db ^ $dbMask;
227207
$seedMask = self::getMGF1($maskedDB, $hash->getLength(), $hash/*MGF*/);
228208
$maskedSeed = $seed ^ $seedMask;
229-
$em = chr(0).$maskedSeed.$maskedDB;
209+
$em = chr(0) . $maskedSeed . $maskedDB;
230210

231211
$m = self::convertOctetStringToInteger($em);
232212
$c = self::getRSAEP($key, $m);
@@ -251,11 +231,11 @@ private static function getRSAESOAEP(RSAKey $key, string $c, Hash $hash): string
251231
$db = $maskedDB ^ $dbMask;
252232
$lHash2 = mb_substr($db, 0, $hash->getLength(), '8bit');
253233
$m = mb_substr($db, $hash->getLength(), null, '8bit');
254-
if (!hash_equals($lHash, $lHash2)) {
234+
if (! hash_equals($lHash, $lHash2)) {
255235
throw new RuntimeException();
256236
}
257237
$m = ltrim($m, chr(0));
258-
if (1 !== ord($m[0])) {
238+
if (ord($m[0]) !== 1) {
259239
throw new RuntimeException();
260240
}
261241

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
},
2222
"require": {
23-
"brick/math": "^0.8.17|^0.9",
23+
"brick/math": "^0.9",
2424
"ext-openssl": "*",
2525
"symfony/polyfill-mbstring": "^1.12",
2626
"web-token/jwt-encryption": "^2.1"

0 commit comments

Comments
 (0)