Path: blob/master/attacks/elgamal_signature/nonce_reuse.py
2589 views
import os1import sys23path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))4if sys.path[1] != path:5sys.path.insert(1, path)67from shared import solve_congruence8910def attack(p, m1, r1, s1, m2, r2, s2):11"""12Recovers the nonce and private key from two messages signed using the same nonce.13:param p: the prime used in the ElGamal scheme14:param m1: the first message15:param r1: the signature of the first message16:param s1: the signature of the first message17:param m2: the second message18:param r2: the signature of the second message19:param s2: the signature of the second message20:return: generates tuples containing the possible nonce and private key21"""22for k in solve_congruence(s1 - s2, m1 - m2, p - 1):23for x in solve_congruence(r1, m1 - k * s1, p - 1):24yield int(k), int(x)252627