Skip to content

Commit b9072e6

Browse files
LinuxJedidanielinux
authored andcommitted
Add get_aad() function
1 parent fbefe7e commit b9072e6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

wolfcrypt/ciphers.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,36 +308,39 @@ def set_aad(self, data):
308308
raise WolfCryptError("AAD can only be set before encrypt() or decrypt() is called")
309309
self._aad = t2b(data)
310310

311+
def get_aad(self):
312+
return self._aad
313+
311314
def encrypt(self, data):
312315
"""
313316
Add more data to the encryption stream
314317
"""
315318
data = t2b(data)
319+
aad = bytes()
316320
if self._mode is None:
317321
self._mode = _ENCRYPTION
322+
aad = self._aad
318323
elif self._mode == _DECRYPTION:
319324
raise WolfCryptError("Class instance already in use for decryption")
320325
self._buf = _ffi.new("byte[%d]" % (len(data)))
321-
ret = _lib.wc_AesGcmEncryptUpdate(self._native_object, self._buf, data, len(data), self._aad, len(self._aad))
326+
ret = _lib.wc_AesGcmEncryptUpdate(self._native_object, self._buf, data, len(data), aad, len(aad))
322327
if ret < 0:
323328
raise WolfCryptError("Decryption error (%d)" % ret)
324-
# Reset aad after first packet
325-
self._aad = bytes()
326329
return bytes(self._buf)
327330

328331
def decrypt(self, data):
329332
"""
330333
Add more data to the decryption stream
331334
"""
335+
aad = bytes()
332336
data = t2b(data)
333337
if self._mode is None:
334338
self._mode = _DECRYPTION
339+
aad = self._aad
335340
elif self._mode == _ENCRYPTION:
336341
raise WolfCryptError("Class instance already in use for decryption")
337342
self._buf = _ffi.new("byte[%d]" % (len(data)))
338-
ret = _lib.wc_AesGcmDecryptUpdate(self._native_object, self._buf, data, len(data), self._aad, len(self._aad))
339-
# Reset after first packet
340-
self._aad = bytes()
343+
ret = _lib.wc_AesGcmDecryptUpdate(self._native_object, self._buf, data, len(data), aad, len(aad))
341344
if ret < 0:
342345
raise WolfCryptError("Decryption error (%d)" % ret)
343346
return bytes(self._buf)

0 commit comments

Comments
 (0)