summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Matveev <stargrave@stargrave.org>2020-07-29 22:00:36 +0300
committerSergey Matveev <stargrave@stargrave.org>2020-07-30 18:31:43 +0300
commit7e5fe8586c0f17834a8df558b7ca86c5d7accd3e (patch)
tree39c3fcb651cff8f0dacd5771384aa36dd8d787bc
parentc9abb42693dfe8288aecb7ef064b907be0561629 (diff)
downloadpygost-7e5fe8586c0f17834a8df558b7ca86c5d7accd3e.tar.xz
Use more constants
-rw-r--r--pygost/gost3412.py2
-rw-r--r--pygost/gost3413.py12
-rw-r--r--pygost/test_gost28147.py32
-rw-r--r--pygost/test_gost3413.py377
-rw-r--r--pygost/test_mgm.py17
5 files changed, 309 insertions, 131 deletions
diff --git a/pygost/gost3412.py b/pygost/gost3412.py
index 296e4fb..0c1d2ce 100644
--- a/pygost/gost3412.py
+++ b/pygost/gost3412.py
@@ -26,7 +26,7 @@ from pygost.utils import strxor
from pygost.utils import xrange
-KEY_SIZE = 32
+KEYSIZE = 32
LC = bytearray((
148, 32, 133, 16, 194, 192, 1, 251, 1, 192, 194, 16, 133, 32, 148, 1,
diff --git a/pygost/gost3413.py b/pygost/gost3413.py
index 495a3f9..5aeaec6 100644
--- a/pygost/gost3413.py
+++ b/pygost/gost3413.py
@@ -24,7 +24,7 @@ from pygost.utils import strxor
from pygost.utils import xrange
-KEY_SIZE = 32
+KEYSIZE = 32
def pad_size(data_size, blocksize):
@@ -112,7 +112,7 @@ def acpkm(encrypter, bs):
"""
return b"".join([
encrypter(bytes(bytearray(range(d, d + bs))))
- for d in range(0x80, 0x80 + bs * (KEY_SIZE // bs), bs)
+ for d in range(0x80, 0x80 + bs * (KEYSIZE // bs), bs)
])
@@ -346,17 +346,17 @@ def mac_acpkm_master(algo_class, encrypter, key_section_size, section_size, bs,
encrypter,
key_section_size,
bs,
- (KEY_SIZE + bs) * sections,
+ (KEYSIZE + bs) * sections,
)
for i in xrange(0, tail_offset, bs):
if i % section_size == 0:
- keymat, keymats = keymats[:KEY_SIZE + bs], keymats[KEY_SIZE + bs:]
- key, k1 = keymat[:KEY_SIZE], keymat[KEY_SIZE:]
+ keymat, keymats = keymats[:KEYSIZE + bs], keymats[KEYSIZE + bs:]
+ key, k1 = keymat[:KEYSIZE], keymat[KEYSIZE:]
encrypter = algo_class(key).encrypt
prev = encrypter(strxor(data[i:i + bs], prev))
tail = data[tail_offset:]
if len(tail) == bs:
- key, k1 = keymats[:KEY_SIZE], keymats[KEY_SIZE:]
+ key, k1 = keymats[:KEYSIZE], keymats[KEYSIZE:]
encrypter = algo_class(key).encrypt
k2 = long2bytes(bytes2long(k1) << 1, size=bs)
if bytearray(k1)[0] & 0x80 != 0:
diff --git a/pygost/test_gost28147.py b/pygost/test_gost28147.py
index 5e3191e..5ccb687 100644
--- a/pygost/test_gost28147.py
+++ b/pygost/test_gost28147.py
@@ -18,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
@@ -27,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
@@ -171,8 +173,8 @@ class CFBTest(TestCase):
def test_steps(self):
""" 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)
@@ -194,8 +196,8 @@ class CFBTest(TestCase):
def test_random(self):
""" 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(
@@ -307,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)
@@ -316,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)
@@ -324,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)
@@ -348,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"
diff --git a/pygost/test_gost3413.py b/pygost/test_gost3413.py
index 6bf2f88..f670d7e 100644
--- a/pygost/test_gost3413.py
+++ b/pygost/test_gost3413.py
@@ -31,7 +31,7 @@ from pygost.gost3413 import ctr
from pygost.gost3413 import ctr_acpkm
from pygost.gost3413 import ecb_decrypt
from pygost.gost3413 import ecb_encrypt
-from pygost.gost3413 import KEY_SIZE
+from pygost.gost3413 import KEYSIZE
from pygost.gost3413 import mac
from pygost.gost3413 import mac_acpkm_master
from pygost.gost3413 import ofb
@@ -45,7 +45,7 @@ from pygost.utils import strxor
class Pad2Test(TestCase):
def test_symmetric(self):
for _ in range(100):
- for blocksize in (8, 16):
+ for blocksize in (GOST3412Magma.blocksize, GOST3412Kuznechik.blocksize):
data = urandom(randint(0, blocksize * 3))
self.assertSequenceEqual(
unpad2(pad2(data, blocksize), blocksize),
@@ -70,20 +70,32 @@ class GOST3412KuznechikModesTest(TestCase):
ciphtext += "f0ca33549d247ceef3f5a5313bd4b157"
ciphtext += "d0b09ccde830b9eb3a02c4c5aa8ada98"
self.assertSequenceEqual(
- hexenc(ecb_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext))),
+ hexenc(ecb_encrypt(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ecb_decrypt(self.ciph.decrypt, 16, hexdec(ciphtext))),
+ hexenc(ecb_decrypt(
+ self.ciph.decrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(ciphtext),
+ )),
self.plaintext,
)
def test_ecb_symmetric(self):
for _ in range(100):
- pt = pad2(urandom(randint(0, 16 * 2)), 16)
- ciph = GOST3412Kuznechik(urandom(32))
- ct = ecb_encrypt(ciph.encrypt, 16, pt)
- self.assertSequenceEqual(ecb_decrypt(ciph.decrypt, 16, ct), pt)
+ pt = pad2(urandom(randint(0, 16 * 2)), GOST3412Kuznechik.blocksize)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ ct = ecb_encrypt(ciph.encrypt, GOST3412Kuznechik.blocksize, pt)
+ self.assertSequenceEqual(ecb_decrypt(
+ ciph.decrypt,
+ GOST3412Kuznechik.blocksize,
+ ct,
+ ), pt)
def test_ctr_vectors(self):
ciphtext = ""
@@ -91,23 +103,38 @@ class GOST3412KuznechikModesTest(TestCase):
ciphtext += "85eee733f6a13e5df33ce4b33c45dee4"
ciphtext += "a5eae88be6356ed3d5e877f13564a3a5"
ciphtext += "cb91fab1f20cbab6d1c6d15820bdba73"
- iv = self.iv[:8]
- self.assertSequenceEqual(
- hexenc(ctr(self.ciph.encrypt, 16, hexdec(self.plaintext), iv)),
+ iv = self.iv[:GOST3412Kuznechik.blocksize // 2]
+ self.assertSequenceEqual(
+ hexenc(ctr(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ctr(self.ciph.encrypt, 16, hexdec(ciphtext), iv)),
+ hexenc(ctr(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(ciphtext),
+ iv,
+ )),
self.plaintext,
)
def test_ctr_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(8)
- ciph = GOST3412Kuznechik(urandom(32))
- ct = ctr(ciph.encrypt, 16, pt, iv)
- self.assertSequenceEqual(ctr(ciph.encrypt, 16, ct, iv), pt)
+ iv = urandom(GOST3412Kuznechik.blocksize // 2)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ ct = ctr(ciph.encrypt, GOST3412Kuznechik.blocksize, pt, iv)
+ self.assertSequenceEqual(ctr(
+ ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_ofb_vectors(self):
ciphtext = ""
@@ -116,32 +143,50 @@ class GOST3412KuznechikModesTest(TestCase):
ciphtext += "66a257ac3ca0b8b1c80fe7fc10288a13"
ciphtext += "203ebbc066138660a0292243f6903150"
self.assertSequenceEqual(
- hexenc(ofb(self.ciph.encrypt, 16, hexdec(self.plaintext), self.iv)),
+ hexenc(ofb(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ self.iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ofb(self.ciph.encrypt, 16, hexdec(ciphtext), self.iv)),
+ hexenc(ofb(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(ciphtext),
+ self.iv,
+ )),
self.plaintext,
)
def test_ofb_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(16 * 2)
- ciph = GOST3412Kuznechik(urandom(32))
- ct = ofb(ciph.encrypt, 16, pt, iv)
- self.assertSequenceEqual(ofb(ciph.encrypt, 16, ct, iv), pt)
+ iv = urandom(GOST3412Kuznechik.blocksize * 2)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ ct = ofb(ciph.encrypt, GOST3412Kuznechik.blocksize, pt, iv)
+ self.assertSequenceEqual(ofb(
+ ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_ofb_manual(self):
- iv = [urandom(16) for _ in range(randint(2, 10))]
- pt = [urandom(16) for _ in range(len(iv), len(iv) + randint(1, 10))]
- ciph = GOST3412Kuznechik(urandom(32))
+ iv = [urandom(GOST3412Kuznechik.blocksize) for _ in range(randint(2, 10))]
+ pt = [
+ urandom(GOST3412Kuznechik.blocksize)
+ for _ in range(len(iv), len(iv) + randint(1, 10))
+ ]
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
r = [ciph.encrypt(i) for i in iv]
for i in range(len(pt) - len(iv)):
r.append(ciph.encrypt(r[i]))
ct = [strxor(g, r) for g, r in zip(pt, r)]
self.assertSequenceEqual(
- ofb(ciph.encrypt, 16, b"".join(pt), b"".join(iv)),
+ ofb(ciph.encrypt, GOST3412Kuznechik.blocksize, b"".join(pt), b"".join(iv)),
b"".join(ct),
)
@@ -152,21 +197,36 @@ class GOST3412KuznechikModesTest(TestCase):
ciphtext += "fe7babf1e91999e85640e8b0f49d90d0"
ciphtext += "167688065a895c631a2d9a1560b63970"
self.assertSequenceEqual(
- hexenc(cbc_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext), self.iv)),
+ hexenc(cbc_encrypt(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ self.iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(cbc_decrypt(self.ciph.decrypt, 16, hexdec(ciphtext), self.iv)),
+ hexenc(cbc_decrypt(
+ self.ciph.decrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(ciphtext),
+ self.iv,
+ )),
self.plaintext,
)
def test_cbc_symmetric(self):
for _ in range(100):
- pt = pad2(urandom(randint(0, 16 * 2)), 16)
- iv = urandom(16 * 2)
- ciph = GOST3412Kuznechik(urandom(32))
- ct = cbc_encrypt(ciph.encrypt, 16, pt, iv)
- self.assertSequenceEqual(cbc_decrypt(ciph.decrypt, 16, ct, iv), pt)
+ pt = pad2(urandom(randint(0, 16 * 2)), GOST3412Kuznechik.blocksize)
+ iv = urandom(GOST3412Kuznechik.blocksize * 2)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ ct = cbc_encrypt(ciph.encrypt, GOST3412Kuznechik.blocksize, pt, iv)
+ self.assertSequenceEqual(cbc_decrypt(
+ ciph.decrypt,
+ GOST3412Kuznechik.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_cfb_vectors(self):
ciphtext = ""
@@ -175,36 +235,55 @@ class GOST3412KuznechikModesTest(TestCase):
ciphtext += "79f2a8eb5cc68d38842d264e97a238b5"
ciphtext += "4ffebecd4e922de6c75bd9dd44fbf4d1"
self.assertSequenceEqual(
- hexenc(cfb_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext), self.iv)),
+ hexenc(cfb_encrypt(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ self.iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(cfb_decrypt(self.ciph.encrypt, 16, hexdec(ciphtext), self.iv)),
+ hexenc(cfb_decrypt(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(ciphtext),
+ self.iv,
+ )),
self.plaintext,
)
def test_cfb_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(16 * 2)
- ciph = GOST3412Kuznechik(urandom(32))
- ct = cfb_encrypt(ciph.encrypt, 16, pt, iv)
- self.assertSequenceEqual(cfb_decrypt(ciph.encrypt, 16, ct, iv), pt)
+ iv = urandom(GOST3412Kuznechik.blocksize * 2)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ ct = cfb_encrypt(ciph.encrypt, GOST3412Kuznechik.blocksize, pt, iv)
+ self.assertSequenceEqual(cfb_decrypt(
+ ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_mac_vectors(self):
- k1, k2 = _mac_ks(self.ciph.encrypt, 16)
+ k1, k2 = _mac_ks(self.ciph.encrypt, GOST3412Kuznechik.blocksize)
self.assertSequenceEqual(hexenc(k1), "297d82bc4d39e3ca0de0573298151dc7")
self.assertSequenceEqual(hexenc(k2), "52fb05789a73c7941bc0ae65302a3b8e")
self.assertSequenceEqual(
- hexenc(mac(self.ciph.encrypt, 16, hexdec(self.plaintext))[:8]),
+ hexenc(mac(
+ self.ciph.encrypt,
+ GOST3412Kuznechik.blocksize,
+ hexdec(self.plaintext),
+ )[:8]),
"336f4d296059fbe3",
)
def test_mac_applies(self):
for _ in range(100):
data = urandom(randint(0, 16 * 2))
- ciph = GOST3412Kuznechik(urandom(32))
- mac(ciph.encrypt, 16, data)
+ ciph = GOST3412Kuznechik(urandom(KEYSIZE))
+ mac(ciph.encrypt, GOST3412Kuznechik.blocksize, data)
class GOST3412MagmaModesTest(TestCase):
@@ -224,20 +303,32 @@ class GOST3412MagmaModesTest(TestCase):
ciphtext += "11d8d9e9eacfbc1e"
ciphtext += "7c68260996c67efb"
self.assertSequenceEqual(
- hexenc(ecb_encrypt(self.ciph.encrypt, 8, hexdec(self.plaintext))),
+ hexenc(ecb_encrypt(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ecb_decrypt(self.ciph.decrypt, 8, hexdec(ciphtext))),
+ hexenc(ecb_decrypt(
+ self.ciph.decrypt,
+ GOST3412Magma.blocksize,
+ hexdec(ciphtext),
+ )),
self.plaintext,
)
def test_ecb_symmetric(self):
for _ in range(100):
pt = pad2(urandom(randint(0, 16 * 2)), 16)
- ciph = GOST3412Magma(urandom(32))
- ct = ecb_encrypt(ciph.encrypt, 8, pt)
- self.assertSequenceEqual(ecb_decrypt(ciph.decrypt, 8, ct), pt)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ ct = ecb_encrypt(ciph.encrypt, GOST3412Magma.blocksize, pt)
+ self.assertSequenceEqual(ecb_decrypt(
+ ciph.decrypt,
+ GOST3412Magma.blocksize,
+ ct,
+ ), pt)
def test_ctr_vectors(self):
ciphtext = ""
@@ -247,21 +338,36 @@ class GOST3412MagmaModesTest(TestCase):
ciphtext += "568eb680ab52a12d"
iv = self.iv[:4]
self.assertSequenceEqual(
- hexenc(ctr(self.ciph.encrypt, 8, hexdec(self.plaintext), iv)),
+ hexenc(ctr(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ctr(self.ciph.encrypt, 8, hexdec(ciphtext), iv)),
+ hexenc(ctr(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(ciphtext),
+ iv,
+ )),
self.plaintext,
)
def test_ctr_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(4)
- ciph = GOST3412Magma(urandom(32))
- ct = ctr(ciph.encrypt, 8, pt, iv)
- self.assertSequenceEqual(ctr(ciph.encrypt, 8, ct, iv), pt)
+ iv = urandom(GOST3412Magma.blocksize // 2)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ ct = ctr(ciph.encrypt, GOST3412Magma.blocksize, pt, iv)
+ self.assertSequenceEqual(ctr(
+ ciph.encrypt,
+ GOST3412Magma.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_ofb_vectors(self):
iv = self.iv[:16]
@@ -271,21 +377,36 @@ class GOST3412MagmaModesTest(TestCase):
ciphtext += "a0f83062430e327e"
ciphtext += "c824efb8bd4fdb05"
self.assertSequenceEqual(
- hexenc(ofb(self.ciph.encrypt, 8, hexdec(self.plaintext), iv)),
+ hexenc(ofb(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(ofb(self.ciph.encrypt, 8, hexdec(ciphtext), iv)),
+ hexenc(ofb(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(ciphtext),
+ iv,
+ )),
self.plaintext,
)
def test_ofb_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(8 * 2)
- ciph = GOST3412Magma(urandom(32))
- ct = ofb(ciph.encrypt, 8, pt, iv)
- self.assertSequenceEqual(ofb(ciph.encrypt, 8, ct, iv), pt)
+ iv = urandom(GOST3412Magma.blocksize * 2)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ ct = ofb(ciph.encrypt, GOST3412Magma.blocksize, pt, iv)
+ self.assertSequenceEqual(ofb(
+ ciph.encrypt,
+ GOST3412Magma.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_cbc_vectors(self):
ciphtext = ""
@@ -294,21 +415,36 @@ class GOST3412MagmaModesTest(TestCase):
ciphtext += "5058b4a1c4bc0019"
ciphtext += "20b78b1a7cd7e667"
self.assertSequenceEqual(
- hexenc(cbc_encrypt(self.ciph.encrypt, 8, hexdec(self.plaintext), self.iv)),
+ hexenc(cbc_encrypt(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ self.iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(cbc_decrypt(self.ciph.decrypt, 8, hexdec(ciphtext), self.iv)),
+ hexenc(cbc_decrypt(
+ self.ciph.decrypt,
+ GOST3412Magma.blocksize,
+ hexdec(ciphtext),
+ self.iv,
+ )),
self.plaintext,
)
def test_cbc_symmetric(self):
for _ in range(100):
pt = pad2(urandom(randint(0, 16 * 2)), 16)
- iv = urandom(8 * 2)
- ciph = GOST3412Magma(urandom(32))
- ct = cbc_encrypt(ciph.encrypt, 8, pt, iv)
- self.assertSequenceEqual(cbc_decrypt(ciph.decrypt, 8, ct, iv), pt)
+ iv = urandom(GOST3412Magma.blocksize * 2)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ ct = cbc_encrypt(ciph.encrypt, GOST3412Magma.blocksize, pt, iv)
+ self.assertSequenceEqual(cbc_decrypt(
+ ciph.decrypt,
+ GOST3412Magma.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_cfb_vectors(self):
iv = self.iv[:16]
@@ -318,36 +454,55 @@ class GOST3412MagmaModesTest(TestCase):
ciphtext += "24bdd2035315d38b"
ciphtext += "bcc0321421075505"
self.assertSequenceEqual(
- hexenc(cfb_encrypt(self.ciph.encrypt, 8, hexdec(self.plaintext), iv)),
+ hexenc(cfb_encrypt(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ iv,
+ )),
ciphtext,
)
self.assertSequenceEqual(
- hexenc(cfb_decrypt(self.ciph.encrypt, 8, hexdec(ciphtext), iv)),
+ hexenc(cfb_decrypt(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(ciphtext),
+ iv,
+ )),
self.plaintext,
)
def test_cfb_symmetric(self):
for _ in range(100):
pt = urandom(randint(0, 16 * 2))
- iv = urandom(8 * 2)
- ciph = GOST3412Magma(urandom(32))
- ct = cfb_encrypt(ciph.encrypt, 8, pt, iv)
- self.assertSequenceEqual(cfb_decrypt(ciph.encrypt, 8, ct, iv), pt)
+ iv = urandom(GOST3412Magma.blocksize * 2)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ ct = cfb_encrypt(ciph.encrypt, GOST3412Magma.blocksize, pt, iv)
+ self.assertSequenceEqual(cfb_decrypt(
+ ciph.encrypt,
+ GOST3412Magma.blocksize,
+ ct,
+ iv,
+ ), pt)
def test_mac_vectors(self):
- k1, k2 = _mac_ks(self.ciph.encrypt, 8)
+ k1, k2 = _mac_ks(self.ciph.encrypt, GOST3412Magma.blocksize)
self.assertSequenceEqual(hexenc(k1), "5f459b3342521424")
self.assertSequenceEqual(hexenc(k2), "be8b366684a42848")
self.assertSequenceEqual(
- hexenc(mac(self.ciph.encrypt, 8, hexdec(self.plaintext))[:4]),
+ hexenc(mac(
+ self.ciph.encrypt,
+ GOST3412Magma.blocksize,
+ hexdec(self.plaintext),
+ )[:4]),
"154e7210",
)
def test_mac_applies(self):
for _ in range(100):
data = urandom(randint(0, 16 * 2))
- ciph = GOST3412Magma(urandom(32))
- mac(ciph.encrypt, 8, data)
+ ciph = GOST3412Magma(urandom(KEYSIZE))
+ mac(ciph.encrypt, GOST3412Magma.blocksize, data)
class TestVectorACPKM(TestCase):
@@ -356,11 +511,11 @@ class TestVectorACPKM(TestCase):
key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF")
def test_magma_ctr_acpkm(self):
- key = acpkm(GOST3412Magma(self.key).encrypt, 8)
+ key = acpkm(GOST3412Magma(self.key).encrypt, GOST3412Magma.blocksize)
self.assertSequenceEqual(key, hexdec("863EA017842C3D372B18A85A28E2317D74BEFC107720DE0C9E8AB974ABD00CA0"))
- key = acpkm(GOST3412Magma(key).encrypt, 8)
+ key = acpkm(GOST3412Magma(key).encrypt, GOST3412Magma.blocksize)
self.assertSequenceEqual(key, hexdec("49A5E2677DE555982B8AD5E826652D17EEC847BF5B3997A81CF7FE7F1187BD27"))
- key = acpkm(GOST3412Magma(key).encrypt, 8)
+ key = acpkm(GOST3412Magma(key).encrypt, GOST3412Magma.blocksize)
self.assertSequenceEqual(key, hexdec("3256BF3F97B5667426A9FB1C5EAABE41893CCDD5A868F9B63B0AA90720FA43C4"))
def test_magma_ctr(self):
@@ -379,20 +534,34 @@ A1 AE 71 14 9E ED 13 82 AB D4 67 18 06 72 EC 6F
84 A2 F1 5B 3F CA 72 C1
""".replace("\n", "").replace(" ", ""))
self.assertSequenceEqual(
- ctr_acpkm(GOST3412Magma, encrypter, bs=8, section_size=16, data=plaintext, iv=iv),
+ ctr_acpkm(
+ GOST3412Magma,
+ encrypter,
+ bs=GOST3412Magma.blocksize,
+ section_size=GOST3412Magma.blocksize * 2,
+ data=plaintext,
+ iv=iv
+ ),
ciphertext,
)
self.assertSequenceEqual(
- ctr_acpkm(GOST3412Magma, encrypter, bs=8, section_size=16, data=ciphertext, iv=iv),
+ ctr_acpkm(
+ GOST3412Magma,
+ encrypter,
+ bs=GOST3412Magma.blocksize,
+ section_size=GOST3412Magma.blocksize * 2,
+ data=ciphertext,
+ iv=iv
+ ),
plaintext,
)
def test_kuznechik_ctr_acpkm(self):
- key = acpkm(GOST3412Kuznechik(self.key).encrypt, 16)
+ key = acpkm(GOST3412Kuznechik(self.key).encrypt, GOST3412Kuznechik.blocksize)
self.assertSequenceEqual(key, hexdec("2666ED40AE687811745CA0B448F57A7B390ADB5780307E8E9659AC403AE60C60"))
- key = acpkm(GOST3412Kuznechik(key).encrypt, 16)
+ key = acpkm(GOST3412Kuznechik(key).encrypt, GOST3412Kuznechik.blocksize)
self.assertSequenceEqual(key, hexdec("BB3DD5402E999B7A3DEBB0DB45448EC530F07365DFEE3ABA8415F77AC8F34CE8"))
- key = acpkm(GOST3412Kuznechik(key).encrypt, 16)
+ key = acpkm(GOST3412Kuznechik(key).encrypt, GOST3412Kuznechik.blocksize)
self.assertSequenceEqual(key, hexdec("23362FD553CAD2178299A5B5A2D4722E3BB83C730A8BF57CE2DD004017F8C565"))
def test_kuznechik_ctr(self):
@@ -420,8 +589,8 @@ DF FD 07 EC 81 36 36 46 0C 4F 3B 74 34 23 16 3E
ctr_acpkm(
GOST3412Kuznechik,
encrypter,
- bs=16,
- section_size=32,
+ bs=GOST3412Kuznechik.blocksize,
+ section_size=GOST3412Kuznechik.blocksize * 2,
data=plaintext,
iv=iv,
),
@@ -431,8 +600,8 @@ DF FD 07 EC 81 36 36 46 0C 4F 3B 74 34 23 16 3E
ctr_acpkm(
GOST3412Kuznechik,
encrypter,
- bs=16,
- section_size=32,
+ bs=GOST3412Kuznechik.blocksize,
+ section_size=GOST3412Kuznechik.blocksize * 2,
data=ciphertext,
iv=iv,
),
@@ -447,8 +616,8 @@ DF FD 07 EC 81 36 36 46 0C 4F 3B 74 34 23 16 3E
GOST3412Magma,
encrypter,
key_section_size=key_section_size,
- bs=8,
- keymat_len=KEY_SIZE + 8,
+ bs=GOST3412Magma.blocksize,
+ keymat_len=KEYSIZE + GOST3412Magma.blocksize,
),
hexdec("0DF2F5273DA328932AC49D81D36B2558A50DBF9BBCAC74A614B2CCB2F1CBCD8A70638E3DE8B3571E"),
)
@@ -458,8 +627,8 @@ DF FD 07 EC 81 36 36 46 0C 4F 3B 74 34 23 16 3E
GOST3412Magma,
encrypter,
key_section_size,
- section_size=16,
- bs=8,
+ section_size=GOST3412Magma.blocksize * 2,
+ bs=GOST3412Magma.blocksize,
data=text,
),
hexdec("A0540E3730ACBCF3"),
@@ -473,8 +642,8 @@ DF FD 07 EC 81 36 36 46 0C 4F 3B 74 34 23 16 3E
GOST3412Magma,
encrypter,
key_section_size=key_section_size,
- bs=8,
- keymat_len=3 * (KEY_SIZE + 8),
+ bs=GOST3412Magma.blocksize,
+ keymat_len=3 * (KEYSIZE + GOST3412Magma.blocksize),
),
hexdec("""
0D F2 F5 27 3D A3 28 93 2A C4 9D 81 D3 6B 25 58
@@ -497,8 +666,8 @@ E2 40 66 40 54 7B 9F 1F 5F 2B 43 61 2A AE AF DA
GOST3412Magma,
encrypter,
key_section_size,
- section_size=16,
- bs=8,
+ section_size=GOST3412Magma.blocksize * 2,
+ bs=GOST3412Magma.blocksize,
data=text,
),
hexdec("34008DAD5496BB8E"),
@@ -512,8 +681,8 @@ E2 40 66 40 54 7B 9F 1F 5F 2B 43 61 2A AE AF DA
GOST3412Kuznechik,
encrypter,
key_section_size=key_section_size,
- bs=16,
- keymat_len=KEY_SIZE + 16,
+ bs=GOST3412Kuznechik.blocksize,
+ keymat_len=KEYSIZE + GOST3412Kuznechik.blocksize,
),
hexdec("""
0C AB F1 F2 EF BC 4A C1 60 48 DF 1A 24 C6 05 B2
@@ -530,8 +699,8 @@ C0 D1 67 3D 75 86 A8 EC 0D D4 2C 45 A4 F9 5B AE
GOST3412Kuznechik,
encrypter,
key_section_size,
- section_size=32,
- bs=16,
+ section_size=GOST3412Kuznechik.blocksize * 2,
+ bs=GOST3412Kuznechik.blocksize,
data=text,
),
hexdec("B5367F47B62B995EEB2A648C5843145E"),
@@ -545,8 +714,8 @@ C0 D1 67 3D 75 86 A8 EC 0D D4 2C 45 A4 F9 5B AE
GOST3412Kuznechik,
encrypter,
key_section_size=key_section_size,
- bs=16,
- keymat_len=3 * (KEY_SIZE + 16),
+ bs=GOST3412Kuznechik.blocksize,
+ keymat_len=3 * (KEYSIZE + GOST3412Kuznechik.blocksize),
),
hexdec("""
0C AB F1 F2 EF BC 4A C1 60 48 DF 1A 24 C6 05 B2
@@ -572,8 +741,8 @@ A8 1C 79 A0 4F 29 66 0E A3 FD A8 74 C6 30 79 9E
GOST3412Kuznechik,
encrypter,
key_section_size,
- section_size=32,
- bs=16,
+ section_size=GOST3412Kuznechik.blocksize * 2,
+ bs=GOST3412Kuznechik.blocksize,
data=text,
),
hexdec("FBB8DCEE45BEA67C35F58C5700898E5D"),
diff --git a/pygost/test_mgm.py b/pygost/test_mgm.py
index 56cfda0..9034158 100644
--- a/pygost/test_mgm.py
+++ b/pygost/test_mgm.py
@@ -20,6 +20,7 @@ from unittest import TestCase
from pygost.gost3412 import GOST3412Kuznechik
from pygost.gost3412 import GOST3412Magma
+from pygost.gost3412 import KEYSIZE
from pygost.mgm import MGM
from pygost.mgm import nonce_prepare
from pygost.utils import hexdec
@@ -30,7 +31,7 @@ class TestVector(TestCase):
key = hexdec("8899AABBCCDDEEFF0011223344556677FEDCBA98765432100123456789ABCDEF")
ad = hexdec("0202020202020202010101010101010104040404040404040303030303030303EA0505050505050505")
plaintext = hexdec("1122334455667700FFEEDDCCBBAA998800112233445566778899AABBCCEEFF0A112233445566778899AABBCCEEFF0A002233445566778899AABBCCEEFF0A0011AABBCC")
- mgm = MGM(GOST3412Kuznechik(key).encrypt, 16)
+ mgm = MGM(GOST3412Kuznechik(key).encrypt, GOST3412Kuznechik.blocksize)
ciphertext = mgm.seal(plaintext[:16], plaintext, ad)
self.assertSequenceEqual(ciphertext[:len(plaintext)], hexdec("A9757B8147956E9055B8A33DE89F42FC8075D2212BF9FD5BD3F7069AADC16B39497AB15915A6BA85936B5D0EA9F6851CC60C14D4D3F883D0AB94420695C76DEB2C7552"))
self.assertSequenceEqual(ciphertext[len(plaintext):], hexdec("CF5D656F40C34F5C46E8BB0E29FCDB4C"))
@@ -49,9 +50,15 @@ class TestSymmetric(TestCase):
self.assertSequenceEqual(mgm.open(nonce, ct, ad), pt)
def test_magma(self):
- mgm = MGM(GOST3412Magma(urandom(32)).encrypt, 8)
- self._itself(mgm, 8)
+ mgm = MGM(
+ GOST3412Magma(urandom(KEYSIZE)).encrypt,
+ GOST3412Magma.blocksize,
+ )
+ self._itself(mgm, GOST3412Magma.blocksize)
def test_kuznechik(self):
- mgm = MGM(GOST3412Kuznechik(urandom(32)).encrypt, 16)
- self._itself(mgm, 16)
+ mgm = MGM(
+ GOST3412Kuznechik(urandom(KEYSIZE)).encrypt,
+ GOST3412Kuznechik.blocksize,
+ )
+ self._itself(mgm, GOST3412Kuznechik.blocksize)