Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /**
  4.  * Montgomery Public Key Handler
  5.  *
  6.  * PHP version 5
  7.  *
  8.  * @category  Crypt
  9.  * @package   EC
  10.  * @author    Jim Wigginton <terrafrost@php.net>
  11.  * @copyright 2015 Jim Wigginton
  12.  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  13.  * @link      http://phpseclib.sourceforge.net
  14.  */
  15.  
  16. namespace phpseclib3\Crypt\EC\Formats\Keys;
  17.  
  18. use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve;
  19. use phpseclib3\Crypt\EC\Curves\Curve25519;
  20. use phpseclib3\Crypt\EC\Curves\Curve448;
  21. use phpseclib3\Math\BigInteger;
  22.  
  23. /**
  24.  * Montgomery Public Key Handler
  25.  *
  26.  * @package EC
  27.  * @author  Jim Wigginton <terrafrost@php.net>
  28.  * @access  public
  29.  */
  30. abstract class MontgomeryPublic
  31. {
  32.     /**
  33.      * Is invisible flag
  34.      *
  35.      * @access private
  36.      */
  37.     const IS_INVISIBLE = true;
  38.  
  39.     /**
  40.      * Break a public or private key down into its constituent components
  41.      *
  42.      * @access public
  43.      * @param string $key
  44.      * @param string $password optional
  45.      * @return array
  46.      */
  47.     public static function load($key, $password = '')
  48.     {
  49.         switch (strlen($key)) {
  50.             case 32:
  51.                 $curve = new Curve25519();
  52.                 break;
  53.             case 56:
  54.                 $curve = new Curve448();
  55.                 break;
  56.             default:
  57.                 throw new \LengthException('The only supported lengths are 32 and 56');
  58.         }
  59.  
  60.         $components = ['curve' => $curve];
  61.         $components['QA'] = [$components['curve']->convertInteger(new BigInteger(strrev($key), 256))];
  62.  
  63.         return $components;
  64.     }
  65.  
  66.     /**
  67.      * Convert an EC public key to the appropriate format
  68.      *
  69.      * @access public
  70.      * @param \phpseclib3\Crypt\EC\BaseCurves\Montgomery $curve
  71.      * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
  72.      * @return string
  73.      */
  74.     public static function savePublicKey(MontgomeryCurve $curve, array $publicKey)
  75.     {
  76.         return strrev($publicKey[0]->toBytes());
  77.     }
  78. }
  79.