1--TEST-- 2Crypto\Cipher::getAAD in GCM mode basic usage. 3--SKIPIF-- 4<?php 5if (!Crypto\Cipher::hasMode(Crypto\Cipher::MODE_GCM)) 6 die("Skip: GCM mode not defined (update OpenSSL version)"); 7?> 8--FILE-- 9<?php 10$key = str_repeat('x', 32); 11$iv = str_repeat('i', 16); 12$data = str_repeat('a', 16); 13$aad = str_repeat('b', 16); 14 15// encryption 16$cipher = new Crypto\Cipher('aes-256-gcm'); 17$cipher->setAAD($aad); 18$ct = $cipher->encrypt($data, $key, $iv); 19$tag = $cipher->getTag(); 20echo bin2hex($ct) . "\n"; 21 22// decryption 23$cipher = new Crypto\Cipher('aes-256-gcm'); 24$cipher->setTag($tag); 25$cipher->setAAD($aad); 26echo $cipher->decrypt($ct, $key, $iv) . "\n"; 27 28// flow exception 29try { 30 $cipher->setAAD($aad); 31} 32catch (Crypto\CipherException $e) { 33 if ($e->getCode() == Crypto\CipherException::AAD_SETTER_FORBIDDEN) { 34 echo "CIPHER_AAD_SETTER_FLOW\n"; 35 } 36} 37 38 39// decryption withou setting AAD 40try { 41 $cipher = new Crypto\Cipher('aes-256-gcm'); 42 $cipher->setTag($tag); 43 echo $cipher->decrypt($ct, $key, $iv) . "\n"; 44} 45catch (Crypto\CipherException $e) { 46 if ($e->getCode() == Crypto\CipherException::TAG_VERIFY_FAILED) { 47 echo "TAG_VERIFY_FAILED\n"; 48 } 49} 50 51 52 53?> 54--EXPECT-- 55622070d3bea6f720943d1198a7e6afa5 56aaaaaaaaaaaaaaaa 57CIPHER_AAD_SETTER_FLOW 58TAG_VERIFY_FAILED 59