]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_wrap.py
9576ba9db5b630e1405244b900834d64d4b8bde3
[pygost.git] / pygost / test_wrap.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2020 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.wrap import unwrap_cryptopro
21 from pygost.wrap import unwrap_gost
22 from pygost.wrap import wrap_cryptopro
23 from pygost.wrap import wrap_gost
24
25
26 class WrapGostTest(TestCase):
27     def test_symmetric(self):
28         for _ in range(1 << 8):
29             kek = urandom(32)
30             cek = urandom(32)
31             ukm = urandom(8)
32             wrapped = wrap_gost(ukm, kek, cek)
33             unwrapped = unwrap_gost(kek, wrapped)
34             self.assertSequenceEqual(unwrapped, cek)
35
36     def test_invalid_length(self):
37         with self.assertRaises(ValueError):
38             unwrap_gost(urandom(32), urandom(41))
39         with self.assertRaises(ValueError):
40             unwrap_gost(urandom(32), urandom(45))
41
42
43 class WrapCryptoproTest(TestCase):
44     def test_symmetric(self):
45         for _ in range(1 << 8):
46             kek = urandom(32)
47             cek = urandom(32)
48             ukm = urandom(8)
49             wrapped = wrap_cryptopro(ukm, kek, cek)
50             unwrapped = unwrap_cryptopro(kek, wrapped)
51             self.assertSequenceEqual(unwrapped, cek)