Skip to content

Commit 2ae2b9c

Browse files
committed
Address PR review feedback
1 parent 17f3332 commit 2ae2b9c

5 files changed

Lines changed: 75 additions & 13 deletions

File tree

scripts/build_ffi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ def build_ffi(local_wolfssl, features):
575575
int wc_ShaUpdate(wc_Sha*, const byte*, word32);
576576
int wc_ShaFinal(wc_Sha*, byte*);
577577
void wc_ShaFree(wc_Sha*);
578+
int wc_ShaCopy(wc_Sha*, wc_Sha*);
578579
"""
579580

580581
if features["SHA256"]:
@@ -584,6 +585,7 @@ def build_ffi(local_wolfssl, features):
584585
int wc_Sha256Update(wc_Sha256*, const byte*, word32);
585586
int wc_Sha256Final(wc_Sha256*, byte*);
586587
void wc_Sha256Free(wc_Sha256*);
588+
int wc_Sha256Copy(wc_Sha256*, wc_Sha256*);
587589
"""
588590

589591
if features["SHA384"]:
@@ -593,6 +595,7 @@ def build_ffi(local_wolfssl, features):
593595
int wc_Sha384Update(wc_Sha384*, const byte*, word32);
594596
int wc_Sha384Final(wc_Sha384*, byte*);
595597
void wc_Sha384Free(wc_Sha384*);
598+
int wc_Sha384Copy(wc_Sha384*, wc_Sha384*);
596599
"""
597600

598601
if features["SHA512"]:
@@ -603,6 +606,7 @@ def build_ffi(local_wolfssl, features):
603606
int wc_Sha512Update(wc_Sha512*, const byte*, word32);
604607
int wc_Sha512Final(wc_Sha512*, byte*);
605608
void wc_Sha512Free(wc_Sha512*);
609+
int wc_Sha512Copy(wc_Sha512*, wc_Sha512*);
606610
"""
607611
if features["SHA3"]:
608612
cdef += """
@@ -623,6 +627,10 @@ def build_ffi(local_wolfssl, features):
623627
void wc_Sha3_256_Free(wc_Sha3*);
624628
void wc_Sha3_384_Free(wc_Sha3*);
625629
void wc_Sha3_512_Free(wc_Sha3*);
630+
int wc_Sha3_224_Copy(wc_Sha3*, wc_Sha3*);
631+
int wc_Sha3_256_Copy(wc_Sha3*, wc_Sha3*);
632+
int wc_Sha3_384_Copy(wc_Sha3*, wc_Sha3*);
633+
int wc_Sha3_512_Copy(wc_Sha3*, wc_Sha3*);
626634
"""
627635

628636
if features["DES3"]:

tests/test_aesgcmstream.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,13 @@ def test_invalid_tag_bytes():
135135
# valid edge cases
136136
AesGcmStream(key, iv, tag_bytes=4)
137137
AesGcmStream(key, iv, tag_bytes=16)
138+
139+
def test_repeated_construction_destruction():
140+
import gc
141+
key = "fedcba9876543210"
142+
iv = "0123456789abcdef"
143+
for _ in range(1000):
144+
gcm = AesGcmStream(key, iv)
145+
gcm.encrypt("hello world")
146+
del gcm
147+
gc.collect()

tests/test_ciphers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,3 +898,15 @@ def test_chacha_non_block_aligned():
898898
def test_chacha_invalid_key_length():
899899
with pytest.raises(ValueError, match="key must be"):
900900
ChaCha(b"\x00" * 20)
901+
902+
903+
if _lib.RSA_ENABLED:
904+
def test_encrypt_oaep_requires_hash_type(vectors):
905+
rsa = RsaPublic(vectors[RsaPublic].key)
906+
with pytest.raises(WolfCryptError, match="Hash type not set"):
907+
rsa.encrypt_oaep(b"plaintext")
908+
909+
def test_decrypt_oaep_requires_hash_type(vectors):
910+
rsa = RsaPrivate(vectors[RsaPrivate].key)
911+
with pytest.raises(WolfCryptError, match="Hash type not set"):
912+
rsa.decrypt_oaep(b"\x00" * rsa.output_size)

wolfcrypt/ciphers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,6 @@ class AesGcmStream(object):
396396
block_size = 16
397397
_key_sizes = [16, 24, 32]
398398
_native_type = "Aes *"
399-
_aad = bytes()
400-
_tag_bytes = 16
401-
_mode = None
402399

403400
def __init__(self, key, IV, tag_bytes=16):
404401
"""
@@ -408,7 +405,9 @@ def __init__(self, key, IV, tag_bytes=16):
408405
IV = t2b(IV)
409406
if tag_bytes < 4 or tag_bytes > 16:
410407
raise ValueError("tag_bytes must be between 4 and 16")
408+
self._aad = bytes()
411409
self._tag_bytes = tag_bytes
410+
self._mode = None
412411
if len(key) not in self._key_sizes:
413412
raise ValueError("key must be %s in length, not %d" %
414413
(self._key_sizes, len(key)))

wolfcrypt/hashes.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ def copy(self):
5858
"""
5959
copy = self.new("")
6060

61-
_ffi.memmove(copy._native_object, # pylint: disable=protected-access
62-
self._native_object,
63-
self._native_size)
61+
if hasattr(self, '_copy'):
62+
ret = self._copy(self._native_object,
63+
copy._native_object) # pylint: disable=protected-access
64+
if ret < 0: # pragma: no cover
65+
raise WolfCryptError("Hash copy error (%d)" % ret)
66+
else:
67+
_ffi.memmove(copy._native_object, # pylint: disable=protected-access
68+
self._native_object,
69+
self._native_size)
6470

6571
return copy
6672

@@ -88,11 +94,21 @@ def digest(self):
8894
if self._native_object:
8995
obj = _ffi.new(self._native_type)
9096

91-
_ffi.memmove(obj, self._native_object, self._native_size)
92-
93-
ret = self._final(obj, result)
94-
if ret < 0: # pragma: no cover
95-
raise WolfCryptError("Hash finalize error (%d)" % ret)
97+
if hasattr(self, '_copy'):
98+
ret = self._copy(self._native_object, obj)
99+
if ret < 0: # pragma: no cover
100+
raise WolfCryptError("Hash copy error (%d)" % ret)
101+
else:
102+
_ffi.memmove(obj, self._native_object, self._native_size)
103+
104+
try:
105+
ret = self._final(obj, result)
106+
if ret < 0: # pragma: no cover
107+
raise WolfCryptError("Hash finalize error (%d)" % ret)
108+
finally:
109+
delete = getattr(self, '_delete', None)
110+
if delete:
111+
delete(obj)
96112

97113
return _ffi.buffer(result, self.digest_size)[:]
98114

@@ -117,6 +133,7 @@ class Sha(_Hash):
117133
_native_type = "wc_Sha *"
118134
_native_size = _ffi.sizeof("wc_Sha")
119135
_delete = _lib.wc_ShaFree
136+
_copy = _lib.wc_ShaCopy
120137

121138
def __del__(self):
122139
self._delete(self._native_object)
@@ -143,6 +160,7 @@ class Sha256(_Hash):
143160
_native_type = "wc_Sha256 *"
144161
_native_size = _ffi.sizeof("wc_Sha256")
145162
_delete = _lib.wc_Sha256Free
163+
_copy = _lib.wc_Sha256Copy
146164

147165
def __del__(self):
148166
self._delete(self._native_object)
@@ -169,6 +187,7 @@ class Sha384(_Hash):
169187
_native_type = "wc_Sha384 *"
170188
_native_size = _ffi.sizeof("wc_Sha384")
171189
_delete = _lib.wc_Sha384Free
190+
_copy = _lib.wc_Sha384Copy
172191

173192
def __del__(self):
174193
self._delete(self._native_object)
@@ -195,6 +214,7 @@ class Sha512(_Hash):
195214
_native_type = "wc_Sha512 *"
196215
_native_size = _ffi.sizeof("wc_Sha512")
197216
_delete = _lib.wc_Sha512Free
217+
_copy = _lib.wc_Sha512Copy
198218

199219
def __del__(self):
200220
self._delete(self._native_object)
@@ -232,14 +252,22 @@ class Sha3(_Hash):
232252
64: _lib.wc_Sha3_512_Free,
233253
}
234254

255+
_SHA3_COPY = {
256+
28: _lib.wc_Sha3_224_Copy,
257+
32: _lib.wc_Sha3_256_Copy,
258+
48: _lib.wc_Sha3_384_Copy,
259+
64: _lib.wc_Sha3_512_Copy,
260+
}
261+
235262
def __del__(self):
236-
if hasattr(self, '_delete'):
263+
if getattr(self, '_delete', None):
237264
self._delete(self._native_object)
238265

239266
def __init__(self, string=None, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231
240267
self._native_object = _ffi.new(self._native_type)
241268
self.digest_size = size
242269
self._delete = self._SHA3_FREE.get(size)
270+
self._copy = self._SHA3_COPY.get(size)
243271
ret = self._init()
244272
if ret < 0: # pragma: no cover
245273
raise WolfCryptError("Sha3 init error (%d)" % ret)
@@ -252,7 +280,12 @@ def new(cls, string=None, size=SHA3_384_DIGEST_SIZE):
252280

253281
def copy(self):
254282
c = Sha3(size=self.digest_size)
255-
_ffi.memmove(c._native_object, self._native_object, self._native_size)
283+
if self._copy:
284+
ret = self._copy(self._native_object, c._native_object)
285+
if ret < 0: # pragma: no cover
286+
raise WolfCryptError("Hash copy error (%d)" % ret)
287+
else:
288+
_ffi.memmove(c._native_object, self._native_object, self._native_size)
256289
return c
257290

258291
def _init(self):

0 commit comments

Comments
 (0)