1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\CryptoBridge; |
6
|
|
|
|
7
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Cipher\CipherAlgorithmIdentifier; |
8
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier; |
9
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo; |
10
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo; |
11
|
|
|
use Sop\CryptoTypes\Signature\Signature; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for crypto engine implementations. |
15
|
|
|
*/ |
16
|
|
|
abstract class Crypto |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Sign data with given algorithm using given private key. |
20
|
|
|
* |
21
|
|
|
* @param string $data Data to sign |
22
|
|
|
* @param PrivateKeyInfo $privkey_info Private key |
23
|
|
|
* @param SignatureAlgorithmIdentifier $algo Signature algorithm |
24
|
|
|
* |
25
|
|
|
* @return Signature |
26
|
|
|
*/ |
27
|
|
|
abstract public function sign(string $data, PrivateKeyInfo $privkey_info, |
28
|
|
|
SignatureAlgorithmIdentifier $algo): Signature; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Verify signature with given algorithm using given public key. |
32
|
|
|
* |
33
|
|
|
* @param string $data Data to verify |
34
|
|
|
* @param Signature $signature Signature |
35
|
|
|
* @param PublicKeyInfo $pubkey_info Public key |
36
|
|
|
* @param SignatureAlgorithmIdentifier $algo Signature algorithm |
37
|
|
|
* |
38
|
|
|
* @return bool True if signature matches |
39
|
|
|
*/ |
40
|
|
|
abstract public function verify(string $data, Signature $signature, |
41
|
|
|
PublicKeyInfo $pubkey_info, SignatureAlgorithmIdentifier $algo): bool; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Encrypt data with given algorithm using given key. |
45
|
|
|
* |
46
|
|
|
* Padding must be added by the caller. Initialization vector is |
47
|
|
|
* taken from the algorithm identifier if available. |
48
|
|
|
* |
49
|
|
|
* @param string $data Plaintext |
50
|
|
|
* @param string $key Encryption key |
51
|
|
|
* @param CipherAlgorithmIdentifier $algo Encryption algorithm |
52
|
|
|
* |
53
|
|
|
* @return string Ciphertext |
54
|
|
|
*/ |
55
|
|
|
abstract public function encrypt(string $data, string $key, |
56
|
|
|
CipherAlgorithmIdentifier $algo): string; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Decrypt data with given algorithm using given key. |
60
|
|
|
* |
61
|
|
|
* Possible padding is not removed and must be handled by the caller. |
62
|
|
|
* Initialization vector is taken from the algorithm identifier if |
63
|
|
|
* available. |
64
|
|
|
* |
65
|
|
|
* @param string $data Ciphertext |
66
|
|
|
* @param string $key Encryption key |
67
|
|
|
* @param CipherAlgorithmIdentifier $algo Encryption algorithm |
68
|
|
|
* |
69
|
|
|
* @return string Plaintext |
70
|
|
|
*/ |
71
|
|
|
abstract public function decrypt(string $data, string $key, |
72
|
|
|
CipherAlgorithmIdentifier $algo): string; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get default supported crypto implementation. |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
1 |
|
public static function getDefault(): self |
80
|
|
|
{ |
81
|
1 |
|
if (defined('OPENSSL_VERSION_NUMBER')) { |
82
|
1 |
|
return new Crypto\OpenSSLCrypto(); |
83
|
|
|
} |
84
|
|
|
// @codeCoverageIgnoreStart |
85
|
|
|
throw new \RuntimeException('No crypto implementation available.'); |
86
|
|
|
// @codeCoverageIgnoreEnd |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|