1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\GCM\Cipher\AES; |
6
|
|
|
|
7
|
|
|
use Sop\GCM\Cipher\Cipher; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base class for AES ciphers for the GCM. |
11
|
|
|
*/ |
12
|
|
|
abstract class AESCipher implements Cipher |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Mapping from key size in bits to AES cipher implementation class name. |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
const MAP_KEYSIZE_TO_CLS = array( |
22
|
|
|
/* @formatter:off */ |
23
|
|
|
128 => AES128Cipher::class, |
24
|
|
|
192 => AES192Cipher::class, |
25
|
|
|
256 => AES256Cipher::class |
26
|
|
|
/* @formatter:on */ |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the cipher method name recognized by OpenSSL. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
abstract protected function _cipherName(): string; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the key size in bytes. |
38
|
|
|
* |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
abstract protected function _keySize(): int; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get AES cipher instance by key length. |
45
|
|
|
* |
46
|
|
|
* @param int $len Key length in bytes |
47
|
|
|
* @throws \UnexpectedValueException |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
40 |
|
public static function fromKeyLength(int $len): self |
51
|
|
|
{ |
52
|
40 |
|
$bits = $len << 3; |
53
|
40 |
|
if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) { |
54
|
1 |
|
throw new \UnexpectedValueException( |
55
|
1 |
|
"No AES implementation for $bits-bit key size."); |
56
|
|
|
} |
57
|
39 |
|
$cls = self::MAP_KEYSIZE_TO_CLS[$bits]; |
58
|
39 |
|
return new $cls(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @see \Sop\GCM\Cipher\Cipher::encrypt() |
64
|
|
|
* @throws \UnexpectedValueException If key size is incorrect |
65
|
|
|
* @throws \RuntimeException For generic errors |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
56 |
|
public function encrypt(string $data, string $key): string |
69
|
|
|
{ |
70
|
56 |
|
$key_size = $this->_keySize(); |
71
|
56 |
|
if (strlen($key) != $key_size) { |
72
|
3 |
|
throw new \UnexpectedValueException( |
73
|
3 |
|
"Key size must be $key_size bytes."); |
74
|
|
|
} |
75
|
53 |
|
$result = openssl_encrypt($data, $this->_cipherName(), $key, |
76
|
53 |
|
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
77
|
53 |
|
if (false === $result) { |
78
|
1 |
|
throw new \RuntimeException( |
79
|
1 |
|
"openssl_encrypt() failed: " . self::_getLastOpenSSLError()); |
80
|
|
|
} |
81
|
52 |
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get latest OpenSSL error message. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
protected static function _getLastOpenSSLError(): string |
90
|
|
|
{ |
91
|
1 |
|
$msg = ''; |
92
|
1 |
|
while (false !== ($err = openssl_error_string())) { |
93
|
1 |
|
$msg = $err; |
94
|
|
|
} |
95
|
1 |
|
return $msg; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|