def test_mac_algs(self): """Unit test MAC algorithms""" for mac_alg in get_mac_algs(): with self.subTest(mac_alg=mac_alg): mac_keysize, _, _ = get_mac_params(mac_alg) mac_key = os.urandom(mac_keysize) packet = os.urandom(256) enc_mac = get_mac(mac_alg, mac_key) dec_mac = get_mac(mac_alg, mac_key) badpacket = bytearray(packet) badpacket[-1] ^= 0xff mac = enc_mac.sign(0, packet) badmac = bytearray(mac) badmac[-1] ^= 0xff self.assertTrue(dec_mac.verify(0, packet, mac)) self.assertFalse(dec_mac.verify(0, bytes(badpacket), mac)) self.assertFalse(dec_mac.verify(0, packet, bytes(badmac)))
def test_mac_algs(self): """Unit test MAC algorithms""" for alg in get_mac_algs(): with self.subTest(alg=alg): keysize, _, _ = get_mac_params(alg) key = os.urandom(keysize) data = os.urandom(256) enc_mac = get_mac(alg, key) dec_mac = get_mac(alg, key) baddata = bytearray(data) baddata[-1] ^= 0xff mac = enc_mac.sign(data) badmac = bytearray(mac) badmac[-1] ^= 0xff self.assertTrue(dec_mac.verify(data, mac)) self.assertFalse(dec_mac.verify(bytes(baddata), mac)) self.assertFalse(dec_mac.verify(data, bytes(badmac)))