Skip to content

Commit 62bfa5f

Browse files
committed
Add tests and fix build issues
1 parent e932119 commit 62bfa5f

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

scripts/build_ffi.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,14 @@ def generate_libwolfssl(fips):
305305

306306
def get_features(local_wolfssl, features):
307307
fips = False
308+
fips_file = None
308309

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

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

626627
if features["DES3"]:
@@ -1117,6 +1118,7 @@ def main(ffibuilder):
11171118
if not get_libwolfssl():
11181119
generate_libwolfssl(features["FIPS"])
11191120

1121+
get_features(local_wolfssl, features)
11201122
build_ffi(local_wolfssl, features)
11211123

11221124

tests/test_aesgcmstream.py

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

tests/test_ciphers.py

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

wolfcrypt/ciphers.py

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

424424
def __del__(self):
425-
_lib.wc_AesFree(self._native_object)
425+
if hasattr(self, '_native_object'):
426+
_lib.wc_AesFree(self._native_object)
426427

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

12351237
return ecc
12361238

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

2250-
return size[0]
2252+
return size[0] - self.pub_key_size
22512253

22522254
def encode_pub_key(self):
22532255
"""

0 commit comments

Comments
 (0)