CFB: added support for "segmentsize"
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_PRESENT.py
blobfb53e43daa12043231f97229c419944dea65ce8f
1 from blockcipher import *
2 from pypresent import Present
4 def new(key,mode=MODE_ECB,IV=None,counter=None,rounds=32,segment_size=8):
5 """Create a new cipher object
7 Wrapper for pure python implementation rijndael.py
9 key = raw string containing the key, AES-128..256 will be selected according to the key length
10 mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
11 -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
12 IV = IV as a raw string, default is "all zero" IV
13 -> needed for CBC, CFB and OFB mode
14 counter = counter object (CryptoPlus.Util.util.Counter)
15 -> only needed for CTR mode
16 -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
17 see CTR example further on in the docstring
18 rounds = amount of rounds
19 rounds = amount of rounds, default = 32
21 Notes:
22 - Always construct a seperate cipher object for encryption and decryption. Once a cipher object has been used for encryption,
23 it can't be used for decryption because it keeps a state (if necessary) for the IV.
25 EXAMPLES:
26 **********
27 IMPORTING:
28 -----------
29 >>> from CryptoPlus.Cipher import python_PRESENT
31 ECB Test Vectors:
32 ------------------
33 >>> key = "00000000000000000000".decode('hex')
34 >>> plain = "0000000000000000".decode('hex')
35 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
36 >>> cipher.encrypt(plain).encode('hex')
37 '5579c1387b228445'
39 >>> key = "00000000000000000000000000000000".decode('hex')
40 >>> plain = "0000000000000000".decode('hex')
41 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
42 >>> cipher.encrypt(plain).encode('hex')
43 '59a27d01607ebf05'
45 >>> key = "00000000000000000000".decode('hex')
46 >>> plain = "0000000000000000".decode('hex')
47 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
48 >>> cipher.encrypt(plain).encode('hex')
49 '13991dd588bc1288'
51 Test Vectors for maximum rounds supported by PRESENT reference C code:
52 -----------------------------------------------------------------------
53 >>> key = "0123456789abcdef0123".decode('hex')
54 >>> plain = "0123456789abcdef".decode('hex')
55 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
56 >>> ciphertext = cipher.encrypt(plain)
57 >>> ciphertext.encode('hex')
58 'a140dc5d7175ca20'
59 >>> cipher.decrypt(ciphertext).encode('hex')
60 '0123456789abcdef'
62 >>> key = "0123456789abcdef0123456789abcdef".decode('hex')
63 >>> plain = "0123456789abcdef".decode('hex')
64 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
65 >>> ciphertext = cipher.encrypt(plain)
66 >>> ciphertext.encode('hex')
67 '21007772e5d4ef14'
68 >>> cipher.decrypt(ciphertext).encode('hex')
69 '0123456789abcdef'
70 """
71 return python_PRESENT(key,mode,IV,counter,rounds,segment_size)
73 class python_PRESENT(BlockCipher):
74 key_error_message = "Key should be 80 or 128 bits"
76 def __init__(self,key,mode,IV,counter,rounds,segment_size):
77 cipher_module = Present
78 args = {'rounds':rounds}
79 self.blocksize = 8
80 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args)
82 def keylen_valid(self,key):
83 return len(key) in (10,16)
85 def _test():
86 import doctest
87 doctest.testmod()
89 if __name__ == "__main__":
90 _test()