X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Ftest_mgm.py;h=c84be2b682e50cef5c775eb9514be714d1ab2dc1;hb=ade04d6c0456bb39793e8b3547ac99aeffe8af73;hp=56cfda0a30cf9421b2af0ddc5b7ce32678e13322;hpb=a63f78066756e6959b7bcbc52738f2ff4a03a308;p=pygost.git diff --git a/pygost/test_mgm.py b/pygost/test_mgm.py index 56cfda0..c84be2b 100644 --- a/pygost/test_mgm.py +++ b/pygost/test_mgm.py @@ -1,6 +1,6 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2020 Sergey Matveev +# Copyright (C) 2015-2021 Sergey Matveev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ from unittest import TestCase from pygost.gost3412 import GOST3412Kuznechik from pygost.gost3412 import GOST3412Magma +from pygost.gost3412 import KEYSIZE from pygost.mgm import MGM from pygost.mgm import nonce_prepare from pygost.utils import hexdec @@ -30,7 +31,7 @@ class TestVector(TestCase): key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF") ad = hexdec("0202020202020202010101010101010104040404040404040303030303030303EA0505050505050505") plaintext = hexdec("1122334455667700FFEEDDCCBBAA998800112233445566778899AABBCCEEFF0A112233445566778899AABBCCEEFF0A002233445566778899AABBCCEEFF0A0011AABBCC") - mgm = MGM(GOST3412Kuznechik(key).encrypt, 16) + mgm = MGM(GOST3412Kuznechik(key).encrypt, GOST3412Kuznechik.blocksize) ciphertext = mgm.seal(plaintext[:16], plaintext, ad) self.assertSequenceEqual(ciphertext[:len(plaintext)], hexdec("A9757B8147956E9055B8A33DE89F42FC8075D2212BF9FD5BD3F7069AADC16B39497AB15915A6BA85936B5D0EA9F6851CC60C14D4D3F883D0AB94420695C76DEB2C7552")) self.assertSequenceEqual(ciphertext[len(plaintext):], hexdec("CF5D656F40C34F5C46E8BB0E29FCDB4C")) @@ -38,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)) @@ -46,12 +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(32)).encrypt, 8) - self._itself(mgm, 8) + 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(32)).encrypt, 16) - self._itself(mgm, 16) + 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)