]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_mgm.py
903415826c7251cc932b4c1bea42c4eb6fbd0919
[pygost.git] / pygost / test_mgm.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 random import randint
19 from unittest import TestCase
20
21 from pygost.gost3412 import GOST3412Kuznechik
22 from pygost.gost3412 import GOST3412Magma
23 from pygost.gost3412 import KEYSIZE
24 from pygost.mgm import MGM
25 from pygost.mgm import nonce_prepare
26 from pygost.utils import hexdec
27
28
29 class TestVector(TestCase):
30     def runTest(self):
31         key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF")
32         ad = hexdec("0202020202020202010101010101010104040404040404040303030303030303EA0505050505050505")
33         plaintext = hexdec("1122334455667700FFEEDDCCBBAA998800112233445566778899AABBCCEEFF0A112233445566778899AABBCCEEFF0A002233445566778899AABBCCEEFF0A0011AABBCC")
34         mgm = MGM(GOST3412Kuznechik(key).encrypt, GOST3412Kuznechik.blocksize)
35         ciphertext = mgm.seal(plaintext[:16], plaintext, ad)
36         self.assertSequenceEqual(ciphertext[:len(plaintext)], hexdec("A9757B8147956E9055B8A33DE89F42FC8075D2212BF9FD5BD3F7069AADC16B39497AB15915A6BA85936B5D0EA9F6851CC60C14D4D3F883D0AB94420695C76DEB2C7552"))
37         self.assertSequenceEqual(ciphertext[len(plaintext):], hexdec("CF5D656F40C34F5C46E8BB0E29FCDB4C"))
38         self.assertSequenceEqual(mgm.open(plaintext[:16], ciphertext, ad), plaintext)
39
40
41 class TestSymmetric(TestCase):
42     def _itself(self, mgm, bs):
43         for _ in range(1000):
44             nonce = nonce_prepare(urandom(bs))
45             ad = urandom(randint(0, 20))
46             pt = urandom(randint(0, 20))
47             if len(ad) + len(pt) == 0:
48                 continue
49             ct = mgm.seal(nonce, pt, ad)
50             self.assertSequenceEqual(mgm.open(nonce, ct, ad), pt)
51
52     def test_magma(self):
53         mgm = MGM(
54             GOST3412Magma(urandom(KEYSIZE)).encrypt,
55             GOST3412Magma.blocksize,
56         )
57         self._itself(mgm, GOST3412Magma.blocksize)
58
59     def test_kuznechik(self):
60         mgm = MGM(
61             GOST3412Kuznechik(urandom(KEYSIZE)).encrypt,
62             GOST3412Kuznechik.blocksize,
63         )
64         self._itself(mgm, GOST3412Kuznechik.blocksize)