From ade04d6c0456bb39793e8b3547ac99aeffe8af73 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 25 Nov 2021 21:11:25 +0300 Subject: [PATCH] Fix MGM tag_size usage --- news.texi | 4 ++++ pygost/__init__.py | 2 +- pygost/mgm.py | 2 +- pygost/test_mgm.py | 33 ++++++++++++++++++++++----------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/news.texi b/news.texi index 7e21260..f5752e6 100644 --- a/news.texi +++ b/news.texi @@ -3,6 +3,10 @@ @table @strong +@anchor{Release 5.7} +@item 5.7 +Fixed MGM ignoring of the set tag size. + @anchor{Release 5.6} @item 5.6 Fixed lint errors for previous release. diff --git a/pygost/__init__.py b/pygost/__init__.py index 6fc85a3..12d2bad 100644 --- a/pygost/__init__.py +++ b/pygost/__init__.py @@ -3,4 +3,4 @@ PyGOST is free software: see the file COPYING for copying conditions. """ -__version__ = "5.6" +__version__ = "5.7" diff --git a/pygost/mgm.py b/pygost/mgm.py index 49c478d..aabce95 100644 --- a/pygost/mgm.py +++ b/pygost/mgm.py @@ -58,7 +58,7 @@ class MGM(object): """ if bs not in (8, 16): raise ValueError("Only 64/128-bit blocksizes allowed") - self.tag_size = bs if tag_size is None else bs + self.tag_size = bs if tag_size is None else tag_size if self.tag_size < 4 or self.tag_size > bs: raise ValueError("Invalid tag_size") self.encrypter = encrypter diff --git a/pygost/test_mgm.py b/pygost/test_mgm.py index a2c5e5e..c84be2b 100644 --- a/pygost/test_mgm.py +++ b/pygost/test_mgm.py @@ -39,7 +39,7 @@ class TestVector(TestCase): class TestSymmetric(TestCase): - def _itself(self, mgm, bs): + def _itself(self, mgm, bs, tag_size): for _ in range(1000): nonce = nonce_prepare(urandom(bs)) ad = urandom(randint(0, 20)) @@ -47,18 +47,29 @@ class TestSymmetric(TestCase): if len(ad) + len(pt) == 0: continue ct = mgm.seal(nonce, pt, ad) + self.assertEqual(len(ct) - tag_size, len(pt)) self.assertSequenceEqual(mgm.open(nonce, ct, ad), pt) def test_magma(self): - mgm = MGM( - GOST3412Magma(urandom(KEYSIZE)).encrypt, - GOST3412Magma.blocksize, - ) - self._itself(mgm, GOST3412Magma.blocksize) + for tag_size in ( + GOST3412Magma.blocksize, + GOST3412Magma.blocksize - 2, + ): + mgm = MGM( + GOST3412Magma(urandom(KEYSIZE)).encrypt, + GOST3412Magma.blocksize, + tag_size, + ) + self._itself(mgm, GOST3412Magma.blocksize, tag_size) def test_kuznechik(self): - mgm = MGM( - GOST3412Kuznechik(urandom(KEYSIZE)).encrypt, - GOST3412Kuznechik.blocksize, - ) - self._itself(mgm, GOST3412Kuznechik.blocksize) + for tag_size in ( + GOST3412Kuznechik.blocksize, + GOST3412Kuznechik.blocksize - 2, + ): + mgm = MGM( + GOST3412Kuznechik(urandom(KEYSIZE)).encrypt, + GOST3412Kuznechik.blocksize, + tag_size, + ) + self._itself(mgm, GOST3412Kuznechik.blocksize, tag_size) -- 2.44.0