| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import random, re |
|---|
| 4 | |
|---|
| 5 | import unittest |
|---|
| 6 | |
|---|
| 7 | from binascii import a2b_hex, b2a_hex |
|---|
| 8 | |
|---|
| 9 | from pycryptopp.cipher import aes, xsalsa, ciphercombiner |
|---|
| 10 | |
|---|
| 11 | from os.path import join |
|---|
| 12 | |
|---|
| 13 | from base64 import b32encode |
|---|
| 14 | |
|---|
| 15 | NIST_G1_RE = re.compile("\nKEY=([0-9a-f]+)\nIV=([0-9a-f]+)\nPLAINTEXT=([0-9a-f]+)\nCIPHERTEXT=([0-9a-f]+)\n") |
|---|
| 16 | TEST_XSALSA_RE = re.compile("\nCOUNT=([0-9]+)\nKEY=([0-9a-f]+)\nIV=([0-9a-f]+)\nPLAINTEXT=([0-9a-f]+)\nCIPHERTEXT=([0-9a-f]+)") |
|---|
| 17 | |
|---|
| 18 | class CipherOfCombinerTest(unittest.TestCase): |
|---|
| 19 | enc0 = "884fecf1f3945eaae55d3892eb79170b" |
|---|
| 20 | |
|---|
| 21 | def test_enc_zeros(self): |
|---|
| 22 | key = "\x00"*16 + a2b_hex("1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389") |
|---|
| 23 | iv = "\x00"*16 + a2b_hex("69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37") |
|---|
| 24 | cipher = ciphercombiner.CipherCombiner(key, iv).process('\x00'*16) |
|---|
| 25 | self.failUnlessEqual(self.enc0, b2a_hex(cipher)) |
|---|
| 26 | |
|---|
| 27 | def test_CipherOfCombiner(self): |
|---|
| 28 | curfile1 = open( '../testvectors/AESCTRMsg.txt', 'r') |
|---|
| 29 | curfile2 = open( '../testvectors/XSalsaMsg.txt', 'r') |
|---|
| 30 | s1 = curfile1.read() |
|---|
| 31 | s2 = curfile2.read() |
|---|
| 32 | |
|---|
| 33 | # i = 1 |
|---|
| 34 | for mo1 in NIST_G1_RE.finditer(s1): |
|---|
| 35 | for mo2 in TEST_XSALSA_RE.finditer(s2): |
|---|
| 36 | # print i, " " |
|---|
| 37 | # i += 1 |
|---|
| 38 | key1 = a2b_hex(mo1.group(1)) |
|---|
| 39 | iv1 = a2b_hex(mo1.group(2)) |
|---|
| 40 | plaintext1 = a2b_hex(mo1.group(3)) |
|---|
| 41 | ciphertext1 = a2b_hex(mo1.group(4)) |
|---|
| 42 | computedciphertext1 = aes.AES(key1, iv1).process(plaintext1) |
|---|
| 43 | |
|---|
| 44 | key2 = a2b_hex(mo2.group(2)) |
|---|
| 45 | iv2 = a2b_hex(mo2.group(3)) |
|---|
| 46 | plaintext2 = a2b_hex(mo2.group(4)) |
|---|
| 47 | ciphertext2 = a2b_hex(mo2.group(5)) |
|---|
| 48 | computedciphertext2 = xsalsa.XSalsa(key2, iv2).process(plaintext2) |
|---|
| 49 | |
|---|
| 50 | key = key1 + key2 |
|---|
| 51 | iv = iv1 + iv2 |
|---|
| 52 | text1len = len(plaintext1) |
|---|
| 53 | text2len = len(plaintext2) |
|---|
| 54 | textlen = text1len if text1len <= text2len else text2len |
|---|
| 55 | plaintext = "".join(chr(ord(plaintext1[i])^ord(plaintext2[i])) for i in xrange(textlen)) |
|---|
| 56 | # computedciphertext = "".join(chr(ord(computedciphertext1[i])^ord(computedciphertext2[i])) for i in xrange(textlen)) |
|---|
| 57 | computedciphertext = ciphercombiner.CipherCombiner(key, iv).process(plaintext) |
|---|
| 58 | ciphertext = "".join(chr(ord(ciphertext1[i])^ord(ciphertext2[i])) for i in xrange(textlen)) |
|---|
| 59 | |
|---|
| 60 | self.failUnlessEqual(ciphertext, computedciphertext, "ciphertext: %s, computedciphertext: %s, key: %s, plaintext: %s" % (b2a_hex(ciphertext), b2a_hex(computedciphertext), b2a_hex(key), b2a_hex(plaintext))) |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | if __name__ == "__main__": |
|---|
| 64 | unittest.main() |
|---|