1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jose\Component\Core\Util\Ecc; |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
* ********************************************************************* |
16
|
|
|
* Copyright (C) 2012 Matyas Danter |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
19
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
20
|
|
|
* to deal in the Software without restriction, including without limitation |
21
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
22
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
23
|
|
|
* Software is furnished to do so, subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included |
26
|
|
|
* in all copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
29
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
30
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
31
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
32
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
33
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
34
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
* *********************************************************************** |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This class is the implementation of ECDH. |
40
|
|
|
* EcDH is safe key exchange and achieves |
41
|
|
|
* that a key is transported securely between two parties. |
42
|
|
|
* The key then can be hashed and used as a basis in |
43
|
|
|
* a dual encryption scheme, along with AES for faster |
44
|
|
|
* two- way encryption. |
45
|
|
|
*/ |
46
|
|
|
final class EcDH |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @param Curve $curve |
50
|
|
|
* @param PublicKey $publicKey |
51
|
|
|
* @param PrivateKey $privateKey |
52
|
|
|
* |
53
|
|
|
* @return \GMP |
54
|
|
|
*/ |
55
|
|
|
public static function computeSharedKey(Curve $curve, PublicKey $publicKey, PrivateKey $privateKey): \GMP |
56
|
|
|
{ |
57
|
|
|
return $curve->mul($publicKey->getPoint(), $privateKey->getSecret())->getX(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|