|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import esptool, espefuse |
| 5 | +import os, hmac, time |
| 6 | + |
| 7 | + |
| 8 | +def burn_hmac_key(key_to_use=1, port=None, do_not_confirm=False): |
| 9 | + # This is the master secret and should be kept secret |
| 10 | + # Initialise it from the environment variable |
| 11 | + MASTER_SECRET = os.environ["MASTER_SECRET"] |
| 12 | + |
| 13 | + # Fix this in the code to the sha256sum of the master secret |
| 14 | + MASTER_HASH='59a61bdad01d1074a37bc6ee2ae4bac0a424fc2fcfcbdfd0c386d1fdac0d5c7e' |
| 15 | + |
| 16 | + # PLEASE PLEASE PLEASE DO NOT REMOVE THIS CHECK |
| 17 | + # IF YOU FLASH THE WRONG SECRET TO THE DEVICE IT CAN NEVER BE UNDONE |
| 18 | + if hashlib.sha256(MASTER_SECRET.encode()).hexdigest() != MASTER_HASH: |
| 19 | + print("Master secret does not match") |
| 20 | + raise Exception("Master secret does not match") |
| 21 | + |
| 22 | + esp=esptool.get_default_connected_device(esptool.get_port_list(), port=port, connect_attempts=1, initial_baud=115200) |
| 23 | + mac_address = esp.read_mac("BASE_MAC") |
| 24 | + mac_str = '-'.join([f"{b:02X}" for b in mac_address]) |
| 25 | + |
| 26 | + print("MAC Address:", mac_str) |
| 27 | + |
| 28 | + COMBINED_SECRET = MASTER_SECRET + mac_str |
| 29 | + |
| 30 | + HMAC_KEY = hashlib.sha256(COMBINED_SECRET.encode()).digest() |
| 31 | + |
| 32 | + test_mac = hmac.digest(HMAC_KEY, b"test", "sha256") |
| 33 | + print(test_mac.hex()) |
| 34 | + |
| 35 | + efuses, operations = espefuse.get_efuses(esp, do_not_confirm=do_not_confirm) |
| 36 | + class Args: |
| 37 | + name_value_pairs = {} |
| 38 | + args = Args() |
| 39 | + args.name_value_pairs[f"KEY_PURPOSE_{key_to_use}"] = 8 |
| 40 | + args.name_value_pairs[f"BLOCK_KEY{key_to_use}"] = HMAC_KEY |
| 41 | + args.name_value_pairs[f"RD_DIS"] = (1<<(key_to_use)) |
| 42 | + # args.name_value_pairs[f"WR_DIS"] = (1<<(23+key_to_use)) |
| 43 | + print(operations.burn_efuse) |
| 44 | + operations.burn_efuse(esp, efuses, args) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + _in = input("Burn in a loop? (y/N): ") |
| 49 | + loop=False |
| 50 | + if _in.lower() == "y": |
| 51 | + loop = True |
| 52 | + print("Burning fuses on loop. Press Ctrl+C to stop") |
| 53 | + else: |
| 54 | + print("Burning single fuse, waiting for device.") |
| 55 | + while(True): |
| 56 | + ports = esptool.get_port_list() |
| 57 | + if not ports: |
| 58 | + time.sleep(1) |
| 59 | + continue |
| 60 | + burn_hmac_key(do_not_confirm=loop) |
| 61 | + while esptool.get_port_list(): |
| 62 | + time.sleep(1) |
| 63 | + if not loop: |
| 64 | + break |
0 commit comments