|
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\Crypto\EcDH; |
|
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
|
|
|
use Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey; |
|
39
|
|
|
use Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey; |
|
40
|
|
|
use Jose\Component\Core\Util\Ecc\Primitives\Curve; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* This class is the implementation of ECDH. |
|
44
|
|
|
* EcDH is safe key exchange and achieves |
|
45
|
|
|
* that a key is transported securely between two parties. |
|
46
|
|
|
* The key then can be hashed and used as a basis in |
|
47
|
|
|
* a dual encryption scheme, along with AES for faster |
|
48
|
|
|
* two- way encryption. |
|
49
|
|
|
*/ |
|
50
|
|
|
final class EcDH |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* @param Curve $curve |
|
54
|
|
|
* @param PublicKey $publicKey |
|
55
|
|
|
* @param PrivateKey $privateKey |
|
56
|
|
|
* |
|
57
|
|
|
* @return \GMP |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function computeSharedKey(Curve $curve, PublicKey $publicKey, PrivateKey $privateKey): \GMP |
|
60
|
|
|
{ |
|
61
|
|
|
return $curve->mul($publicKey->getPoint(), $privateKey->getSecret())->getX(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|