-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.py
284 lines (240 loc) · 8.29 KB
/
encryption.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
from logging import debug
from libutils import run_cmd
from metastream import EncStream
from bryckrecovery import EncRecovery
from os import path,cpu_count,mkdir
import multiprocessing as mp
import concurrent.futures
from itertools import repeat
from datetime import datetime
from os.path import dirname, exists
from json import load
config = dirname(__file__) + "/config.json"
with open(config) as cfg:
config = load(cfg)
def parallel_exe(func_name,*values, num_threads = int(0.8*mp.cpu_count())):
with concurrent.futures.ThreadPoolExecutor(max_workers = num_threads) as executor:
results = executor.map(func_name, *values)
return results
def encrypt_is_enabled(drives):
for drive in drives:
cmd = "sudo blkid " + drive
rc, out, err = run_cmd(cmd)
if rc:
continue
if "crypto_LUKS" in out:
return True
return False
def encrypt_setup_drive(drive, key_path):
"""setups encryption on a drive.
Args:
drive: Name of the drive that that needs to encrypted
key_path: path of the key file
Returns:
A tuple: return code, stderr, stdout
"""
debug("Setting up the encryption for Drive : " + drive)
cmd = "sudo cryptsetup -q luksFormat " + drive + " " + key_path + ""
return run_cmd(cmd)
def encrypt_unlock_drive(drive, key_path):
"""Unlocking an encrypted drive.
Args:
drive: Name of the drive that that needs to be unlocked
key_path: path of the key file
Returns:
A tuple: return code, stderr, stdout
"""
encrypt_drive = encrypt_drive_name(drive)
debug("Unlocking the drive " + encrypt_drive)
cmd = "sudo cryptsetup open --key-file " + key_path + " " + drive + \
" " + encrypt_drive + ""
rc, out, err = run_cmd(cmd)
if rc:
return rc, out, err,drive
cmd = "sudo partprobe"
run_cmd(cmd)
return 0, "", "", ""
def encrypt_lock_drive(drive):
"""Lock an encrypted drive
Args:
drive: Name of the drive to be locked
Returns:
A tuple: return code, stderr, stdout
"""
encrypt_drive = encrypt_drive_name(drive)
# deactivate any partitions in the drive
debug("Deactivating partitions on " + encrypt_drive)
cmd = "ls /dev/mapper/{}[p]*".format(encrypt_drive) #NVME partition p1 and p2
rc, out, err = run_cmd(cmd)
if rc:
cmd = "ls /dev/mapper/{}[1-2]".format(encrypt_drive) #SATA partition 1 and 2
rc, out, err = run_cmd(cmd)
if out:
parts = out.strip().rstrip("\n").split()
for part in parts:
cmd = "sudo dmsetup remove "+part
rc, out, err = run_cmd(cmd)
if rc:
return rc, out, err
debug("Locking the encrypted drive " + encrypt_drive)
cmd = "sudo cryptsetup close " + encrypt_drive + ""
return run_cmd(cmd)
def encrypt_reset_drive(drive):
"""resets encryption on a drive.
Args:
drive: Name of the drive to be reset
Returns:
A tuple: return code, stderr, stdout
"""
debug("Resetting the encryption on drive " + drive)
encrypt_lock_drive(drive)
encrypt_drive = encrypt_drive_name(drive)
cmd = "sudo cryptsetup remove " + encrypt_drive + ""
return run_cmd(cmd)
def encrypt_drive_name(drive):
"""Returns a name for the encrypted drive
Args:
drive: Name of the drive
Returns:
Logical name that represents the encrypted device
"""
return 'crypt' + drive.split('/')[-1]
def encrypt_reset_drives(drives, parallel=True):
debug("Setting encryption on drives:" + ','.join(drives))
errmsg = ""
outmsg = ""
return_code = 0
if(parallel):
results = parallel_exe(encrypt_reset_drive,drives)
else:
for drive in drives:
results.append(encrypt_reset_drive(drive))
for result in results:
rc, out, err = result
if rc:
errmsg += err
return_code = 1
outmsg += out
return return_code, outmsg, errmsg
def encrypt_setup_drives(drives, keyfile, parallel=True):
debug("Setting up encryption on drives:" + ','.join(drives))
errmsg = ""
outmsg = ""
return_code = 0
if(parallel):
values = drives,repeat(keyfile)
results = parallel_exe(encrypt_setup_drive,*values)
else:
for drive in drives:
results.append(encrypt_setup_drive(drive))
for result in results:
rc, out, err = result
if rc:
errmsg += err
return_code = 1
outmsg += out
return return_code, outmsg, errmsg
def encrypt_lock_drives(drives, parallel=True):
debug("Locking the drives:" + ','.join(drives))
errmsg = ""
outmsg = ""
return_code = 0
if(parallel):
results = parallel_exe(encrypt_lock_drive,drives)
else:
for drive in drives:
results.append(encrypt_lock_drive(drive))
for result in results:
rc, out, err = result
if rc:
errmsg += err
return_code = 1
outmsg += out
return return_code, outmsg, errmsg
def encrypt_change_key_drive(drive, old_key, new_key):
debug("Changing the keys for the drive:" + drive)
cmd = "sudo cryptsetup luksChangeKey {} --key-file {} {}".format(drive,
old_key, new_key)
return run_cmd(cmd)
def encrypt_change_key_drives(drives, old_key, new_key):
debug("Changing key for the drives:" + ','.join(drives))
errmsg = ""
outmsg = ""
return_code = 0
values = drives,repeat(old_key),repeat(new_key)
results = parallel_exe(encrypt_change_key_drive,*values)
for result in results:
rc, out, err = result
if rc:
errmsg += err
return_code = 1
outmsg += out
return return_code, outmsg, errmsg
def encrypt_unlock_drives(drives, keyfile, parallel=True):
debug("Unlocking the drives:" + ','.join(drives))
errmsg = ""
outmsg = ""
return_code = 0
results = []
errdrives = []
if(parallel):
values = drives,repeat(keyfile)
results = parallel_exe(encrypt_unlock_drive,*values)
else:
for drive in drives:
results.append(encrypt_unlock_drive(drive,keyfile))
for result in results:
rc, out, err, drive = result
if rc:
errmsg += err
return_code = 1
outmsg += out
if not drive is None:
errdrives.append(drive)
return return_code, outmsg, errmsg,errdrives
def encrypt_backup(directory, drives):
debug("Backup the encryption drives:" + ','.join(drives))
outmsg = ""
return_code = 0
results = []
stream = "estream.bin"
stream_type = "encryption"
enc_dir = directory + config['enc_dir']
if not exists(enc_dir):
mkdir(enc_dir)
file = directory + stream
enc_stream = EncStream(config['id'], stream_type, desc=None, filename=file)
for drive in drives:
results.append(enc_stream.backup_header(enc_dir,drive))
rc,msg = enc_stream.persist(stream_type)
if rc:
return 1, msg
for result in results:
rc, out = result
if rc:
return_code = 1
outmsg += out
return return_code, outmsg
def encrypt_recovery(directory,drives,key_file):
debug("Recovering the corrupted encryption drives ")
outmsg = ""
return_code = 0
results = []
stream_type = "encryption"
enc_rec = EncRecovery()
suc_count, err_count, err_files, types = enc_rec.read_streams(directory)
if stream_type in types:
for drive in drives:
results.append(enc_rec.restore_header(stream_type,drive))
for result in results:
rc, out = result
if rc:
return_code = 1
outmsg += out
lock, stdout, stderr, errdrives = encrypt_unlock_drives(drives, key_file)
if lock: # unlock fails
debug("Failed to recover : "+ ','.join(drives))
debug(stderr)
return 1, stderr
return return_code, "Recovered successfully from corruption"
return 1, "Backup file is not exists"