mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / DES3.py
blob7e9155a8515d142e3b2c2ed28b32d7100db9ec30
1 from blockcipher import *
2 import Crypto.Cipher.DES3
4 def new(key,mode=MODE_ECB,IV=None,counter=None):
5 """Create a new cipher object
7 DES using pycrypto for algo and pycryptoplus for ciphermode
9 new(key,mode=MODE_ECB,IV=None,counter=None):
10 key = raw string containing the 2/3 keys
11 - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2
12 - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3
13 mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
14 IV = IV as a raw string
15 -> only needed for CBC mode
16 counter = counter object (CryptoPlus.Util.util.Counter)
17 -> only needed for CTR mode
20 CBC TDES-EDE3 EXAMPLE: (using test vectors from http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf)
21 ------------
22 >>> import DES3
23 >>> from binascii import hexlify, unhexlify
24 >>> key = unhexlify('37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae')
25 >>> IV = unhexlify('3d1de3cc132e3b65')
26 >>> cipher = DES3.new(key, DES3.MODE_CBC, IV)
27 >>> ciphertext = cipher.encrypt(unhexlify('84401f78fe6c10876d8ea23094ea5309'))
28 >>> hexlify(ciphertext)
29 '7b1f7c7e3b1c948ebd04a75ffba7d2f5'
30 >>> decipher = DES3.new(key, DES3.MODE_CBC, IV)
31 >>> plaintext = decipher.decrypt(ciphertext)
32 >>> hexlify(plaintext)
33 '84401f78fe6c10876d8ea23094ea5309'
35 CMAC TDES-EDE3 EXAMPLE: (http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf)
36 -------------
37 >>> key = '8aa83bf8cbda10620bc1bf19fbb6cd58bc313d4a371ca8b5'.decode('hex')
38 >>> plaintext = '6bc1bee22e409f96e93d7e117393172aae2d8a57'.decode('hex')
39 >>> cipher = DES3.new(key, DES3.MODE_CMAC)
40 >>> cipher.encrypt(plaintext).encode('hex')
41 '743ddbe0ce2dc2ed'
43 CMAC TDES-EDE2 EXAMPLE:
44 -----------------------
45 testvector: http://csrc.nist.gov/groups/STM/cavp/documents/mac/cmactestvectors.zip
47 >>> key1 = "5104f2c76180c1d3".decode('hex')
48 >>> key2 = "b9df763e31ada716".decode('hex')
49 >>> key = key1 + key2
50 >>> plaintext = 'a6866be2fa6678f264a19c4474968e3f4eec24f5086d'.decode('hex')
51 >>> cipher = DES3.new(key, DES3.MODE_CMAC)
52 >>> cipher.encrypt(plaintext).encode('hex')
53 '32e7758f3f614dbf'
54 """
55 return DES3(key,mode,IV,counter)
57 class DES3(BlockCipher):
58 def __init__(self,key,mode,IV,counter):
59 self.cipher = Crypto.Cipher.DES3.new(key)
60 self.blocksize = Crypto.Cipher.DES3.block_size
61 BlockCipher.__init__(self,key,mode,IV,counter)
63 def _test():
64 import doctest
65 doctest.testmod()
67 if __name__ == "__main__":
68 _test()