mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
fix backalyzer for hosted mode; add support for multiple basis with same name
this is all on the path to fix #355
This commit is contained in:
parent
4a694662c4
commit
ca02b79c63
3 changed files with 53 additions and 40 deletions
|
|
@ -73,14 +73,14 @@ def main():
|
||||||
raise ValueError('Invalid log level: %s' % args.loglevel)
|
raise ValueError('Invalid log level: %s' % args.loglevel)
|
||||||
logging.basicConfig(level=numeric_level)
|
logging.basicConfig(level=numeric_level)
|
||||||
|
|
||||||
basis_credentials = {}
|
basis_credentials = []
|
||||||
if args.basis:
|
if args.basis:
|
||||||
for pair in args.basis:
|
for pair in args.basis:
|
||||||
credpair = pair[0].split(':', 1)
|
credpair = pair[0].split(':', 1)
|
||||||
if len(credpair) != 2:
|
if len(credpair) != 2:
|
||||||
logging.error("Basis credential pair with name {} has a formatting problem, aborting!".format(credpair[0]))
|
logging.error("Basis credential pair with name {} has a formatting problem, aborting!".format(credpair[0]))
|
||||||
exit(1)
|
exit(1)
|
||||||
basis_credentials[credpair[0]] = credpair[1]
|
basis_credentials += [credpair]
|
||||||
|
|
||||||
# insert your mnemonic here. This is the "zero-key" mnemonic.
|
# insert your mnemonic here. This is the "zero-key" mnemonic.
|
||||||
mnemonic = args.backup_key
|
mnemonic = args.backup_key
|
||||||
|
|
@ -303,11 +303,16 @@ def main():
|
||||||
|
|
||||||
keyrom = pt_data[i:i+1024]
|
keyrom = pt_data[i:i+1024]
|
||||||
keys = extract_keys(keyrom, backup[4096:], args.pin, basis_credentials=basis_credentials)
|
keys = extract_keys(keyrom, backup[4096:], args.pin, basis_credentials=basis_credentials)
|
||||||
|
pddb = backup[4096:]
|
||||||
|
|
||||||
|
else:
|
||||||
|
dna_int = 0
|
||||||
|
keyrom = [0] * 1024
|
||||||
|
keys = extract_keys(keyrom, backup, args.pin, basis_credentials=basis_credentials)
|
||||||
|
pddb = backup
|
||||||
|
|
||||||
pddb = backup[4096:]
|
|
||||||
pddb_len = len(pddb)
|
pddb_len = len(pddb)
|
||||||
pddb_size_pages = pddb_len // PAGE_SIZE
|
pddb_size_pages = pddb_len // PAGE_SIZE
|
||||||
|
|
||||||
# data_aad = SYSTEM_BASIS + PDDB_VERSION + dna
|
# data_aad = SYSTEM_BASIS + PDDB_VERSION + dna
|
||||||
|
|
||||||
# now that we have the credentials, extract the baseline image
|
# now that we have the credentials, extract the baseline image
|
||||||
|
|
@ -349,7 +354,11 @@ def main():
|
||||||
pp_start = v2p_table[VPAGE_SIZE]
|
pp_start = v2p_table[VPAGE_SIZE]
|
||||||
pp_data = data[pp_start:pp_start + PAGE_SIZE]
|
pp_data = data[pp_start:pp_start + PAGE_SIZE]
|
||||||
try:
|
try:
|
||||||
pt_data = keycommit_decrypt(key[1], basis_aad(name, dna=dna_int), pp_data)
|
if ':' in name:
|
||||||
|
basis_name = name.split(':')[0]
|
||||||
|
else:
|
||||||
|
basis_name = name
|
||||||
|
pt_data = keycommit_decrypt(key[1], basis_aad(basis_name, dna=dna_int), pp_data)
|
||||||
basis_data.extend(bytearray(pt_data))
|
basis_data.extend(bytearray(pt_data))
|
||||||
logging.debug("decrypted vpage @ {:x} ppage @ {:x}".format(VPAGE_SIZE, v2p_table[VPAGE_SIZE]))
|
logging.debug("decrypted vpage @ {:x} ppage @ {:x}".format(VPAGE_SIZE, v2p_table[VPAGE_SIZE]))
|
||||||
basis = Basis(basis_data)
|
basis = Basis(basis_data)
|
||||||
|
|
@ -359,7 +368,7 @@ def main():
|
||||||
dicts_found = 0
|
dicts_found = 0
|
||||||
dict_index = 0
|
dict_index = 0
|
||||||
while dict_index < MAX_DICTS and dicts_found < basis.num_dicts:
|
while dict_index < MAX_DICTS and dicts_found < basis.num_dicts:
|
||||||
bdict = BasisDicts(dict_index, v2p_table, data, key[1], name, dna=dna_int)
|
bdict = BasisDicts(dict_index, v2p_table, data, key[1], basis_name, dna=dna_int)
|
||||||
if bdict.valid:
|
if bdict.valid:
|
||||||
basis_dicts[bdict.name] = bdict
|
basis_dicts[bdict.name] = bdict
|
||||||
dicts_found += 1
|
dicts_found += 1
|
||||||
|
|
|
||||||
|
|
@ -778,42 +778,45 @@ def get_key(index, keyrom, length):
|
||||||
# - basis_credentials is a list of the secret Bases
|
# - basis_credentials is a list of the secret Bases
|
||||||
# Returns:
|
# Returns:
|
||||||
# - A dictionary of keys, by Basis name
|
# - A dictionary of keys, by Basis name
|
||||||
def extract_keys(keyrom, pddb, boot_pw, basis_credentials={}):
|
def extract_keys(keyrom, pddb, boot_pw, basis_credentials=[]):
|
||||||
user_key_enc = get_key(40, keyrom, 32)
|
user_key_enc = get_key(40, keyrom, 32)
|
||||||
pepper = get_key(248, keyrom, 16)
|
pepper = get_key(248, keyrom, 16)
|
||||||
pepper[0] = pepper[0] ^ 1 # encodes the "boot" password type into the pepper
|
pepper[0] = pepper[0] ^ 1 # encodes the "boot" password type into the pepper
|
||||||
|
|
||||||
# acquire and massage the password so that we can decrypt the encrypted user key
|
if boot_pw is not None:
|
||||||
boot_pw_array = [0] * 73
|
# acquire and massage the password so that we can decrypt the encrypted user key
|
||||||
pw_len = 0
|
boot_pw_array = [0] * 73
|
||||||
for b in bytes(boot_pw.encode('utf-8')):
|
pw_len = 0
|
||||||
boot_pw_array[pw_len] = b
|
for b in bytes(boot_pw.encode('utf-8')):
|
||||||
pw_len += 1
|
boot_pw_array[pw_len] = b
|
||||||
pw_len += 1 # null terminate, so even the null password is one character long
|
pw_len += 1
|
||||||
bcrypter = bcrypt.BCrypt()
|
pw_len += 1 # null terminate, so even the null password is one character long
|
||||||
# logging.debug("{}".format(boot_pw_array[:pw_len]))
|
bcrypter = bcrypt.BCrypt()
|
||||||
logging.debug("user_key_enc: {}".format(list(user_key_enc)))
|
# logging.debug("{}".format(boot_pw_array[:pw_len]))
|
||||||
logging.debug("private_key_enc: {}".format(list(get_key(8, keyrom, 32))))
|
logging.debug("user_key_enc: {}".format(list(user_key_enc)))
|
||||||
logging.debug("salt: {}".format(list(pepper)))
|
logging.debug("private_key_enc: {}".format(list(get_key(8, keyrom, 32))))
|
||||||
hashed_pw = bcrypter.crypt_raw(boot_pw_array[:pw_len], pepper, 7)
|
logging.debug("salt: {}".format(list(pepper)))
|
||||||
logging.debug("hashed_pw: {}".format(list(hashed_pw)))
|
hashed_pw = bcrypter.crypt_raw(boot_pw_array[:pw_len], pepper, 7)
|
||||||
hasher = SHA512.new(truncate="256")
|
logging.debug("hashed_pw: {}".format(list(hashed_pw)))
|
||||||
hasher.update(hashed_pw)
|
|
||||||
user_pw = hasher.digest()
|
|
||||||
|
|
||||||
user_key = []
|
|
||||||
for (a, b) in zip(user_key_enc, user_pw):
|
|
||||||
user_key += [a ^ b]
|
|
||||||
logging.debug("user_key: {}".format(user_key))
|
|
||||||
|
|
||||||
rollback_limit = 255 - int.from_bytes(keyrom[254 * 4 : 254 * 4 + 4], 'little')
|
|
||||||
logging.info("rollback limit: {}".format(rollback_limit))
|
|
||||||
for i in range(rollback_limit):
|
|
||||||
hasher = SHA512.new(truncate="256")
|
hasher = SHA512.new(truncate="256")
|
||||||
hasher.update(bytes(user_key))
|
hasher.update(hashed_pw)
|
||||||
user_key = hasher.digest()
|
user_pw = hasher.digest()
|
||||||
|
|
||||||
logging.debug("hashed_key: {}".format(list(user_key)))
|
user_key = []
|
||||||
|
for (a, b) in zip(user_key_enc, user_pw):
|
||||||
|
user_key += [a ^ b]
|
||||||
|
logging.debug("user_key: {}".format(user_key))
|
||||||
|
|
||||||
|
rollback_limit = 255 - int.from_bytes(keyrom[254 * 4 : 254 * 4 + 4], 'little')
|
||||||
|
logging.info("rollback limit: {}".format(rollback_limit))
|
||||||
|
for i in range(rollback_limit):
|
||||||
|
hasher = SHA512.new(truncate="256")
|
||||||
|
hasher.update(bytes(user_key))
|
||||||
|
user_key = hasher.digest()
|
||||||
|
|
||||||
|
logging.debug("hashed_key: {}".format(list(user_key)))
|
||||||
|
else:
|
||||||
|
user_key = [0] * 32
|
||||||
|
|
||||||
pddb_len = len(pddb)
|
pddb_len = len(pddb)
|
||||||
pddb_size_pages = pddb_len // PAGE_SIZE
|
pddb_size_pages = pddb_len // PAGE_SIZE
|
||||||
|
|
@ -840,7 +843,8 @@ def extract_keys(keyrom, pddb, boot_pw, basis_credentials={}):
|
||||||
keys[SYSTEM_BASIS] = [key_pt, key_data]
|
keys[SYSTEM_BASIS] = [key_pt, key_data]
|
||||||
|
|
||||||
# extract the secret basis keys
|
# extract the secret basis keys
|
||||||
for name, pw in basis_credentials.items():
|
for [name, pw] in basis_credentials:
|
||||||
|
logging.debug("extracting basis: {}:{}".format(name, pw))
|
||||||
bname_copy = [0]*64
|
bname_copy = [0]*64
|
||||||
plaintext_pw = [0]*73
|
plaintext_pw = [0]*73
|
||||||
i = 0
|
i = 0
|
||||||
|
|
@ -866,7 +870,7 @@ def extract_keys(keyrom, pddb, boot_pw, basis_credentials={}):
|
||||||
pt_key = hkdf.derive(hashed_pw)
|
pt_key = hkdf.derive(hashed_pw)
|
||||||
hkdf = HKDF(algorithm=hashes.SHA256(), length=32, salt=pddb_salt[:32], info=b"pddb data key")
|
hkdf = HKDF(algorithm=hashes.SHA256(), length=32, salt=pddb_salt[:32], info=b"pddb data key")
|
||||||
data_key = hkdf.derive(hashed_pw)
|
data_key = hkdf.derive(hashed_pw)
|
||||||
keys[name] = [pt_key, data_key]
|
keys[name + ':' + pw] = [pt_key, data_key]
|
||||||
|
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,14 +47,14 @@ def main():
|
||||||
if args.renode is True:
|
if args.renode is True:
|
||||||
keyfile = "./emulation/renode-keybox.bin"
|
keyfile = "./emulation/renode-keybox.bin"
|
||||||
imagefile = "./tools/pddb-images/renode.bin"
|
imagefile = "./tools/pddb-images/renode.bin"
|
||||||
basis_credentials = {}
|
basis_credentials = []
|
||||||
if args.basis:
|
if args.basis:
|
||||||
for pair in args.basis:
|
for pair in args.basis:
|
||||||
credpair = pair[0].split(':', 1)
|
credpair = pair[0].split(':', 1)
|
||||||
if len(credpair) != 2:
|
if len(credpair) != 2:
|
||||||
logging.error("Basis credential pair with name {} has a formatting problem, aborting!".format(credpair[0]))
|
logging.error("Basis credential pair with name {} has a formatting problem, aborting!".format(credpair[0]))
|
||||||
exit(1)
|
exit(1)
|
||||||
basis_credentials[credpair[0]] = credpair[1]
|
basis_credentials += [credpair]
|
||||||
|
|
||||||
with open(keyfile, 'rb') as key_f:
|
with open(keyfile, 'rb') as key_f:
|
||||||
keyrom = key_f.read()
|
keyrom = key_f.read()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue