#!/usr/bin/env python3 """Run unchanged pinned Linux cdc_ncm_fix_modulus for ordinary Ethernet. Usage: cdc_ncm_alignment_oracle.py CDC_NCM_C OUTPUT_DIRECTORY Only admitted, valid power-of-two divisors/alignments are compared. The shim supplies metadata, an ordinary 14-byte Ethernet header and discarded debug logging. It does not execute USB traffic or full NCM initialization. """ import hashlib import json from pathlib import Path import re import subprocess import sys source, out = map(Path, sys.argv[1:]) source_bytes = source.read_bytes() assert hashlib.sha256(source_bytes).hexdigest() == "4e6a3df21dd32c5b08695173fb9bc90744368ff6c657515991ff52d74572523b" text = source_bytes.decode() match = re.search(r"^static void cdc_ncm_fix_modulus\([^;{}]*\)\n\{", text, re.M) assert match at, depth = match.end(), 1 while depth: depth += (text[at] == "{") - (text[at] == "}") at += 1 function = text[match.start():at] prelude = r'''// SPDX-License-Identifier: GPL-2.0-only // Copyright (C) ST-Ericsson 2010-2012 // Original author: Hans Petter Selasky #include #include typedef uint32_t u32; #define USB_CDC_NCM_NDP_ALIGN_MIN_SIZE 4 #define dev_dbg(...) ((void)0) struct cdc_ncm_ctx {u32 tx_ndp_modulus,tx_modulus,tx_remainder,tx_max;}; struct usbnet {uintptr_t data[1];}; static u32 cdc_ncm_eth_hlen(struct usbnet *dev){(void)dev;return 14;} ''' main = r''' int main(void){ for(u32 divisor=4;divisor<=1024;divisor*=2) for(u32 remainder=0;remainder