-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpython_PRESENT.py
More file actions
95 lines (80 loc) · 4.12 KB
/
python_PRESENT.py
File metadata and controls
95 lines (80 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from .blockcipher import *
from .pypresent import Present
def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None,rounds=32):
"""Create a new cipher object
Wrapper for pure python implementation rijndael.py
key = raw string containing the key, AES-128..256 will be selected according to the key length
mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
-> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
IV = IV as a raw string, default is "all zero" IV
-> needed for CBC, CFB and OFB mode
counter = counter object (CryptoPlus.Util.util.Counter)
-> only needed for CTR mode
-> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
see CTR example further on in the docstring
rounds = amount of rounds
segment_size = amount of bits to use from the keystream in each chain part
-> supported values: multiple of 8 between 8 and the blocksize
of the cipher (only per byte access possible), default is 8
-> only needed for CFB mode
rounds = amount of rounds, default = 32
Notes:
- Always construct a seperate cipher object for encryption and decryption. Once a cipher object has been used for encryption,
it can't be used for decryption because it keeps a state (if necessary) for the IV.
EXAMPLES:
**********
IMPORTING:
-----------
>>> import codecs
>>> from CryptoPlus.Cipher import python_PRESENT
ECB Test Vectors:
------------------
>>> key = codecs.decode("00000000000000000000", 'hex')
>>> plain = codecs.decode("0000000000000000", 'hex')
>>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
>>> codecs.encode(cipher.encrypt(plain), 'hex')
b'5579c1387b228445'
>>> key = codecs.decode("00000000000000000000000000000000", 'hex')
>>> plain = codecs.decode("0000000000000000", 'hex')
>>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
>>> codecs.encode(cipher.encrypt(plain), 'hex')
b'59a27d01607ebf05'
>>> key = codecs.decode("00000000000000000000", 'hex')
>>> plain = codecs.decode("0000000000000000", 'hex')
>>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64)
>>> codecs.encode(cipher.encrypt(plain), 'hex')
b'13991dd588bc1288'
Test Vectors for maximum rounds supported by PRESENT reference C code:
-----------------------------------------------------------------------
>>> key = codecs.decode("0123456789abcdef0123", 'hex')
>>> plain = codecs.decode("0123456789abcdef", 'hex')
>>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
>>> ciphertext = cipher.encrypt(plain)
>>> codecs.encode(ciphertext, 'hex')
b'a140dc5d7175ca20'
>>> codecs.encode(cipher.decrypt(ciphertext), 'hex')
b'0123456789abcdef'
>>> key = codecs.decode("0123456789abcdef0123456789abcdef", 'hex')
>>> plain = codecs.decode("0123456789abcdef", 'hex')
>>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534)
>>> ciphertext = cipher.encrypt(plain)
>>> codecs.encode(ciphertext, 'hex')
b'21007772e5d4ef14'
>>> codecs.encode(cipher.decrypt(ciphertext), 'hex')
b'0123456789abcdef'
"""
return python_PRESENT(key,mode,IV,counter,rounds,segment_size)
class python_PRESENT(BlockCipher):
key_error_message = "Key should be 80 or 128 bits"
def __init__(self,key,mode,IV,counter,rounds,segment_size):
cipher_module = Present
args = {'rounds':rounds}
self.blocksize = 8
BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args)
def keylen_valid(self,key):
return len(key) in (10,16)
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()