pssht  latest
SSH server library written in PHP
Curve25519.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
19 {
21  protected $H;
22 
24  protected $Q_S;
25 
27  protected $K;
28 
30  protected $K_S;
31 
33  protected $kexDHInit;
34 
36  protected $kexAlgo;
37 
39  protected $serverKEX;
40 
42  protected $clientKEX;
43 
45  protected $serverIdent;
46 
48  protected $clientIdent;
49 
50 
78  public function __construct(
79  \fpoirotte\Pssht\Messages\KEX\ECDH\INIT\Curve25519 $kexDHInit,
80  \fpoirotte\Pssht\Key\KeyInterface $key,
81  \fpoirotte\Pssht\Encryption\EncryptionInterface $encryptionAlgo,
82  \fpoirotte\Pssht\KEX\KEXInterface $kexAlgo,
83  \fpoirotte\Pssht\Messages\KEXINIT $serverKEX,
84  \fpoirotte\Pssht\Messages\KEXINIT $clientKEX,
87  ) {
88  if (!is_string($serverIdent)) {
89  throw new \InvalidArgumentException();
90  }
91 
92  if (!is_string($clientIdent)) {
93  throw new \InvalidArgumentException();
94  }
95 
96  $curve = \fpoirotte\Pssht\ECC\Curve25519::getInstance();
97  $d_S = openssl_random_pseudo_bytes(32);
98  $this->Q_S = $curve->getPublic($d_S);
99  $Q_C = $kexDHInit->getQ();
100  $this->K = gmp_init(bin2hex($curve->getShared($d_S, $Q_C)), 16);
101  $this->K_S = $key;
102  $this->kexDHInit = $kexDHInit;
103  $this->kexAlgo = $kexAlgo;
104  $this->serverKEX = $serverKEX;
105  $this->clientKEX = $clientKEX;
106  $this->serverIdent = $serverIdent;
107  $this->clientIdent = $clientIdent;
108 
109  $msgId = chr(\fpoirotte\Pssht\Messages\KEXINIT::getMessageId());
110  // $sub is used to create the structure for the hashing function.
111  $sub = new \fpoirotte\Pssht\Wire\Encoder(new \fpoirotte\Pssht\Buffer());
112  $this->K_S->serialize($sub);
113  $K_S = $sub->getBuffer()->get(0);
114  $sub->encodeString($this->clientIdent);
115  $sub->encodeString($this->serverIdent);
116  // $sub2 is used to compute the value
117  // of various fields inside the structure.
118  $sub2 = new \fpoirotte\Pssht\Wire\Encoder(new \fpoirotte\Pssht\Buffer());
119  $sub2->encodeBytes($msgId); // Add message identifier.
120  $this->clientKEX->serialize($sub2);
121  $sub->encodeString($sub2->getBuffer()->get(0));
122  $sub2->encodeBytes($msgId); // Add message identifier.
123  $this->serverKEX->serialize($sub2);
124  $sub->encodeString($sub2->getBuffer()->get(0));
125  $sub->encodeString($K_S);
126  $sub->encodeString($Q_C);
127  $sub->encodeString($this->Q_S);
128  $sub->encodeMpint($this->K);
129 
130  $logging = \Plop\Plop::getInstance();
131  $origData = $sub->getBuffer()->get(0);
132  $data = wordwrap(bin2hex($origData), 4, ' ', true);
133  $data = wordwrap($data, 32 + 7, PHP_EOL, true);
134  $logging->debug("Signature payload:\r\n%s", array($data));
135 
136  $this->H = $this->kexAlgo->hash($origData);
137  }
138 
139  public static function getMessageId()
140  {
141  return 31;
142  }
143 
144  public function serialize(\fpoirotte\Pssht\Wire\Encoder $encoder)
145  {
146  $sub = new \fpoirotte\Pssht\Wire\Encoder(new \fpoirotte\Pssht\Buffer());
147  $this->K_S->serialize($sub);
148 
149  $encoder->encodeString($sub->getBuffer()->get(0));
150  $encoder->encodeString($this->Q_S);
151 
152  $sub->encodeString($this->K_S->getName());
153  $sub->encodeString($this->K_S->sign($this->H));
154  $encoder->encodeString($sub->getBuffer()->get(0));
155  return $this;
156  }
157 
158  public static function unserialize(\fpoirotte\Pssht\Wire\Decoder $decoder)
159  {
161  throw new \RuntimeException();
162  }
163 
171  public function getSharedSecret()
172  {
173  return $this->K;
174  }
175 
182  public function getExchangeHash()
183  {
184  return $this->H;
185  }
186 }
$kexAlgo
Key exchange algorithm to use.
Definition: Curve25519.php:36
$serverIdent
Server&#39;s identification string.
Definition: Curve25519.php:45
$clientIdent
Client&#39;s identification string.
Definition: Curve25519.php:48
serialize(\fpoirotte\Pssht\Wire\Encoder $encoder)
Definition: Curve25519.php:144
static unserialize(\fpoirotte\Pssht\Wire\Decoder $decoder)
Definition: Curve25519.php:158
$clientKEX
Algorithms supported by the client.
Definition: Curve25519.php:42
$kexDHInit
Client&#39;s contribution to the Diffie-Hellman Key Exchange.
Definition: Curve25519.php:33
$serverKEX
Algorithms supported by the server.
Definition: Curve25519.php:39
__construct(\fpoirotte\Pssht\Messages\KEX\ECDH\INIT\Curve25519 $kexDHInit,\fpoirotte\Pssht\Key\KeyInterface $key,\fpoirotte\Pssht\Encryption\EncryptionInterface $encryptionAlgo,\fpoirotte\Pssht\KEX\KEXInterface $kexAlgo,\fpoirotte\Pssht\Messages\KEXINIT $serverKEX,\fpoirotte\Pssht\Messages\KEXINIT $clientKEX, $serverIdent, $clientIdent)
Definition: Curve25519.php:78
$Q_S
Server&#39;s ephemeral public key.
Definition: Curve25519.php:24