Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Tutorial for application with RSA. We are going to use openSSL, openSSH and pycryptodome for key generation, key extraction and some implementation with python
Pycryptodome is a python library about cryptography, see the documentation below: https://www.pycryptodome.org/en/latest/ There is an example of RSA key generation with pycryptodome:
OpenSSL is a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library
Boneh-Durfee attack is an extension of Wiener's attack. That is, it also attacks on low private component with a further relaxed condition. If satisfies:
Then we can use Boneh-Durfee attack to retrive
this, using a graphical directed point of view, can be seen as:
Consider for first, see that
As stated above, the RSA's totient function can be espressed as:
continuing with the equation, we see that
Wiener's attack is an attack on RSA that uses continued fractions to find the private exponent when it's small (less than , where is the modulus). We know that when we pick the public exponent to be a small number and calcute its inverse
Wiener's attack is based on the following theorem:
Let , with . Let . Given and with , the attacker can efficiently recover .
In order to better understand Wiener's Attack, it may be useful to take note of certain properties of RSA:
We may start by noting that the congruence can be written as the equality for some value , we may additionally note that , since both and are much shorter than , we can say that .
Dividing the former equation by gives us , and using the latter approximation, we can write this as . Notice how the left-hand side of this equation is composed entirely of public information, this will become important later.
It is possible to quickly factor by knowing and . Consider the quadratic polynomial , this polynomial will have the roots and . Expanding it gives us , and substituting for the variables we know we can write this as . Applying the quadratic formula gives us and : , where , , and .
Wiener's attack works by expanding to a continued fraction and iterating through the terms to check various approximations of . In order to make this checking process more efficient, we can make a few observations (this part is optional):
Since is even, and and are both by definition coprime to , we know that is odd.
Given the above equations and the values of , , , and , we can solve for with the equation , thus we know that has to be divisible by .
If our is correct, the polynomial will have roots and , which we can verify by checking if .
Suppose we have the public key , this attack will determine
If all convergents have been tried, and none of them work, then the given RSA parameters are not vulnerable to Wiener's attack.
Here's a sage implementation to play around with:
//TODO: Proof of Wiener's theorem
The Python module owiener
simplifies the scripting process of Wiener's attack:
Here is a Wiener's attack template:
What to do when the same message is encrypted twice with the same modulus but a different public key?
Imagine we have Alice and Bob. Alice sends the SAME message to Bob more than once using the same public key. The internet being the internet, a problem may happen; a bit is flipped, and the public key changed while the modulus stayed the same.
Let be the following:
m
the message in plaintext
e1
the public key of the first ciphertext
c1
the first ciphertext
e2
the public key of the second ciphertext
c2
the second ciphertext
n
the modulus that is common to both ciphertexts
All of these but m
are essentially given to us.
Because we are going to need to calculate inverses for this attack, we must first make sure that these inverses exist in the first place:
We know that RSA goes as follows:
Now to truly recover the plaintext, we are actually doing:
Will be introduced in this page the fundamentals of RSA, mathematical requirement and also some application with python and openSSL.
This page is pretty long, probably could be split up
Edit: I haved deleted the last part, application with RSA, and i made a special part for this. Maybe we can do the same with the second part: Arithmetic for RSA.
RSA is a that is widely used in the world today to provide a secure transmission system to millions of communications, is one of the oldest such systems in existence. The RSA comes from the surnames of , , and , who publicly described the algorithm in 1977. An equivalent system was developed secretly, in 1973 at (the British agency), by the English mathematician . That system was in 1997.
All public-key systems are based on the concept of , functions that are simple to compute in one direction but computationally hard to reverse without knowledge of some special information called the trapdoor. In RSA, the trapdoor function is based on the . The function involves the use of a public keyto encrypt data, which is (supposed to be) encrypted in such a way that the function cannot be reversed without knowledge of the prime factorisation of, something that should be kept private. Except in certain cases, there exists no efficient algorithm for factoring huge integers.
Further reading:
Formalize the introduction and include a discussion of the security based on the hardness of factoring integers.
Before starting to introducing you RSA, a few arithmetic notions need to be introduce to understand perfectly other steps.
We pick two primes and
Using and , we calculate modulus and its Euler's totient
Now, choose the public exponent such as
By using the Extended Euclidean algorithm, we compute the invert of : which is our private exponent.
Public key:
Private key:
Now, chose a message that you convert into integers
We can encrypt this plaintext and receive a ciphertext
We can decrypt a ciphertext with
A digital signature is a proof of the authenticity of a message, i.e. a proof that the message has not been tampered with. RSA allows us to sign messages by "encrypting" them using the private key, the result being a signature that anyone can verify by "decrypting" it with the public key and comparing it to the associated message. Any attempt to tamper with the message will result in it no longer matching the signature, and vice-versa. Futhermore, a signature can only be generated using the private key, making it a secure and efficient method of confirming the authenticity of messages.
We now consider necessary for the successful description of an RSA ciphertext. The core of this result is due to which states
for all coprime integers and is .
As a reminder, we say two integers are coprime if they share no non-trivial factors. This is the same statement that .
From the definition of the protocol, we have that
for some . Combining this with Euler's theorem, we see that we recover from the ciphertext
When the requirement does not hold, we can instead look at the equivalences modulo and respectively. Clearly, when we have that and our correctness still holds. Now, consider the case where we have that and Since we have already excluded the case that we can conclude that as is prime. This means that and by the multiplicative properties of the function, we determine that We conclude by invoking the Chinese Remainder theorem with
When you want to recover N given some (plaintext, ciphertext) pairings
Consider the case that that you know a set of (plaintext, ciphertext) pairings - this may be that you are provided them, or that you have access to some functionality that returns ciphertexts for provided plaintexts. If you do not know the modulus, but know the exponent used (note: this may be prone to a brute-force regardless), then given these pairings you can recover the modulus used.
Let the following be known:
plaintext && ciphertext pairings:
public exponent e
(e.g. e = 65537 = 0x10001)
The idea behind this attack is effectively finding common factors between pairings. Recall that, under general RSA encryption, we have:
and recall what modular arithmetic tells us about the relation between these terms, namely that:
This, rearranged, tells us that
However, this is only true for the case that
Thus:
In reality, you're likely to only need two or three (plaintext, ciphertext) pairings (in the context of ctf challenges and exercises), and as such computations can be manual if needed, but shouldn't be too complex
As it's likely you'll be dealing with large numbers, overflows and precision errors may arise in code - using libraries like gmpy
provide support for integers of (theoretically) infinite size, and some nice accompanying features too (like in-built gcd and efficient modular exponentiation)
These two statements are mathematically equivalent, but one is easier to implement in code:
and if we decide to consider and , we will have:
At this point, finding is equivalent to find the 2 small solutions and to the congruence
now let and this will preserve the scomposed subtraction
consider (with any ), we deduct that must be really closed to because is in the same order of the length of (so ), we will get
Convert the fraction into a continued fraction
Iterate over each convergent in the continued fraction:
Check if the convergent is by doing the following:
Set the numerator to be and denominator to be
Check if is odd, if not, move on to the next convergent
Check if , if not, move on to the next convergent
Set and find the roots of the polynomial
If the roots of the polynomial are integers, then we've found . (If not, move on to the next convergent)
From the conditions above we also know that and are co-prime. Thus using Bezout's Theorem we can get:
Using this, we can derive the original message :
NB: all the calculations are done mod
In general, Bezout's Theorem gives a pair of positive and negative numbers. We just need to adapt this equation a little to make it work for us. In this case, let's assume is the negative number:
Say Alice wants to send a message to Bob, but does not want Mallory, who has established herself as a middleman, to make changes to the message or swap it out entirely. Fortunately, Bob knows Alice's public key, and since and are inverses such that , Alice can sign her message by "encrypting" it with the private key such that , where is the signature verifying that the message came from Alice. Alice can now send and to Bob, who is now able to check the authenticity of the message by checking if . If Mallory tries to change , this congruence no longer holds, and since she does not have the private key, she is also unable to provide a maching for her tampered message.
that The case for follows in a parallel manner.
What this means for our known pairings is that, given we know and , we can form the relationship:
Thus we can calculate for the value , though don't know either value individually - we want to somehow derive .
Observe that any two pairings will equate to such a value, both with as a factor. We can take the gcd of these two values, and it is probable that the resulting value will be our value, such that:
i.e., both and are coprime. In the case that they are not, i.e. , we have that
In such a case, we don't have sufficient information to completely recover the modulus, and require more plaintext-ciphertext pairs to be successful. In general, the more pairings you have, the more confident you can be the value you calculate is . More specifically: