@@ -119,23 +119,19 @@ def _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding):
119119
120120 outlen = backend ._ffi .new ("size_t *" , buf_size )
121121 buf = backend ._ffi .new ("unsigned char[]" , buf_size )
122+ # Everything from this line onwards is written with the goal of being as
123+ # constant-time as is practical given the constraints of Python and our
124+ # API. See Bleichenbacher's '98 attack on RSA, and its many many variants.
125+ # As such, you should not attempt to change this (particularly to "clean it
126+ # up") without understanding why it was written this way (see
127+ # Chesterton's Fence), and without measuring to verify you have not
128+ # introduced observable time differences.
122129 res = crypt (pkey_ctx , buf , outlen , data , len (data ))
130+ resbuf = backend ._ffi .buffer (buf )[: outlen [0 ]]
131+ backend ._lib .ERR_clear_error ()
123132 if res <= 0 :
124- _handle_rsa_enc_dec_error (backend , key )
125-
126- return backend ._ffi .buffer (buf )[: outlen [0 ]]
127-
128-
129- def _handle_rsa_enc_dec_error (backend , key ):
130- errors = backend ._consume_errors_with_text ()
131- if isinstance (key , _RSAPublicKey ):
132- raise ValueError (
133- "Data too long for key size. Encrypt less data or use a "
134- "larger key size." ,
135- errors ,
136- )
137- else :
138- raise ValueError ("Decryption failed." , errors )
133+ raise ValueError ("Encryption/decryption failed." )
134+ return resbuf
139135
140136
141137def _rsa_sig_determine_padding (backend , key , padding , algorithm ):
0 commit comments