Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/ecc/ecdsa_nonce_reuse.py
2589 views
1
import os
2
import sys
3
4
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))
5
if sys.path[1] != path:
6
sys.path.insert(1, path)
7
8
from shared import solve_congruence
9
10
11
def attack(n, m1, r1, s1, m2, r2, s2):
12
"""
13
Recovers the nonce and private key from two messages signed using the same nonce.
14
:param n: the order of the elliptic curve
15
:param m1: the first message
16
:param r1: the signature of the first message
17
:param s1: the signature of the first message
18
:param m2: the second message
19
:param r2: the signature of the second message
20
:param s2: the signature of the second message
21
:return: generates tuples containing the possible nonce and private key
22
"""
23
for k in solve_congruence(int(s1 - s2), int(m1 - m2), int(n)):
24
for x in solve_congruence(int(r1), int(k * s1 - m1), int(n)):
25
yield int(k), int(x)
26
27