default "segment size" now set in blockcipher.py instead of wrappers
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_DES3.py
blob16b078229aee5bc85fd376760b37ccd91924a354
1 from blockcipher import *
2 import pyDes
4 def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None):
5 """Create a DES-EDE3 or DES-EDE2 cipher object
7 wrapper for pure python 3DES implementation pyDes.py
9 key = raw string containing the 2/3 keys
10 - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2
11 - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3
12 mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
13 IV = IV as a raw string, default is "all zero" IV
14 -> only needed for CBC mode
15 counter = counter object (CryptoPlus.Util.util.Counter)
16 -> only needed for CTR mode
18 EXAMPLES:
19 **********
20 IMPORTING:
21 -----------
22 >>> from CryptoPlus.Cipher import python_DES3
24 CBC TDES-EDE3 EXAMPLE: (using test vectors from http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf)
25 ------------
26 >>> key = ('37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae').decode('hex')
27 >>> IV = ('3d1de3cc132e3b65').decode('hex')
28 >>> cipher = python_DES3.new(key, python_DES3.MODE_CBC, IV)
29 >>> ciphertext = cipher.encrypt(('84401f78fe6c10876d8ea23094ea5309').decode('hex'))
30 >>> (ciphertext).encode('hex')
31 '7b1f7c7e3b1c948ebd04a75ffba7d2f5'
32 >>> decipher = python_DES3.new(key, python_DES3.MODE_CBC, IV)
33 >>> plaintext = decipher.decrypt(ciphertext)
34 >>> (plaintext).encode('hex')
35 '84401f78fe6c10876d8ea23094ea5309'
37 CMAC TDES-EDE3 EXAMPLE:
38 -------------
39 testvector: http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
41 >>> key = '8aa83bf8cbda10620bc1bf19fbb6cd58bc313d4a371ca8b5'.decode('hex')
42 >>> plaintext = '6bc1bee22e409f96e93d7e117393172aae2d8a57'.decode('hex')
43 >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC)
44 >>> cipher.encrypt(plaintext).encode('hex')
45 '743ddbe0ce2dc2ed'
47 CMAC TDES-EDE2 EXAMPLE:
48 -----------------------
49 testvector: http://csrc.nist.gov/groups/STM/cavp/documents/mac/cmactestvectors.zip
51 >>> key1 = "5104f2c76180c1d3".decode('hex')
52 >>> key2 = "b9df763e31ada716".decode('hex')
53 >>> key = key1 + key2
54 >>> plaintext = 'a6866be2fa6678f264a19c4474968e3f4eec24f5086d'.decode('hex')
55 >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC)
56 >>> cipher.encrypt(plaintext).encode('hex')
57 '32e7758f3f614dbf'"""
58 return python_DES3(key,mode,IV,counter,segment_size)
60 class python_DES3(BlockCipher):
61 key_error_message = "Key should be 128 or 192 bits"
63 def __init__(self,key,mode,IV,counter,segment_size):
64 cipher_module = pyDes.triple_des
65 self.blocksize = 8
66 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size)
68 def keylen_valid(self,key):
69 return len(key) in (16,24)
71 def _test():
72 import doctest
73 doctest.testmod()
75 if __name__ == "__main__":
76 _test()