3 declare(strict_types=1);
5 namespace Jose\Component\Encryption\Algorithm\ContentEncryption;
7 use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
8 use ParagonIE\ConstantTime\Base64UrlSafe;
10 use function extension_loaded;
11 use const OPENSSL_RAW_DATA;
13 abstract class AESGCM implements ContentEncryptionAlgorithm
15 public function __construct()
17 if (! extension_loaded('openssl')) {
18 throw new RuntimeException('Please install the OpenSSL extension');
22 public function allowedKeyTypes(): array
24 return []; //Irrelevant
27 public function encryptContent(
32 string $encoded_protected_header,
35 $calculated_aad = $encoded_protected_header;
37 $calculated_aad .= '.' . Base64UrlSafe::encodeUnpadded($aad);
40 $result = openssl_encrypt($data, $this->getMode(), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
41 if ($result === false) {
42 throw new RuntimeException('Unable to encrypt the content');
48 public function decryptContent(
53 string $encoded_protected_header,
56 $calculated_aad = $encoded_protected_header;
58 $calculated_aad .= '.' . Base64UrlSafe::encodeUnpadded($aad);
61 $result = openssl_decrypt($data, $this->getMode(), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
62 if ($result === false) {
63 throw new RuntimeException('Unable to decrypt the content');
69 public function getIVSize(): int
74 abstract protected function getMode(): string;