-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the EAX mode of operation
- Loading branch information
Showing
12 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace fpoirotte\Cryptal\Modes; | ||
|
||
use fpoirotte\Cryptal\Implementers\CryptoInterface; | ||
use fpoirotte\Cryptal\AsymmetricModeInterface; | ||
use fpoirotte\Cryptal\DefaultAlgorithms\Cmac; | ||
use fpoirotte\Cryptal\MacEnum; | ||
|
||
/** | ||
* Cipher Block Chaining mode | ||
*/ | ||
class EAX implements AsymmetricModeInterface | ||
{ | ||
/// Cipher | ||
protected $cipher; | ||
|
||
/// Nonce | ||
protected $nonce; | ||
|
||
/// Output tag length | ||
protected $taglen; | ||
|
||
public function __construct(CryptoInterface $cipher, $iv, $tagLength) | ||
{ | ||
$this->cipher = $cipher; | ||
$this->nonce = $iv; | ||
$this->taglen = $tagLength; | ||
$this->omac = new Cmac(MacEnum::MAC_CMAC(), $cipher->getCipher(), $cipher->getKey()); | ||
} | ||
|
||
public function encrypt($data, $context) | ||
{ | ||
$options = stream_context_get_options($context); | ||
$H = isset($options['cryptal']['data']) ? (string) $options['cryptal']['data'] : ''; | ||
$blockSize = $this->cipher->getBlockSize(); | ||
$pad = str_repeat("\x00", $blockSize - 1); | ||
$tN = (clone $this->omac)->update($pad . "\x00" . $this->nonce)->finish(true); | ||
$tH = (clone $this->omac)->update($pad . "\x01" . $H)->finish(true); | ||
|
||
$ctr = new CTR($this->cipher, $tN, $this->taglen); | ||
$C = ''; | ||
foreach (str_split($data, $blockSize) as $block) { | ||
$C .= $ctr->encrypt($block, null); | ||
} | ||
|
||
$tC = (clone $this->omac)->update($pad . "\x02" . $C)->finish(true); | ||
stream_context_set_option($context, 'cryptal', 'tag', (string) substr($tN ^ $tH ^ $tC, 0, $this->taglen)); | ||
return $C; | ||
} | ||
|
||
public function decrypt($data, $context) | ||
{ | ||
$options = stream_context_get_options($context); | ||
$H = isset($options['cryptal']['data']) ? (string) $options['cryptal']['data'] : ''; | ||
$T = isset($options['cryptal']['tag']) ? (string) $options['cryptal']['tag'] : ''; | ||
$blockSize = $this->cipher->getBlockSize(); | ||
$pad = str_repeat("\x00", $blockSize - 1); | ||
$tN = (clone $this->omac)->update($pad . "\x00" . $this->nonce)->finish(true); | ||
$tH = (clone $this->omac)->update($pad . "\x01" . $H)->finish(true); | ||
$tC = (clone $this->omac)->update($pad . "\x02" . $data)->finish(true); | ||
$T2 = (string) substr($tN ^ $tH ^ $tC, 0, $this->taglen); | ||
|
||
if ($T2 !== $T) { | ||
throw new \InvalidArgumentException('Tag does not match expected value'); | ||
} | ||
|
||
$ctr = new CTR($this->cipher, $tN, $this->taglen); | ||
$P = ''; | ||
foreach (str_split($data, $blockSize) as $block) { | ||
$P .= $ctr->encrypt($block, null); | ||
} | ||
|
||
return $P; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace fpoirotte\Cryptal\Tests\API\Modes; | ||
|
||
use fpoirotte\Cryptal\Registry; | ||
use fpoirotte\Cryptal\CipherEnum; | ||
use fpoirotte\Cryptal\ModeEnum; | ||
use fpoirotte\Cryptal\ImplementationTypeEnum; | ||
use fpoirotte\Cryptal\Tests\AesBasedTestCase; | ||
|
||
class EAXTest extends AesBasedTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$registry = Registry::getInstance(); | ||
$registry->addCipher( | ||
'', | ||
'\\fpoirotte\\Cryptal\\Tests\\AesEcbStub', | ||
CipherEnum::CIPHER_AES_128(), | ||
ModeEnum::MODE_ECB(), | ||
ImplementationTypeEnum::TYPE_USERLAND() | ||
); | ||
} | ||
|
||
public function vectors() | ||
{ | ||
// P, K, N, A, C, T | ||
return array( | ||
// Test vectors from https://cseweb.ucsd.edu/~mihir/papers/eax.html | ||
array( | ||
'', | ||
'233952DEE4D5ED5F9B9C6D6FF80FF478', | ||
'62EC67F9C3A4A407FCB2A8C49031A8B3', | ||
'6BFB914FD07EAE6B', | ||
'', | ||
'E037830E8389F27B025A2D6527E79D01', | ||
), | ||
array( | ||
'F7FB', | ||
'91945D3F4DCBEE0BF45EF52255F095A4', | ||
'BECAF043B0A23D843194BA972C66DEBD', | ||
'FA3BFD4806EB53FA', | ||
'19DD', | ||
'5C4C9331049D0BDAB0277408F67967E5', | ||
), | ||
array( | ||
'1A47CB4933', | ||
'01F74AD64077F2E704C0F60ADA3DD523', | ||
'70C3DB4F0D26368400A10ED05D2BFF5E', | ||
'234A3463C1264AC6', | ||
'D851D5BAE0', | ||
'3A59F238A23E39199DC9266626C40F80', | ||
), | ||
array( | ||
'481C9E39B1', | ||
'D07CF6CBB7F313BDDE66B727AFD3C5E8', | ||
'8408DFFF3C1A2B1292DC199E46B7D617', | ||
'33CCE2EABFF5A79D', | ||
'632A9D131A', | ||
'D4C168A4225D8E1FF755939974A7BEDE', | ||
), | ||
array( | ||
'40D0C07DA5E4', | ||
'35B6D0580005BBC12B0587124557D2C2', | ||
'FDB6B06676EEDC5C61D74276E1F8E816', | ||
'AEB96EAEBE2970E9', | ||
'071DFE16C675', | ||
'CB0677E536F73AFE6A14B74EE49844DD', | ||
), | ||
array( | ||
'4DE3B35C3FC039245BD1FB7D', | ||
'BD8E6E11475E60B268784C38C62FEB22', | ||
'6EAC5C93072D8E8513F750935E46DA1B', | ||
'D4482D1CA78DCE0F', | ||
'835BB4F15D743E350E728414', | ||
'ABB8644FD6CCB86947C5E10590210A4F', | ||
), | ||
array( | ||
'8B0A79306C9CE7ED99DAE4F87F8DD61636', | ||
'7C77D6E813BED5AC98BAA417477A2E7D', | ||
'1A8C98DCD73D38393B2BF1569DEEFC19', | ||
'65D2017990D62528', | ||
'02083E3979DA014812F59F11D52630DA30', | ||
'137327D10649B0AA6E1C181DB617D7F2', | ||
), | ||
array( | ||
'1BDA122BCE8A8DBAF1877D962B8592DD2D56', | ||
'5FFF20CAFAB119CA2FC73549E20F5B0D', | ||
'DDE59B97D722156D4D9AFF2BC7559826', | ||
'54B9F04E6A09189A', | ||
'2EC47B2C4954A489AFC7BA4897EDCDAE8CC3', | ||
'3B60450599BD02C96382902AEF7F832A', | ||
), | ||
array( | ||
'6CF36720872B8513F6EAB1A8A44438D5EF11', | ||
'A4A4782BCFFD3EC5E7EF6D8C34A56123', | ||
'B781FCF2F75FA5A8DE97A9CA48E522EC', | ||
'899A175897561D7E', | ||
'0DE18FD0FDD91E7AF19F1D8EE8733938B1E8', | ||
'E7F6D2231618102FDB7FE55FF1991700', | ||
), | ||
array( | ||
'CA40D7446E545FFAED3BD12A740A659FFBBB3CEAB7', | ||
'8395FCF1E95BEBD697BD010BC766AAC3', | ||
'22E7ADD93CFC6393C57EC0B3C17D6B44', | ||
'126735FCC320D25A', | ||
'CB8920F87A6C75CFF39627B56E3ED197C552D295A7', | ||
'CFC46AFC253B4652B1AF3795B124AB6E', | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider vectors | ||
*/ | ||
public function testEAX_Mode($P, $K, $N, $A, $C, $T) | ||
{ | ||
$K = pack('H*', $K); | ||
$P = pack('H*', $P); | ||
$A = pack('H*', $A); | ||
$N = pack('H*', $N); | ||
$C = strtolower($C); | ||
$T = strtolower($T); | ||
|
||
$cipher = $this->getCipher($K); | ||
$eax = new \fpoirotte\Cryptal\Modes\EAX($cipher, $N, strlen($T) >> 1); | ||
$ctx = stream_context_create(array('cryptal' => array('data' => $A))); | ||
|
||
$res = $eax->encrypt($P, $ctx); | ||
$options = stream_context_get_options($ctx); | ||
$this->assertSame($C, bin2hex($res)); | ||
$this->assertSame($T, bin2hex($options['cryptal']['tag'])); | ||
|
||
$res = $eax->decrypt($res, $ctx); | ||
$this->assertSame(bin2hex($P), bin2hex($res)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
00000000000000000000000000000000 9bbfc198d72ce4df00e0e9350f9740d9 | ||
00000000000000000000000000000001 1e3eb37e6d39ef42eb1519e10a2803b1 | ||
00000000000000000000000000000002 118d6cdf58c857bb39127fa67f17309d | ||
dc0399e674531be50180358f4d923eb2 b07b497604d0cbd3b6b77c35ee770752 | ||
538b817ef0ac36f86896bd35347501db 31cb5c5ace73a85e9955b9e8e54d6721 | ||
b07b497604d0cbd3b6b77c35ee770752 c2161ef3d37d9169da5fdcbbae449dd4 | ||
a723bf06e4fbc4c73a91db72414a32f7 bbe9e714689d5a94b22be3bb2dfe6ff3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
00000000000000000000000000000000 7883efa66d408a805e403bef4d3e453c | ||
eb68571374653b871e72e4f5477367f7 a23f496c4ac129cb9859459f55717392 | ||
00000000000000000000000000000001 4030183eb0609cc339d61833b3815e99 | ||
c9c437e8d51c18a9c0d6f78e87784aee e1acf5b446598d9cc27fdc8d6b2b9e56 | ||
62ec67f9c3a4a407fcb2a8c49031a8b3 a704505db20f2519b805c2038c9fd3eb | ||
f107df4cda811500bc8077de9a7c8a7a a3a43fd68f11562c587cb47719bd70c5 | ||
a23f496c4ac129cb9859459f55717392 47fa1aa71979b061398d725182cfffe8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
00000000000000000000000000000000 3ed8949aee6a56d0ecfc63892bf71d49 | ||
00000000000000000000000000000001 a62b7f0d10f1cb2798ca8434ac1aadb1 | ||
00000000000000000000000000000002 d7c7f0f8c2b9b262cbacbcccb25ae490 | ||
bedf0dc94450272d54d3e6ed9de1cfcd 68e116f83d96c879856bcfe454e20c9e | ||
f3f043c81771e08dab3b0a1003c6d895 612774bc8c1fbfbfa3cf883d55b60358 | ||
68e116f83d96c879856bcfe454e20c9e 47cd3e6b63915f43961701e1e825ae73 | ||
2bb85c85bd656921785d32e81d8691b4 c2c015a1877e4d384cb0f097e5cc4b1b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
00000000000000000000000000000000 56e63c222251cf4483e74b54872f4b78 | ||
00000000000000000000000000000001 9536be179e43f5078500ffbb34ea81ad | ||
00000000000000000000000000000002 4672aa7b97609da28873abf36dbee91d | ||
26cfdff1b1d044a0c9b322d64e2445ae 0c261ef30156c0a8cbd01f84c52c413f | ||
9a17bed17d0dd08f0a9dd2e92857acca 5774463010718b8d1c01911c3d733816 | ||
0c261ef30156c0a8cbd01f84c52c413f 351e690787de29335e40c7debc685f73 | ||
0c261ef30156c0a8cbd01f84c52c4140 a195318dcfdd359845f4530717664303 | ||
68b6d157de34392b27b411bbfa5324b3 e31894cb7ed0c9853b1a6e548074dbef | ||
3443e443f797f497348743069cc9f688 60321dc6889a49ecb4531eb21720fa03 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
00000000000000000000000000000000 b0666bf8e81d0b2c9edf624fa0762b5e | ||
00000000000000000000000000000001 e60f9fa8efa1a1f128dd667db1382810 | ||
00000000000000000000000000000002 955f645584cc46693f2304ae110021e1 | ||
ca2624d5ef1a254c984a57867d74817c d025ae53e3d36ccd1fa5db108780d284 | ||
42443132df03a86bd3a0ef4330e08466 3d7da5cd52da1ba1368f4c515db143ed | ||
d025ae53e3d36ccd1fa5db108780d284 890247091546e6a58b2f7be9aaabe6cc | ||
d025ae53e3d36ccd1fa5db108780d285 06b6e02886326426b7ed741e36de7269 | ||
97575a6cfd1647212dd69bbfc426113b e90fcb52856f92a56b516847cbf137d0 | ||
181664b1251bbe17102ce1794a299ba6 fe2b2c4fb740c7c647368f5c6c26469b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
00000000000000000000000000000000 b636e700a8ddf75436ebfca7b005d10d | ||
f8bc84d8c59a7a6f9e42c55b117318d4 f88934e1510d7a7f9c1ffa5a0e83a927 | ||
00000000000000000000000000000001 845936d2297345e282fdf9566a236f9a | ||
4ee59f2c49244ae8d9520bc8aa342aa0 0310632bd431ab4b4bd0e86734009066 | ||
22e7add93cfc6393c57ec0b3c17d6b44 3761db40cc0122358e86386d71feab3d | ||
00000000000000000000000000000002 bbdc64f462580bd2ea586576f553c970 | ||
2a90a6f191b698b5e432757e90aca54f a68e89800e43b001583f386c30bc66d3 | ||
22e7add93cfc6393c57ec0b3c17d6b45 f53e3da4a1486eaa5833ff277bc5ade7 | ||
46fd68f0c00d761d89e58c31f0a707d2 14ef1cf58801c9da821dca1aa09aba7b | ||
c2b181b93df6148a59b23884608dff41 35d091b7d07379dc232233e06d04791d | ||
f88934e1510d7a7f9c1ffa5a0e83a927 01c9f7bc14382a351eadf69f1a34b408 | ||
f88934e1510d7a7f9c1ffa5a0e83a928 3ee9ee7f10ea04640b28249b1fc35fd1 | ||
7055440c18347e1d19ce42c39b6d18e7 ba2e33007f6470d347b9233896ab7fc3 | ||
a7a77d977b93ad839c16d1a656bc3af9 345d3d36a0079766666025a88ba7922f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
00000000000000000000000000000000 1178d366408dba28b7aeb17c4967e6e6 | ||
00000000000000000000000000000001 e51f1df33462fa3b280b06ce2a28ca9d | ||
00000000000000000000000000000002 54088215e7ac0ce6b89c8d10c0b7213d | ||
8d4385e97134f3fde9676913f7cef597 9fa5076fb1ade0c5bfe3e82b7e55ab1d | ||
5ac7ad2230bf416376b1c33f0fb75105 e2a488b0bb52aba00b310f4097fbe6b6 | ||
becaf043b0a23d843194ba972c66debd 96305ece11a3788f3bf2f1608821fdd0 | ||
70204f8ce59ae444662648e1e528baa5 5d1bc960cbaab2fea40e8ada44296f44 | ||
9fa5076fb1ade0c5bfe3e82b7e55ab1d ee2647f463425ce6bb4f151b323b13d3 | ||
08364f8ce59ae444662648e1e528baa5 214d1cee0e6240bf04f593631fd72a4e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
00000000000000000000000000000000 16652401f30a7e7626911d83fd352bd0 | ||
00000000000000000000000000000001 3d1f120c4e487fb5cd3e35de7bf72cd2 | ||
00000000000000000000000000000002 30bf1d1ab79d9d75bc40ea849b74697c | ||
8d2e90f0e2412732b5248f4e4fba5e9c f54149b84dc30505508da47b53e03648 | ||
ed11955315379b13d77a43d18f238392 b2b1d89fb2c89e2be27059f2b2b10ea3 | ||
f54149b84dc30505508da47b53e03648 6112e8f07af29b690775ac264c3701ed | ||
f54149b84dc30505508da47b53e03649 5ef94d110166ed14ec01136e3389da58 | ||
3d5e92ca4a44830f4ddff70a73075044 c7c89580c3f287b6dd55096535c82dbb | ||
2fb485870fdb7e6e47117f6ac11c82fb a0064304e9138b01698218d610c82feb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
00000000000000000000000000000000 6a9e6ad1259b629ea67b501aef2d9b08 | ||
00000000000000000000000000000001 fec0feebd8010bc2973165250620cf50 | ||
00000000000000000000000000000002 7495a452cbe33e14cca9f1c7fe8ce9f2 | ||
d10ee3e069802926f97aa0bc6f307703 2e8747f9efa6dfbc5fcdf2ef8e261fb9 | ||
80f178b3e9e14fb78edc254eba96a3f7 78f15dae93255a507f643560453e8ea1 | ||
2e8747f9efa6dfbc5fcdf2ef8e261fb9 ceb807ad62b4071155a37f69be5de8db | ||
5db7bbe700fa8a5b5b3635b8c23a8555 fdce7e18aa4f3d85676c268a5b399b57 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
00000000000000000000000000000000 74fd12c97766f85ef71a40c6d70a00ab | ||
00000000000000000000000000000001 615f8d20337050100fe4ec1622a2bb2f | ||
00000000000000000000000000000002 9192f3113fa97f9e22fdf189e1b9050a | ||
190fe8a4a5b123f18bf2d8d53fa9d7ea d318ff3cb69d5cd30c9522883bbed4d4 | ||
816724ef511e16f6538def0d7e8ab904 ec13202b930009a7992ffa4d64c5583f | ||
d318ff3cb69d5cd30c9522883bbed4d4 2b36032aab7f8f97c25e1c6f3c95e773 | ||
214c2527f8b29ee5fe94f292bd910721 ebcab7b307c0db6b62ef4b5c2bdc3235 |