2626from wolfcrypt ._ffi import lib as _lib
2727from wolfcrypt .ciphers import MODE_CTR , MODE_ECB , MODE_CBC , WolfCryptError
2828from wolfcrypt .utils import t2b , h2b
29+ from wolfcrypt .random import Random
2930import os
3031
3132certs_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
327328if _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
0 commit comments