-
Notifications
You must be signed in to change notification settings - Fork 23
/
rncryptor.py
298 lines (203 loc) · 8.17 KB
/
rncryptor.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Python implementation of RNCryptor."""
from __future__ import print_function
import hashlib
import hmac
import sys
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Protocol import KDF
__all__ = ('RNCryptor', 'decrypt', 'encrypt')
__version__ = '3.3.0'
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY2:
def to_bytes(s):
if isinstance(s, str):
return s
if isinstance(s, unicode):
return s.encode('utf-8')
to_str = to_bytes
def bchr(s):
return chr(s)
def bord(s):
return ord(s)
elif PY3:
unicode = str # hack for pyflakes (https://bugs.launchpad.net/pyflakes/+bug/1585991)
def to_bytes(s):
if isinstance(s, bytes):
return s
if isinstance(s, str):
return s.encode('utf-8')
def to_str(s):
if isinstance(s, bytes):
return s.decode('utf-8')
if isinstance(s, str):
return s
def bchr(s):
return bytes([s])
def bord(s):
return s
if hasattr(hmac, 'compare_digest'):
def compare_in_constant_time(left, right):
return hmac.compare_digest(left, right)
else:
def compare_in_constant_time(left, right):
length_left = len(left)
length_right = len(right)
result = length_left - length_right
for i, byte in enumerate(right):
result |= bord(left[i % length_left]) ^ bord(byte)
return result == 0
compare_in_constant_time.__doc__ = """\
Compare two values in time proportional to the second one.
Return True if the values are equal, False otherwise.
"""
class RNCryptorError(Exception):
"""Base error for when anything goes wrong with RNCryptor."""
class DecryptionError(RNCryptorError):
"""Raised when bad data is inputted."""
class RNCryptor(object):
"""Cryptor for RNCryptor."""
SALT_SIZE = 8
IV_SIZE = 16
HMAC_SIZE = 32
def pre_decrypt_data(self, data):
"""Handle data before decryption."""
data = to_bytes(data)
return data
def post_decrypt_data(self, data):
"""Remove useless symbols which appear over padding for AES (PKCS#7)."""
data = data[:-bord(data[-1])]
return to_str(data)
def decrypt(self, data, password):
"""Decrypt `data` using `password`."""
data = self.pre_decrypt_data(data)
password = to_bytes(password)
n = len(data)
# version + options + encryption_salt + hmac_salt + iv + hmac
# = 1 + 1 + 8 + 8 + 16 + 32 = 66
min_length = 1 + 1 + self.SALT_SIZE + self.SALT_SIZE + self.IV_SIZE + self.HMAC_SIZE
if n < min_length:
# data doesn't even match the required header + hmac length
raise DecryptionError("Invalid length")
version = data[0]
if not version == b'\x03':
# required version is version 3
raise DecryptionError("Unsupported version")
options = data[1]
if not options == b'\x01':
# when using keys options should be `0`
raise DecryptionError("Invalid credential type")
encryption_salt = data[2:10]
hmac_salt = data[10:18]
iv = data[18:34]
cipher_text = data[34:n - 32]
hmac = data[n - 32:]
encryption_key = self._pbkdf2(password, encryption_salt)
hmac_key = self._pbkdf2(password, hmac_salt)
if not compare_in_constant_time(self._hmac(hmac_key, data[:n - 32]), hmac):
raise DecryptionError("Bad data")
decrypted_data = self._aes_decrypt(encryption_key, iv, cipher_text)
return self.post_decrypt_data(decrypted_data)
def decrypt_with_keys(self, data, hmac_key, encryption_key):
"""Decrypt `data` using `hmac_key` and `encryption_key`."""
data = self.pre_decrypt_data(data)
n = len(data)
# version + options + iv + hmac = 1 + 1 + 16 + 32 = 50
min_length = 1 + 1 + self.IV_SIZE + self.HMAC_SIZE
if n < min_length:
# data doesn't even match the required header + hmac length
raise DecryptionError("Invalid length")
version = data[0]
if not version == b'\x03':
# required version is version 3
raise DecryptionError("Unsupported version")
options = data[1]
if not options == b'\x00':
# when using keys options should be `0`
raise DecryptionError("Invalid credential type")
iv = data[2:18]
cipher_text = data[18:n - 32]
hmac = data[n - 32:]
if not compare_in_constant_time(self._hmac(hmac_key, data[:n - 32]), hmac):
raise DecryptionError("Bad data")
decrypted_data = self._aes_decrypt(encryption_key, iv, cipher_text)
return self.post_decrypt_data(decrypted_data)
def pre_encrypt_data(self, data):
"""Do padding for the data for AES (PKCS#7)."""
data = to_bytes(data)
aes_block_size = AES.block_size
rem = aes_block_size - len(data) % aes_block_size
return data + bchr(rem) * rem
def post_encrypt_data(self, data):
"""Handle data after encryption."""
return data
def encrypt(self, data, password):
"""Encrypt `data` using `password`."""
data = self.pre_encrypt_data(data)
password = to_bytes(password)
encryption_salt = self.encryption_salt
encryption_key = self._pbkdf2(password, encryption_salt)
hmac_salt = self.hmac_salt
hmac_key = self._pbkdf2(password, hmac_salt)
iv = self.iv
cipher_text = self._aes_encrypt(encryption_key, iv, data)
version = b'\x03'
options = b'\x01'
new_data = b''.join([version, options, encryption_salt, hmac_salt, iv, cipher_text])
encrypted_data = new_data + self._hmac(hmac_key, new_data)
return self.post_encrypt_data(encrypted_data)
def encrypt_with_keys(self, data, hmac_key, encryption_key):
"""Encrypt `data` using `hmac_key` and `encryption_key`."""
data = self.pre_encrypt_data(data)
iv = self.iv
cipher_text = self._aes_encrypt(encryption_key, iv, data)
version = b'\x03'
options = b'\x00'
new_data = b''.join([version, options, iv, cipher_text])
encrypted_data = new_data + self._hmac(hmac_key, new_data)
return self.post_encrypt_data(encrypted_data)
def make_key(self, password, salt):
"""Derive a key via PBKDF2 from `password` using `salt`."""
key = self._pbkdf2(password, salt)
return key
@property
def encryption_salt(self):
return Random.new().read(self.SALT_SIZE)
@property
def hmac_salt(self):
return Random.new().read(self.SALT_SIZE)
@property
def iv(self):
return Random.new().read(AES.block_size)
def _aes_encrypt(self, key, iv, text):
return AES.new(key, AES.MODE_CBC, iv).encrypt(text)
def _aes_decrypt(self, key, iv, text):
return AES.new(key, AES.MODE_CBC, iv).decrypt(text)
def _hmac(self, key, data):
return hmac.new(key, data, hashlib.sha256).digest()
def _prf(self, secret, salt):
return hmac.new(secret, salt, hashlib.sha1).digest()
def _pbkdf2(self, password, salt, iterations=10000, key_length=32):
return KDF.PBKDF2(password, salt, dkLen=key_length, count=iterations, prf=self._prf)
def decrypt(data, password):
cryptor = RNCryptor()
return cryptor.decrypt(data, password)
decrypt.__doc__ = RNCryptor.decrypt.__doc__
def encrypt(data, password):
cryptor = RNCryptor()
return cryptor.encrypt(data, password)
encrypt.__doc__ = RNCryptor.encrypt.__doc__
def encrypt_with_keys(data, hmac_key, encryption_key):
cryptor = RNCryptor()
return cryptor.encrypt_with_keys(data, hmac_key, encryption_key)
encrypt_with_keys.__doc__ = RNCryptor.encrypt_with_keys.__doc__
def decrypt_with_keys(data, hmac_key, encryption_key):
cryptor = RNCryptor()
return cryptor.decrypt_with_keys(data, hmac_key, encryption_key)
decrypt_with_keys.__doc__ = RNCryptor.decrypt_with_keys.__doc__
def make_key(password, salt):
cryptor = RNCryptor()
key = cryptor.make_key(password, salt)
return key
make_key.__doc__ = RNCryptor.make_key.__doc__