X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Ftest_gost28147.py;h=5e9da8d10d4805b91fb409ea0d29e1faf4c24b4d;hb=31b08d5a78505f0ae1a144e58d023d84eda2cc6e;hp=b79b6e44d2581ea575fc0a0201361167b8c01858;hpb=0fcc8bc147ada51d2a9a912f18ac362d54b7d49a;p=pygost.git diff --git a/pygost/test_gost28147.py b/pygost/test_gost28147.py index b79b6e4..5e9da8d 100644 --- a/pygost/test_gost28147.py +++ b/pygost/test_gost28147.py @@ -1,11 +1,10 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2019 Sergey Matveev +# Copyright (C) 2015-2020 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 @@ -19,6 +18,7 @@ from os import urandom from unittest import TestCase from pygost.gost28147 import block2ns +from pygost.gost28147 import BLOCKSIZE from pygost.gost28147 import cbc_decrypt from pygost.gost28147 import cbc_encrypt from pygost.gost28147 import cfb_decrypt @@ -28,6 +28,7 @@ from pygost.gost28147 import DEFAULT_SBOX from pygost.gost28147 import ecb_decrypt from pygost.gost28147 import ecb_encrypt from pygost.gost28147 import encrypt +from pygost.gost28147 import KEYSIZE from pygost.gost28147 import MESH_MAX_DATA from pygost.gost28147 import ns2block from pygost.utils import hexdec @@ -36,7 +37,7 @@ from pygost.utils import strxor class ECBTest(TestCase): def test_gcl(self): - """ Test vectors from libgcl3 + """Test vectors from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd") @@ -114,7 +115,7 @@ class ECBTest(TestCase): self.assertSequenceEqual(decrypted, plaintext) def test_cryptopp(self): - """ Test vectors from Crypto++ 5.6.2 + """Test vectors from Crypto++ 5.6.2 """ sbox = "AppliedCryptography" data = ( @@ -134,7 +135,7 @@ class ECBTest(TestCase): self.assertSequenceEqual(ecb_encrypt(key, pt, sbox=sbox), ct) def test_cryptomanager(self): - """ Test vector from http://cryptomanager.com/tv.html + """Test vector from http://cryptomanager.com/tv.html """ sbox = "id-GostR3411-94-TestParamSet" key = hexdec(b"75713134B60FEC45A607BB83AA3746AF4FF99DA6D1B53B5B1B402A1BAA030D1B") @@ -146,7 +147,7 @@ class ECBTest(TestCase): class CFBTest(TestCase): def test_cryptomanager(self): - """ Test vector from http://cryptomanager.com/tv.html + """Test vector from http://cryptomanager.com/tv.html """ key = hexdec(b"75713134B60FEC45A607BB83AA3746AF4FF99DA6D1B53B5B1B402A1BAA030D1B") sbox = "id-GostR3411-94-TestParamSet" @@ -170,10 +171,10 @@ class CFBTest(TestCase): ) def test_steps(self): - """ Check step-by-step operation manually + """Check step-by-step operation manually """ - key = urandom(32) - iv = urandom(8) + key = urandom(KEYSIZE) + iv = urandom(BLOCKSIZE) plaintext = urandom(20) ciphertext = cfb_encrypt(key, plaintext, iv) @@ -193,10 +194,10 @@ class CFBTest(TestCase): self.assertSequenceEqual(step[:4], ciphertext[16:]) def test_random(self): - """ Random data with various sizes + """Random data with various sizes """ - key = urandom(32) - iv = urandom(8) + key = urandom(KEYSIZE) + iv = urandom(BLOCKSIZE) for size in (5, 8, 16, 120): pt = urandom(size) self.assertSequenceEqual( @@ -207,7 +208,7 @@ class CFBTest(TestCase): class CTRTest(TestCase): def test_gcl(self): - """ Test vectors from libgcl3 + """Test vectors from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd") @@ -286,7 +287,7 @@ class CTRTest(TestCase): self.assertSequenceEqual(decrypted, plaintext) def test_gcl2(self): - """ Test vectors 2 from libgcl3 + """Test vectors 2 from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"fc7ad2886f455b50d29008fa622b57d5c65b3c637202025799cadf0768519e8a") @@ -308,7 +309,7 @@ class CTRTest(TestCase): 0x13, 0xcc, 0x55, 0x38, 0xb5, 0x63, 0x32, 0xc5, 0x23, 0xa4, 0xcb, 0x7d, 0x51, ))) - iv = 8 * b"\x00" + iv = BLOCKSIZE * b"\x00" encrypted = cnt(key, plaintext, iv=iv, sbox=sbox) self.assertSequenceEqual(encrypted, ciphertext) decrypted = cnt(key, encrypted, iv=iv, sbox=sbox) @@ -317,7 +318,7 @@ class CTRTest(TestCase): class CBCTest(TestCase): def test_pad_requirement(self): - key = 32 * b"x" + key = KEYSIZE * b"x" for s in (b"", b"foo", b"foobarbaz"): with self.assertRaises(ValueError): cbc_encrypt(key, s, pad=False) @@ -325,23 +326,23 @@ class CBCTest(TestCase): cbc_decrypt(key, s, pad=False) def test_passes(self): - iv = urandom(8) - key = 32 * b"x" + iv = urandom(BLOCKSIZE) + key = KEYSIZE * b"x" for pt in (b"foo", b"foobarba", b"foobarbaz", 16 * b"x"): ct = cbc_encrypt(key, pt, iv) dt = cbc_decrypt(key, ct) self.assertSequenceEqual(pt, dt) def test_iv_existence_check(self): - key = 32 * b"x" + key = KEYSIZE * b"x" with self.assertRaises(ValueError): - cbc_decrypt(key, 8 * b"x") - iv = urandom(8) - cbc_decrypt(key, cbc_encrypt(key, 8 * b"x", iv)) + cbc_decrypt(key, BLOCKSIZE * b"x") + iv = urandom(BLOCKSIZE) + cbc_decrypt(key, cbc_encrypt(key, BLOCKSIZE * b"x", iv)) def test_meshing(self): pt = urandom(MESH_MAX_DATA * 3) - key = urandom(32) + key = urandom(KEYSIZE) ct = cbc_encrypt(key, pt) dt = cbc_decrypt(key, ct) self.assertSequenceEqual(pt, dt) @@ -349,8 +350,8 @@ class CBCTest(TestCase): class CFBMeshingTest(TestCase): def setUp(self): - self.key = urandom(32) - self.iv = urandom(8) + self.key = urandom(KEYSIZE) + self.iv = urandom(BLOCKSIZE) def test_single(self): pt = b"\x00"