1 | <?php |
||
17 | final class RSA |
||
18 | { |
||
19 | /** |
||
20 | * Integer-to-Octet-String primitive. |
||
21 | * |
||
22 | * @param \Jose\Util\BigInteger $x |
||
23 | * @param int $xLen |
||
24 | * |
||
25 | * @return string |
||
26 | */ |
||
27 | private static function convertIntegerToOctetString($x, $xLen) |
||
36 | |||
37 | /** |
||
38 | * Octet-String-to-Integer primitive. |
||
39 | * |
||
40 | * @param string $x |
||
41 | * |
||
42 | * @return \Jose\Util\BigInteger |
||
43 | */ |
||
44 | private static function convertOctetStringToInteger($x) |
||
48 | |||
49 | /** |
||
50 | * Exponentiate with or without Chinese Remainder Theorem. |
||
51 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
52 | * |
||
53 | * @param \Jose\KeyConverter\RSAKey $key |
||
54 | * @param \Jose\Util\BigInteger $c |
||
55 | * |
||
56 | * @return \Jose\Util\BigInteger |
||
57 | */ |
||
58 | private static function _exponentiate(RSAKey $key, $c) |
||
77 | |||
78 | /** |
||
79 | * RSAEP. |
||
80 | * |
||
81 | * @param \Jose\KeyConverter\RSAKey $key |
||
82 | * @param \Jose\Util\BigInteger $m |
||
83 | * |
||
84 | * @return \Jose\Util\BigInteger|false |
||
85 | */ |
||
86 | private static function _rsaep(RSAKey $key, BigInteger $m) |
||
94 | |||
95 | /** |
||
96 | * RSADP. |
||
97 | * |
||
98 | * @param \Jose\KeyConverter\RSAKey $key |
||
99 | * @param \Jose\Util\BigInteger $c |
||
100 | * |
||
101 | * @return \Jose\Util\BigInteger|false |
||
102 | */ |
||
103 | private static function _rsadp(RSAKey $key, BigInteger $c) |
||
111 | |||
112 | /** |
||
113 | * RSASP1. |
||
114 | * |
||
115 | * @param \Jose\KeyConverter\RSAKey $key |
||
116 | * @param \Jose\Util\BigInteger $m |
||
117 | * |
||
118 | * @return \Jose\Util\BigInteger|false |
||
119 | */ |
||
120 | private static function _rsasp1(RSAKey $key, BigInteger $m) |
||
128 | |||
129 | /** |
||
130 | * RSAVP1. |
||
131 | * |
||
132 | * @param \Jose\KeyConverter\RSAKey $key |
||
133 | * @param \Jose\Util\BigInteger $s |
||
134 | * |
||
135 | * @return \Jose\Util\BigInteger|false |
||
136 | */ |
||
137 | private static function _rsavp1(RSAKey $key, BigInteger $s) |
||
145 | |||
146 | /** |
||
147 | * MGF1. |
||
148 | * |
||
149 | * @param string $mgfSeed |
||
150 | * @param int $maskLen |
||
151 | * @param \Jose\Util\Hash $mgfHash |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | private static function _mgf1($mgfSeed, $maskLen, Hash $mgfHash) |
||
166 | |||
167 | /** |
||
168 | * RSAES-OAEP-ENCRYPT. |
||
169 | * |
||
170 | * @param \Jose\KeyConverter\RSAKey $key |
||
171 | * @param string $m |
||
172 | * @param \Jose\Util\Hash $hash |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | private static function _rsaes_oaep_encrypt(RSAKey $key, $m, Hash $hash) |
||
195 | |||
196 | /** |
||
197 | * RSAES-OAEP-DECRYPT. |
||
198 | * |
||
199 | * @param \Jose\KeyConverter\RSAKey $key |
||
200 | * @param string $c |
||
201 | * @param \Jose\Util\Hash $hash |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | private static function _rsaes_oaep_decrypt(RSAKey $key, $c, Hash $hash) |
||
232 | |||
233 | /** |
||
234 | * EMSA-PSS-ENCODE. |
||
235 | * |
||
236 | * @param string $m |
||
237 | * @param int $emBits |
||
238 | * @param \Jose\Util\Hash $hash |
||
239 | * |
||
240 | * @return string|bool |
||
241 | */ |
||
242 | private static function _emsa_pss_encode($m, $emBits, Hash $hash) |
||
267 | |||
268 | /** |
||
269 | * EMSA-PSS-VERIFY. |
||
270 | * |
||
271 | * @param string $m |
||
272 | * @param string $em |
||
273 | * @param int $emBits |
||
274 | * @param \Jose\Util\Hash $hash |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | private static function _emsa_pss_verify($m, $em, $emBits, Hash $hash) |
||
314 | |||
315 | /** |
||
316 | * Encryption. |
||
317 | * |
||
318 | * @param \Jose\KeyConverter\RSAKey $key |
||
319 | * @param string $plaintext |
||
320 | * @param string $hash_algorithm |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | public static function encrypt(RSAKey $key, $plaintext, $hash_algorithm) |
||
339 | |||
340 | /** |
||
341 | * Decryption. |
||
342 | * |
||
343 | * @param \Jose\KeyConverter\RSAKey $key |
||
344 | * @param string $ciphertext |
||
345 | * @param string $hash_algorithm |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | public static function decrypt(RSAKey $key, $ciphertext, $hash_algorithm) |
||
370 | |||
371 | /** |
||
372 | * Create a signature. |
||
373 | * |
||
374 | * @param \Jose\KeyConverter\RSAKey $key |
||
375 | * @param string $message |
||
376 | * @param string $hash |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | public static function sign(RSAKey $key, $message, $hash) |
||
399 | |||
400 | /** |
||
401 | * Verifies a signature. |
||
402 | * |
||
403 | * @param \Jose\KeyConverter\RSAKey $key |
||
404 | * @param string $message |
||
405 | * @param string $signature |
||
406 | * @param string $hash |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | public static function verify(RSAKey $key, $message, $signature, $hash) |
||
429 | } |
||
430 |