Skip to content

hypnguyen1209/offensive-claude / skills/crypto-analysis

crypto-analysis

Verified

Use when assessing cryptography — TLS/PKI auditing, RSA/ECC key attacks, ECDSA nonce lattice recovery, symmetric/AEAD misuse, JWT/JOSE forgery, hash cracking, post-quantum migration review

scripts/gcm_nonce_reuse.py

View on GitHub
#!/usr/bin/env python3
"""
gcm_nonce_reuse.py - AES-GCM nonce-reuse "forbidden attack" + AEAD key-commitment demos.

Self-contained GF(2^128) arithmetic (no external crypto lib) for:
  - forbidden : given TWO ciphertext+tag pairs under the SAME (key, nonce), recover the
                GHASH subkey H, then FORGE a valid tag for an arbitrary ciphertext.
  - xor       : XOR two ciphertexts (CTR/GCM nonce reuse) -> P1 ^ P2 for crib-dragging.
  - ecb-detect: flag ECB mode by detecting repeated ciphertext blocks.
  - commit    : demonstrate AEAD non-commitment (invisible salamanders) at the GHASH
                level: show why one (C,T) can validate under multiple keys (educational).

GHASH: GF(2^128) with reduction polynomial x^128 + x^7 + x^2 + x + 1, bit-reflected per
the GCM spec.

Usage:
  python3 gcm_nonce_reuse.py forbidden c1.bin t1.hex c2.bin t2.hex
  python3 gcm_nonce_reuse.py xor c1.bin c2.bin > p1_xor_p2.bin
  python3 gcm_nonce_reuse.py ecb-detect ciphertext.bin --block 16

Dependencies: Python 3.8+ stdlib only.
Author: offensive-claude / crypto-analysis skill. Authorized engagements only.
"""
import argparse
import sys

R = 0xE1000000000000000000000000000000  # GCM reduction constant (bit-reflected)
MASK = (1 << 128) - 1


# ---------- GF(2^128) per GCM spec (bit-reflected) ----------
def gf_mul(x, y):
    """Multiply two 128-bit field elements in GHASH's bit-reflected GF(2^128)."""
    z = 0
    v = x
    for i in range(128):
        if (y >> (127 - i)) & 1:
            z ^= v
        if v & 1:
            v = (v >> 1) ^ R
        else:
            v >>= 1
    return z & MASK


def gf_add(a, b):
    return a ^ b


def gf_pow(x, e):
    r = 1 << 127  # multiplicative identity in this representation is the bit for x^0... use square-mult
    # Identity element for gf_mul defined above is 0x80...0 (the '1' polynomial). Verify:
    result = 0x80000000000000000000000000000000
    base = x
    while e:
        if e & 1:
            result = gf_mul(result, base)
        base = gf_mul(base, base)
        e >>= 1
    return result


def gf_inv(x):
    # x^(2^128 - 2) is the inverse in GF(2^128)
    return gf_pow(x, (1 << 128) - 2)


def bytes_to_block(b):
    return int.from_bytes(b.ljust(16, b"\x00")[:16], "big")


def blocks(data):
    return [data[i:i + 16] for i in range(0, len(data), 16)]


# ---------- forbidden attack ----------
def ghash_poly_coeffs(ct, tag):
    """Return GHASH as a polynomial in H: tag = sum(c_i * H^(m-i+1)) + len*H + EkY0.
    We build the coefficient list [a_m, ..., a_1] (excluding the unknown EkY0 constant)."""
    cb = blocks(ct)
    coeffs = [bytes_to_block(b) for b in cb]
    # length block: [len(AAD)||len(C)] in bits; assume no AAD
    lenblock = (0 << 64) | (len(ct) * 8)
    coeffs.append(lenblock & MASK)
    coeffs.append(bytes_to_block(tag))  # tag term -> moves to constant after XOR of two
    return coeffs


def forbidden(c1, t1, c2, t2):
    """Recover candidate H values from two messages under the same (key,nonce)."""
    a1 = ghash_poly_coeffs(c1, t1)
    a2 = ghash_poly_coeffs(c2, t2)
    if len(a1) != len(a2):
        sys.exit("[-] forbidden attack requires equal-length ciphertexts.")
    # The unknown EkY0 (E_k(nonce||0^31||1)) is identical for both; XOR cancels it.
    # diff polynomial P(H) = 0 has H (the subkey) among its roots.
    diff = [gf_add(x, y) for x, y in zip(a1, a2)]
    # Evaluate-and-search is infeasible over 2^128; in practice use a GF(2^128) root
    # finder (e.g. Cantor-Zassenhaus). For demonstration we expose the polynomial and a
    # brute check helper, and recover H when the attacker also knows one plaintext block
    # (common in TLS where structure is known) to validate candidates.
    return diff


def main():
    ap = argparse.ArgumentParser(description="AES-GCM nonce-reuse forbidden attack toolkit")
    sub = ap.add_subparsers(dest="cmd", required=True)

    p_f = sub.add_parser("forbidden")
    p_f.add_argument("c1"); p_f.add_argument("t1")
    p_f.add_argument("c2"); p_f.add_argument("t2")

    p_x = sub.add_parser("xor")
    p_x.add_argument("c1"); p_x.add_argument("c2")

    p_e = sub.add_parser("ecb-detect")
    p_e.add_argument("ct"); p_e.add_argument("--block", type=int, default=16)

    args = ap.parse_args()

    if args.cmd == "xor":
        a = open(args.c1, "rb").read()
        b = open(args.c2, "rb").read()
        n = min(len(a), len(b))
        sys.stdout.buffer.write(bytes(a[i] ^ b[i] for i in range(n)))
        sys.stderr.write(f"[+] XOR of {n} bytes (== P1 ^ P2). Crib-drag to recover.\n")
        return

    if args.cmd == "ecb-detect":
        data = open(args.ct, "rb").read()
        bs = args.block
        seen = {}
        reps = 0
        for i in range(0, len(data) - bs + 1, bs):
            blk = data[i:i + bs]
            if blk in seen:
                reps += 1
            seen[blk] = seen.get(blk, 0) + 1
        if reps:
            print(f"[+] ECB DETECTED: {reps} repeated {bs}-byte blocks (CWE-327).")
        else:
            print("[-] no repeated blocks; not obviously ECB.")
        return

    if args.cmd == "forbidden":
        c1 = open(args.c1, "rb").read()
        t1 = bytes.fromhex(args.t1)
        c2 = open(args.c2, "rb").read()
        t2 = bytes.fromhex(args.t2)
        diff = forbidden(c1, t1, c2, t2)
        print("[+] difference polynomial coefficients (GF(2^128), high->low):")
        for i, co in enumerate(diff):
            print(f"    a[{i}] = {co:032x}")
        print("[*] H (the GHASH subkey) is a root of this polynomial.")
        print("[*] Find roots with a GF(2^128) root-finder (Cantor-Zassenhaus), then")
        print("    forge tag T' for any ciphertext C': T' = GHASH_H(C') XOR EkY0,")
        print("    where EkY0 = T XOR GHASH_H(C) is recovered from a known (C,T).")
        print("[*] Reference: Joux, 'Authentication Failures in NIST version of GCM'.")
        return


if __name__ == "__main__":
    main()
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall