|
28 | 28 |
|
29 | 29 | class ECEException(Exception): |
30 | 30 | """Exception for ECE encryption functions""" |
31 | | - pass |
| 31 | + def __init__(self, message): |
| 32 | + self.message = message |
32 | 33 |
|
33 | 34 | # TODO: turn this into a class so that we don't grow/stomp keys. |
34 | 35 |
|
@@ -78,7 +79,7 @@ def length_prefix(key): |
78 | 79 | else: |
79 | 80 | raise ECEException(u"unknown 'mode' specified: " + mode) |
80 | 81 | if version == "aes128gcm": |
81 | | - context = "WebPush: info\x00" + receiver_pub_key + sender_pub_key |
| 82 | + context = b"WebPush: info\x00" + receiver_pub_key + sender_pub_key |
82 | 83 | else: |
83 | 84 | label = labels.get(keyid, 'P-256').encode('utf-8') |
84 | 85 | context = (label + b"\0" + length_prefix(receiver_pub_key) + |
@@ -192,7 +193,7 @@ def parse_content_header(content): |
192 | 193 | :type content: str |
193 | 194 |
|
194 | 195 | """ |
195 | | - id_len = struct.unpack("!B", content[20])[0] |
| 196 | + id_len = struct.unpack("!B", content[20:21])[0] |
196 | 197 | return { |
197 | 198 | "salt": content[:16], |
198 | 199 | "rs": struct.unpack("!L", content[16:20])[0], |
@@ -281,13 +282,14 @@ def encrypt(content, salt=None, key=None, keyid=None, dh=None, rs=4096, |
281 | 282 | :rtype str |
282 | 283 |
|
283 | 284 | """ |
284 | | - def encrypt_record(key, nonce, counter, buffer): |
| 285 | + def encrypt_record(key, nonce, counter, buf): |
285 | 286 | encryptor = Cipher( |
286 | 287 | algorithms.AES(key), |
287 | 288 | modes.GCM(iv(nonce, counter)), |
288 | 289 | backend=default_backend() |
289 | 290 | ).encryptor() |
290 | | - data = encryptor.update(b"\0\0" + buffer) + encryptor.finalize() |
| 291 | + data = encryptor.update(b"\0\0" + buf) |
| 292 | + data += encryptor.finalize() |
291 | 293 | data += encryptor.tag |
292 | 294 | return data |
293 | 295 |
|
|
0 commit comments