Created
December 24, 2021 19:03
-
-
Save Scar26/079658ba91fbd3282b89af8732efe147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import os | |
from lfsr import LFSR | |
from secret import FLAG, PRIMITIVE_POLYNOMIAL | |
def bytes_to_bits(_bytes): | |
str_bits = ''.join([bin(int(_byte))[2:].zfill(8) for _byte in _bytes]) | |
int_bits = [int(bit) for bit in str_bits] | |
return int_bits | |
def keystream_encrypt(pt, prng): | |
return bytes([a^b for a,b in zip(pt, prng.get_random_bytes(len(pt)))]) | |
seed_len_in_bytes = len(PRIMITIVE_POLYNOMIAL) // 8 # seed_len_in_bytes does not exceed 32 | |
seed = bytes_to_bits(os.urandom(seed_len_in_bytes)) | |
coeffs = PRIMITIVE_POLYNOMIAL | |
encrypted_flag = keystream_encrypt(FLAG, LFSR(seed=seed, coeffs=coeffs)) | |
if __name__ == "__main__": | |
print("The encrypted flag is " + encrypted_flag.hex()) | |
pt = bytes.fromhex(input("Enter Flag: ")) | |
prng = LFSR(seed=seed, coeffs=coeffs) | |
ct = keystream_encrypt(pt, prng) | |
print(prng.power_trace) | |
if ct == encrypted_flag: | |
print("Probabilistically Impossible") | |
sys.exit(0) | |
else: | |
print("Try again...\n") | |
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lfsr_hardware | |
class LFSR: | |
def __init__(self, **kwargs): | |
lfsr_hardware.init() | |
lfsr_hardware.set_coeffs(kwargs['coeffs']) | |
lfsr_hardware.set_state(kwargs['seed']) | |
self.power_trace = [] | |
def get_next(self): | |
lfsr_hardware.trigger_high() | |
bit = lfsr_hardware.get_next() | |
lfsr_hardware.trigger_low() | |
power_consumed = lfsr_hardware.get_power() | |
self.power_trace.append(power_consumed) | |
return bit | |
def get_next_byte(self): | |
next_byte = 0 | |
for i in range(8): | |
next_byte |= self.get_next() | |
if (i != 7): next_byte <<= 1 | |
return bytes([next_byte]) | |
def get_random_bytes(self, n): | |
rand_bytes = b"" | |
for i in range(n): | |
rand_bytes += self.get_next_byte() | |
return rand_bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lfsr_hardware: | |
- n: length of lfsr | |
- s: state | |
- c: coeffs | |
- init(): clear s and c | |
- set_coeffs(coeffs): set c[i] = coeffs[i] for i = 0..n-1 | |
- set_state(state): set s[n-i-1] = state[i] for i = 0..n-1 | |
- get_next(): | |
compute s[n] = summation s[i] xor c[i] for i = 0..n-1 | |
return s[0] to the software interface | |
set {s[0], s[1], ..., s[n-1]} = {s[1], s[2], ..., s[n]} | |
- trigger_high(): start recording power | |
- trigger_low(): stop recording power | |
- get_power(): return the power recorded between trigger_high and trigger_low to the software interface |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment