X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Ftest_wrap.py;h=92660f8a5bafb7b7f8a437da791842c9b7c17414;hb=9af4461c6af50f9cf83030867e7054d1f6311b32;hp=451125bc1a8f6b7b566d7e9f1a65e8e48b49918b;hpb=dacca17a69ca9a920bf69bc1dc47386bdd6d4def;p=pygost.git diff --git a/pygost/test_wrap.py b/pygost/test_wrap.py index 451125b..92660f8 100644 --- a/pygost/test_wrap.py +++ b/pygost/test_wrap.py @@ -1,11 +1,10 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2017 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 -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +# the Free Software Foundation, version 3 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,6 +17,11 @@ from os import urandom from unittest import TestCase +from pygost.gost3412 import GOST3412Kuznechik +from pygost.gost3412 import GOST3412Magma +from pygost.utils import hexdec +from pygost.wrap import kexp15 +from pygost.wrap import kimp15 from pygost.wrap import unwrap_cryptopro from pygost.wrap import unwrap_gost from pygost.wrap import wrap_cryptopro @@ -32,7 +36,7 @@ class WrapGostTest(TestCase): ukm = urandom(8) wrapped = wrap_gost(ukm, kek, cek) unwrapped = unwrap_gost(kek, wrapped) - self.assertEqual(unwrapped, cek) + self.assertSequenceEqual(unwrapped, cek) def test_invalid_length(self): with self.assertRaises(ValueError): @@ -49,4 +53,56 @@ class WrapCryptoproTest(TestCase): ukm = urandom(8) wrapped = wrap_cryptopro(ukm, kek, cek) unwrapped = unwrap_cryptopro(kek, wrapped) - self.assertEqual(unwrapped, cek) + self.assertSequenceEqual(unwrapped, cek) + + +class TestVectorKExp15(TestCase): + """Test vectors from Р 1323565.1.017-2018 + """ + key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF") + key_enc = hexdec("202122232425262728292A2B2C2D2E2F38393A3B3C3D3E3F3031323334353637") + key_mac = hexdec("08090A0B0C0D0E0F0001020304050607101112131415161718191A1B1C1D1E1F") + + def test_magma(self): + iv = hexdec("67BED654") + kexp = kexp15( + GOST3412Magma(self.key_enc).encrypt, + GOST3412Magma(self.key_mac).encrypt, + GOST3412Magma.blocksize, + self.key, + iv, + ) + self.assertSequenceEqual(kexp, hexdec(""" +CF D5 A1 2D 5B 81 B6 E1 E9 9C 91 6D 07 90 0C 6A +C1 27 03 FB 3A BD ED 55 56 7B F3 74 2C 89 9C 75 +5D AF E7 B4 2E 3A 8B D9 + """.replace("\n", "").replace(" ", ""))) + self.assertSequenceEqual(kimp15( + GOST3412Magma(self.key_enc).encrypt, + GOST3412Magma(self.key_mac).encrypt, + GOST3412Magma.blocksize, + kexp, + iv, + ), self.key) + + def test_kuznechik(self): + iv = hexdec("0909472DD9F26BE8") + kexp = kexp15( + GOST3412Kuznechik(self.key_enc).encrypt, + GOST3412Kuznechik(self.key_mac).encrypt, + GOST3412Kuznechik.blocksize, + self.key, + iv, + ) + self.assertSequenceEqual(kexp, hexdec(""" +E3 61 84 E8 4E 8D 73 6F F3 6C C2 E5 AE 06 5D C6 +56 B2 3C 20 F5 49 B0 2F DF F8 8E 1F 3F 30 D8 C2 +9A 53 F3 CA 55 4D BA D8 0D E1 52 B9 A4 62 5B 32 + """.replace("\n", "").replace(" ", ""))) + self.assertSequenceEqual(kimp15( + GOST3412Kuznechik(self.key_enc).encrypt, + GOST3412Kuznechik(self.key_mac).encrypt, + GOST3412Kuznechik.blocksize, + kexp, + iv, + ), self.key)