Signature.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Elliptic\EC;
  3. use Elliptic\Utils;
  4. use BN\BN;
  5. class Signature
  6. {
  7. public $r;
  8. public $s;
  9. public $recoveryParam;
  10. function __construct($options, $enc = false)
  11. {
  12. if ($options instanceof Signature) {
  13. $this->r = $options->r;
  14. $this->s = $options->s;
  15. $this->recoveryParam = $options->recoveryParam;
  16. return;
  17. }
  18. if (isset($options['r'])) {
  19. assert(isset($options["r"]) && isset($options["s"])); //, "Signature without r or s");
  20. $this->r = new BN($options["r"], 16);
  21. $this->s = new BN($options["s"], 16);
  22. if( isset($options["recoveryParam"]) )
  23. $this->recoveryParam = $options["recoveryParam"];
  24. else
  25. $this->recoveryParam = null;
  26. return;
  27. }
  28. if (!$this->_importDER($options, $enc))
  29. throw new \Exception('Unknown signature format');
  30. }
  31. private static function getLength($buf, &$pos)
  32. {
  33. $initial = $buf[$pos++];
  34. if( !($initial & 0x80) )
  35. return $initial;
  36. $octetLen = $initial & 0xf;
  37. $val = 0;
  38. for($i = 0; $i < $octetLen; $i++)
  39. {
  40. $val = $val << 8;
  41. $val = $val | $buf[$pos];
  42. $pos++;
  43. }
  44. return $val;
  45. }
  46. private static function rmPadding(&$buf)
  47. {
  48. $i = 0;
  49. $len = count($buf) - 1;
  50. while($i < $len && !$buf[$i] && !($buf[$i+1] & 0x80) )
  51. $i++;
  52. if( $i === 0 )
  53. return $buf;
  54. return array_slice($buf, $i);
  55. }
  56. private function _importDER($data, $enc)
  57. {
  58. $data = Utils::toArray($data, $enc);
  59. $dataLen = count($data);
  60. $place = 0;
  61. if( $data[$place++] !== 0x30)
  62. return false;
  63. $len = self::getLength($data, $place);
  64. if( ($len + $place) !== $dataLen )
  65. return false;
  66. if( $data[$place++] !== 0x02 )
  67. return false;
  68. $rlen = self::getLength($data, $place);
  69. $r = array_slice($data, $place, $rlen);
  70. $place += $rlen;
  71. if( $data[$place++] !== 0x02 )
  72. return false;
  73. $slen = self::getLength($data, $place);
  74. if( $dataLen !== $slen + $place )
  75. return false;
  76. $s = array_slice($data, $place, $slen);
  77. if( $r[0] === 0 && ($r[1] & 0x80 ) )
  78. $r = array_slice($r, 1);
  79. if( $s[0] === 0 && ($s[1] & 0x80 ) )
  80. $s = array_slice($s, 1);
  81. $this->r = new BN($r);
  82. $this->s = new BN($s);
  83. $this->recoveryParam = null;
  84. return true;
  85. }
  86. private static function constructLength(&$arr, $len)
  87. {
  88. if( $len < 0x80 )
  89. {
  90. array_push($arr, $len);
  91. return;
  92. }
  93. $octets = 1 + (log($len) / M_LN2 >> 3);
  94. array_push($arr, $octets | 0x80);
  95. while(--$octets)
  96. array_push($arr, ($len >> ($octets << 3)) & 0xff);
  97. array_push($arr, $len);
  98. }
  99. public function toDER($enc = false)
  100. {
  101. $r = $this->r->toArray();
  102. $s = $this->s->toArray();
  103. //Pad values
  104. if( $r[0] & 0x80 )
  105. array_unshift($r, 0);
  106. if( $s[0] & 0x80 )
  107. array_unshift($s, 0);
  108. $r = self::rmPadding($r);
  109. $s = self::rmPadding($s);
  110. while(!$s[0] && !($s[1] & 0x80))
  111. array_slice($s, 1);
  112. $arr = array(0x02);
  113. self::constructLength($arr, count($r));
  114. $arr = array_merge($arr, $r, array(0x02));
  115. self::constructLength($arr, count($s));
  116. $backHalf = array_merge($arr, $s);
  117. $res = array(0x30);
  118. self::constructLength($res, count($backHalf));
  119. $res = array_merge($res, $backHalf);
  120. return Utils::encode($res, $enc);
  121. }
  122. }
  123. ?>