def test_invalid_mac(self): from crypto.Util.strxor import strxor_c cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) ct, mac = cipher.encrypt_and_digest(self.data_128) invalid_mac = strxor_c(mac, 0x01) cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, invalid_mac)
def runTest(self): key = binascii.a2b_hex(b(self.key)) data = binascii.a2b_hex(b(self.input)) # Strip whitespace from the expected string (which should be in lowercase-hex) expected = b("".join(self.result.split())) h = self.module.new(key, **self.params) h.update(data) out1_bin = h.digest() out1 = binascii.b2a_hex(h.digest()) out2 = h.hexdigest() # Verify that correct MAC does not raise any exception h.hexverify(out1) h.verify(out1_bin) # Verify that incorrect MAC does raise ValueError exception wrong_mac = strxor_c(out1_bin, 255) self.assertRaises(ValueError, h.verify, wrong_mac) self.assertRaises(ValueError, h.hexverify, "4556") h = self.module.new(key, data, **self.params) out3 = h.hexdigest() out4 = binascii.b2a_hex(h.digest()) # Test .copy() h2 = h.copy() h.update(b("blah blah blah")) # Corrupt the original hash object out5 = binascii.b2a_hex(h2.digest()) # The copied hash object should return the correct result # PY3K: Check that hexdigest() returns str and digest() returns bytes if sys.version_info[0] > 2: self.assertTrue(isinstance(h.digest(), type(b("")))) self.assertTrue(isinstance(h.hexdigest(), type(""))) # PY3K: Check that .hexverify() accepts bytes or str if sys.version_info[0] > 2: h.hexverify(h.hexdigest()) h.hexverify(h.hexdigest().encode('ascii')) # PY3K: hexdigest() should return str, and digest() should return bytes self.assertEqual(expected, out1) if sys.version_info[0] == 2: self.assertEqual(expected, out2) self.assertEqual(expected, out3) else: self.assertEqual(expected.decode(), out2) self.assertEqual(expected.decode(), out3) self.assertEqual(expected, out4) self.assertEqual(expected, out5)
def test_degradation(self): sub_key1 = bchr(1) * 8 sub_key2 = bchr(255) * 8 # K1 == K2 self.assertRaises(ValueError, DES3.adjust_key_parity, sub_key1 * 2 + sub_key2) # K2 == K3 self.assertRaises(ValueError, DES3.adjust_key_parity, sub_key1 + sub_key2 * 2) # K1 == K2 == K3 self.assertRaises(ValueError, DES3.adjust_key_parity, sub_key1 * 3) # K1 == K2 (with different parity) self.assertRaises(ValueError, DES3.adjust_key_parity, sub_key1 + strxor_c(sub_key1, 1) + sub_key2)
def runTest(self): sub_key1 = bchr(1) * 8 sub_key2 = bchr(255) * 8 # K1 == K2 self.assertRaises(ValueError, DES3.new, sub_key1 * 2 + sub_key2, DES3.MODE_ECB) # K2 == K3 self.assertRaises(ValueError, DES3.new, sub_key1 + sub_key2 * 2, DES3.MODE_ECB) # K1 == K2 == K3 self.assertRaises(ValueError, DES3.new, sub_key1 *3, DES3.MODE_ECB) # K2 == K3 (parity is ignored) self.assertRaises(ValueError, DES3.new, sub_key1 + sub_key2 + strxor_c(sub_key2, 0x1), DES3.MODE_ECB)
def test3(self): self.assertEqual(strxor_c(b"", 90), b"")
def test2(self): term1 = unhexlify(b"ff339a83e5cd4cdf5649") self.assertEqual(strxor_c(term1, 0), term1)
def test1(self): term1 = unhexlify(b"ff339a83e5cd4cdf5649") result = unhexlify(b"be72dbc2a48c0d9e1708") self.assertEqual(strxor_c(term1, 65), result)
def test_memoryview(self): term1 = unhexlify(b"ff339a83e5cd4cdf5649") term1_mv = memoryview(term1) result = unhexlify(b"be72dbc2a48c0d9e1708") self.assertEqual(strxor_c(term1_mv, 65), result)
def test_bytearray(self): term1 = unhexlify(b"ff339a83e5cd4cdf5649") term1_ba = bytearray(term1) result = unhexlify(b"be72dbc2a48c0d9e1708") self.assertEqual(strxor_c(term1_ba, 65), result)
src = b"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" src = binascii.unhexlify(src) keys = [] for key in range(0, 255): keys.append(key) answers = {} mydict = {} def printScores(answer): score = 0 for i in answer: char = chr(i) if char.lower() in freqs: score = score + freqs[char.lower()] mydict[answer] = score return mydict # for key in keys: answer = strxor_c(src, key) answers =printScores(answer) inverse = [(value, key) for key, value in mydict.items()] print(max(inverse)[1])
def test_verify(self): h = self.BLAKE2.new(digest_bytes=self.max_bytes, key=b"4") mac = h.digest() h.verify(mac) wrong_mac = strxor_c(mac, 255) self.assertRaises(ValueError, h.verify, wrong_mac)