1 from blockcipher
import *
2 from pypresent
import Present
4 def new(key
,mode
=MODE_ECB
,IV
=None,counter
=None,segment_size
=None,rounds
=32):
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 segment_size = amount of bits to use from the keystream in each chain part
20 -> supported values: multiple of 8 between 8 and the blocksize
21 of the cipher (only per byte access possible), default is 8
22 -> only needed for CFB mode
23 rounds = amount of rounds, default = 32
26 - Always construct a seperate cipher object for encryption and decryption. Once a cipher object has been used for encryption,
27 it can't be used for decryption because it keeps a state (if necessary) for the IV.
33 >>> from CryptoPlus.Cipher import python_PRESENT
37 >>> key = "00000000000000000000".decode('hex')
38 >>> plain = "0000000000000000".decode('hex')
39 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
40 >>> cipher.encrypt(plain).encode('hex')
43 >>> key = "00000000000000000000000000000000".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 >>> key = "00000000000000000000".decode('hex')
50 >>> plain = "0000000000000000".decode('hex')
51 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
52 >>> cipher.encrypt(plain).encode('hex')
55 Test Vectors for maximum rounds supported by PRESENT reference C code:
56 -----------------------------------------------------------------------
57 >>> key = "0123456789abcdef0123".decode('hex')
58 >>> plain = "0123456789abcdef".decode('hex')
59 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
60 >>> ciphertext = cipher.encrypt(plain)
61 >>> ciphertext.encode('hex')
63 >>> cipher.decrypt(ciphertext).encode('hex')
66 >>> key = "0123456789abcdef0123456789abcdef".decode('hex')
67 >>> plain = "0123456789abcdef".decode('hex')
68 >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
69 >>> ciphertext = cipher.encrypt(plain)
70 >>> ciphertext.encode('hex')
72 >>> cipher.decrypt(ciphertext).encode('hex')
75 return python_PRESENT(key
,mode
,IV
,counter
,rounds
,segment_size
)
77 class python_PRESENT(BlockCipher
):
78 key_error_message
= "Key should be 80 or 128 bits"
80 def __init__(self
,key
,mode
,IV
,counter
,rounds
,segment_size
):
81 cipher_module
= Present
82 args
= {'rounds':rounds
}
84 BlockCipher
.__init
__(self
,key
,mode
,IV
,counter
,cipher_module
,segment_size
,args
)
86 def keylen_valid(self
,key
):
87 return len(key
) in (10,16)
93 if __name__
== "__main__":