8282ECC_BRAINPOOLP384R1 = 26
8383ECC_BRAINPOOLP512R1 = 27
8484
85+ if _lib .RSA_ENABLED :
86+ MGF1NONE = _lib .WC_MGF1NONE
87+ MGF1SHA1 = _lib .WC_MGF1SHA1
88+ MGF1SHA224 = _lib .WC_MGF1SHA224
89+ MGF1SHA256 = _lib .WC_MGF1SHA256
90+ MGF1SHA384 = _lib .WC_MGF1SHA384
91+ MGF1SHA512 = _lib .WC_MGF1SHA512
92+
93+ HASH_TYPE_NONE = _lib .WC_HASH_TYPE_NONE
94+ HASH_TYPE_MD2 = _lib .WC_HASH_TYPE_MD2
95+ HASH_TYPE_MD4 = _lib .WC_HASH_TYPE_MD4
96+ HASH_TYPE_MD5 = _lib .WC_HASH_TYPE_MD5
97+ HASH_TYPE_SHA = _lib .WC_HASH_TYPE_SHA
98+ HASH_TYPE_SHA224 = _lib .WC_HASH_TYPE_SHA224
99+ HASH_TYPE_SHA256 = _lib .WC_HASH_TYPE_SHA256
100+ HASH_TYPE_SHA384 = _lib .WC_HASH_TYPE_SHA384
101+ HASH_TYPE_SHA512 = _lib .WC_HASH_TYPE_SHA512
102+ HASH_TYPE_MD5_SHA = _lib .WC_HASH_TYPE_MD5_SHA
103+ HASH_TYPE_SHA3_224 = _lib .WC_HASH_TYPE_SHA3_224
104+ HASH_TYPE_SHA3_256 = _lib .WC_HASH_TYPE_SHA3_256
105+ HASH_TYPE_SHA3_384 = _lib .WC_HASH_TYPE_SHA3_384
106+ HASH_TYPE_SHA3_512 = _lib .WC_HASH_TYPE_SHA3_512
107+ HASH_TYPE_BLAKE2B = _lib .WC_HASH_TYPE_BLAKE2B
108+ HASH_TYPE_BLAKE2S = _lib .WC_HASH_TYPE_BLAKE2S
109+
110+
85111
86112class _Cipher (object ):
87113 """
@@ -473,6 +499,23 @@ def encrypt(self, plaintext):
473499
474500 return _ffi .buffer (ciphertext )[:]
475501
502+ def encrypt_oaep (self , plaintext , hash_type , mgf , label ):
503+ plaintext = t2b (plaintext )
504+ label = t2b (label )
505+ ciphertext = _ffi .new ("byte[%d]" % self .output_size )
506+
507+ ret = _lib .wc_RsaPublicEncrypt_ex (plaintext , len (plaintext ),
508+ ciphertext , self .output_size ,
509+ self .native_object ,
510+ self ._random .native_object ,
511+ _lib .WC_RSA_OAEP_PAD , hash_type ,
512+ mgf , label , len (label ))
513+
514+ if ret != self .output_size : # pragma: no cover
515+ raise WolfCryptError ("Encryption error (%d)" % ret )
516+
517+ return _ffi .buffer (ciphertext )[:]
518+
476519 def verify (self , signature ):
477520 """
478521 Verifies **signature**, using the public key data in the
@@ -494,6 +537,33 @@ def verify(self, signature):
494537
495538 return _ffi .buffer (plaintext , ret )[:]
496539
540+ if _lib .RSA_PSS_ENABLED :
541+ def verify_pss (self , plaintext , signature , hash_type , mgf ):
542+ """
543+ Verifies **signature**, using the public key data in the
544+ object. The signature's length must be equal to:
545+
546+ **self.output_size**
547+
548+ Returns a string containing the plaintext.
549+ """
550+ plaintext = t2b (plaintext )
551+ signature = t2b (signature )
552+ verify = _ffi .new ("byte[%d]" % self .output_size )
553+
554+ ret = _lib .wc_RsaPSS_Verify (signature , len (signature ),
555+ verify , self .output_size ,
556+ hash_type , mgf ,
557+ self .native_object )
558+
559+ if ret < 0 : # pragma: no cover
560+ raise WolfCryptError ("Verify error (%d)" % ret )
561+ ret = _lib .wc_RsaPSS_CheckPadding (plaintext , len (plaintext ),
562+ verify , ret , hash_type )
563+
564+ return ret
565+
566+
497567
498568 class RsaPrivate (RsaPublic ):
499569 if _lib .KEYGEN_ENABLED :
@@ -597,6 +667,29 @@ def decrypt(self, ciphertext):
597667
598668 return _ffi .buffer (plaintext , ret )[:]
599669
670+ def decrypt_oaep (self , ciphertext , hash_type , mgf , label ):
671+ """
672+ Decrypts **ciphertext**, using the private key data in the
673+ object. The ciphertext's length must be equal to:
674+
675+ **self.output_size**
676+
677+ Returns a string containing the plaintext.
678+ """
679+ ciphertext = t2b (ciphertext )
680+ label = t2b (label )
681+ plaintext = _ffi .new ("byte[%d]" % self .output_size )
682+ ret = _lib .wc_RsaPrivateDecrypt_ex (ciphertext , len (ciphertext ),
683+ plaintext , self .output_size ,
684+ self .native_object ,
685+ _lib .WC_RSA_OAEP_PAD , hash_type ,
686+ mgf , label , len (label ))
687+
688+ if ret < 0 : # pragma: no cover
689+ raise WolfCryptError ("Decryption error (%d)" % ret )
690+
691+ return _ffi .buffer (plaintext , ret )[:]
692+
600693 def sign (self , plaintext ):
601694 """
602695 Signs **plaintext**, using the private key data in the object.
@@ -619,6 +712,30 @@ def sign(self, plaintext):
619712
620713 return _ffi .buffer (signature , self .output_size )[:]
621714
715+ if _lib .RSA_PSS_ENABLED :
716+ def sign_pss (self , plaintext , hash_type , mgf ):
717+ """
718+ Signs **plaintext**, using the private key data in the object.
719+ The plaintext's length must not be greater than:
720+
721+ **self.output_size - self.RSA_MIN_PAD_SIZE**
722+
723+ Returns a string containing the signature.
724+ """
725+ plaintext = t2b (plaintext )
726+ signature = _ffi .new ("byte[%d]" % self .output_size )
727+
728+ ret = _lib .wc_RsaPSS_Sign (plaintext , len (plaintext ),
729+ signature , self .output_size ,
730+ hash_type , mgf ,
731+ self .native_object ,
732+ self ._random .native_object )
733+
734+ if ret != self .output_size : # pragma: no cover
735+ raise WolfCryptError ("Signature error (%d)" % ret )
736+
737+ return _ffi .buffer (signature , self .output_size )[:]
738+
622739
623740if _lib .ECC_ENABLED :
624741 class _Ecc (object ): # pylint: disable=too-few-public-methods
0 commit comments