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.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) self.assertSequenceEqual(pt, dec) self.assertNotEqual(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) self.assertEqual(pt, dec) self.assertEqual(pt, dec_plain)
def __next__(self): start = int(datetime.now().strftime('%f')) ts = datetime.now().strftime(self.__time_format) if self.cipher != "gost": T = self.cipher.encrypt(bytes(ts, 'utf-8')) out = self.cipher.encrypt(strxor(T, self.__state)) self.__state = self.cipher.encrypt(strxor(T, out)) else: T = cfb_encrypt(self.key, data=bytes(ts, 'utf-8'), iv=self.IV) out = cfb_encrypt(self.key, data=strxor(T, self.__state), iv=self.IV) self.__state = cfb_encrypt(self.key, data=strxor(T, out), iv=self.IV) return out.hex(), (int(datetime.now().strftime('%f')) - start)
def test_random(self): """ Random data with various sizes """ key = urandom(32) iv = urandom(8) for size in (5, 8, 16, 120): pt = urandom(size) self.assertEqual( cfb_decrypt(key, cfb_encrypt(key, pt, iv), iv), pt, )
def diversify(kek, ukm, sbox=DEFAULT_SBOX): out = kek for i in range(8): s1, s2 = 0, 0 for j in range(8): k, = unpack("<i", out[j * 4:j * 4 + 4]) if (ukm[i] >> j) & 1: s1 += k else: s2 += k iv = pack("<I", s1 % 2 ** 32) + pack("<I", s2 % 2 ** 32) out = cfb_encrypt(out, out, iv=iv, sbox=sbox) return out
def diversify(kek, ukm): out = kek for i in range(8): s1, s2 = 0, 0 for j in range(8): k, = unpack("<i", out[j * 4:j * 4 + 4]) if (ukm[i] >> j) & 1: s1 += k else: s2 += k iv = pack("<I", s1 % 2 ** 32) + pack("<I", s2 % 2 ** 32) out = cfb_encrypt(out, out, iv=iv) return out
def encrypt(text, mode, key, iv=Random.new().read(BLOCKSIZE)): encrypted_data = None if mode == 'ECB': encrypted_data = ecb_encrypt( key, GOST.add_padding(text.encode('utf-8'))) iv = b'' elif mode == 'CBC': encrypted_data = cbc_encrypt( key, GOST.add_padding(text.encode('utf-8')), iv) elif mode == 'CFB': encrypted_data = cfb_encrypt( key, GOST.add_padding(text.encode('utf-8')), iv) return iv.hex(), b64encode(encrypted_data).decode('utf-8')
def test_steps(self): """ Check step-by-step operation manually """ key = urandom(32) iv = urandom(8) 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)) self.assertSequenceEqual(step, ciphertext[:8]) # Second full block step = encrypt(DEFAULT_SBOX, key, block2ns(step)) step = strxor(plaintext[8:16], ns2block(step)) self.assertSequenceEqual(step, ciphertext[8:16]) # Third non-full block step = encrypt(DEFAULT_SBOX, key, block2ns(step)) step = strxor(plaintext[16:] + 4 * b"\x00", ns2block(step)) self.assertSequenceEqual(step[:4], ciphertext[16:])
def test_cryptomanager(self): """ Test vector from http://cryptomanager.com/tv.html """ key = hexdec(b"75713134B60FEC45A607BB83AA3746AF4FF99DA6D1B53B5B1B402A1BAA030D1B") sbox = "id-GostR3411-94-TestParamSet" self.assertSequenceEqual( cfb_encrypt( key, hexdec(b"112233445566778899AABBCCDD800000"), iv=hexdec(b"0102030405060708"), sbox=sbox, ), hexdec(b"6EE84586DD2BCA0CAD3616940E164242"), ) self.assertSequenceEqual( cfb_decrypt( key, hexdec(b"6EE84586DD2BCA0CAD3616940E164242"), iv=hexdec(b"0102030405060708"), sbox=sbox, ), hexdec(b"112233445566778899AABBCCDD800000"), )
def test_single(self): pt = b"\x00" ct = cfb_encrypt(self.key, pt, mesh=True) dec = cfb_decrypt(self.key, ct, mesh=True) self.assertSequenceEqual(pt, dec)
def test_single(self): pt = b'\x00' ct = cfb_encrypt(self.key, pt, mesh=True) dec = cfb_decrypt(self.key, ct, mesh=True) self.assertEqual(pt, dec)