CFB: added segment_size to docstring of the cipher wrappers
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_DES3.py
blob9123a97e0ed350f0dc008070fe444089b0a2373c
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
17 segment_size = amount of bits to use from the keystream in each chain part
18 -> supported values: multiple of 8 between 8 and the blocksize
19 of the cipher (only per byte access possible), default is 8
20 -> only needed for CFB mode
22 EXAMPLES:
23 **********
24 IMPORTING:
25 -----------
26 >>> from CryptoPlus.Cipher import python_DES3
28 CBC TDES-EDE3 EXAMPLE: (using test vectors from http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf)
29 ------------
30 >>> key = ('37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae').decode('hex')
31 >>> IV = ('3d1de3cc132e3b65').decode('hex')
32 >>> cipher = python_DES3.new(key, python_DES3.MODE_CBC, IV)
33 >>> ciphertext = cipher.encrypt(('84401f78fe6c10876d8ea23094ea5309').decode('hex'))
34 >>> (ciphertext).encode('hex')
35 '7b1f7c7e3b1c948ebd04a75ffba7d2f5'
36 >>> decipher = python_DES3.new(key, python_DES3.MODE_CBC, IV)
37 >>> plaintext = decipher.decrypt(ciphertext)
38 >>> (plaintext).encode('hex')
39 '84401f78fe6c10876d8ea23094ea5309'
41 CMAC TDES-EDE3 EXAMPLE:
42 -------------
43 testvector: http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
45 >>> key = '8aa83bf8cbda10620bc1bf19fbb6cd58bc313d4a371ca8b5'.decode('hex')
46 >>> plaintext = '6bc1bee22e409f96e93d7e117393172aae2d8a57'.decode('hex')
47 >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC)
48 >>> cipher.encrypt(plaintext).encode('hex')
49 '743ddbe0ce2dc2ed'
51 CMAC TDES-EDE2 EXAMPLE:
52 -----------------------
53 testvector: http://csrc.nist.gov/groups/STM/cavp/documents/mac/cmactestvectors.zip
55 >>> key1 = "5104f2c76180c1d3".decode('hex')
56 >>> key2 = "b9df763e31ada716".decode('hex')
57 >>> key = key1 + key2
58 >>> plaintext = 'a6866be2fa6678f264a19c4474968e3f4eec24f5086d'.decode('hex')
59 >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC)
60 >>> cipher.encrypt(plaintext).encode('hex')
61 '32e7758f3f614dbf'"""
62 return python_DES3(key,mode,IV,counter,segment_size)
64 class python_DES3(BlockCipher):
65 key_error_message = "Key should be 128 or 192 bits"
67 def __init__(self,key,mode,IV,counter,segment_size):
68 cipher_module = pyDes.triple_des
69 self.blocksize = 8
70 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size)
72 def keylen_valid(self,key):
73 return len(key) in (16,24)
75 def _test():
76 import doctest
77 doctest.testmod()
79 if __name__ == "__main__":
80 _test()