-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathRSA15.php
More file actions
79 lines (68 loc) · 1.73 KB
/
Copy pathRSA15.php
File metadata and controls
79 lines (68 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\RSAKey;
use Jose\Component\Encryption\Algorithm\KeyEncryption\Util\RSACrypt;
use Override;
use function is_string;
final readonly class RSA15 extends RSA
{
/**
* @var array<string, int>
*/
private const CEK_LENGTHS = [
'A128GCM' => 16,
'A192GCM' => 24,
'A256GCM' => 32,
'A128CBC-HS256' => 32,
'A192CBC-HS384' => 48,
'A256CBC-HS512' => 64,
];
#[Override]
public function name(): string
{
return 'RSA1_5';
}
/**
* @param array<string, mixed> $header
*/
#[Override]
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string
{
$this->checkKey($key);
if (! $key->has('d')) {
throw new InvalidArgumentException('The key is not a private key');
}
$priv = RSAKey::createFromJWK($key);
return RSACrypt::decrypt(
$priv,
$encrypted_cek,
RSACrypt::ENCRYPTION_PKCS1,
null,
$this->getExpectedCekLength($header)
);
}
#[Override]
protected function getEncryptionMode(): int
{
return RSACrypt::ENCRYPTION_PKCS1;
}
#[Override]
protected function getHashAlgorithm(): ?string
{
return null;
}
/**
* @param array<string, mixed> $header
*/
private function getExpectedCekLength(array $header): ?int
{
$enc = $header['enc'] ?? null;
if (! is_string($enc)) {
return null;
}
return self::CEK_LENGTHS[$enc] ?? null;
}
}