[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * OpenSSL Modular Exponentiation Engine 5 * 6 * PHP version 5 and 7 7 * 8 * @author Jim Wigginton <terrafrost@php.net> 9 * @copyright 2017 Jim Wigginton 10 * @license http://www.opensource.org/licenses/mit-license.html MIT License 11 * @link http://pear.php.net/package/Math_BigInteger 12 */ 13 14 namespace phpseclib3\Math\BigInteger\Engines; 15 16 use phpseclib3\Crypt\RSA\Formats\Keys\PKCS8; 17 use phpseclib3\Math\BigInteger; 18 19 /** 20 * OpenSSL Modular Exponentiation Engine 21 * 22 * @author Jim Wigginton <terrafrost@php.net> 23 */ 24 abstract class OpenSSL 25 { 26 /** 27 * Test for engine validity 28 * 29 * @return bool 30 */ 31 public static function isValidEngine() 32 { 33 return extension_loaded('openssl') && static::class != __CLASS__; 34 } 35 36 /** 37 * Performs modular exponentiation. 38 * 39 * @param Engine $x 40 * @param Engine $e 41 * @param Engine $n 42 * @return Engine 43 */ 44 public static function powModHelper(Engine $x, Engine $e, Engine $n) 45 { 46 if ($n->getLengthInBytes() < 31 || $n->getLengthInBytes() > 16384) { 47 throw new \OutOfRangeException('Only modulo between 31 and 16384 bits are accepted'); 48 } 49 50 $key = PKCS8::savePublicKey( 51 new BigInteger($n), 52 new BigInteger($e) 53 ); 54 55 $plaintext = str_pad($x->toBytes(), $n->getLengthInBytes(), "\0", STR_PAD_LEFT); 56 57 // this is easily prone to failure. if the modulo is a multiple of 2 or 3 or whatever it 58 // won't work and you'll get a "failure: error:0906D06C:PEM routines:PEM_read_bio:no start line" 59 // error. i suppose, for even numbers, we could do what PHP\Montgomery.php does, but then what 60 // about odd numbers divisible by 3, by 5, etc? 61 if (!openssl_public_encrypt($plaintext, $result, $key, OPENSSL_NO_PADDING)) { 62 throw new \UnexpectedValueException(openssl_error_string()); 63 } 64 65 $class = get_class($x); 66 return new $class($result, 256); 67 } 68 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body