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
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.
29 >>> from CryptoPlus.Cipher import python_PRESENT
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')
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')
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')
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')
59 >>> cipher.decrypt(ciphertext).encode('hex')
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')
68 >>> cipher.decrypt(ciphertext).encode('hex')
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
}
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)
89 if __name__
== "__main__":