Failed Conditions
Push — v7 ( 896db9...bffd87 )
by Florent
03:38
created

EcDH::setRecipientKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Jose\Component\Core\Util\Ecc\Crypto\EcDH;
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\Key\PrivateKey;
30
use Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey;
31
use Jose\Component\Core\Util\Ecc\Primitives\Point;
32
33
/**
34
 * This class is the implementation of ECDH.
35
 * EcDH is safe key exchange and achieves
36
 * that a key is transported securely between two parties.
37
 * The key then can be hashed and used as a basis in
38
 * a dual encryption scheme, along with AES for faster
39
 * two- way encryption.
40
 */
41
final class EcDH
42
{
43
    /**
44
     * Secret key between the two parties
45
     *
46
     * @var Point
47
     */
48
    private $secretKey = null;
49
50
    /**
51
     *
52
     * @var PublicKey
53
     */
54
    private $recipientKey;
55
56
    /**
57
     *
58
     * @var PrivateKey
59
     */
60
    private $senderKey;
61
62
    /**
63
     * @return \GMP
64
     */
65
    public function calculateSharedKey(): \GMP
66
    {
67
        $this->calculateKey();
68
69
        return $this->secretKey->getX();
70
    }
71
72
    /**
73
     * @param PublicKey|null $key
74
     */
75
    public function setRecipientKey(?PublicKey $key = null)
76
    {
77
        $this->recipientKey = $key;
78
    }
79
80
    /**
81
     * @param PrivateKey $key
82
     */
83
    public function setSenderKey(PrivateKey $key)
84
    {
85
        $this->senderKey = $key;
86
    }
87
88
    /**
89
     *
90
     */
91
    private function calculateKey()
92
    {
93
        $this->checkExchangeState();
94
95
        if ($this->secretKey === null) {
96
            $this->secretKey = $this->recipientKey->getPoint()->mul($this->senderKey->getSecret());
97
        }
98
    }
99
100
    /**
101
     * Verifies that the shared secret is known, or that the required keys are available
102
     * to calculate the shared secret.
103
     * @throws \RuntimeException when the exchange has not been made.
104
     */
105
    private function checkExchangeState()
106
    {
107
        if ($this->secretKey !== null) {
108
            return;
109
        }
110
111
        if ($this->senderKey === null) {
112
            throw new \RuntimeException('Sender key not set.');
113
        }
114
115
        if ($this->recipientKey === null) {
116
            throw new \RuntimeException('Recipient key not set.');
117
        }
118
    }
119
}
120