Subversion Repositories oidplus

Rev

Rev 846 | Rev 1042 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 846 Rev 874
Line 14... Line 14...
14
 * $secret = DH::computeSecret($ourPrivate, $theirPublic);
14
 * $secret = DH::computeSecret($ourPrivate, $theirPublic);
15
 *
15
 *
16
 * ?>
16
 * ?>
17
 * </code>
17
 * </code>
18
 *
18
 *
-
 
19
 * @category  Crypt
-
 
20
 * @package   DH
19
 * @author    Jim Wigginton <terrafrost@php.net>
21
 * @author    Jim Wigginton <terrafrost@php.net>
20
 * @copyright 2016 Jim Wigginton
22
 * @copyright 2016 Jim Wigginton
21
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
23
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @link      http://phpseclib.sourceforge.net
24
 * @link      http://phpseclib.sourceforge.net
23
 */
25
 */
Line 33... Line 35...
33
use phpseclib3\Math\BigInteger;
35
use phpseclib3\Math\BigInteger;
34
 
36
 
35
/**
37
/**
36
 * Pure-PHP (EC)DH implementation
38
 * Pure-PHP (EC)DH implementation
37
 *
39
 *
-
 
40
 * @package DH
38
 * @author  Jim Wigginton <terrafrost@php.net>
41
 * @author  Jim Wigginton <terrafrost@php.net>
-
 
42
 * @access  public
39
 */
43
 */
40
abstract class DH extends AsymmetricKey
44
abstract class DH extends AsymmetricKey
41
{
45
{
42
    /**
46
    /**
43
     * Algorithm Name
47
     * Algorithm Name
44
     *
48
     *
45
     * @var string
49
     * @var string
-
 
50
     * @access private
46
     */
51
     */
47
    const ALGORITHM = 'DH';
52
    const ALGORITHM = 'DH';
48
 
53
 
49
    /**
54
    /**
50
     * DH prime
55
     * DH prime
51
     *
56
     *
52
     * @var \phpseclib3\Math\BigInteger
57
     * @var \phpseclib3\Math\BigInteger
-
 
58
     * @access private
53
     */
59
     */
54
    protected $prime;
60
    protected $prime;
55
 
61
 
56
    /**
62
    /**
57
     * DH Base
63
     * DH Base
58
     *
64
     *
59
     * Prime divisor of p-1
65
     * Prime divisor of p-1
60
     *
66
     *
61
     * @var \phpseclib3\Math\BigInteger
67
     * @var \phpseclib3\Math\BigInteger
-
 
68
     * @access private
62
     */
69
     */
63
    protected $base;
70
    protected $base;
64
 
71
 
65
    /**
72
    /**
66
     * Create DH parameters
73
     * Create DH parameters
Line 68... Line 75...
68
     * This method is a bit polymorphic. It can take any of the following:
75
     * This method is a bit polymorphic. It can take any of the following:
69
     *  - two BigInteger's (prime and base)
76
     *  - two BigInteger's (prime and base)
70
     *  - an integer representing the size of the prime in bits (the base is assumed to be 2)
77
     *  - an integer representing the size of the prime in bits (the base is assumed to be 2)
71
     *  - a string (eg. diffie-hellman-group14-sha1)
78
     *  - a string (eg. diffie-hellman-group14-sha1)
72
     *
79
     *
-
 
80
     * @access public
73
     * @return Parameters
81
     * @return Parameters
74
     */
82
     */
75
    public static function createParameters(...$args)
83
    public static function createParameters(...$args)
76
    {
84
    {
77
        $params = new Parameters();
85
        $params = new Parameters();
Line 229... Line 237...
229
     *
237
     *
230
     * $length is in bits
238
     * $length is in bits
231
     *
239
     *
232
     * @param Parameters $params
240
     * @param Parameters $params
233
     * @param int $length optional
241
     * @param int $length optional
-
 
242
     * @access public
234
     * @return DH\PrivateKey
243
     * @return DH\PrivateKey
235
     */
244
     */
236
    public static function createKey(Parameters $params, $length = 0)
245
    public static function createKey(Parameters $params, $length = 0)
237
    {
246
    {
238
        $one = new BigInteger(1);
247
        $one = new BigInteger(1);
Line 254... Line 263...
254
    /**
263
    /**
255
     * Compute Shared Secret
264
     * Compute Shared Secret
256
     *
265
     *
257
     * @param PrivateKey|EC $private
266
     * @param PrivateKey|EC $private
258
     * @param PublicKey|BigInteger|string $public
267
     * @param PublicKey|BigInteger|string $public
-
 
268
     * @access public
259
     * @return mixed
269
     * @return mixed
260
     */
270
     */
261
    public static function computeSecret($private, $public)
271
    public static function computeSecret($private, $public)
262
    {
272
    {
263
        if ($private instanceof PrivateKey) { // DH\PrivateKey
273
        if ($private instanceof PrivateKey) { // DH\PrivateKey
Line 324... Line 334...
324
 
334
 
325
    /**
335
    /**
326
     * OnLoad Handler
336
     * OnLoad Handler
327
     *
337
     *
328
     * @return bool
338
     * @return bool
-
 
339
     * @access protected
329
     * @param array $components
340
     * @param array $components
330
     */
341
     */
331
    protected static function onLoad($components)
342
    protected static function onLoad($components)
332
    {
343
    {
333
        if (!isset($components['privateKey']) && !isset($components['publicKey'])) {
344
        if (!isset($components['privateKey']) && !isset($components['publicKey'])) {
Line 352... Line 363...
352
    }
363
    }
353
 
364
 
354
    /**
365
    /**
355
     * Determines which hashing function should be used
366
     * Determines which hashing function should be used
356
     *
367
     *
-
 
368
     * @access public
357
     * @param string $hash
369
     * @param string $hash
358
     */
370
     */
359
    public function withHash($hash)
371
    public function withHash($hash)
360
    {
372
    {
361
        throw new UnsupportedOperationException('DH does not use a hash algorithm');
373
        throw new UnsupportedOperationException('DH does not use a hash algorithm');
362
    }
374
    }
363
 
375
 
364
    /**
376
    /**
365
     * Returns the hash algorithm currently being used
377
     * Returns the hash algorithm currently being used
366
     *
378
     *
-
 
379
     * @access public
367
     */
380
     */
368
    public function getHash()
381
    public function getHash()
369
    {
382
    {
370
        throw new UnsupportedOperationException('DH does not use a hash algorithm');
383
        throw new UnsupportedOperationException('DH does not use a hash algorithm');
371
    }
384
    }
Line 375... Line 388...
375
     *
388
     *
376
     * A public / private key is only returned if the currently loaded "key" contains an x or y
389
     * A public / private key is only returned if the currently loaded "key" contains an x or y
377
     * value.
390
     * value.
378
     *
391
     *
379
     * @see self::getPublicKey()
392
     * @see self::getPublicKey()
-
 
393
     * @access public
380
     * @return mixed
394
     * @return mixed
381
     */
395
     */
382
    public function getParameters()
396
    public function getParameters()
383
    {
397
    {
384
        $type = self::validatePlugin('Keys', 'PKCS1', 'saveParameters');
398
        $type = self::validatePlugin('Keys', 'PKCS1', 'saveParameters');