mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / python_PRESENT.py
blobb400d6ff0c7c88da3fd6e768e85e6e7d5ad5991a
1 from blockcipher import *
2 from pypresent import Present
4 def new(key,mode=MODE_ECB,IV=None,counter=None,rounds=32):
5 """Create a new cipher object
7 Wrapper for pure python implementation rijndael.py
9 new(key,mode=MODE_ECB,IV=None,counter=None,rounds=32):
10 key = raw string containing the key, AES-128..256 will be selected according to the key length
11 -> when using XTS mode: the key should be a tuple containing the 2 keys needed
12 mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB
13 -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
14 IV = IV as a raw string
15 -> needed for CBC, CFB and OFB mode
16 counter = counter object (CryptoPlus.Util.util.Counter)
17 -> only needed for CTR mode
18 -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
19 see CTR example further on in the docstring
20 rounds = amount of rounds
22 Notes:
23 - Always construct a seperate cipher object for encryption and decryption. Once a cipher object has been used for encryption,
24 it can't be used for decryption because it keeps a state (if necessary) for the IV.
27 ECB Test Vectors:
28 ------------------
29 >>> from CryptoPlus.Cipher import python_PRESENT
31 >>> key = "00000000000000000000".decode('hex')
32 >>> plain = "0000000000000000".decode('hex')
33 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
34 >>> cipher.encrypt(plain).encode('hex')
35 '5579c1387b228445'
37 >>> key = "00000000000000000000000000000000".decode('hex')
38 >>> plain = "0000000000000000".decode('hex')
39 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
40 >>> cipher.encrypt(plain).encode('hex')
41 '67d38fb0f5a371fd'
43 >>> key = "00000000000000000000".decode('hex')
44 >>> plain = "0000000000000000".decode('hex')
45 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
46 >>> cipher.encrypt(plain).encode('hex')
47 '13991dd588bc1288'
49 Test Vectors for maximum rounds supported by PRESENT reference C code:
50 -----------------------------------------------------------------------
51 >>> key = "0123456789abcdef0123".decode('hex')
52 >>> plain = "0123456789abcdef".decode('hex')
53 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
54 >>> ciphertext = cipher.encrypt(plain)
55 >>> ciphertext.encode('hex')
56 'a140dc5d7175ca20'
57 >>> cipher.decrypt(ciphertext).encode('hex')
58 '0123456789abcdef'
60 >>> key = "0123456789abcdef0123456789abcdef".decode('hex')
61 >>> plain = "0123456789abcdef".decode('hex')
62 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
63 >>> ciphertext = cipher.encrypt(plain)
64 >>> ciphertext.encode('hex')
65 'c913bc146bc89c4e'
66 >>> cipher.decrypt(ciphertext).encode('hex')
67 '0123456789abcdef'
68 """
69 return python_PRESENT(key,mode,IV,counter,rounds)
71 class python_PRESENT(BlockCipher):
72 def __init__(self,key,mode,IV,counter,rounds):
73 self.cipher = Present(key,rounds)
74 self.blocksize = 8
75 BlockCipher.__init__(self,key,mode,IV,counter)
77 def _test():
78 import doctest
79 doctest.testmod()
81 if __name__ == "__main__":
82 _test()