[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/EC/Formats/Signature/ -> SSH2.php (source)

   1  <?php
   2  
   3  /**
   4   * SSH2 Signature Handler
   5   *
   6   * PHP version 5
   7   *
   8   * Handles signatures in the format used by SSH2
   9   *
  10   * @author    Jim Wigginton <terrafrost@php.net>
  11   * @copyright 2016 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\Signature;
  17  
  18  use phpseclib3\Common\Functions\Strings;
  19  use phpseclib3\Math\BigInteger;
  20  
  21  /**
  22   * SSH2 Signature Handler
  23   *
  24   * @author  Jim Wigginton <terrafrost@php.net>
  25   */
  26  abstract class SSH2
  27  {
  28      /**
  29       * Loads a signature
  30       *
  31       * @param string $sig
  32       * @return mixed
  33       */
  34      public static function load($sig)
  35      {
  36          if (!is_string($sig)) {
  37              return false;
  38          }
  39  
  40          $result = Strings::unpackSSH2('ss', $sig);
  41          if ($result === false) {
  42              return false;
  43          }
  44          list($type, $blob) = $result;
  45          switch ($type) {
  46              // see https://tools.ietf.org/html/rfc5656#section-3.1.2
  47              case 'ecdsa-sha2-nistp256':
  48              case 'ecdsa-sha2-nistp384':
  49              case 'ecdsa-sha2-nistp521':
  50                  break;
  51              default:
  52                  return false;
  53          }
  54  
  55          $result = Strings::unpackSSH2('ii', $blob);
  56          if ($result === false) {
  57              return false;
  58          }
  59  
  60          return [
  61              'r' => $result[0],
  62              's' => $result[1]
  63          ];
  64      }
  65  
  66      /**
  67       * Returns a signature in the appropriate format
  68       *
  69       * @param \phpseclib3\Math\BigInteger $r
  70       * @param \phpseclib3\Math\BigInteger $s
  71       * @param string $curve
  72       * @return string
  73       */
  74      public static function save(BigInteger $r, BigInteger $s, $curve)
  75      {
  76          switch ($curve) {
  77              case 'secp256r1':
  78                  $curve = 'nistp256';
  79                  break;
  80              case 'secp384r1':
  81                  $curve = 'nistp384';
  82                  break;
  83              case 'secp521r1':
  84                  $curve = 'nistp521';
  85                  break;
  86              default:
  87                  return false;
  88          }
  89  
  90          $blob = Strings::packSSH2('ii', $r, $s);
  91  
  92          return Strings::packSSH2('ss', 'ecdsa-sha2-' . $curve, $blob);
  93      }
  94  }