-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcip_fileformat.py
486 lines (345 loc) · 13.8 KB
/
cip_fileformat.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env python3
"""
This file is part of Notelocked.
Copyright (C) 2022 Leonardo A. Reichert
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
#you can see the full license at: "LICENSE" file
#https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# Contact: [email protected]
# cip_fileformat or cipher module of program
#"text locked"(ltxt) file format and its encryption used
#we need: "pip install pycryptodome"
#or see https://www.pycryptodome.org/src/installation
from Crypto.Cipher import AES;
from Crypto.Util.Padding import pad, unpad;
from Crypto.Random import get_random_bytes;
from hashlib import algorithms_available;
from hashlib import new as newSha;
from sys import getdefaultencoding;
from sys import version_info as py_version;
from os.path import exists;
if py_version[0] < 3:
#Ok, I couldn't accept the way to encode bytes in Python 2
raise Exception("Need Python >= 3");
SHA_NAME = "sha256";
#SHA validator :
hashLenght = 64
#file descriptors:
ext = "ltxt";
format_name = "Locked Text";
VECTOR_LENGTH = 16;
#default value used by the system encode o decode local text plain:
default_encoding = getdefaultencoding();
####################
### HEADERS FILE ###
idFileIdentifier = b"_Locked_"*10; #80 unique bytes
idFileVersion = b"0.7"; #0.5 was the first version
idFileShaName = SHA_NAME.encode();
idFileModeCipher = b"MODE_CBC";
MIN_PASSWORD = 12;
MAX_PASSWORD = 64;
assert MAX_PASSWORD % 16 == 0; #mode cbc x16
############
## Errors ##
ERR_SUCCES = 0;
ERR_PASSWORD_INCORRECT = -1;
ERR_FILE_INCORRECT = -2;
ERR_FILE_CORRUPT = -3;
ERR_FILE_OPEN = -4; #cannot open
ERR_NEED_RETRY = -5; #an error ocurred
ERR_FILE_VERSION = -6; #file version
ERR_NOALGO_HASH = -7; #no have algorithm sha
ERR_FILE_NOEXISTS = -8;
def getMessageErrorString(err):
if err == ERR_PASSWORD_INCORRECT:
return "Password incorrect";
elif err == ERR_FILE_INCORRECT:
return "File incorrect, not have the correct identifier";
elif err == ERR_FILE_CORRUPT:
return "File corrupt can't open";
elif err == ERR_FILE_OPEN:
return "File cannot open";
elif err == ERR_NEED_RETRY:
return "An error ocurred, please retry";
elif err == ERR_FILE_VERSION:
return "Error: File version incorrect or file corrupt";
elif err == ERR_NOALGO_HASH: #no have algorithm sha
return "Error: hashlib no have algorithm for the hash";
elif err == ERR_FILE_NOEXISTS:
return "Error: file not exists";
return "Error unknown";
def existsAlgoHash(nameshafunc):
""" we have algorithm sha's in hashlib ? """
if nameshafunc in algorithms_available:
return True;
return False;
if not existsAlgoHash(SHA_NAME):
#no have algorithm on hashlib
raise Exception( getMessageErrorString(ERR_NOALGO_HASH) +": " + SHA_NAME );
def encrypt(sbytes, password, iv=None):
#CBC: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
#sbytes is str or bytes
if type(sbytes) == str:
sbytes = sbytes.encode(errors="replace");
sbytes = pad(sbytes, VECTOR_LENGTH, "iso7816"); #fill with 0x80.. 0 ...
#if len(password)%VECTOR_LENGTH:
# password = pad(password, VECTOR_LENGTH, "iso7816"); #+0x80.. 0 ...
#new versions >= 0.7, password to key cipher:
password = newSha(SHA_NAME, password+newSha(SHA_NAME, password).digest()).digest(); #32bytes
if not iv:
iv = get_random_bytes(VECTOR_LENGTH); #happens anyway
cip = AES.new(password, AES.MODE_CBC, iv);
return (cip.iv, cip.encrypt(sbytes));
def decrypt(iv, block, password, shaName=SHA_NAME, versionFile=idFileVersion):
#password type are bytes
if versionFile in (b"0.5", b"0.6"): #old versions 0.5, 0.6
if len(password)%VECTOR_LENGTH:
password = pad(password, VECTOR_LENGTH, "iso7816"); #+0x80.. 0 ...
else:
#new versions >= 0.7
password = newSha(shaName, password+newSha(shaName, password).digest()).digest(); #32bytes
cip = AES.new(password, AES.MODE_CBC, iv);
des = cip.decrypt(block);
try:
return unpad(des, VECTOR_LENGTH, "iso7816");
except:
return des;
def save_filename(filename, plain_text, password, text_encoding=default_encoding):
"""
paded used: iso7816
b'asd\x80\x00\x00..\x00'
save file bytes as:
+ unique identifier header 80 unique bytes
+ identifier file version
+ sha func name (usually "sha256")
+ hash of of password
+ name mode encryption (usually mode_cbc)
+ encoding used (usually "utf-8", paded to 25 bytes)
+ hash of (password+vector+encrypted)
+ random vector generated (usually 16 bytes)
return:
code == 0 ( Succes ),
getMessageErrorString(code)
"""
if type(plain_text) == str:
plain_text = plain_text.encode(text_encoding, errors="replace");
if type(password) == str:
password = password.encode();
try:
text_encoding = text_encoding.encode()
fp = open(filename, "wb");
hashPassword = newSha(SHA_NAME, password).hexdigest().encode();
######################################
vectorRandom, encrypted = encrypt(plain_text, password);
#fill with a hash for cheeck original content:
hashOriginality = newSha(SHA_NAME, password+vectorRandom+encrypted).hexdigest().encode();
fp.write(idFileIdentifier+b"\n");
fp.write(idFileVersion+b"\n");
fp.write(idFileShaName+b"\n");
fp.write(hashPassword+b"\n");
fp.write(idFileModeCipher+b"\n");
fp.write(text_encoding+b"\n");
fp.write(hashOriginality+b"\n");
fp.write(vectorRandom.hex().encode()+b"\n");
#double line separator
fp.write(b"\n"+encrypted);
fp.close();
except Exception as msg:
#print("msg", msg);
return ERR_NEED_RETRY;
return ERR_SUCCES;
def get_hash_saved(filename):
"""
return: (int: error code,
code == 0 ( Succes ),
getMessageErrorString(code)
str: name sha function (usually sha256),
str: hash representation of the user key on file)
"""
try:
file = open(filename, "rb");
except:
if not exists(filename):
return (ERR_FILE_NOEXISTS, "", "");
return (ERR_FILE_OPEN, "", "");
#no have the unique identifier of file?
if file.read(len(idFileIdentifier)+1) != idFileIdentifier+b"\n":
file.close();
return (ERR_FILE_INCORRECT, "", "");
metadata = b"";
while not metadata.endswith(b"\n\n"):
bchar = file.read(1);
metadata += bchar;
if not bchar:
file.close();
return (ERR_FILE_CORRUPT, "", "");
metadata = metadata.replace(b"\n\n", b""); #replace at the end
fversion = "";
try:
fversion, nameSha, hashPassword, metadata = metadata.split(b"\n", 3);
assert len(hashPassword) == hashLenght; #<- sha256?
except:
file.close();
if fversion and fversion != idFileVersion:
return (ERR_FILE_VERSION, "", ""); #version wrong ?
return (ERR_FILE_CORRUPT, "", "");
return (ERR_SUCCES, nameSha.decode(), hashPassword.decode());
def load_filename(filename, password):
"""
return [int: error code,
string plain text: decrypted file if password is correct,
bool: check True if content is original, calculated by the hash of
(password+vector+encrypted)
]
"""
try:
file = open(filename, "rb");
except:
if not exists(filename):
return (ERR_FILE_NOEXISTS, "", 0);
return (ERR_FILE_OPEN, "", 0);
#no have the unique identifier of file?
if file.read(len(idFileIdentifier)+1) != idFileIdentifier+b"\n":
file.close();
return (ERR_FILE_INCORRECT, "", 0);
metadata = b"";
while not metadata.endswith(b"\n\n"):
bchar = file.read(1);
metadata += bchar;
if not bchar:
file.close();
return (ERR_FILE_CORRUPT, "", 0);
metadata = metadata.replace(b"\n\n", b""); #replace at the end
try:
fver,nameSha,hashPassword,mode,encoding,hashContent,vector = metadata.split(b"\n");
assert len(hashPassword) == hashLenght; #assert while sha256
assert len(hashContent) == hashLenght;
vector = b"".fromhex(vector.decode());
assert len(vector) == VECTOR_LENGTH;
except (AssertionError, ValueError):
file.close();
if fver != idFileVersion:
return (ERR_FILE_VERSION, "", 0); #version wrong ?
return (ERR_FILE_CORRUPT, "", 0);
encrypted = file.read();
nameSha = nameSha.decode();
if not existsAlgoHash(nameSha):
file.close();
return (ERR_NOALGO_HASH, "", 0);
password = password.encode();
hashPassword = hashPassword.decode();
if newSha(nameSha, password).hexdigest() != hashPassword:
file.close();
return (ERR_PASSWORD_INCORRECT, "", 0);
encoding = encoding.decode();
try:
plainText = decrypt(vector, encrypted, password, nameSha, fver);
plainText = plainText.decode(encoding);
except:
return (ERR_FILE_CORRUPT, "", 0);
#check the isOriginallity is used to warn if the file is artificially changed by others
hashContent = hashContent.decode();
if fver == b"0.5": #old method 0.5
isOriginal = newSha(nameSha, password+encrypted).hexdigest() == hashContent;
else: #new method version >= 0.6
isOriginal = newSha(nameSha, password+vector+encrypted).hexdigest() == hashContent;
return (ERR_SUCCES, plainText, isOriginal);
'''
def _lot_updater(from_folder, new_foldername, passwords):
"""
Dev function, this is used when I need to update my existant files
First I update the save file function,
and after update the loader function
Basically it has helped me to update my files
when I have been changing things in the file format.
"""
from glob import glob;
from os.path import basename, splitext, exists;
from os import mkdir, getcwd;
print("The current dir is: %s" % getcwd());
print("Workin in folder: %s" % from_folder);
if not exists(new_foldername):
print(f"The directory {new_foldername} not exits, it will created..", end="");
mkdir(new_foldername);
print("ok");
new_foldername = new_foldername.removesuffix("/").removesuffix("\\");
print("Save new files in: %s\n" % new_foldername);
filenames = glob("%s/*.*" % from_folder);
print("%d files" % len(filenames))
toUpdate = [];
passwordFails = False;
for fname in filenames:
isOk = False;
errcode, nameSha, theHash = get_hash_saved(fname);
if errcode:
print(" Error %d " % errcode, getMessageErrorString(errcode), fname);
continue;
for psw in passwords:
if newSha(nameSha, psw.encode("utf-8")).hexdigest() == theHash:
#is password correct:
isOk = True;
break;
if not isOk:
passwordFails = True;
print(fname, " \tneed other password");
continue;
print(fname, " \tpassword correcta\t", psw);
toUpdate.append((fname, psw));
if passwordFails:
print("This test have incorrect passwords on input files.");
try:
#alarm sound, have incorret passwords
from win32api import Beep;
Beep(500, 500);
except ImportError:
pass;
else:
print("All password are correct on input files.");
if not toUpdate:
return;
if input("Enter \"y\" for continue with update all files to a new version: ").upper() != "Y":
return;
print("\n ** updating to new version, %d files ** \n" % len(toUpdate));
toTest = [];
for fname, psw in toUpdate:
errcode, plain_text, isOriginal = load_filename(fname, psw);
if errcode:
print("Error of file %s:" % fname, getMessageErrorString(errcode));
continue;
print("old ingetegrity:", "yes" if isOriginal else "not",
end="");
new_filename = new_foldername+"/"+basename(fname);
new_filename = splitext(new_filename)[0] + "." + ext;
errcode = save_filename(new_filename, plain_text, psw);
if not errcode:
print(" Success, saved on %s" % new_filename);
else:
print(" Error %d " % errcode, getMessageErrorString(errcode), new_filename);
continue;
toTest.append( (new_filename, psw) );
print("\n ** testing %d new files ** \n" % len(toTest) );
count = 0;
for fname, psw in toTest:
count += 1;
err = load_filename(fname, psw)[0];
if not err:
print("#%d ok:" % count, fname);
else:
print("#%d error:" % count, fname, getMessageErrorString(err));
input("end - pause");
_lot_updater("C:/old saves", "C:/new saves",
["mypassw", ],
);
'''
#end