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\Math\GmpMath; |
30
|
|
|
use Jose\Component\Core\Util\Ecc\Primitives\CurveFp; |
31
|
|
|
use Jose\Component\Core\Util\Ecc\Primitives\GeneratorPoint; |
32
|
|
|
use Jose\Component\Core\Util\Ecc\Primitives\Point; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This class serves as public- private key exchange for signature verification |
36
|
|
|
*/ |
37
|
|
|
final class PublicKey |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var CurveFp |
42
|
|
|
*/ |
43
|
|
|
protected $curve; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @var GeneratorPoint |
48
|
|
|
*/ |
49
|
|
|
protected $generator; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @var Point |
54
|
|
|
*/ |
55
|
|
|
protected $point; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* @var GmpMath |
60
|
|
|
*/ |
61
|
|
|
protected $adapter; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize a new instance. |
65
|
|
|
* |
66
|
|
|
* @param GmpMath $adapter |
67
|
|
|
* @param GeneratorPoint $generator |
68
|
|
|
* @param Point $point |
69
|
|
|
* @throws \LogicException |
70
|
|
|
* @throws \RuntimeException |
71
|
|
|
*/ |
72
|
|
|
public function __construct(GmpMath $adapter, GeneratorPoint $generator, Point $point) |
73
|
|
|
{ |
74
|
|
|
$this->curve = $generator->getCurve(); |
75
|
|
|
$this->generator = $generator; |
76
|
|
|
$this->point = $point; |
77
|
|
|
$this->adapter = $adapter; |
78
|
|
|
|
79
|
|
|
$n = $generator->getOrder(); |
80
|
|
|
|
81
|
|
|
if ($adapter->cmp($point->getX(), gmp_init(0, 10)) < 0 || $adapter->cmp($n, $point->getX()) <= 0 |
82
|
|
|
|| $adapter->cmp($point->getY(), gmp_init(0, 10)) < 0 || $adapter->cmp($n, $point->getY()) <= 0 |
83
|
|
|
) { |
84
|
|
|
throw new \RuntimeException("Generator point has x and y out of range."); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey::getCurve() |
91
|
|
|
*/ |
92
|
|
|
public function getCurve() |
93
|
|
|
{ |
94
|
|
|
return $this->curve; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {$inheritDoc} |
99
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey::getGenerator() |
100
|
|
|
*/ |
101
|
|
|
public function getGenerator() |
102
|
|
|
{ |
103
|
|
|
return $this->generator; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey::getPoint() |
109
|
|
|
*/ |
110
|
|
|
public function getPoint() |
111
|
|
|
{ |
112
|
|
|
return $this->point; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|