Skip to content

Commit cb3e8bb

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

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

scripts/build_ffi.py

Lines changed: 14 additions & 13 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"]:
@@ -1106,17 +1107,17 @@ def main(ffibuilder):
11061107
e = "Local wolfssl installation path {} doesn't exist.".format(local_wolfssl)
11071108
raise FileNotFoundError(e)
11081109

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

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

11221123

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
"""

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)