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
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.
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')
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')
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')
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')
57 >>> cipher.decrypt(ciphertext).encode('hex')
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')
66 >>> cipher.decrypt(ciphertext).encode('hex')
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
)
75 BlockCipher
.__init
__(self
,key
,mode
,IV
,counter
)
81 if __name__
== "__main__":