Skip to content

Instantly share code, notes, and snippets.

@ottosch
Last active November 27, 2021 23:01
Show Gist options
  • Save ottosch/339aafe8dd5284a47e624b44fba70426 to your computer and use it in GitHub Desktop.
Save ottosch/339aafe8dd5284a47e624b44fba70426 to your computer and use it in GitHub Desktop.
Fixes an ECDSA signature performed by OpenSSL.
#! /usr/bin/env python
import sys
if not sys.stdin.isatty():
hex = sys.stdin.read()
elif len(sys.argv) < 2:
print(f"usage: {sys.argv[0]} hex_signature", file=sys.stderr)
exit(1)
else:
hex = sys.argv[1]
b = bytes.fromhex(hex)
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141; rSize = b[3]; s = int.from_bytes(b[rSize + 6:], 'big')
if s <= n / 2:
print(b.hex())
else:
s = n - s
if s < 0: # pad
s = f"00{-s:02x}"
else:
s = f"{s:02x}"
size = rSize + len(s) // 2 + 4
print(f"30{size:02x}{b[2:5 + rSize].hex()}{len(s) // 2:02x}{s}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment