def _do_mct_aes_test(self, file_name, segment_size): test_vectors = load_tests("AES", file_name, "AES CFB%d Montecarlo" % segment_size) assert test_vectors assert segment_size in (8, 128) for tv in test_vectors: self.description = tv.desc cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv, segment_size=segment_size) def get_input(input_text, output_seq, j): # CFB128 if segment_size == 128: if j >= 2: return output_seq[-2] return [input_text, tv.iv][j] # CFB8 if j == 0: return input_text elif j <= 16: return tv.iv[j - 1 : j] return output_seq[j - 17] if tv.direction == "ENC": cts = [] for j in xrange(1000): plaintext = get_input(tv.plaintext, cts, j) cts.append(cipher.encrypt(plaintext)) self.assertEqual(cts[-1], tv.ciphertext) else: pts = [] for j in xrange(1000): ciphertext = get_input(tv.ciphertext, pts, j) pts.append(cipher.decrypt(ciphertext)) self.assertEqual(pts[-1], tv.plaintext)
def _do_kat_aes_test(self, file_name, segment_size): test_vectors = load_tests("AES", file_name, "AES CFB%d KAT" % segment_size) assert test_vectors for tv in test_vectors: self.description = tv.desc cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv, segment_size=segment_size) if tv.direction == "ENC": self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) else: self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
def _do_kat_aes_test(self, file_name): test_vectors = load_tests("AES", file_name, "AES KAT") assert(test_vectors) for tv in test_vectors: self.description = tv.desc cipher = AES.new(tv.key, self.aes_mode, tv.iv) if tv.direction == "ENC": self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) else: self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
def _do_tdes_test(self, file_name, segment_size): test_vectors = load_tests("TDES", file_name, "TDES CFB%d KAT" % segment_size) assert test_vectors for tv in test_vectors: self.description = tv.desc if hasattr(tv, "keys"): cipher = DES.new(tv.keys, DES.MODE_CFB, tv.iv, segment_size=segment_size) else: if tv.key1 != tv.key3: key = tv.key1 + tv.key2 + tv.key3 # Option 3 else: key = tv.key1 + tv.key2 # Option 2 cipher = DES3.new(key, DES3.MODE_CFB, tv.iv, segment_size=segment_size) if tv.direction == "ENC": self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) else: self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
def _do_tdes_test(self, file_name): test_vectors = load_tests("TDES", file_name, "TDES CBC KAT") assert(test_vectors) for tv in test_vectors: self.description = tv.desc if hasattr(tv, "keys"): cipher = DES.new(tv.keys, self.des_mode, tv.iv) else: if tv.key1 != tv.key3: key = tv.key1 + tv.key2 + tv.key3 # Option 3 else: key = tv.key1 + tv.key2 # Option 2 cipher = DES3.new(key, self.des3_mode, tv.iv) if tv.direction == "ENC": self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) else: self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
def _do_mct_aes_test(self, file_name): test_vectors = load_tests("AES", file_name, "AES Montecarlo") assert(test_vectors) for tv in test_vectors: self.description = tv.desc cipher = AES.new(tv.key, self.aes_mode, tv.iv) if tv.direction == 'ENC': cts = [ tv.iv ] for count in xrange(1000): cts.append(cipher.encrypt(tv.plaintext)) tv.plaintext = cts[-2] self.assertEqual(cts[-1], tv.ciphertext) else: pts = [ tv.iv] for count in xrange(1000): pts.append(cipher.decrypt(tv.ciphertext)) tv.ciphertext = pts[-2] self.assertEqual(pts[-1], tv.plaintext)
'a826fd8ce53b855fcce21c8112256fe668d5c05dd9b6b900', '0123456789abcdef23456789abcdef01456789abcdef0123', 'NIST SP800-67 B.1'), # This test is designed to test the DES3 API, not the correctness of the # output. ('21e81b7ade88a259', '5c577d4d9b20c0f8', '9b397ebf81b1181e282f4bb8adbadc6b', 'Two-key 3DES'), ] # NIST CAVP test vectors nist_tdes_mmt_files = ("TECBMMT2.rsp", "TECBMMT3.rsp") for tdes_file in nist_tdes_mmt_files: test_vectors = load_tests("TDES", tdes_file, "TDES ECB (%s)" % tdes_file) assert(test_vectors) for index, tv in enumerate(test_vectors): key = tv.key1 + tv.key2 + tv.key3 test_data_item = (tostr(hexlify(tv.plaintext)), tostr(hexlify(tv.ciphertext)), tostr(hexlify(key)), "%s (%s)" % (tdes_file, index)) test_data.append(test_data_item) class CheckParity(unittest.TestCase): def test_parity_option2(self): before_2k = unhexlify("CABF326FA56734324FFCCABCDEFACABF") after_2k = DES3.adjust_key_parity(before_2k)