Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/rsa/crt_fault_attack.py
2589 views
1
from math import gcd
2
3
4
def attack_known_m(n, e, m, s):
5
"""
6
Recovers the prime factors from a modulus using a known message and its faulty signature.
7
:param n: the modulus
8
:param e: the public exponent
9
:param m: the message
10
:param s: the faulty signature
11
:return: a tuple containing the prime factors, or None if the signature wasn't actually faulty
12
"""
13
g = gcd(m - pow(s, e, n), n)
14
return None if g == 1 else (g, n // g)
15
16
17
def attack_unknown_m(n, e, sv, sf):
18
"""
19
Recovers the prime factors from a modulus using a correct valid and a faulty signature from the same (unknown) message.
20
:param n: the modulus
21
:param e: the public exponent
22
:param sv: the valid signature
23
:param sf: the faulty signature
24
:return: a tuple containing the prime factors, or None if the signatures were both valid, or both faulty
25
"""
26
assert sv != sf
27
g = gcd(sv - sf, n)
28
return None if g == 1 else (g, n // g)
29
30