1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import random, re |
---|
4 | import unittest |
---|
5 | |
---|
6 | from binascii import a2b_hex, b2a_hex |
---|
7 | from pkg_resources import resource_string |
---|
8 | |
---|
9 | from pycryptopp.cipher import xsalsa |
---|
10 | TEST_XSALSA_RE=re.compile("\nCOUNT=([0-9]+)\nKEY=([0-9a-f]+)\nIV=([0-9a-f]+)\nPLAINTEXT=([0-9a-f]+)\nCIPHERTEXT=([0-9a-f]+)") |
---|
11 | |
---|
12 | class XSalsaTest(unittest.TestCase): |
---|
13 | |
---|
14 | enc0="eea6a7251c1e72916d11c2cb214d3c252539121d8e234e652d651fa4c8cff880309e645a74e9e0a60d8243acd9177ab51a1beb8d5a2f5d700c093c5e5585579625337bd3ab619d615760d8c5b224a85b1d0efe0eb8a7ee163abb0376529fcc09bab506c618e13ce777d82c3ae9d1a6f972d4160287cbfe60bf2130fc0a6ff6049d0a5c8a82f429231f0080" |
---|
15 | |
---|
16 | def test_zero_XSalsa(self): |
---|
17 | key="1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389" |
---|
18 | iv="69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37" |
---|
19 | computedcipher=xsalsa.XSalsa(a2b_hex(key),a2b_hex(iv)).process('\x00'*139) |
---|
20 | self.failUnlessEqual(a2b_hex(self.enc0), computedcipher, "enc0: %s, computedciper: %s" % (self.enc0, b2a_hex(computedcipher))) |
---|
21 | |
---|
22 | cryptor=xsalsa.XSalsa(a2b_hex(key),a2b_hex(iv)) |
---|
23 | |
---|
24 | computedcipher1=cryptor.process('\x00'*69) |
---|
25 | computedcipher2=cryptor.process('\x00'*69) |
---|
26 | computedcipher3=cryptor.process('\x00') |
---|
27 | computedcipher12=b2a_hex(computedcipher1)+b2a_hex(computedcipher2)+b2a_hex(computedcipher3) |
---|
28 | self.failUnlessEqual(self.enc0, computedcipher12) |
---|
29 | |
---|
30 | def test_XSalsa(self): |
---|
31 | curfile = open( '../testvectors/testx1.txt', 'r') |
---|
32 | s = curfile.read() |
---|
33 | print s,"\n" |
---|
34 | return self._test_XSalsa(s) |
---|
35 | |
---|
36 | def _test_XSalsa(self, vects_str): |
---|
37 | for mo in TEST_XSALSA_RE.finditer(vects_str): |
---|
38 | count = int(mo.group(1)) |
---|
39 | key = a2b_hex(mo.group(2)) |
---|
40 | iv = a2b_hex(mo.group(3)) |
---|
41 | # plaintext = a2b_hex(mo.group(4)) |
---|
42 | # ciphertext= a2b_hex(mo.group(5)) |
---|
43 | plaintext = mo.group(4) |
---|
44 | ciphertext = mo.group(5) |
---|
45 | computedcipher=xsalsa.XSalsa(key,iv).process(a2b_hex(plaintext)) |
---|
46 | # print "ciphertext", b2a_hex(computedcipher), '\n' |
---|
47 | # print "computedtext", ciphertext, '\n' |
---|
48 | # print count, ": \n" |
---|
49 | self.failUnlessEqual(computedcipher,a2b_hex(ciphertext),"computedcipher: %s, ciphertext: %s" % (b2a_hex(computedcipher), ciphertext)) |
---|
50 | |
---|
51 | #the random decomposing |
---|
52 | plaintext1 = "" |
---|
53 | plaintext2 = "" |
---|
54 | length = len(plaintext) |
---|
55 | rccipher = "" |
---|
56 | cryptor = xsalsa.XSalsa(key,iv) |
---|
57 | if length > 2: |
---|
58 | point = random.randint(0,length-3) |
---|
59 | if (point%2) !=0: |
---|
60 | point -= 1 |
---|
61 | plaintext1 += plaintext[:point+2] |
---|
62 | plaintext2 += plaintext[point+2:] |
---|
63 | rccipher += b2a_hex(cryptor.process(a2b_hex(plaintext1))) |
---|
64 | rccipher += b2a_hex(cryptor.process(a2b_hex(plaintext2))) |
---|
65 | self.failUnlessEqual(rccipher, ciphertext, "random computed cipher: %s, ciphertext: %s" % (rccipher, ciphertext)) |
---|
66 | |
---|
67 | #every byte encrypted |
---|
68 | cryptor = xsalsa.XSalsa(key,iv) |
---|
69 | eccipher="" |
---|
70 | l = 0 |
---|
71 | while l<=(length-2): |
---|
72 | eccipher += b2a_hex(cryptor.process(a2b_hex(plaintext[l:l+2]))) |
---|
73 | l += 2 |
---|
74 | self.failUnlessEqual(eccipher, ciphertext, "every byte computed cipher: %s, ciphertext: %s" % (eccipher, ciphertext)) |
---|
75 | |
---|
76 | |
---|
77 | def test_init_type_check(self): |
---|
78 | self.failUnlessRaises(TypeError, xsalsa.XSalsa, None) |
---|
79 | self.failUnlessRaises(xsalsa.Error, xsalsa.XSalsa, "a"*1) |
---|
80 | self.failUnlessRaises(xsalsa.Error, xsalsa.XSalsa, "a"*17) |
---|
81 | self.failUnlessRaises(xsalsa.Error, xsalsa.XSalsa, "a"*18) |
---|
82 | # self.failUnlessRaises(xsalsa.Error, xsalsa.XSalsa, "a"*32) |
---|
83 | |
---|
84 | if __name__ == "__main__": |
---|
85 | unittest.main() |
---|