-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmodes.py
160 lines (126 loc) · 6.25 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from iterators import eof_signal_iterator
""" Classes implementing modes of encryption:
* We can extend this, currently only ECB, CBC and CTR is supported
"""
class ModeOfOperation():
def __init__(self, cipher, iv = None, nonce = None):
self.cipher = cipher
self.iv = iv
self.nonce = nonce
self.block_size = cipher.block_size
def _xor(self, block1, block2):
return bytes([a ^ b for a, b in zip(block1, block2)])
class ECB(ModeOfOperation):
def __init__(self, cipher, padding_scheme):
super(ECB, self).__init__(cipher)
self.padding_scheme = padding_scheme
def encrypt(self, block_iterator):
# Wrap file / list iterator inside eof_signal_iterator
eof_iterator = eof_signal_iterator(block_iterator)
for data, eof in eof_iterator:
if not eof:
ciphertext = self.cipher.encrypt_block(data)
else:
block = data if not eof else self.padding_scheme.apply(data)
# Padding should return 1 or 2 blocks
if len(block) == self.block_size:
ciphertext = self.cipher.encrypt_block(block)
elif len(block) == self.block_size * 2:
ciphertext = self.cipher.encrypt_block(block[:self.block_size]) \
+ self.cipher.encrypt_block(block[self.block_size:])
else:
raise Exception("Padding error: Padding scheme returned data that is not a multiple of the block length")
yield ciphertext
def decrypt(self, block_iterator):
# Wrap file / list iterator inside eof_signal_iterator
eof_iterator = eof_signal_iterator(block_iterator)
for data, eof in eof_iterator:
plaintext = self.cipher.decrypt_block(data)
block = plaintext if not eof else self.padding_scheme.remove(plaintext)
yield block
#CBC mode implemented by Lucas V. Araujo <https://github.com/LvMalware/>
#CBC encrypts each block xor'd against the cipher text of the previous block.
#The first block is xor'd against a 0th block, the initialization vector (IV)
class CBC(ModeOfOperation):
def __init__(self, cipher, iv, padding_scheme):
super(CBC, self).__init__(cipher=cipher, iv=iv)
#initialize cipher_block with value None
self.cipher_block = None
self.padding_scheme = padding_scheme
def encrypt(self, block_iterator):
eof_iterator = eof_signal_iterator(block_iterator)
for data, eof in eof_iterator:
if not self.cipher_block:
#executed only once, on the first iteration
self.cipher_block = self.iv
#just return the IV, to be used as the first 64 bytes of the file
yield self.cipher_block
if not eof:
self.cipher_block = self.cipher.encrypt_block(
self._xor(data, self.cipher_block)
)
else:
#executed only once, for the last block of the file
block = data if not eof else self.padding_scheme.apply(data)
if len(block) == self.block_size:
self.cipher_block = self.cipher.encrypt_block(
self._xor(block, self.cipher_block)
)
elif len(block) == 2 * self.block_size:
last_block = self.cipher.encrypt_block(
self._xor(block[:self.block_size], self.cipher_block)
)
#This will append an entire block of padding (??)
self.cipher_block = self.cipher.encrypt_block(
self._xor(block[self.block_size:], last_block)
)
#set the cipher_block variable to be last block of real data
#prepended to the extra block of padding
self.cipher_block = last_block + self.cipher_block
else:
raise Exception("Padding error: Padding scheme returned " +
"data that is not a multiple of the block length"
)
yield self.cipher_block
def decrypt(self, block_iterator):
eof_iterator = eof_signal_iterator(block_iterator)
#Always get the first 64 bytes of the data as IV. Even if it was already
#supplied on the command line
self.cipher_block, eof = next(eof_iterator)
for data, eof in eof_iterator:
plaintext = self._xor(
self.cipher.decrypt_block(data), self.cipher_block
)
self.cipher_block = data
block = plaintext if not eof else \
self.padding_scheme.remove(plaintext)
yield block
"""
CTR (counter) mode implemented by Niklas Mollenhauer <https://github.com/nikeee>.
For more information, visit https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)
"""
class CTR(ModeOfOperation):
def __init__(self, cipher, nonce):
super(CTR, self).__init__(cipher)
self.nonce = nonce
def _get_xor_block(self, counter):
# len(nonce + counter) should be equal to the block size
# Since len(nonce) is block_size/2, len(counter) should also be block_size/2 (and therefore, len(nonce))
counter_bytes = counter.to_bytes(len(self.nonce), byteorder='big')
nonce_and_counter = self.nonce + counter_bytes
assert len(nonce_and_counter) == self.block_size, 'Nonce and counter must be the same size as the block size.'
return self.cipher.encrypt_block(nonce_and_counter)
def encrypt(self, block_iterator):
counter = 0
eof_iterator = eof_signal_iterator(block_iterator)
for data, eof in eof_iterator:
xor_block = self._get_xor_block(counter)
if eof and len(data) < self.block_size:
xor_block = xor_block[:len(data)]
ciphertext = self._xor(data, xor_block)
counter += 1
yield ciphertext
def decrypt(self, block_iterator):
# Decryption actually performs the same steps as encryption.
# We also don't have to remove any padding. Therefore, we can just use the encryption procedure.
return self.encrypt(block_iterator)