22
33declare (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-
145namespace Jose \Component \Encryption \Algorithm \KeyEncryption \Util ;
156
167use function chr ;
178use function count ;
189use InvalidArgumentException ;
19- use function is_array ;
2010use Jose \Component \Core \Util \BigInteger ;
2111use Jose \Component \Core \Util \Hash ;
2212use Jose \Component \Core \Util \RSAKey ;
2313use function ord ;
2414use 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
0 commit comments