1 | <?php |
||
20 | final class RSA |
||
21 | { |
||
22 | /** |
||
23 | * Probabilistic Signature Scheme. |
||
24 | */ |
||
25 | public const SIGNATURE_PSS = 1; |
||
26 | |||
27 | /** |
||
28 | * Use the PKCS#1. |
||
29 | */ |
||
30 | public const SIGNATURE_PKCS1 = 2; |
||
31 | |||
32 | /** |
||
33 | * @param BigInteger $x |
||
34 | * @param int $xLen |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
||
47 | |||
48 | /** |
||
49 | * Exponentiate with or without Chinese Remainder Theorem. |
||
50 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
51 | * |
||
52 | * @param RSAKey $key |
||
53 | * @param BigInteger $c |
||
54 | * |
||
55 | * @return BigInteger |
||
56 | */ |
||
57 | private static function exponentiate(RSAKey $key, BigInteger $c): BigInteger |
||
79 | |||
80 | /** |
||
81 | * MGF1. |
||
82 | * |
||
83 | * @param string $mgfSeed |
||
84 | * @param int $maskLen |
||
85 | * @param Hash $mgfHash |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
||
100 | |||
101 | /** |
||
102 | * EMSA-PSS-ENCODE. |
||
103 | * |
||
104 | * @param string $message |
||
105 | * @param int $modulusLength |
||
106 | * @param Hash $hash |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private static function encodeEMSAPSS(string $message, int $modulusLength, Hash $hash): string |
||
130 | |||
131 | /** |
||
132 | * EMSA-PSS-VERIFY. |
||
133 | * |
||
134 | * @param string $m |
||
135 | * @param string $em |
||
136 | * @param int $emBits |
||
137 | * @param Hash $hash |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | private static function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): bool |
||
174 | |||
175 | /** |
||
176 | * @param string $m |
||
177 | * @param int $emBits |
||
178 | * @param Hash $hash |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | private static function encodeEMSA15(string $m, int $emBits, Hash $hash): string |
||
208 | |||
209 | /** |
||
210 | * @param RSAKey $key |
||
211 | * @param string $message |
||
212 | * @param string $hash |
||
213 | * @param int $mode |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public static function sign(RSAKey $key, string $message, string $hash, int $mode): string |
||
228 | |||
229 | /** |
||
230 | * Create a signature. |
||
231 | * |
||
232 | * @param RSAKey $key |
||
233 | * @param string $message |
||
234 | * @param string $hash |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public static function signWithPSS(RSAKey $key, string $message, string $hash): string |
||
249 | |||
250 | /** |
||
251 | * Create a signature. |
||
252 | * |
||
253 | * @param RSAKey $key |
||
254 | * @param string $message |
||
255 | * @param string $hash |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | public static function signWithPKCS15(RSAKey $key, string $message, string $hash): string |
||
270 | |||
271 | /** |
||
272 | * @param RSAKey $key |
||
273 | * @param string $message |
||
274 | * @param string $signature |
||
275 | * @param string $hash |
||
276 | * @param int $mode |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | public static function verify(RSAKey $key, string $message, string $signature, string $hash, int $mode): bool |
||
291 | |||
292 | /** |
||
293 | * Verifies a signature. |
||
294 | * |
||
295 | * @param RSAKey $key |
||
296 | * @param string $message |
||
297 | * @param string $signature |
||
298 | * @param string $hash |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | public static function verifyWithPSS(RSAKey $key, string $message, string $signature, string $hash): bool |
||
317 | |||
318 | /** |
||
319 | * Verifies a signature. |
||
320 | * |
||
321 | * @param RSAKey $key |
||
322 | * @param string $message |
||
323 | * @param string $signature |
||
324 | * @param string $hash |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | public static function verifyWithPKCS15(RSAKey $key, string $message, string $signature, string $hash): bool |
||
342 | } |
||
343 |