andrewkiluk / RSA-Library

This is a C library for RSA encryption. It provides three functions for key generation, encryption, and decryption.
MIT License
378 stars 145 forks source link

Problem with exiting in rsa_modExp #9

Open sqbi-q opened 4 years ago

sqbi-q commented 4 years ago

Edit: As shipof123 says, there is problem with rsa_modExp function, which is exiting when: (b < 0 || e < 0 || m <= 0)

Everything compiles fine, but sometimes output of test.c is only showing half of prints: primes are 74077 and 71537 Private Key: Modulus: 5299246349 Exponent: 3525860801 Public Key: Modulus: 5299246349 Exponent: 257 Original: 49 50 51 97 98 99

instead of:

primes are 25463 and 24793 Private Key: Modulus: 631304159 Exponent: 363523649 Public Key: Modulus: 631304159 Exponent: 257 Original: 49 50 51 97 98 99 Encrypted: 436385454 7093305 328497852 488616667 550174409 97597229 Decrypted: 49 50 51 97 98 99

Any solution?

shipof123 commented 4 years ago

I have the same issue

shipof123 commented 4 years ago

echo $? shows that it exited on an error, I'd run a debugger and step through it.

shipof123 commented 4 years ago

I analyzed it, and this is because the function long long rsa_modExp(long long b, long long e, long long m) fails if any of its parameters is 0, which is fairly often. This would be fine if it didn't exit instead of aborting its function and provides no reason for the users to see

omarahmad81 commented 4 years ago

Hi, I seem to have resolved the issue by replacing long long with unsigned long long throughout the library. Need your feedback on this solution. I believe the multiplication in rsa_modExp(b * b % m, e/2, m), line 56 of rsa.c was causing the overflow.

Prendre-soin-de-soi commented 3 years ago

change the line 56 of rsa.c into *return ( rsa_modExp(((b % m) b) % m, e/2, m) % m );**

arctumn commented 3 years ago

Hi, I seem to have resolved the issue by replacing long long with unsigned long long throughout the library. Need your feedback on this solution. I believe the multiplication in rsa_modExp(b * b % m, e/2, m), line 56 of rsa.c was causing the overflow.

yup what you said did work, still dont know why the dev did not care about the overflow issues....

oskarvonephesos commented 3 years ago

hi, i have created a fork that adresses this issue and provides further encryption (OAEP): https://github.com/oskarvonephesos/RSA-Library