2727from wolfcrypt .ciphers import MODE_CTR , MODE_ECB , MODE_CBC , WolfCryptError
2828from wolfcrypt .random import Random
2929from wolfcrypt .utils import t2b , h2b
30+ from wolfcrypt .random import Random
3031import os
3132
3233certs_dir = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "certs" )
@@ -326,10 +327,18 @@ def test_chacha_enc_dec(chacha_obj):
326327 assert plaintext == dec
327328
328329if _lib .RSA_ENABLED :
330+ @pytest .fixture
331+ def rng ():
332+ return Random ()
333+
329334 @pytest .fixture
330335 def rsa_private (vectors ):
331336 return RsaPrivate (vectors [RsaPrivate ].key )
332337
338+ @pytest .fixture
339+ def rsa_private_rng (vectors , rng ):
340+ return RsaPrivate (vectors [RsaPrivate ].key , rng = rng )
341+
333342 @pytest .fixture
334343 def rsa_private_oaep (vectors ):
335344 return RsaPrivate (vectors [RsaPrivate ].key , hash_type = HASH_TYPE_SHA )
@@ -346,6 +355,10 @@ def rsa_private_pkcs8(vectors):
346355 def rsa_public (vectors ):
347356 return RsaPublic (vectors [RsaPublic ].key )
348357
358+ @pytest .fixture
359+ def rsa_public_rng (vectors , rng ):
360+ return RsaPublic (vectors [RsaPublic ].key , rng = rng )
361+
349362 @pytest .fixture
350363 def rsa_public_oaep (vectors ):
351364 return RsaPublic (vectors [RsaPublic ].key , hash_type = HASH_TYPE_SHA )
@@ -366,6 +379,17 @@ def rsa_public_pem(vectors):
366379 pem = f .read ()
367380 return RsaPublic .from_pem (pem )
368381
382+ @pytest .fixture
383+ def rsa_private_pem_rng (vectors , rng ):
384+ with open (vectors [RsaPrivate ].pem , "rb" ) as f :
385+ pem = f .read ()
386+ return RsaPrivate .from_pem (pem , rng = rng )
387+
388+ @pytest .fixture
389+ def rsa_public_pem_rng (vectors , rng ):
390+ with open (vectors [RsaPublic ].pem , "rb" ) as f :
391+ pem = f .read ()
392+ return RsaPublic .from_pem (pem , rng = rng )
369393
370394 def test_new_rsa_raises (vectors ):
371395 with pytest .raises (WolfCryptError ):
@@ -395,6 +419,22 @@ def test_rsa_encrypt_decrypt(rsa_private, rsa_public):
395419 assert 1024 / 8 == len (ciphertext ) == rsa_private .output_size
396420 assert plaintext == rsa_private .decrypt (ciphertext )
397421
422+ def test_rsa_encrypt_decrypt_rng (rsa_private_rng , rsa_public_rng ):
423+ plaintext = t2b ("Everyone gets Friday off." )
424+
425+ # normal usage, encrypt with public, decrypt with private
426+ ciphertext = rsa_public_rng .encrypt (plaintext )
427+
428+ assert 1024 / 8 == len (ciphertext ) == rsa_public_rng .output_size
429+ assert plaintext == rsa_private_rng .decrypt (ciphertext )
430+
431+ # private object holds both private and public info, so it can also encrypt
432+ # using the known public key.
433+ ciphertext = rsa_private_rng .encrypt (plaintext )
434+
435+ assert 1024 / 8 == len (ciphertext ) == rsa_private_rng .output_size
436+ assert plaintext == rsa_private_rng .decrypt (ciphertext )
437+
398438 def test_rsa_encrypt_decrypt_pad_oaep (rsa_private_oaep , rsa_public_oaep ):
399439 plaintext = t2b ("Everyone gets Friday off." )
400440
@@ -478,6 +518,22 @@ def test_rsa_sign_verify_pem(rsa_private_pem, rsa_public_pem):
478518 assert 256 == len (signature ) == rsa_private_pem .output_size
479519 assert plaintext == rsa_private_pem .verify (signature )
480520
521+ def test_rsa_sign_verify_pem_rng (rsa_private_pem_rng , rsa_public_pem_rng ):
522+ plaintext = t2b ("Everyone gets Friday off." )
523+
524+ # normal usage, sign with private, verify with public
525+ signature = rsa_private_pem_rng .sign (plaintext )
526+
527+ assert 256 == len (signature ) == rsa_private_pem_rng .output_size
528+ assert plaintext == rsa_public_pem_rng .verify (signature )
529+
530+ # private object holds both private and public info, so it can also verify
531+ # using the known public key.
532+ signature = rsa_private_pem_rng .sign (plaintext )
533+
534+ assert 256 == len (signature ) == rsa_private_pem_rng .output_size
535+ assert plaintext == rsa_private_pem_rng .verify (signature )
536+
481537 def test_rsa_pkcs8_sign_verify (rsa_private_pkcs8 , rsa_public ):
482538 plaintext = t2b ("Everyone gets Friday off." )
483539
0 commit comments