@@ -47,8 +47,13 @@ public static function encrypt(RSAKey $key, string $data, int $mode, ?string $ha
4747 }
4848 }
4949
50- public static function decrypt (RSAKey $ key , string $ plaintext , int $ mode , ?string $ hash = null ): string
51- {
50+ public static function decrypt (
51+ RSAKey $ key ,
52+ string $ plaintext ,
53+ int $ mode ,
54+ ?string $ hash = null ,
55+ ?int $ expectedKeyLength = null
56+ ): string {
5257 switch ($ mode ) {
5358 case self ::ENCRYPTION_OAEP :
5459 if ($ hash === null ) {
@@ -57,7 +62,7 @@ public static function decrypt(RSAKey $key, string $plaintext, int $mode, ?strin
5762
5863 return self ::decryptWithRSAOAEP ($ key , $ plaintext , $ hash );
5964 case self ::ENCRYPTION_PKCS1 :
60- return self ::decryptWithRSA15 ($ key , $ plaintext );
65+ return self ::decryptWithRSA15 ($ key , $ plaintext, $ expectedKeyLength );
6166 default :
6267 throw new InvalidArgumentException ('Unsupported mode. ' );
6368 }
@@ -86,24 +91,79 @@ public static function encryptWithRSA15(RSAKey $key, string $data): string
8691 return self ::convertIntegerToOctetString ($ c , $ key ->getModulusLength ());
8792 }
8893
89- public static function decryptWithRSA15 (RSAKey $ key , string $ c ): string
94+ public static function decryptWithRSA15 (RSAKey $ key , string $ c, ? int $ expectedKeyLength = null ): string
9095 {
9196 if (strlen ($ c ) !== $ key ->getModulusLength ()) {
9297 throw new InvalidArgumentException ('Unable to decrypt ' );
9398 }
9499 $ c = BigInteger ::createFromBinaryString ($ c );
95100 $ m = self ::getRSADP ($ key , $ c );
96101 $ em = self ::convertIntegerToOctetString ($ m , $ key ->getModulusLength ());
97- if (ord ($ em [0 ]) !== 0 || ord ($ em [1 ]) > 2 ) {
98- throw new InvalidArgumentException ('Unable to decrypt ' );
102+ if ($ expectedKeyLength === null ) {
103+ if (ord ($ em [0 ]) !== 0 || ord ($ em [1 ]) > 2 ) {
104+ throw new InvalidArgumentException ('Unable to decrypt ' );
105+ }
106+ $ ps = substr ($ em , 2 , (int ) strpos ($ em , chr (0 ), 2 ) - 2 );
107+ $ m = substr ($ em , strlen ($ ps ) + 3 );
108+ if (strlen ($ ps ) < 8 ) {
109+ throw new InvalidArgumentException ('Unable to decrypt ' );
110+ }
111+
112+ return $ m ;
99113 }
100- $ ps = substr ($ em , 2 , (int ) strpos ($ em , chr (0 ), 2 ) - 2 );
101- $ m = substr ($ em , strlen ($ ps ) + 3 , null );
102- if (strlen ($ ps ) < 8 ) {
103- throw new InvalidArgumentException ('Unable to decrypt ' );
114+
115+ return self ::extractRSA15KeyOrRandom ($ em , $ expectedKeyLength );
116+ }
117+
118+ private static function extractRSA15KeyOrRandom (string $ em , int $ expectedKeyLength ): string
119+ {
120+ $ k = strlen ($ em );
121+ $ random = random_bytes ($ expectedKeyLength );
122+
123+ if ($ k < $ expectedKeyLength + 11 ) {
124+ return $ random ;
125+ }
126+ $ candidate = substr ($ em , $ k - $ expectedKeyLength );
127+
128+ $ valid = self ::ctEq (ord ($ em [0 ]), 0x00 ) & self ::ctEq (ord ($ em [1 ]), 0x02 );
129+
130+ $ seenSeparator = 0 ;
131+ $ separatorIndex = 0 ;
132+ $ psLength = 0 ;
133+ for ($ i = 2 ; $ i < $ k ; ++$ i ) {
134+ $ isZero = self ::ctEq (ord ($ em [$ i ]), 0x00 );
135+ $ firstZero = $ isZero & (1 - $ seenSeparator );
136+ $ separatorIndex |= $ firstZero * $ i ;
137+ $ psLength += (1 - $ seenSeparator ) & (1 - $ isZero );
138+ $ seenSeparator |= $ isZero ;
104139 }
105140
106- return $ m ;
141+ $ valid &= $ seenSeparator ;
142+ $ valid &= self ::ctGe ($ psLength , 8 );
143+
144+ $ messageLength = $ k - $ separatorIndex - 1 ;
145+ $ valid &= self ::ctEq ($ messageLength , $ expectedKeyLength );
146+
147+ return self ::ctSelect ($ valid , $ candidate , $ random );
148+ }
149+
150+ private static function ctEq (int $ a , int $ b ): int
151+ {
152+ $ diff = $ a ^ $ b ;
153+
154+ return (($ diff - 1 ) >> 63 ) & 1 ;
155+ }
156+
157+ private static function ctGe (int $ a , int $ b ): int
158+ {
159+ return (($ b - $ a - 1 ) >> 63 ) & 1 ;
160+ }
161+
162+ private static function ctSelect (int $ condition , string $ a , string $ b ): string
163+ {
164+ $ mask = str_repeat (chr (($ condition * 0xFF ) & 0xFF ), strlen ($ a ));
165+
166+ return ($ a & $ mask ) | ($ b & ~$ mask );
107167 }
108168
109169 /**
0 commit comments