Skip to content

Commit 62d27e2

Browse files
author
auxten
committed
Add padding str check
1 parent b013270 commit 62d27e2

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

pycovenantsql/e2ee.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,65 @@
77
BLOCK_SIZE = AES.block_size # Bytes
88
pad = lambda s: s + ((BLOCK_SIZE - len(s) % BLOCK_SIZE) *
99
chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)).encode('utf-8')
10+
11+
1012
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
1113

1214
salt = unhexlify("3fb8877d37fdc04e4a4765EFb8ab7d36")
1315

16+
class PaddingError(Exception):
17+
"""Exception raised for errors in the padding.
18+
19+
Attributes:
20+
message -- explanation of the error
21+
"""
22+
23+
def __init__(self, message):
24+
self.message = message
25+
26+
27+
def unpad(s):
28+
inLen = len(s)
29+
if inLen == 0:
30+
raise PaddingError("empty input")
31+
padChar = s[-1]
32+
padLen = ord(s[inLen-1:])
33+
if padLen > BLOCK_SIZE:
34+
raise PaddingError("padding length > 16")
35+
for i in s[inLen-padLen:]:
36+
if i != padChar:
37+
raise PaddingError("unknown padding char")
38+
return s[:-padLen]
39+
1440

1541
# kdf does 2 times sha256 and takes the first 16 bytes
1642
def kdf(raw_key):
17-
return hashlib.sha256(hashlib.sha256(raw_key.encode('utf-8') + salt).digest()).digest()[:16]
43+
return hashlib.sha256(hashlib.sha256(raw_key + salt).digest()).digest()[:16]
1844

1945

2046
def encrypt(raw, password):
47+
"""
48+
encrypt encrypts data with given password by AES-128-CBC PKCS#7, iv will be placed
49+
at head of cipher data.
50+
51+
:param raw: input raw byte array
52+
:param password: password byte array
53+
:return: encrypted byte array
54+
"""
2155
iv = Random.new().read(AES.block_size)
2256
cipher = AES.new(kdf(password), AES.MODE_CBC, iv)
2357
return iv + cipher.encrypt(pad(raw))
2458

2559

2660
def decrypt(enc, password):
61+
"""
62+
decrypt decrypts data with given password by AES-128-CBC PKCS#7. iv will be read from
63+
the head of raw.
64+
65+
:param enc: input encrypted byte array
66+
:param password: password byte array
67+
:return: decrypted byte array
68+
"""
2769
iv = enc[:16]
2870
cipher = AES.new(kdf(password), AES.MODE_CBC, iv)
2971
return unpad(cipher.decrypt(enc[16:]))

pycovenantsql/tests/test_e2ee.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def test_enc_dec(self):
6060
for case in cases:
6161
print("Case: #" + str(i))
6262
i += 1
63-
enc = encrypt(unhexlify(case["raw"]), case["pass"])
64-
dec = decrypt(enc, case["pass"])
63+
enc = encrypt(unhexlify(case["raw"]), case["pass"].encode())
64+
dec = decrypt(enc, case["pass"].encode())
6565
self.assertEqual(unhexlify(case["raw"]), dec)
66-
dec2 = decrypt(unhexlify(case["possibleEncrypted"]), case["pass"])
66+
dec2 = decrypt(unhexlify(case["possibleEncrypted"]), case["pass"].encode())
6767
self.assertEqual(unhexlify(case["raw"]), dec2)

0 commit comments

Comments
 (0)