|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the DUKPT package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016-2017 Tonic Health <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace TonicForHealth\DUKPT\Key; |
|
12
|
|
|
|
|
13
|
|
|
use TonicForHealth\DUKPT\Helper\Encryption\DESEncryptionHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DerivedKeyFactory |
|
17
|
|
|
* |
|
18
|
|
|
* @author Vitalii Ekert <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class DerivedKeyFactory implements KeyFactoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
const MODIFIED_KSN_SIZE_BYTES = 8; |
|
23
|
|
|
const ENCRYPTION_KEY_SIZE_BYTES = 8; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var DESEncryptionHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $encryptionHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* DerivedKeyFactory constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param DESEncryptionHelper $encryptionHelper |
|
34
|
|
|
*/ |
|
35
|
15 |
|
public function __construct(DESEncryptionHelper $encryptionHelper) |
|
36
|
|
|
{ |
|
37
|
15 |
|
$this->encryptionHelper = $encryptionHelper; |
|
38
|
15 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
* |
|
43
|
|
|
* @return DerivedKey |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function createFromHexadecimal($hexKey) |
|
46
|
|
|
{ |
|
47
|
3 |
|
return new DerivedKey(hex2bin($hexKey)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns a key from KSN and IPEK |
|
52
|
|
|
* |
|
53
|
|
|
* @param KeySerialNumber $ksn |
|
54
|
|
|
* @param InitialPinEncryptionKey $ipek |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \InvalidArgumentException If the provided key has a wrong size |
|
57
|
|
|
* |
|
58
|
|
|
* @return DerivedKey |
|
59
|
|
|
*/ |
|
60
|
11 |
|
public function create(KeySerialNumber $ksn, InitialPinEncryptionKey $ipek) |
|
61
|
|
|
{ |
|
62
|
11 |
|
$binKey = $ipek->toBinary(); |
|
63
|
|
|
|
|
64
|
11 |
|
$binInitKSN = $ksn->getInitialKey(); |
|
65
|
11 |
|
$binModifiedKSN = substr($binInitKSN, -self::MODIFIED_KSN_SIZE_BYTES); |
|
66
|
11 |
|
$counter = hexdec(bin2hex($ksn->getEncryptionCounter())); |
|
67
|
|
|
|
|
68
|
11 |
|
for ($mask = 0x100000; $mask > 0; $mask >>= 1) { |
|
69
|
11 |
|
if (0 < ($value = $counter & $mask)) { |
|
70
|
10 |
|
$hexValue = str_pad(dechex($value), DerivedKey::KEY_SIZE_BYTES, '0', STR_PAD_LEFT); |
|
71
|
10 |
|
$binModifiedKSN |= hex2bin($hexValue); |
|
72
|
10 |
|
$binKey = $this->calculateKey($binKey, $binModifiedKSN); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
11 |
|
return new DerivedKey($binKey); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $binCurrentSK |
|
81
|
|
|
* @param string $binModifiedKSN |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
10 |
|
protected function calculateKey($binCurrentSK, $binModifiedKSN) |
|
86
|
|
|
{ |
|
87
|
10 |
|
$binEncLowerMask = hex2bin(InitialPinEncryptionKeyFactory::ENCRYPTION_LOWER_KEY_MASK); |
|
88
|
|
|
|
|
89
|
10 |
|
$binUpperBytes = $this->calculateKeyPart($binCurrentSK ^ $binEncLowerMask, $binModifiedKSN); |
|
90
|
10 |
|
$binLowerBytes = $this->calculateKeyPart($binCurrentSK, $binModifiedKSN); |
|
91
|
|
|
|
|
92
|
10 |
|
return $binUpperBytes.$binLowerBytes; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $binCurrentSK |
|
97
|
|
|
* @param string $binModifiedKSN |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
10 |
|
protected function calculateKeyPart($binCurrentSK, $binModifiedKSN) |
|
102
|
|
|
{ |
|
103
|
10 |
|
$binUpperKey = substr($binCurrentSK, 0, self::ENCRYPTION_KEY_SIZE_BYTES); |
|
104
|
10 |
|
$binLowerKey = substr($binCurrentSK, -self::ENCRYPTION_KEY_SIZE_BYTES); |
|
105
|
10 |
|
$binMessage = $binLowerKey ^ $binModifiedKSN; |
|
106
|
|
|
|
|
107
|
10 |
|
$cryptText = $this->encryptionHelper->encrypt($binUpperKey, $binMessage); |
|
108
|
|
|
|
|
109
|
10 |
|
return $cryptText ^ $binLowerKey; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|