1--TEST-- 2Crypto\Cipher::setTagLength in CCM mode basic usage. 3--SKIPIF-- 4<?php 5if (!Crypto\Cipher::hasMode(Crypto\Cipher::MODE_CCM)) 6 die("Skip: CCM mode not defined (update OpenSSL version)"); 7?> 8--FILE-- 9<?php 10$key = pack("H*", 'ceb009aea4454451feadf0e6b36f45555dd04723baa448e8'); 11$nonce = pack("H*", '764043c49460b7'); 12 13$data = pack("H*", 'c8d275f919e17d7fe69c2a1f58939dfe4d403791b5df1310'); 14 15$cipher = new Crypto\Cipher('aes-192-ccm'); 16 17try { 18 $cipher->setTagLength(1); 19} 20catch (Crypto\CipherException $e) { 21 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_LOW) { 22 echo "LOW\n"; 23 } 24} 25 26$cipher = new Crypto\Cipher('aes-192-ccm'); 27try { 28 $cipher->setTagLength(100); 29} 30catch (Crypto\CipherException $e) { 31 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_HIGH) { 32 echo "HIGH\n"; 33 } 34} 35 36$cipher = new Crypto\Cipher('aes-192-ccm'); 37$cipher->decryptInit($key, $nonce); 38try { 39 $cipher->setTagLength(12); 40} 41catch (Crypto\CipherException $e) { 42 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_SETTER_FORBIDDEN) { 43 echo "FLOW\n"; 44 } 45} 46 47// test encryption 48$cipher = new Crypto\Cipher('aes-192-ccm'); 49$cipher->setTagLength(12); 50$ct = $cipher->encrypt($data, $key, $nonce); 51echo bin2hex($ct) . "\n"; 52$tag = $cipher->getTag(); 53echo bin2hex($tag) . "\n"; 54 55// test decryption (if tag is accepted) 56$cipher = new Crypto\Cipher('aes-192-ccm'); 57$cipher->setTag($tag); 58echo bin2hex($cipher->decrypt($ct, $key, $nonce)) . "\n"; 59 60?> 61--EXPECT-- 62LOW 63HIGH 64FLOW 658a0f3d8229e48e7487fd95a28ad392c80b3681d4fbc7bbfd 66b0b16bbb246d585ca392b045 67c8d275f919e17d7fe69c2a1f58939dfe4d403791b5df1310 68