Skip to content

Commit 377542f

Browse files
Add rng parameter to from_pem classmethod, add unit tests.
1 parent 96bf7b1 commit 377542f

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

tests/test_ciphers.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from wolfcrypt._ffi import lib as _lib
2727
from wolfcrypt.ciphers import MODE_CTR, MODE_ECB, MODE_CBC, WolfCryptError
2828
from wolfcrypt.utils import t2b, h2b
29+
from wolfcrypt.random import Random
2930
import os
3031

3132
certs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "certs")
@@ -325,10 +326,18 @@ def test_chacha_enc_dec(chacha_obj):
325326
assert plaintext == dec
326327

327328
if _lib.RSA_ENABLED:
329+
@pytest.fixture
330+
def rng():
331+
return Random()
332+
328333
@pytest.fixture
329334
def rsa_private(vectors):
330335
return RsaPrivate(vectors[RsaPrivate].key)
331336

337+
@pytest.fixture
338+
def rsa_private_rng(vectors, rng):
339+
return RsaPrivate(vectors[RsaPrivate].key, rng=rng)
340+
332341
@pytest.fixture
333342
def rsa_private_oaep(vectors):
334343
return RsaPrivate(vectors[RsaPrivate].key, hash_type=HASH_TYPE_SHA)
@@ -345,6 +354,10 @@ def rsa_private_pkcs8(vectors):
345354
def rsa_public(vectors):
346355
return RsaPublic(vectors[RsaPublic].key)
347356

357+
@pytest.fixture
358+
def rsa_public_rng(vectors, rng):
359+
return RsaPublic(vectors[RsaPublic].key, rng=rng)
360+
348361
@pytest.fixture
349362
def rsa_public_oaep(vectors):
350363
return RsaPublic(vectors[RsaPublic].key, hash_type=HASH_TYPE_SHA)
@@ -365,6 +378,17 @@ def rsa_public_pem(vectors):
365378
pem = f.read()
366379
return RsaPublic.from_pem(pem)
367380

381+
@pytest.fixture
382+
def rsa_private_pem_rng(vectors, rng):
383+
with open(vectors[RsaPrivate].pem, "rb") as f:
384+
pem = f.read()
385+
return RsaPrivate.from_pem(pem, rng=rng)
386+
387+
@pytest.fixture
388+
def rsa_public_pem_rng(vectors, rng):
389+
with open(vectors[RsaPublic].pem, "rb") as f:
390+
pem = f.read()
391+
return RsaPublic.from_pem(pem, rng=rng)
368392

369393
def test_new_rsa_raises(vectors):
370394
with pytest.raises(WolfCryptError):
@@ -394,6 +418,22 @@ def test_rsa_encrypt_decrypt(rsa_private, rsa_public):
394418
assert 1024 / 8 == len(ciphertext) == rsa_private.output_size
395419
assert plaintext == rsa_private.decrypt(ciphertext)
396420

421+
def test_rsa_encrypt_decrypt_rng(rsa_private_rng, rsa_public_rng):
422+
plaintext = t2b("Everyone gets Friday off.")
423+
424+
# normal usage, encrypt with public, decrypt with private
425+
ciphertext = rsa_public_rng.encrypt(plaintext)
426+
427+
assert 1024 / 8 == len(ciphertext) == rsa_public_rng.output_size
428+
assert plaintext == rsa_private_rng.decrypt(ciphertext)
429+
430+
# private object holds both private and public info, so it can also encrypt
431+
# using the known public key.
432+
ciphertext = rsa_private_rng.encrypt(plaintext)
433+
434+
assert 1024 / 8 == len(ciphertext) == rsa_private_rng.output_size
435+
assert plaintext == rsa_private_rng.decrypt(ciphertext)
436+
397437
def test_rsa_encrypt_decrypt_pad_oaep(rsa_private_oaep, rsa_public_oaep):
398438
plaintext = t2b("Everyone gets Friday off.")
399439

@@ -477,6 +517,22 @@ def test_rsa_sign_verify_pem(rsa_private_pem, rsa_public_pem):
477517
assert 256 == len(signature) == rsa_private_pem.output_size
478518
assert plaintext == rsa_private_pem.verify(signature)
479519

520+
def test_rsa_sign_verify_pem_rng(rsa_private_pem_rng, rsa_public_pem_rng):
521+
plaintext = t2b("Everyone gets Friday off.")
522+
523+
# normal usage, sign with private, verify with public
524+
signature = rsa_private_pem_rng.sign(plaintext)
525+
526+
assert 256 == len(signature) == rsa_private_pem_rng.output_size
527+
assert plaintext == rsa_public_pem_rng.verify(signature)
528+
529+
# private object holds both private and public info, so it can also verify
530+
# using the known public key.
531+
signature = rsa_private_pem_rng.sign(plaintext)
532+
533+
assert 256 == len(signature) == rsa_private_pem_rng.output_size
534+
assert plaintext == rsa_private_pem_rng.verify(signature)
535+
480536
def test_rsa_pkcs8_sign_verify(rsa_private_pkcs8, rsa_public):
481537
plaintext = t2b("Everyone gets Friday off.")
482538

wolfcrypt/ciphers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,9 @@ def __init__(self, key=None, hash_type=None, rng=None):
750750

751751
if _lib.ASN_ENABLED:
752752
@classmethod
753-
def from_pem(cls, file, hash_type=None):
753+
def from_pem(cls, file, hash_type=None, rng=None):
754754
der = pem_to_der(file, _lib.PUBLICKEY_TYPE)
755-
return cls(key=der, hash_type=hash_type)
755+
return cls(key=der, hash_type=hash_type, rng=rng)
756756

757757
def encrypt(self, plaintext):
758758
"""
@@ -916,9 +916,9 @@ def __init__(self, key=None, hash_type=None, rng=None): # pylint: disable=super
916916

917917
if _lib.ASN_ENABLED:
918918
@classmethod
919-
def from_pem(cls, file, hash_type=None):
919+
def from_pem(cls, file, hash_type=None, rng=None):
920920
der = pem_to_der(file, _lib.PRIVATEKEY_TYPE)
921-
return cls(key=der, hash_type=hash_type)
921+
return cls(key=der, hash_type=hash_type, rng=rng)
922922

923923
if _lib.KEYGEN_ENABLED:
924924
def encode_key(self):

0 commit comments

Comments
 (0)