def make_bootloader(): """ Build the bootloader from source. This also loads all keys (symmetric and non-symmetric) into secret_build_output.txt Return: True if successful, False otherwise. """ # Change into directory containing bootloader. bootloader = FILE_DIR / '..' / 'bootloader' os.chdir(bootloader) rsa_key = RSA.generate(2048) #need to provision: RSA modulus, exponent, exponent size modulus = rsa_key.publickey().n exponent = rsa_key.publickey().e exponent_size = len(exponent) # f = open('mykey.pem','wb') # f.write(rsa_key.export_key('PEM')) # f.close() aes_key = AES.get_random_bytes(16) # print('BEFORE WRITING: \n') # print('RSA key: ', rsa_key) # print('AES key: ', aes_key) with open('secret_build_output.txt', 'w+b') as fh: fh.write(aes_key)
def encrypt(key, data): iv = AES.get_random_bytes(16) cipher = AES.new(key, AES.MODE_CBC, iv=iv) b_data = (data).encode() b_data += b'\x00' * (16 - (len(b_data) % 16)) print("plain_text = ", b_data) ciphertext = cipher.encrypt(b_data) print("cipher_text = ", ciphertext) return ciphertext, iv
def make_bootloader(): """ Build the bootloader from source. This also loads all keys (symmetric and non-symmetric) into secret_build_output.txt Return: True if successful, False otherwise. """ # Change into directory containing bootloader. rsa_key = RSA.generate( 2048 ) # generates a private RSA key object so that a public exponent and modulus can be created #need to provision: RSA modulus, exponent, exponent size # public RSA modulus modulus = rsa_key.publickey().n # public RSA exponent exponent = rsa_key.publickey().e # size of exponent, for later use in authentication exponent_size = 8 aes_key = AES.get_random_bytes(16) # generates a random 16 byte AES key with open( 'secret_build_output.txt', 'wb+' ) as fh: # writes the AES and RSA private key in the {secret_build_output.txt} file print("test") fh.write( aes_key ) # this allows the fw_protect tool to import these keys and encrypt/sign data fh.write(rsa_key.export_key()) bootloader = FILE_DIR / '..' / 'bootloader' os.chdir(bootloader) subprocess.call( 'make clean', shell=True) #allows us to pass in arguments to the make file #sets all variables in makefile according to ones above (the aes symmetric key, modulus, exponent, and exponent size) status = subprocess.call( f'make KEY={to_c_array(aes_key)} MOD={to_c_array((rsa_key.publickey().n).to_bytes(256, "big"))} EXP={to_c_array(struct.pack(">Q", rsa_key.publickey().e))} E_SIZE=8', shell=True) # Return True if make returned 0, otherwise return False. return (status == 0)
def aes_make_key(key_len=128): key = AES.get_random_bytes(key_len//8) iv = AES.get_random_bytes(128//8) return key, iv
tmp = (tmp >> 1) ^ self.poly else: tmp >>= 1 byte >>= 1 self.table.append(tmp) def calc(self, string): value = self.mask for c in string: value = self.table[(ord(c) ^ value) & 0xFF] ^ (value >> 8) return struct.pack('>I', (-1 - value) & self.mask) sys.stdout = Unbuffered(sys.stdout) FLAG = open('flag.txt').read().strip() KEY = AES.get_random_bytes(16) CC = Cyclic() def intro(): print ''' ```````````````````````````````````````````````````````````````` `````````````````````...----::::::::::---..````````````````````` `````````````-/+syhdddmmmmmmddddddddmmmmmmddhhyyso+:```````````` ````````````.yhhhhdddmmmmmmddddddddddmmmmmdddddhhhhh+``````````` ````````````-yhhhhdddmmmmmddddddddddddddmmmddddhhhhhs``````````` ````````````:yhhhhhhhddddmdddddddddddddddddhhhhhhhhhy``````````` ````````````/yyo------:/+shdddddddddddhso++::--:+yyyy``````````` ````````````/yyoyyyyys+:.``:ohdddddho-``-/osyhyyo+yyy.`````````` ````````````/syyyhhhhhhhys/-`-hdddy.`-+syhhhhhhhyyyyy.`````````` ````````````/ssyyyyyyyyyyyssosddddhoosyyyyyyyyyyyyyss```````````