[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Montgomery Private Key Handler 5 * 6 * "Naked" Curve25519 private keys can pretty much be any sequence of random 32x bytes so unless 7 * we have a "hidden" key handler pretty much every 32 byte string will be loaded as a curve25519 8 * private key even if it probably isn't one by PublicKeyLoader. 9 * 10 * "Naked" Curve25519 public keys also a string of 32 bytes so distinguishing between a "naked" 11 * curve25519 private key and a public key is nigh impossible, hence separate plugins for each 12 * 13 * PHP version 5 14 * 15 * @author Jim Wigginton <terrafrost@php.net> 16 * @copyright 2015 Jim Wigginton 17 * @license http://www.opensource.org/licenses/mit-license.html MIT License 18 * @link http://phpseclib.sourceforge.net 19 */ 20 21 namespace phpseclib3\Crypt\EC\Formats\Keys; 22 23 use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve; 24 use phpseclib3\Crypt\EC\Curves\Curve25519; 25 use phpseclib3\Crypt\EC\Curves\Curve448; 26 use phpseclib3\Exception\UnsupportedFormatException; 27 use phpseclib3\Math\BigInteger; 28 29 /** 30 * Montgomery Curve Private Key Handler 31 * 32 * @author Jim Wigginton <terrafrost@php.net> 33 */ 34 abstract class MontgomeryPrivate 35 { 36 /** 37 * Is invisible flag 38 * 39 */ 40 const IS_INVISIBLE = true; 41 42 /** 43 * Break a public or private key down into its constituent components 44 * 45 * @param string $key 46 * @param string $password optional 47 * @return array 48 */ 49 public static function load($key, $password = '') 50 { 51 switch (strlen($key)) { 52 case 32: 53 $curve = new Curve25519(); 54 break; 55 case 56: 56 $curve = new Curve448(); 57 break; 58 default: 59 throw new \LengthException('The only supported lengths are 32 and 56'); 60 } 61 62 $components = ['curve' => $curve]; 63 $components['dA'] = new BigInteger($key, 256); 64 $curve->rangeCheck($components['dA']); 65 // note that EC::getEncodedCoordinates does some additional "magic" (it does strrev on the result) 66 $components['QA'] = $components['curve']->multiplyPoint($components['curve']->getBasePoint(), $components['dA']); 67 68 return $components; 69 } 70 71 /** 72 * Convert an EC public key to the appropriate format 73 * 74 * @param \phpseclib3\Crypt\EC\BaseCurves\Montgomery $curve 75 * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey 76 * @return string 77 */ 78 public static function savePublicKey(MontgomeryCurve $curve, array $publicKey) 79 { 80 return strrev($publicKey[0]->toBytes()); 81 } 82 83 /** 84 * Convert a private key to the appropriate format. 85 * 86 * @param \phpseclib3\Math\BigInteger $privateKey 87 * @param \phpseclib3\Crypt\EC\BaseCurves\Montgomery $curve 88 * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey 89 * @param string $secret optional 90 * @param string $password optional 91 * @return string 92 */ 93 public static function savePrivateKey(BigInteger $privateKey, MontgomeryCurve $curve, array $publicKey, $secret = null, $password = '') 94 { 95 if (!empty($password) && is_string($password)) { 96 throw new UnsupportedFormatException('MontgomeryPrivate private keys do not support encryption'); 97 } 98 99 return $privateKey->toBytes(); 100 } 101 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body