Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt side-channel free RSA decryption #438

Merged
merged 5 commits into from
Dec 8, 2020
Merged

Conversation

tomato42
Copy link
Member

@tomato42 tomato42 commented Dec 3, 2020

currently we leak severely where the issue is in the padding check of RSA decryption, so attempt depadding in side-channel free way (at least to the degree that Python allows)


This change is Reviewable

with M2Crypto we can process byte strings, and for padding/depadding
we need bytes, so don't convert back and forth between the
formats

also put all the integer to bytes conversion in a single place
for the implementations that can't handle bytes as input
@tomato42 tomato42 added the bug unintented behaviour in tlslite-ng code label Dec 3, 2020
@tomato42 tomato42 added this to the v0.8.0 milestone Dec 3, 2020
@tomato42 tomato42 self-assigned this Dec 3, 2020
@tomato42 tomato42 requested a review from ep69 December 3, 2020 16:48
@lgtm-com
Copy link

lgtm-com bot commented Dec 3, 2020

This pull request fixes 1 alert when merging 982cfe2 into 0812ed6 - view on LGTM.com

fixed alerts:

  • 1 for Unused import

@tomato42 tomato42 force-pushed the bleichenbacher-fixes branch 2 times, most recently from 79b609a to 77e8b06 Compare December 3, 2020 16:57
@lgtm-com
Copy link

lgtm-com bot commented Dec 3, 2020

This pull request fixes 1 alert when merging 77e8b06 into 0812ed6 - view on LGTM.com

fixed alerts:

  • 1 for Unused import

@tomato42 tomato42 force-pushed the bleichenbacher-fixes branch from 77e8b06 to 477d681 Compare December 3, 2020 17:32
@@ -33,9 +33,39 @@ def parsePEM(s, passwordCallback=None):
elif pemSniff(s, "EC PRIVATE KEY"):
bytes = dePem(s, "EC PRIVATE KEY")
return Python_Key._parse_ecc_ssleay(bytes)
elif pemSniff(s, "PUBLIC KEY"):
bytes = dePem(s, "PUBLIC KEY")
return Python_Key._parse_public_key(bytes)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this function.

@lgtm-com
Copy link

lgtm-com bot commented Dec 3, 2020

This pull request fixes 1 alert when merging 477d681 into 0812ed6 - view on LGTM.com

fixed alerts:

  • 1 for Unused import

ep69
ep69 previously approved these changes Dec 7, 2020
if cryptomath.m2cryptoLoaded:
if type(key) == Python_RSAKey:
return _createPrivateKey(key)
assert type(key) in (OpenSSL_RSAKey, Python_ECDSAKey), type(key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the syntax. Why is type(key) at the end of the line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert will print the second element in the tuple on error, it's so that if the assert fails, it will print result of type(key)

raise ValueError("only multiples of 8 supported as output size")

iterator = 0
while len(out) <= out_len // 8:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would < be enough perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would be, will fix

@@ -67,13 +68,21 @@ def _rawPrivateKeyOp(self, message):
ciphertext = bytesToNumber(bytearray(string))
return ciphertext

def _raw_private_key_op_bytes(self, message):
return bytearray(m2.rsa_private_encrypt(self.rsa, bytes(message),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using _rawPrivateKeyOp instead of m2 internally? It would be nice if the pattern is the same as in tlslite/utils/rsakey.py. (same for public key operations)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. this is the class for wrapping m2crypto specifically
  2. we do direct call to m2crypto here instead of reusing the code from rsakey because we don't want to do the int() -> bytes() conversion in python, as it can't be done in side-channel safe manner

@lgtm-com
Copy link

lgtm-com bot commented Dec 7, 2020

This pull request fixes 1 alert when merging 44d6fd4 into 0812ed6 - view on LGTM.com

fixed alerts:

  • 1 for Unused import

@tomato42 tomato42 merged commit a6cbf7e into master Dec 8, 2020
@tomato42 tomato42 deleted the bleichenbacher-fixes branch December 8, 2020 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug unintented behaviour in tlslite-ng code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants