Skip to content

Commit 17f3332

Browse files
committed
Add tests and fix build issues
1 parent c8574cc commit 17f3332

5 files changed

Lines changed: 67 additions & 25 deletions

File tree

scripts/build_ffi.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,14 @@ def generate_libwolfssl(fips):
304304

305305
def get_features(local_wolfssl, features):
306306
fips = False
307+
fips_file = None
307308

308-
if sys.platform == "win32":
309+
if local_wolfssl and sys.platform == "win32":
309310
# On Windows, we assume the local_wolfssl path is to a wolfSSL source
310311
# directory where the library has been built.
311312
fips_file = os.path.join(local_wolfssl, "wolfssl", "wolfcrypt",
312313
"fips.h")
313-
else:
314+
elif local_wolfssl:
314315
# On non-Windows platforms, first assume local_wolfssl is an
315316
# installation directory with an include subdirectory.
316317
fips_file = os.path.join(local_wolfssl, "include", "wolfssl",
@@ -320,7 +321,7 @@ def get_features(local_wolfssl, features):
320321
fips_file = os.path.join(local_wolfssl, "wolfssl", "wolfcrypt",
321322
"fips.h")
322323

323-
if os.path.exists(fips_file):
324+
if fips_file and os.path.exists(fips_file):
324325
with open(fips_file, "r") as f:
325326
contents = f.read()
326327
if not contents.isspace():
@@ -618,10 +619,10 @@ def build_ffi(local_wolfssl, features):
618619
int wc_Sha3_256_Final(wc_Sha3*, byte*);
619620
int wc_Sha3_384_Final(wc_Sha3*, byte*);
620621
int wc_Sha3_512_Final(wc_Sha3*, byte*);
621-
int wc_Sha3_224_Free(wc_Sha3*);
622-
int wc_Sha3_256_Free(wc_Sha3*);
623-
int wc_Sha3_384_Free(wc_Sha3*);
624-
int wc_Sha3_512_Free(wc_Sha3*);
622+
void wc_Sha3_224_Free(wc_Sha3*);
623+
void wc_Sha3_256_Free(wc_Sha3*);
624+
void wc_Sha3_384_Free(wc_Sha3*);
625+
void wc_Sha3_512_Free(wc_Sha3*);
625626
"""
626627

627628
if features["DES3"]:
@@ -1107,17 +1108,17 @@ def main(ffibuilder):
11071108
e = "Local wolfssl installation path {} doesn't exist.".format(local_wolfssl)
11081109
raise FileNotFoundError(e)
11091110

1110-
get_features(local_wolfssl, features)
1111-
1112-
if features["RSA_BLINDING"] and features["FIPS"]:
1113-
# These settings can't coexist. See settings.h.
1114-
features["RSA_BLINDING"] = 0
1115-
11161111
if not local_wolfssl:
11171112
print("Building wolfSSL...")
11181113
if not get_libwolfssl():
11191114
generate_libwolfssl(features["FIPS"])
11201115

1116+
get_features(local_wolfssl, features)
1117+
1118+
if features["RSA_BLINDING"] and features["FIPS"]:
1119+
# These settings can't coexist. See settings.h.
1120+
features["RSA_BLINDING"] = 0
1121+
11211122
build_ffi(local_wolfssl, features)
11221123

11231124

tests/test_aesgcmstream.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,16 @@ def test_encrypt_aad_bad():
122122
gcmdec.decrypt(buf)
123123
with pytest.raises(WolfCryptError):
124124
gcmdec.final(authTag)
125+
126+
def test_invalid_tag_bytes():
127+
key = "fedcba9876543210"
128+
iv = "0123456789abcdef"
129+
with pytest.raises(ValueError, match="tag_bytes must be between 4 and 16"):
130+
AesGcmStream(key, iv, tag_bytes=0)
131+
with pytest.raises(ValueError, match="tag_bytes must be between 4 and 16"):
132+
AesGcmStream(key, iv, tag_bytes=3)
133+
with pytest.raises(ValueError, match="tag_bytes must be between 4 and 16"):
134+
AesGcmStream(key, iv, tag_bytes=17)
135+
# valid edge cases
136+
AesGcmStream(key, iv, tag_bytes=4)
137+
AesGcmStream(key, iv, tag_bytes=16)

tests/test_ciphers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,29 @@ def test_aessiv_decrypt_kat_openssl():
872872
TEST_VECTOR_CIPHERTEXT_OPENSSL
873873
)
874874
assert plaintext == TEST_VECTOR_PLAINTEXT_OPENSSL
875+
876+
877+
if _lib.DES3_ENABLED:
878+
def test_des3_rejects_mode_ctr():
879+
key = b"\x01\x23\x45\x67\x89\xab\xcd\xef" * 3
880+
iv = b"\xfe\xdc\xba\x98\x76\x54\x32\x10"
881+
with pytest.raises(ValueError, match="Des3 only supports MODE_CBC"):
882+
Des3(key, MODE_CTR, iv)
883+
884+
885+
if _lib.CHACHA_ENABLED:
886+
def test_chacha_non_block_aligned():
887+
key = b"\x00" * 32
888+
chacha = ChaCha(key)
889+
chacha.set_iv(b"\x00" * 12)
890+
plaintext = b"This is 25 bytes of text!"
891+
assert len(plaintext) == 25
892+
ciphertext = chacha.encrypt(plaintext)
893+
assert len(ciphertext) == 25
894+
chacha2 = ChaCha(key)
895+
chacha2.set_iv(b"\x00" * 12)
896+
assert chacha2.decrypt(ciphertext) == plaintext
897+
898+
def test_chacha_invalid_key_length():
899+
with pytest.raises(ValueError, match="key must be"):
900+
ChaCha(b"\x00" * 20)

wolfcrypt/ciphers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ def __init__(self, key, IV, tag_bytes=16):
421421
raise WolfCryptError("Init error (%d)" % ret)
422422

423423
def __del__(self):
424-
_lib.wc_AesFree(self._native_object)
424+
if hasattr(self, '_native_object'):
425+
_lib.wc_AesFree(self._native_object)
425426

426427
def set_aad(self, data):
427428
"""
@@ -1230,6 +1231,7 @@ def make_key(cls, size, rng=None):
12301231
ret = _lib.wc_ecc_set_rng(ecc.native_object, rng.native_object)
12311232
if ret < 0:
12321233
raise WolfCryptError("Error setting ECC RNG (%d)" % ret)
1234+
ecc._rng = rng
12331235

12341236
return ecc
12351237

@@ -2246,9 +2248,7 @@ def priv_key_size(self):
22462248
if ret < 0: # pragma: no cover
22472249
raise WolfCryptError("wc_MlDsaKey_GetPrivLen() error (%d)" % ret)
22482250

2249-
key_pair_size = size[0]
2250-
2251-
return key_pair_size - self.pub_key_size
2251+
return size[0] - self.pub_key_size
22522252

22532253
def encode_pub_key(self):
22542254
"""

wolfcrypt/hashes.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,21 @@ class Sha3(_Hash):
225225
SHA3_384_DIGEST_SIZE = 48
226226
SHA3_512_DIGEST_SIZE = 64
227227

228+
_SHA3_FREE = {
229+
28: _lib.wc_Sha3_224_Free,
230+
32: _lib.wc_Sha3_256_Free,
231+
48: _lib.wc_Sha3_384_Free,
232+
64: _lib.wc_Sha3_512_Free,
233+
}
234+
228235
def __del__(self):
229-
if self.digest_size == Sha3.SHA3_224_DIGEST_SIZE:
230-
_lib.wc_Sha3_224_Free(self._native_object)
231-
elif self.digest_size == Sha3.SHA3_256_DIGEST_SIZE:
232-
_lib.wc_Sha3_256_Free(self._native_object)
233-
elif self.digest_size == Sha3.SHA3_384_DIGEST_SIZE:
234-
_lib.wc_Sha3_384_Free(self._native_object)
235-
elif self.digest_size == Sha3.SHA3_512_DIGEST_SIZE:
236-
_lib.wc_Sha3_512_Free(self._native_object)
236+
if hasattr(self, '_delete'):
237+
self._delete(self._native_object)
237238

238239
def __init__(self, string=None, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231
239240
self._native_object = _ffi.new(self._native_type)
240241
self.digest_size = size
242+
self._delete = self._SHA3_FREE.get(size)
241243
ret = self._init()
242244
if ret < 0: # pragma: no cover
243245
raise WolfCryptError("Sha3 init error (%d)" % ret)

0 commit comments

Comments
 (0)