#
# 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
#
# 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
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
from pygost.gost28147 import cbc_decrypt
from pygost.gost28147 import cbc_encrypt
from pygost.gost28147 import cfb_decrypt
from pygost.gost28147 import cbc_decrypt
from pygost.gost28147 import cbc_encrypt
from pygost.gost28147 import cfb_decrypt
from pygost.gost28147 import ecb_decrypt
from pygost.gost28147 import ecb_encrypt
from pygost.gost28147 import encrypt
from pygost.gost28147 import ecb_decrypt
from pygost.gost28147 import ecb_encrypt
from pygost.gost28147 import encrypt
from pygost.gost28147 import MESH_MAX_DATA
from pygost.gost28147 import ns2block
from pygost.utils import hexdec
from pygost.gost28147 import MESH_MAX_DATA
from pygost.gost28147 import ns2block
from pygost.utils import hexdec
key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x5a, 0x5f, 0xca, 0x58, 0x9a, 0xb2, 0x2d, 0xb2,
)))
encrypted = ecb_encrypt(key, plaintext, sbox=sbox)
0x5a, 0x5f, 0xca, 0x58, 0x9a, 0xb2, 0x2d, 0xb2,
)))
encrypted = ecb_encrypt(key, plaintext, sbox=sbox)
- self.assertEqual(ecb_encrypt(key, pt, sbox=sbox), ct)
+ self.assertSequenceEqual(ecb_encrypt(key, pt, sbox=sbox), ct)
ecb_encrypt(key, hexdec(b"1122334455667788"), sbox=sbox),
hexdec(b"03251E14F9D28ACB"),
)
ecb_encrypt(key, hexdec(b"1122334455667788"), sbox=sbox),
hexdec(b"03251E14F9D28ACB"),
)
plaintext = urandom(20)
ciphertext = cfb_encrypt(key, plaintext, iv)
# First full block
step = encrypt(DEFAULT_SBOX, key, block2ns(iv))
step = strxor(plaintext[:8], ns2block(step))
plaintext = urandom(20)
ciphertext = cfb_encrypt(key, plaintext, iv)
# First full block
step = encrypt(DEFAULT_SBOX, key, block2ns(iv))
step = strxor(plaintext[:8], ns2block(step))
# Second full block
step = encrypt(DEFAULT_SBOX, key, block2ns(step))
step = strxor(plaintext[8:16], ns2block(step))
# Second full block
step = encrypt(DEFAULT_SBOX, key, block2ns(step))
step = strxor(plaintext[8:16], ns2block(step))
# Third non-full block
step = encrypt(DEFAULT_SBOX, key, block2ns(step))
step = strxor(plaintext[16:] + 4 * b"\x00", ns2block(step))
# Third non-full block
step = encrypt(DEFAULT_SBOX, key, block2ns(step))
step = strxor(plaintext[16:] + 4 * b"\x00", ns2block(step))
for size in (5, 8, 16, 120):
pt = urandom(size)
for size in (5, 8, 16, 120):
pt = urandom(size)
- self.assertEqual(
- cfb_decrypt(key, cfb_encrypt(key, pt, iv), iv), pt,
+ self.assertSequenceEqual(
+ cfb_decrypt(key, cfb_encrypt(key, pt, iv), iv),
+ pt,
key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
)))
iv = b"\x02\x01\x01\x01\x01\x01\x01\x01"
encrypted = cnt(key, plaintext, iv=iv, sbox=sbox)
)))
iv = b"\x02\x01\x01\x01\x01\x01\x01\x01"
encrypted = cnt(key, plaintext, iv=iv, sbox=sbox)
decrypted = cnt(key, encrypted, iv=iv, sbox=sbox)
decrypted = cnt(key, encrypted, iv=iv, sbox=sbox)
key = hexdec(b"fc7ad2886f455b50d29008fa622b57d5c65b3c637202025799cadf0768519e8a")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
key = hexdec(b"fc7ad2886f455b50d29008fa622b57d5c65b3c637202025799cadf0768519e8a")
plaintext = bytes(bytearray((
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x13, 0xcc, 0x55, 0x38, 0xb5, 0x63, 0x32, 0xc5,
0x23, 0xa4, 0xcb, 0x7d, 0x51,
)))
0x13, 0xcc, 0x55, 0x38, 0xb5, 0x63, 0x32, 0xc5,
0x23, 0xa4, 0xcb, 0x7d, 0x51,
)))
encrypted = cnt(key, plaintext, iv=iv, sbox=sbox)
encrypted = cnt(key, plaintext, iv=iv, sbox=sbox)
decrypted = cnt(key, encrypted, iv=iv, sbox=sbox)
decrypted = cnt(key, encrypted, iv=iv, sbox=sbox)
for s in (b"", b"foo", b"foobarbaz"):
with self.assertRaises(ValueError):
cbc_encrypt(key, s, pad=False)
for s in (b"", b"foo", b"foobarbaz"):
with self.assertRaises(ValueError):
cbc_encrypt(key, s, pad=False)
for pt in (b"foo", b"foobarba", b"foobarbaz", 16 * b"x"):
ct = cbc_encrypt(key, pt, iv)
dt = cbc_decrypt(key, ct)
for pt in (b"foo", b"foobarba", b"foobarbaz", 16 * b"x"):
ct = cbc_encrypt(key, pt, iv)
dt = cbc_decrypt(key, ct)
- 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))
ct = cbc_encrypt(key, pt)
dt = cbc_decrypt(key, ct)
ct = cbc_encrypt(key, pt)
dt = cbc_decrypt(key, ct)
def test_single(self):
pt = b"\x00"
ct = cfb_encrypt(self.key, pt, mesh=True)
dec = cfb_decrypt(self.key, ct, mesh=True)
def test_single(self):
pt = b"\x00"
ct = cfb_encrypt(self.key, pt, mesh=True)
dec = cfb_decrypt(self.key, ct, mesh=True)
def test_short(self):
pt = urandom(MESH_MAX_DATA - 1)
ct = cfb_encrypt(self.key, pt, mesh=True)
dec = cfb_decrypt(self.key, ct, mesh=True)
dec_plain = cfb_decrypt(self.key, ct)
def test_short(self):
pt = urandom(MESH_MAX_DATA - 1)
ct = cfb_encrypt(self.key, pt, mesh=True)
dec = cfb_decrypt(self.key, ct, mesh=True)
dec_plain = cfb_decrypt(self.key, ct)
- self.assertEqual(pt, dec)
- self.assertEqual(pt, dec_plain)
+ self.assertSequenceEqual(pt, dec)
+ self.assertSequenceEqual(pt, dec_plain)
def test_short_iv(self):
pt = urandom(MESH_MAX_DATA - 1)
ct = cfb_encrypt(self.key, pt, iv=self.iv, mesh=True)
dec = cfb_decrypt(self.key, ct, iv=self.iv, mesh=True)
dec_plain = cfb_decrypt(self.key, ct, iv=self.iv)
def test_short_iv(self):
pt = urandom(MESH_MAX_DATA - 1)
ct = cfb_encrypt(self.key, pt, iv=self.iv, mesh=True)
dec = cfb_decrypt(self.key, ct, iv=self.iv, mesh=True)
dec_plain = cfb_decrypt(self.key, ct, iv=self.iv)
- self.assertEqual(pt, dec)
- self.assertEqual(pt, dec_plain)
+ self.assertSequenceEqual(pt, dec)
+ self.assertSequenceEqual(pt, dec_plain)
def test_longer_iv(self):
pt = urandom(MESH_MAX_DATA * 3)
ct = cfb_encrypt(self.key, pt, iv=self.iv, mesh=True)
dec = cfb_decrypt(self.key, ct, iv=self.iv, mesh=True)
dec_plain = cfb_decrypt(self.key, ct, iv=self.iv)
def test_longer_iv(self):
pt = urandom(MESH_MAX_DATA * 3)
ct = cfb_encrypt(self.key, pt, iv=self.iv, mesh=True)
dec = cfb_decrypt(self.key, ct, iv=self.iv, mesh=True)
dec_plain = cfb_decrypt(self.key, ct, iv=self.iv)