]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_wrap.py
Raise copyright years
[pygost.git] / pygost / test_wrap.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2021 Sergey Matveev <stargrave@stargrave.org>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from os import urandom
18 from unittest import TestCase
19
20 from pygost.gost3412 import GOST3412Kuznechik
21 from pygost.gost3412 import GOST3412Magma
22 from pygost.utils import hexdec
23 from pygost.wrap import kexp15
24 from pygost.wrap import kimp15
25 from pygost.wrap import unwrap_cryptopro
26 from pygost.wrap import unwrap_gost
27 from pygost.wrap import wrap_cryptopro
28 from pygost.wrap import wrap_gost
29
30
31 class WrapGostTest(TestCase):
32     def test_symmetric(self):
33         for _ in range(1 << 8):
34             kek = urandom(32)
35             cek = urandom(32)
36             ukm = urandom(8)
37             wrapped = wrap_gost(ukm, kek, cek)
38             unwrapped = unwrap_gost(kek, wrapped)
39             self.assertSequenceEqual(unwrapped, cek)
40
41     def test_invalid_length(self):
42         with self.assertRaises(ValueError):
43             unwrap_gost(urandom(32), urandom(41))
44         with self.assertRaises(ValueError):
45             unwrap_gost(urandom(32), urandom(45))
46
47
48 class WrapCryptoproTest(TestCase):
49     def test_symmetric(self):
50         for _ in range(1 << 8):
51             kek = urandom(32)
52             cek = urandom(32)
53             ukm = urandom(8)
54             wrapped = wrap_cryptopro(ukm, kek, cek)
55             unwrapped = unwrap_cryptopro(kek, wrapped)
56             self.assertSequenceEqual(unwrapped, cek)
57
58
59 class TestVectorKExp15(TestCase):
60     """Test vectors from Р 1323565.1.017-2018
61     """
62     key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF")
63     key_enc = hexdec("202122232425262728292A2B2C2D2E2F38393A3B3C3D3E3F3031323334353637")
64     key_mac = hexdec("08090A0B0C0D0E0F0001020304050607101112131415161718191A1B1C1D1E1F")
65
66     def test_magma(self):
67         iv = hexdec("67BED654")
68         kexp = kexp15(
69             GOST3412Magma(self.key_enc).encrypt,
70             GOST3412Magma(self.key_mac).encrypt,
71             GOST3412Magma.blocksize,
72             self.key,
73             iv,
74         )
75         self.assertSequenceEqual(kexp, hexdec("""
76 CF D5 A1 2D 5B 81 B6 E1 E9 9C 91 6D 07 90 0C 6A
77 C1 27 03 FB 3A BD ED 55 56 7B F3 74 2C 89 9C 75
78 5D AF E7 B4 2E 3A 8B D9
79         """.replace("\n", "").replace(" ", "")))
80         self.assertSequenceEqual(kimp15(
81             GOST3412Magma(self.key_enc).encrypt,
82             GOST3412Magma(self.key_mac).encrypt,
83             GOST3412Magma.blocksize,
84             kexp,
85             iv,
86         ), self.key)
87
88     def test_kuznechik(self):
89         iv = hexdec("0909472DD9F26BE8")
90         kexp = kexp15(
91             GOST3412Kuznechik(self.key_enc).encrypt,
92             GOST3412Kuznechik(self.key_mac).encrypt,
93             GOST3412Kuznechik.blocksize,
94             self.key,
95             iv,
96         )
97         self.assertSequenceEqual(kexp, hexdec("""
98 E3 61 84 E8 4E 8D 73 6F F3 6C C2 E5 AE 06 5D C6
99 56 B2 3C 20 F5 49 B0 2F DF F8 8E 1F 3F 30 D8 C2
100 9A 53 F3 CA 55 4D BA D8 0D E1 52 B9 A4 62 5B 32
101         """.replace("\n", "").replace(" ", "")))
102         self.assertSequenceEqual(kimp15(
103             GOST3412Kuznechik(self.key_enc).encrypt,
104             GOST3412Kuznechik(self.key_mac).encrypt,
105             GOST3412Kuznechik.blocksize,
106             kexp,
107             iv,
108         ), self.key)