
IoT 펌웨어 서명에서 ECDSA nonce 재사용을 시연하는 교육용 Python 시뮬레이션으로, 동일한 k를 공유하는 두 서명으로부터 공격자가 개인 키를 복구할 수 있는 방법을 보여줍니다.
# ecdsa_nonce_reuse_sim.py - Signing firmware with repeated nonce (k)
import ecdsa, hashlib
sk = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
vk = sk.get_verifying_key()
# Sign two different firmware images with same nonce due to bad RNG
# In reality, this can happen with deterministic k if seed is constant.
# We'll simulate by using the same k manually (not possible with ecdsa library, so fake it)
def sign_with_fixed_k(sk, msg_hash, k):
# Simplified: return signature (r,s) using fixed k (for educational purposes)
# Not actual ECDSA, but shows concept.
r = (k * ecdsa.NIST256p.generator).x()
k_inv = pow(k, -1, ecdsa.NIST256p.order)
s = k_inv * (int.from_bytes(msg_hash, 'big') + r * sk.privkey.secret_multiplier) % ecdsa.NIST256p.order
return ecdsa.ecdsa.Signature(r, s)
msg1 = b"Firmware v1.0"
msg2 = b"Firmware v2.0"
h1 = hashlib.sha256(msg1).digest()
h2 = hashlib.sha256(msg2).digest()
# Use same k
k = 123456789
sig1 = sign_with_fixed_k(sk, h1, k)
sig2 = sign_with_fixed_k(sk, h2, k)
print("Two signatures with same k. Attacker can recover private key from (r,s1) and (r,s2).")
IoT 기기가 ECDSA를 사용해 펌웨어 업데이트에 서명하지만, 취약한 난수 생성기로 인해 두 서명에 동일한 nonce(k)가 재사용됩니다. 두 서명을 모두 관찰한 공격자는 개인 키를 계산하고 악성 펌웨어에 서명할 수 있습니다.
k로 생성된 두 서명이 주어지면 개인 키를 대수적으로 유도할 수 있습니다.시뮬레이션을 실행합니다:
pip install ecdsa
python ecdsa_nonce_reuse_sim.py
이 스크립트는 동일한 k로 두 개의 서명이 생성되는 과정을 보여줍니다. 실제 공격자는 수식 k = (h1 - h2) / (s1 - s2)를 사용해 키를 복구한 다음 d = (s1*k - h1) / r을 계산합니다.