-
Notifications
You must be signed in to change notification settings - Fork 3
/
Modes.py
117 lines (87 loc) · 3.03 KB
/
Modes.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
The Modes module has several wrapper classes, which add
a specific ciphering mode to a ECB-only class
"""
from Crypto.Util.strxor import strxor
class CBC(object):
"""
Cipher Block Chaining (`CBC`) wrapper
CBC is defined by the following formula:
C[i] = Encrypt(P[i] ^ C[i-1]), C[0] = IV
P[i] = Decrypt(C[i]) ^ C[i-1], C[0] = IV
Usage:
cipher = CBC(MyBlockCipher(key), IV=iv)
The result should be the same as:
cipher = MyBlockCipher(key, mode=MODE_CBC, IV=iv)
"""
def __init__(self, cipher, **kwargs):
self.cipher = cipher
self.IV = kwargs["IV"]
def encrypt(self, data):
result = b''
for o in range(0, len(data), self.cipher.block_size):
blk = data[o:o+self.cipher.block_size]
self.IV = self.cipher.encrypt(strxor(self.IV, blk))
result += self.IV
return result
def decrypt(self, data):
result = b''
for o in range(0, len(data), self.cipher.block_size):
blk = data[o:o+self.cipher.block_size]
res = strxor(self.cipher.decrypt(blk), self.IV)
self.IV = blk
result += res
return result
class CFB(object):
"""
Cipher FeedBack (`CFB`) mode wrapper.
CFB is defined by the following formula:
C[i] = Encrypt(C[i-1]) ^ P[i], C[0] = IV
P[i] = Encrypt(C[i-1]) ^ C[i], C[0] = IV
Usage:
cipher = CFB(MyBlockCipher(key), IV=iv)
The result should be the same as:
cipher = MyBlockCipher(key, mode=MODE_CFB, IV=iv, segment_size=128)
"""
def __init__(self, cipher, **kwargs):
self.cipher = cipher
self.IV = kwargs["IV"]
def encrypt(self, data):
result = b''
for o in range(0, len(data), self.cipher.block_size):
blk = data[o:o+self.cipher.block_size]
self.IV = strxor(self.cipher.encrypt(self.IV), blk)
result += self.IV
return result
def decrypt(self, data):
result = b''
for o in range(0, len(data), self.cipher.block_size):
blk = data[o:o+self.cipher.block_size]
res = strxor(self.cipher.encrypt(self.IV), blk)
self.IV = blk
result += res
return result
class OFB(object):
"""
Output FeedBack (`OFB`) mode wrapper.
CFB is defined by the following formula:
O[-1] = IV
O[j] = Encrypt(O[j-1])
C[j] = P[j] ^ O[j]
Usage:
cipher = OFB(MyBlockCipher(key), IV=iv)
The result should be the same as:
cipher = MyBlockCipher(key, mode=MODE_OFB, IV=iv)
"""
def __init__(self, cipher, **kwargs):
self.cipher = cipher
self.IV = kwargs["IV"]
def encrypt(self, data):
result = b''
for o in range(0, len(data), self.cipher.block_size):
self.IV = self.cipher.encrypt(self.IV)
blk = data[o:o+self.cipher.block_size]
result += strxor(blk, self.IV)
return result
def decrypt(self, data):
return self.encrypt(data)