1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jose\Component\Core\Util\Ecc\Crypto\Key; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ********************************************************************* |
7
|
|
|
* Copyright (C) 2012 Matyas Danter |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
10
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
11
|
|
|
* to deal in the Software without restriction, including without limitation |
12
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
13
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
14
|
|
|
* Software is furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included |
17
|
|
|
* in all copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
20
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
23
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
24
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
25
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
26
|
|
|
* *********************************************************************** |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
use Jose\Component\Core\Util\Ecc\Crypto\EcDH\EcDH; |
30
|
|
|
use Jose\Component\Core\Util\Ecc\Math\GmpMath; |
31
|
|
|
use Jose\Component\Core\Util\Ecc\Primitives\GeneratorPoint; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This class serves as public - private key exchange for signature verification. |
35
|
|
|
*/ |
36
|
|
|
final class PrivateKey |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var GeneratorPoint |
40
|
|
|
*/ |
41
|
|
|
private $generator; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \GMP |
45
|
|
|
*/ |
46
|
|
|
private $secretMultiplier; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var GmpMath |
50
|
|
|
*/ |
51
|
|
|
private $adapter; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param GmpMath $adapter |
55
|
|
|
* @param GeneratorPoint $generator |
56
|
|
|
* @param \GMP $secretMultiplier |
57
|
|
|
*/ |
58
|
|
|
public function __construct(GmpMath $adapter, GeneratorPoint $generator, \GMP $secretMultiplier) |
59
|
|
|
{ |
60
|
|
|
$this->adapter = $adapter; |
61
|
|
|
$this->generator = $generator; |
62
|
|
|
$this->secretMultiplier = $secretMultiplier; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey::getPublicKey() |
68
|
|
|
*/ |
69
|
|
|
public function getPublicKey() |
70
|
|
|
{ |
71
|
|
|
return new PublicKey($this->adapter, $this->generator, $this->generator->mul($this->secretMultiplier)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey::getPoint() |
77
|
|
|
*/ |
78
|
|
|
public function getPoint() |
79
|
|
|
{ |
80
|
|
|
return $this->generator; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey::getCurve() |
86
|
|
|
*/ |
87
|
|
|
public function getCurve() |
88
|
|
|
{ |
89
|
|
|
return $this->generator->getCurve(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey::getSecret() |
95
|
|
|
*/ |
96
|
|
|
public function getSecret() |
97
|
|
|
{ |
98
|
|
|
return $this->secretMultiplier; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey::createExchange() |
104
|
|
|
*/ |
105
|
|
|
public function createExchange(PublicKey $recipient = null) |
106
|
|
|
{ |
107
|
|
|
$ecdh = new EcDH($this->adapter); |
108
|
|
|
$ecdh |
109
|
|
|
->setSenderKey($this) |
110
|
|
|
->setRecipientKey($recipient); |
111
|
|
|
|
112
|
|
|
return $ecdh; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|