Skip to content

Commit 5b13e52

Browse files
authored
Merge pull request #87 from JeremiahM37/fenrir-fixes
Fix Fenrir findings in SHA3, RSA-PSS, FFI feature detection, and ChaCha
2 parents f68ac28 + 6e8c43b commit 5b13e52

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

scripts/build_ffi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def get_features(local_wolfssl, features):
375375
features["WC_RNG_SEED_CB"] = 1 if '#define WC_RNG_SEED_CB' in defines else 0
376376
features["AESGCM_STREAM"] = 1 if '#define WOLFSSL_AESGCM_STREAM' in defines else 0
377377
features["RSA_PSS"] = 1 if '#define WC_RSA_PSS' in defines else 0
378-
features["CHACHA20_POLY1305"] = 1 if '#define HAVE_CHACHA' and '#define HAVE_POLY1305' in defines else 0
378+
features["CHACHA20_POLY1305"] = 1 if ('#define HAVE_CHACHA' in defines and '#define HAVE_POLY1305' in defines) else 0
379379
features["ML_DSA"] = 1 if '#define HAVE_DILITHIUM' in defines else 0
380380
features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0
381381
features["HKDF"] = 1 if "#define HAVE_HKDF" in defines else 0
@@ -386,7 +386,7 @@ def get_features(local_wolfssl, features):
386386
raise RuntimeError(e)
387387

388388
features["FIPS"] = 1
389-
version_match = re.search(r'#define HAVE_FIPS_VERSION\s+(\d+)', defines)
389+
version_match = re.search(r'#define HAVE_FIPS_VERSION\s+(\d+)', '\n'.join(defines))
390390
if version_match is not None:
391391
features["FIPS_VERSION"] = int(version_match.group(1))
392392
else:
@@ -756,6 +756,8 @@ def build_ffi(local_wolfssl, features):
756756
int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
757757
byte* out, word32 outLen, RsaKey* key, int type,
758758
enum wc_HashType hash, int mgf, byte* label, word32 labelSz);
759+
int wc_RsaSSL_Sign(const byte*, word32, byte*, word32, RsaKey*, WC_RNG*);
760+
int wc_RsaSSL_Verify(const byte*, word32, byte*, word32, RsaKey*);
759761
"""
760762

761763
if features["RSA_PSS"]:
@@ -766,8 +768,6 @@ def build_ffi(local_wolfssl, features):
766768
enum wc_HashType hash, int mgf, RsaKey* key);
767769
int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, byte* sig,
768770
word32 sigSz, enum wc_HashType hashType);
769-
int wc_RsaSSL_Sign(const byte*, word32, byte*, word32, RsaKey*, WC_RNG*);
770-
int wc_RsaSSL_Verify(const byte*, word32, byte*, word32, RsaKey*);
771771
"""
772772

773773
if features["RSA_BLINDING"]:

tests/test_ciphers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,14 @@ def test_rsa_pss_sign_verify(rsa_private_pss, rsa_public_pss):
456456
signature = rsa_private_pss.sign_pss(plaintext)
457457

458458
assert 1024 / 8 == len(signature) == rsa_private_pss.output_size
459-
assert 0 == rsa_public_pss.verify_pss(plaintext, signature)
459+
assert rsa_public_pss.verify_pss(plaintext, signature) is True
460460

461461
# private object holds both private and public info, so it can also verify
462462
# using the known public key.
463463
signature = rsa_private_pss.sign_pss(plaintext)
464464

465465
assert 1024 / 8 == len(signature) == rsa_private_pss.output_size
466-
assert 0 == rsa_private_pss.verify_pss(plaintext, signature)
466+
assert rsa_private_pss.verify_pss(plaintext, signature) is True
467467

468468
def test_rsa_sign_verify_pem(rsa_private_pem, rsa_public_pem):
469469
plaintext = t2b("Everyone gets Friday off.")

wolfcrypt/ciphers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,13 @@ def _decrypt(self, destination, source):
530530
return _lib.wc_Chacha_Process(self._dec,
531531
destination, source, len(source))
532532

533+
_NONCE_SIZE = 12
534+
533535
def set_iv(self, nonce, counter = 0):
534536
self._IV_nonce = t2b(nonce)
537+
if len(self._IV_nonce) != self._NONCE_SIZE:
538+
raise ValueError("nonce must be %d bytes, got %d" %
539+
(self._NONCE_SIZE, len(self._IV_nonce)))
535540
self._IV_counter = counter
536541
self._set_key(0)
537542

@@ -811,8 +816,10 @@ def verify_pss(self, plaintext, signature):
811816
ret = _lib.wc_RsaPSS_CheckPadding(digest, len(digest),
812817
verify, ret, self._hash_type)
813818

814-
return ret
819+
if ret < 0: # pragma: no cover
820+
raise WolfCryptError("PSS padding check error (%d)" % ret)
815821

822+
return ret == 0
816823

817824

818825
class RsaPrivate(RsaPublic):

wolfcrypt/hashes.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,7 @@ class Sha3(_Hash):
209209
SHA3_384_DIGEST_SIZE = 48
210210
SHA3_512_DIGEST_SIZE = 64
211211

212-
def __init__(self): # pylint: disable=W0231
213-
self._native_object = _ffi.new(self._native_type)
214-
self.digest_size = SHA3_384_DIGEST_SIZE
215-
ret = self._init()
216-
if ret < 0: # pragma: no cover
217-
raise WolfCryptError("Sha3 init error (%d)" % ret)
218-
219-
def __init__(self, string, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231
212+
def __init__(self, string=None, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231
220213
self._native_object = _ffi.new(self._native_type)
221214
self.digest_size = size
222215
ret = self._init()
@@ -225,6 +218,15 @@ def __init__(self, string, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231
225218
if string:
226219
self.update(string)
227220

221+
@classmethod
222+
def new(cls, string=None, size=SHA3_384_DIGEST_SIZE):
223+
return cls(string, size)
224+
225+
def copy(self):
226+
c = Sha3(size=self.digest_size)
227+
_ffi.memmove(c._native_object, self._native_object, self._native_size)
228+
return c
229+
228230
def _init(self):
229231
if (self.digest_size != Sha3.SHA3_224_DIGEST_SIZE and
230232
self.digest_size != Sha3.SHA3_256_DIGEST_SIZE and

0 commit comments

Comments
 (0)