2021-11-14 11:02:34 +00:00
#! /usr/bin/env python3
import argparse
import sys
from Crypto . Cipher import AES
from rfc8452 import AES_GCM_SIV
evict salamanders from AES-GCM-SIV in the PDDB
So you'd like to think that an authenticating cipher would like,
authenticate stuff on decryption. And AES-GCM-SIV was billed as sort
of AES with the pointy edges taken off.
Well, there's always one more bug, actually this time a salamander:
https://eprint.iacr.org/2020/1456.pdf Apparently one can forge
authentication if the attacker can control keys and ciphertext. In the
case of the PDDB I think the attack is largely theoretical, because
you'd have to forge through both the AES-GCM-SIV and the rather
insufficient nonce/murmur hash check on the PTE that locates the page
to be decrypted.
But it's never good to have cryptographic properties depend upon
something that's known to be insufficient, so let's assume the attacker
can simply forge keys and stick some data on the device.
This wouldn't be a problem for confidentiality but it could lead to
the ability to plant evidence, but more importantly could lead to code
paths that were thought to be protected against fuzzing being fuzzed
as you could conceivably create a Basis root record that "checked out"
and then the larger body of the PDDB code would try to chomp on other
pages that are forged, leading to a bigger attack surface. Plus who
knows what else might exist beyond salamanders...
This patch does a "key commitment" on the Basis root page, so that it
is now cryptographically hard to create a salamander. Basically
instead of using the system root key directly in AES-GCM-SIV, the key
goes through a KDF that expands it with yet another nonce plus a bit
of header data. The KDF is SHA512/256 and it expands it twice,
changing one bit in the header data (as prescribed by the paper cited
above), to create a version of the key that is written to disk (the
commitment), and a version of the key that is used to
decrypt/authenticate the AES-GCM-SIV data. Only if both the commitment
and the authentication check out, do we accept a Basis root page as
valid.
I had some concerns about the slightly different structure of the
Basis Root page being detectable, but I have been assured that the key
commitment form and the MAC in AES-GCM-SIV satisfy a property known as
IND$-CPA ("indistinguishable from uniform randomness by a
chosen-plaintext attacker") by Rogaway[1], and, more strongly, "Secure
nonce misuse-resistant authenticated encryption (mrAE)" by Gueron and
Lindell (see appendix for def)[2]. Still, just out of total paranoia
and nothing else, I organize the basis root page such that the MAC is
in the identical location as the other non-committed pages...just in
case there is some mathematical property of the MAC that could be
leveraged to leak info about the existence of the basis root record.
The commitment nonce and sha512/256 result I understand better and
feel pretty comfortable that they are going to be strongly random and
indistinguishable from AES ciphertext.
Thanks to Michael Rosenberg for taking the time to explain all this to
me and to guide me through the process of making the fixes. It is a
major PDDB API-breaking change and I'm glad it's able to be rolled out
before most users have received a device.
[1] https://web.cs.ucdavis.edu/~rogaway/papers/ad.pdf
[2] https://eprint.iacr.org/2015/102
2022-02-27 19:13:26 +00:00
from Crypto . Hash import SHA512
import binascii
2021-11-14 11:02:34 +00:00
2021-12-27 05:07:01 +00:00
import logging
2021-11-14 11:02:34 +00:00
SYSTEM_BASIS = ' .System '
2021-11-15 11:05:03 +00:00
PAGE_SIZE = 4096
2021-11-17 18:01:30 +00:00
VPAGE_SIZE = 4064
2021-11-15 11:05:03 +00:00
MBBB_PAGES = 10
2021-11-14 11:02:34 +00:00
2021-11-22 10:26:19 +00:00
# build a table mapping all non-printable characters to None
NOPRINT_TRANS_TABLE = {
i : None for i in range ( 0 , sys . maxunicode + 1 ) if not chr ( i ) . isprintable ( )
}
def make_printable ( s ) :
global NOPRINT_TRANS_TABLE
""" Replace non-printable characters in a string. """
# the translate method on str removes characters
# that map to None from the string
return s . translate ( NOPRINT_TRANS_TABLE )
2021-11-14 11:02:34 +00:00
# from https://github.com/wc-duck/pymmh3/blob/master/pymmh3.py
def xrange ( a , b , c ) :
return range ( a , b , c )
def xencode ( x ) :
if isinstance ( x , bytes ) or isinstance ( x , bytearray ) :
return x
else :
return x . encode ( )
def mm3_hash ( key , seed = 0x0 ) :
''' Implements 32bit murmur3 hash. '''
key = bytearray ( xencode ( key ) )
def fmix ( h ) :
h ^ = h >> 16
h = ( h * 0x85ebca6b ) & 0xFFFFFFFF
h ^ = h >> 13
h = ( h * 0xc2b2ae35 ) & 0xFFFFFFFF
h ^ = h >> 16
return h
length = len ( key )
nblocks = int ( length / 4 )
h1 = seed
c1 = 0xcc9e2d51
c2 = 0x1b873593
# body
for block_start in xrange ( 0 , nblocks * 4 , 4 ) :
# ??? big endian?
k1 = key [ block_start + 3 ] << 24 | \
key [ block_start + 2 ] << 16 | \
key [ block_start + 1 ] << 8 | \
key [ block_start + 0 ]
k1 = ( c1 * k1 ) & 0xFFFFFFFF
k1 = ( k1 << 15 | k1 >> 17 ) & 0xFFFFFFFF # inlined ROTL32
k1 = ( c2 * k1 ) & 0xFFFFFFFF
h1 ^ = k1
h1 = ( h1 << 13 | h1 >> 19 ) & 0xFFFFFFFF # inlined ROTL32
h1 = ( h1 * 5 + 0xe6546b64 ) & 0xFFFFFFFF
# tail
tail_index = nblocks * 4
k1 = 0
tail_size = length & 3
if tail_size > = 3 :
k1 ^ = key [ tail_index + 2 ] << 16
if tail_size > = 2 :
k1 ^ = key [ tail_index + 1 ] << 8
if tail_size > = 1 :
k1 ^ = key [ tail_index + 0 ]
if tail_size > 0 :
k1 = ( k1 * c1 ) & 0xFFFFFFFF
k1 = ( k1 << 15 | k1 >> 17 ) & 0xFFFFFFFF # inlined ROTL32
k1 = ( k1 * c2 ) & 0xFFFFFFFF
h1 ^ = k1
#finalization
return fmix ( h1 ^ length )
# weird, this code breaks things compared to the reference Rust implementation
unsigned_val = fmix ( h1 ^ length )
if unsigned_val & 0x80000000 == 0 :
return unsigned_val
else :
return - ( ( unsigned_val ^ 0xFFFFFFFF ) + 1 )
evict salamanders from AES-GCM-SIV in the PDDB
So you'd like to think that an authenticating cipher would like,
authenticate stuff on decryption. And AES-GCM-SIV was billed as sort
of AES with the pointy edges taken off.
Well, there's always one more bug, actually this time a salamander:
https://eprint.iacr.org/2020/1456.pdf Apparently one can forge
authentication if the attacker can control keys and ciphertext. In the
case of the PDDB I think the attack is largely theoretical, because
you'd have to forge through both the AES-GCM-SIV and the rather
insufficient nonce/murmur hash check on the PTE that locates the page
to be decrypted.
But it's never good to have cryptographic properties depend upon
something that's known to be insufficient, so let's assume the attacker
can simply forge keys and stick some data on the device.
This wouldn't be a problem for confidentiality but it could lead to
the ability to plant evidence, but more importantly could lead to code
paths that were thought to be protected against fuzzing being fuzzed
as you could conceivably create a Basis root record that "checked out"
and then the larger body of the PDDB code would try to chomp on other
pages that are forged, leading to a bigger attack surface. Plus who
knows what else might exist beyond salamanders...
This patch does a "key commitment" on the Basis root page, so that it
is now cryptographically hard to create a salamander. Basically
instead of using the system root key directly in AES-GCM-SIV, the key
goes through a KDF that expands it with yet another nonce plus a bit
of header data. The KDF is SHA512/256 and it expands it twice,
changing one bit in the header data (as prescribed by the paper cited
above), to create a version of the key that is written to disk (the
commitment), and a version of the key that is used to
decrypt/authenticate the AES-GCM-SIV data. Only if both the commitment
and the authentication check out, do we accept a Basis root page as
valid.
I had some concerns about the slightly different structure of the
Basis Root page being detectable, but I have been assured that the key
commitment form and the MAC in AES-GCM-SIV satisfy a property known as
IND$-CPA ("indistinguishable from uniform randomness by a
chosen-plaintext attacker") by Rogaway[1], and, more strongly, "Secure
nonce misuse-resistant authenticated encryption (mrAE)" by Gueron and
Lindell (see appendix for def)[2]. Still, just out of total paranoia
and nothing else, I organize the basis root page such that the MAC is
in the identical location as the other non-committed pages...just in
case there is some mathematical property of the MAC that could be
leveraged to leak info about the existence of the basis root record.
The commitment nonce and sha512/256 result I understand better and
feel pretty comfortable that they are going to be strongly random and
indistinguishable from AES ciphertext.
Thanks to Michael Rosenberg for taking the time to explain all this to
me and to guide me through the process of making the fixes. It is a
major PDDB API-breaking change and I'm glad it's able to be rolled out
before most users have received a device.
[1] https://web.cs.ucdavis.edu/~rogaway/papers/ad.pdf
[2] https://eprint.iacr.org/2015/102
2022-02-27 19:13:26 +00:00
def keycommit_decrypt ( key , aad , pp_data ) :
# print([hex(d) for d in pp_data[:32]])
nonce = pp_data [ : 12 ]
ct = pp_data [ 12 : 12 + 4004 ]
kcom_nonce = pp_data [ 12 + 4004 : 12 + 4004 + 32 ]
kcom = pp_data [ 12 + 4004 + 32 : 12 + 4004 + 32 + 32 ]
mac = pp_data [ - 16 : ]
#print([hex(d) for d in kcom_nonce])
# print([hex(d) for d in kcom])
#h_test = SHA512.new(truncate="256") # just something to make sure our hash functions are sane
#h_test.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]))
#print(h_test.hexdigest())
h_enc = SHA512 . new ( truncate = " 256 " )
h_enc . update ( key )
h_enc . update ( bytes ( [ 0x43 , 0x6f , 0x6 , 0xd6 , 0xd , 0x69 , 0x74 , 0x01 , 0x01 ] ) )
h_enc . update ( kcom_nonce )
k_enc = h_enc . digest ( )
h_com = SHA512 . new ( truncate = " 256 " )
h_com . update ( key )
h_com . update ( bytes ( [ 0x43 , 0x6f , 0x6 , 0xd6 , 0xd , 0x69 , 0x74 , 0x01 , 0x02 ] ) )
h_com . update ( kcom_nonce )
k_com_derived = h_com . digest ( )
print ( ' kcom_stored: ' + binascii . hexlify ( kcom ) . decode ( ' utf-8 ' ) )
print ( ' kcom_derived: ' + binascii . hexlify ( k_com_derived ) . decode ( ' utf-8 ' ) )
cipher = AES_GCM_SIV ( k_enc , nonce )
pt_data = cipher . decrypt ( ct + mac , aad )
if k_com_derived != kcom :
print ( " basis failed key commit test " )
raise Exception ( ValueError )
return pt_data
2021-11-14 11:02:34 +00:00
def main ( ) :
parser = argparse . ArgumentParser ( description = " Debug PDDB Images " )
parser . add_argument (
2021-11-22 06:00:43 +00:00
" --name " , required = False , help = " pddb disk image root name " , type = str , nargs = ' ? ' , metavar = ( ' name ' ) , const = ' ./pddb '
2021-11-14 11:02:34 +00:00
)
2021-12-27 05:07:01 +00:00
parser . add_argument (
2021-12-27 08:15:51 +00:00
" --loglevel " , required = False , help = " set logging level (INFO/DEBUG/WARNING/ERROR) " , type = str , default = " INFO " ,
2021-12-27 05:07:01 +00:00
)
2021-11-14 11:02:34 +00:00
args = parser . parse_args ( )
2021-11-22 06:00:43 +00:00
if args . name == None :
keyfile = ' ./tools/pddb-images/pddb.key '
imagefile = ' ./tools/pddb-images/pddb.bin '
2021-11-14 11:02:34 +00:00
else :
2021-11-22 06:00:43 +00:00
keyfile = ' ./tools/pddb-images/ {} .key ' . format ( args . name )
imagefile = ' ./tools/pddb-images/ {} .bin ' . format ( args . name )
2021-11-14 11:02:34 +00:00
2021-12-27 05:07:01 +00:00
numeric_level = getattr ( logging , args . loglevel . upper ( ) , None )
if not isinstance ( numeric_level , int ) :
raise ValueError ( ' Invalid log level: %s ' % args . loglevel )
logging . basicConfig ( level = numeric_level )
2021-11-14 11:02:34 +00:00
keys = { }
with open ( keyfile , ' rb ' ) as key_f :
raw_key = key_f . read ( )
num_keys = int . from_bytes ( raw_key [ : 4 ] , ' little ' )
for i in range ( num_keys ) :
name_all = raw_key [ 4 + i * 96 : 4 + i * 96 + 64 ]
name_bytes = bytearray ( b ' ' )
for b in name_all :
if b != 0 :
name_bytes . append ( b )
else :
break
name = name_bytes . decode ( ' utf8 ' , errors = ' ignore ' )
key = raw_key [ 4 + i * 96 + 64 : 4 + i * 96 + 96 ]
keys [ name ] = key
2021-12-27 05:07:01 +00:00
logging . info ( " Found basis keys: " )
logging . info ( str ( keys ) )
2021-11-14 11:02:34 +00:00
2021-11-27 13:23:42 +00:00
# tunable parameters for a filesystem
2021-11-15 11:05:03 +00:00
global MBBB_PAGES
2021-11-27 13:23:42 +00:00
FSCB_PAGES = 16
FSCB_LEN_PAGES = 2
2021-11-14 11:02:34 +00:00
KEY_PAGES = 1
2021-11-15 11:05:03 +00:00
global PAGE_SIZE
2021-11-17 18:01:30 +00:00
global VPAGE_SIZE
2021-11-22 18:57:13 +00:00
MAX_DICTS = 16384
2021-11-14 11:02:34 +00:00
with open ( imagefile , ' rb ' ) as img_f :
raw_img = img_f . read ( )
pddb_len = len ( raw_img )
pddb_size_pages = pddb_len / / PAGE_SIZE
2022-01-06 15:15:24 +00:00
logging . info ( " Disk size: 0x {:x} " . format ( pddb_len ) )
2021-11-14 11:02:34 +00:00
2021-11-15 11:05:03 +00:00
mbbb_offset = pddb_size_pages * Pte . PTE_LEN + PAGE_SIZE * KEY_PAGES
2022-01-06 15:15:24 +00:00
if mbbb_offset & ( PAGE_SIZE - 1 ) != 0 :
mbbb_offset = ( mbbb_offset + PAGE_SIZE ) & 0xFFFF_F000 # round up to nearest page
logging . info ( " MBBB: 0x {:x} " . format ( mbbb_offset ) )
2021-11-14 11:02:34 +00:00
img_index = 0
2021-11-15 11:05:03 +00:00
tables = decode_pagetable ( raw_img , pddb_size_pages , keys , raw_img [ mbbb_offset : mbbb_offset + MBBB_PAGES * PAGE_SIZE ] )
2021-11-14 11:02:34 +00:00
img_index + = pddb_size_pages * Pte . PTE_LEN
2022-01-06 15:15:24 +00:00
if img_index & ( PAGE_SIZE - 1 ) != 0 :
img_index = ( img_index + PAGE_SIZE ) & 0xFFFF_F000
2021-11-14 11:02:34 +00:00
rawkeys = raw_img [ img_index : img_index + PAGE_SIZE * KEY_PAGES ]
2022-01-06 15:15:24 +00:00
logging . debug ( " Keys: 0x {:x} " . format ( img_index ) )
2021-11-14 11:02:34 +00:00
img_index + = PAGE_SIZE * KEY_PAGES
2022-01-06 15:15:24 +00:00
2021-11-14 11:02:34 +00:00
mbbb = raw_img [ img_index : img_index + PAGE_SIZE * MBBB_PAGES ]
2022-01-06 15:15:24 +00:00
logging . debug ( " MBBB check: 0x {:x} " . format ( img_index ) )
2021-11-14 11:02:34 +00:00
img_index + = PAGE_SIZE * MBBB_PAGES
2022-01-06 15:15:24 +00:00
2021-11-27 13:23:42 +00:00
fscb = decode_fscb ( raw_img [ img_index : img_index + PAGE_SIZE * FSCB_PAGES ] , keys , FSCB_LEN_PAGES = FSCB_LEN_PAGES )
2022-01-06 15:15:24 +00:00
logging . debug ( " FSCB: 0x {:x} " . format ( img_index ) )
2021-11-14 20:03:55 +00:00
img_index + = PAGE_SIZE * FSCB_PAGES
2022-01-06 15:15:24 +00:00
logging . debug ( " Data: 0x {:x} " . format ( img_index ) )
2021-11-14 20:03:55 +00:00
data = raw_img [ img_index : ]
for name , key in keys . items ( ) :
if name in tables :
2021-12-27 05:07:01 +00:00
logging . info ( " Basis {} , key: {} " . format ( name , key . hex ( ) ) )
2021-11-14 20:03:55 +00:00
v2p_table = tables [ name ] [ 0 ]
p2v_table = tables [ name ] [ 1 ]
2021-11-23 15:33:06 +00:00
# v2p_table[0xfe0fe0] = 0x1200* 0x100
2021-11-14 20:03:55 +00:00
basis_data = bytearray ( )
2021-11-17 18:01:30 +00:00
pp_start = v2p_table [ VPAGE_SIZE ]
evict salamanders from AES-GCM-SIV in the PDDB
So you'd like to think that an authenticating cipher would like,
authenticate stuff on decryption. And AES-GCM-SIV was billed as sort
of AES with the pointy edges taken off.
Well, there's always one more bug, actually this time a salamander:
https://eprint.iacr.org/2020/1456.pdf Apparently one can forge
authentication if the attacker can control keys and ciphertext. In the
case of the PDDB I think the attack is largely theoretical, because
you'd have to forge through both the AES-GCM-SIV and the rather
insufficient nonce/murmur hash check on the PTE that locates the page
to be decrypted.
But it's never good to have cryptographic properties depend upon
something that's known to be insufficient, so let's assume the attacker
can simply forge keys and stick some data on the device.
This wouldn't be a problem for confidentiality but it could lead to
the ability to plant evidence, but more importantly could lead to code
paths that were thought to be protected against fuzzing being fuzzed
as you could conceivably create a Basis root record that "checked out"
and then the larger body of the PDDB code would try to chomp on other
pages that are forged, leading to a bigger attack surface. Plus who
knows what else might exist beyond salamanders...
This patch does a "key commitment" on the Basis root page, so that it
is now cryptographically hard to create a salamander. Basically
instead of using the system root key directly in AES-GCM-SIV, the key
goes through a KDF that expands it with yet another nonce plus a bit
of header data. The KDF is SHA512/256 and it expands it twice,
changing one bit in the header data (as prescribed by the paper cited
above), to create a version of the key that is written to disk (the
commitment), and a version of the key that is used to
decrypt/authenticate the AES-GCM-SIV data. Only if both the commitment
and the authentication check out, do we accept a Basis root page as
valid.
I had some concerns about the slightly different structure of the
Basis Root page being detectable, but I have been assured that the key
commitment form and the MAC in AES-GCM-SIV satisfy a property known as
IND$-CPA ("indistinguishable from uniform randomness by a
chosen-plaintext attacker") by Rogaway[1], and, more strongly, "Secure
nonce misuse-resistant authenticated encryption (mrAE)" by Gueron and
Lindell (see appendix for def)[2]. Still, just out of total paranoia
and nothing else, I organize the basis root page such that the MAC is
in the identical location as the other non-committed pages...just in
case there is some mathematical property of the MAC that could be
leveraged to leak info about the existence of the basis root record.
The commitment nonce and sha512/256 result I understand better and
feel pretty comfortable that they are going to be strongly random and
indistinguishable from AES ciphertext.
Thanks to Michael Rosenberg for taking the time to explain all this to
me and to guide me through the process of making the fixes. It is a
major PDDB API-breaking change and I'm glad it's able to be rolled out
before most users have received a device.
[1] https://web.cs.ucdavis.edu/~rogaway/papers/ad.pdf
[2] https://eprint.iacr.org/2015/102
2022-02-27 19:13:26 +00:00
# print("pp_start: {:x}".format(pp_start))
2021-11-17 18:01:30 +00:00
pp_data = data [ pp_start : pp_start + PAGE_SIZE ]
try :
evict salamanders from AES-GCM-SIV in the PDDB
So you'd like to think that an authenticating cipher would like,
authenticate stuff on decryption. And AES-GCM-SIV was billed as sort
of AES with the pointy edges taken off.
Well, there's always one more bug, actually this time a salamander:
https://eprint.iacr.org/2020/1456.pdf Apparently one can forge
authentication if the attacker can control keys and ciphertext. In the
case of the PDDB I think the attack is largely theoretical, because
you'd have to forge through both the AES-GCM-SIV and the rather
insufficient nonce/murmur hash check on the PTE that locates the page
to be decrypted.
But it's never good to have cryptographic properties depend upon
something that's known to be insufficient, so let's assume the attacker
can simply forge keys and stick some data on the device.
This wouldn't be a problem for confidentiality but it could lead to
the ability to plant evidence, but more importantly could lead to code
paths that were thought to be protected against fuzzing being fuzzed
as you could conceivably create a Basis root record that "checked out"
and then the larger body of the PDDB code would try to chomp on other
pages that are forged, leading to a bigger attack surface. Plus who
knows what else might exist beyond salamanders...
This patch does a "key commitment" on the Basis root page, so that it
is now cryptographically hard to create a salamander. Basically
instead of using the system root key directly in AES-GCM-SIV, the key
goes through a KDF that expands it with yet another nonce plus a bit
of header data. The KDF is SHA512/256 and it expands it twice,
changing one bit in the header data (as prescribed by the paper cited
above), to create a version of the key that is written to disk (the
commitment), and a version of the key that is used to
decrypt/authenticate the AES-GCM-SIV data. Only if both the commitment
and the authentication check out, do we accept a Basis root page as
valid.
I had some concerns about the slightly different structure of the
Basis Root page being detectable, but I have been assured that the key
commitment form and the MAC in AES-GCM-SIV satisfy a property known as
IND$-CPA ("indistinguishable from uniform randomness by a
chosen-plaintext attacker") by Rogaway[1], and, more strongly, "Secure
nonce misuse-resistant authenticated encryption (mrAE)" by Gueron and
Lindell (see appendix for def)[2]. Still, just out of total paranoia
and nothing else, I organize the basis root page such that the MAC is
in the identical location as the other non-committed pages...just in
case there is some mathematical property of the MAC that could be
leveraged to leak info about the existence of the basis root record.
The commitment nonce and sha512/256 result I understand better and
feel pretty comfortable that they are going to be strongly random and
indistinguishable from AES ciphertext.
Thanks to Michael Rosenberg for taking the time to explain all this to
me and to guide me through the process of making the fixes. It is a
major PDDB API-breaking change and I'm glad it's able to be rolled out
before most users have received a device.
[1] https://web.cs.ucdavis.edu/~rogaway/papers/ad.pdf
[2] https://eprint.iacr.org/2015/102
2022-02-27 19:13:26 +00:00
pt_data = keycommit_decrypt ( key , basis_aad ( name ) , pp_data )
2021-11-17 18:01:30 +00:00
basis_data . extend ( bytearray ( pt_data ) )
2021-12-27 05:07:01 +00:00
logging . debug ( " decrypted vpage @ {:x} ppage @ {:x} " . format ( VPAGE_SIZE , v2p_table [ VPAGE_SIZE ] ) )
2021-11-17 16:09:55 +00:00
# print([hex(x) for x in basis_data[:256]])
2021-11-14 20:03:55 +00:00
basis = Basis ( basis_data )
2021-12-27 05:07:01 +00:00
logging . info ( basis . as_str ( ) )
2021-11-14 20:03:55 +00:00
2021-11-17 18:01:30 +00:00
basis_dicts = { }
2021-11-22 18:57:13 +00:00
dicts_found = 0
dict_index = 0
while dict_index < MAX_DICTS and dicts_found < basis . num_dicts :
2021-11-17 18:01:30 +00:00
bdict = BasisDicts ( dict_index , v2p_table , data , key , name )
2021-11-22 18:57:13 +00:00
if bdict . valid :
basis_dicts [ bdict . name ] = bdict
dicts_found + = 1
dict_index + = 1
if dicts_found != basis . num_dicts :
2021-12-27 05:07:01 +00:00
logging . error ( " Expected {} dictionaries, only found {} ; searched {} " . format ( basis . num_dicts , dicts_found , dict_index ) )
2021-11-22 18:57:13 +00:00
found_all_dicts = False
else :
found_all_dicts = True
2021-11-17 18:01:30 +00:00
2021-12-27 05:07:01 +00:00
logging . debug ( " Dictionaries: " )
2021-11-17 18:01:30 +00:00
for bdict in basis_dicts . values ( ) :
2021-12-27 05:07:01 +00:00
logging . debug ( bdict . as_str ( ) )
2021-11-17 18:01:30 +00:00
2021-12-27 22:47:33 +00:00
#d2 = basis_dicts["dict2"]
#logging.info("listing {} keys".format(len(d2.keys)))
#for key in d2.keys:
# logging.info("{}".format(key))
2021-12-27 05:07:01 +00:00
logging . info ( " CI checks: " )
2021-11-22 10:26:19 +00:00
for bdict in basis_dicts . values ( ) :
bdict . ci_check ( )
2021-11-22 18:57:13 +00:00
if found_all_dicts :
2021-12-27 05:07:01 +00:00
logging . info ( " All dicts were found. " )
2021-11-22 18:57:13 +00:00
else :
2021-12-27 05:07:01 +00:00
logging . error ( " Missing dictionaries, something is wrong. " )
2021-11-22 10:26:19 +00:00
2021-11-17 18:01:30 +00:00
except ValueError :
2021-12-27 05:07:01 +00:00
logging . error ( " couldn ' t decrypt basis root vpage @ {:x} ppage @ {:x} " . format ( VPAGE_SIZE , v2p_table [ VPAGE_SIZE ] ) )
2021-11-17 18:01:30 +00:00
2021-11-23 15:33:06 +00:00
PRINTED_FULL = False
2021-11-20 18:21:25 +00:00
class KeyDescriptor :
MAX_NAME_LEN = 95
def __init__ ( self , record , v2p , disk , key , name ) :
2021-11-21 20:46:36 +00:00
# print(record.hex())
2021-11-20 18:21:25 +00:00
i = 0
self . start = int . from_bytes ( record [ i : i + 8 ] , ' little ' )
i + = 8
self . len = int . from_bytes ( record [ i : i + 8 ] , ' little ' )
i + = 8
self . reserved = int . from_bytes ( record [ i : i + 8 ] , ' little ' )
i + = 8
self . flags_code = int . from_bytes ( record [ i : i + 4 ] , ' little ' )
i + = 4
self . age = int . from_bytes ( record [ i : i + 4 ] , ' little ' )
2021-11-22 10:26:19 +00:00
i + = 4
2022-01-06 10:53:01 +00:00
self . name = record [ i + 1 : i + 1 + record [ i ] ] . decode ( ' utf8 ' , errors = ' ignore ' )
2021-11-20 18:21:25 +00:00
if self . flags_code & 1 != 0 :
self . valid = True
else :
self . valid = False
if self . flags_code & 2 != 0 :
self . unresolved = True
else :
self . unresolved = False
if self . valid :
2021-11-25 19:08:08 +00:00
# print('valid key')
2021-11-20 18:21:25 +00:00
page_addr = self . start
self . data = bytearray ( )
remaining = self . len
read_start = self . start % VPAGE_SIZE # reads can start not page-aligned
while page_addr < self . start + self . len :
data_base = ( page_addr / / VPAGE_SIZE ) * VPAGE_SIZE
2021-12-27 22:47:33 +00:00
try :
pp_start = v2p [ data_base ]
except KeyError :
logging . error ( " key {} is missing data allocation at va {:x} " . format ( self . name , data_base ) )
self . ci_ok = False
raise KeyError
2021-11-20 18:21:25 +00:00
pp_data = disk [ pp_start : pp_start + PAGE_SIZE ]
try :
cipher = AES_GCM_SIV ( key , pp_data [ : 12 ] )
pt_data = cipher . decrypt ( pp_data [ 12 : ] , basis_aad ( name ) ) [ 4 : ] # skip the journal, first 4 bytes
if read_start + remaining > VPAGE_SIZE :
read_end = VPAGE_SIZE
else :
read_end = read_start + remaining
self . data + = pt_data [ read_start : read_end ]
bytes_read = read_end - read_start
remaining - = bytes_read
read_start = ( read_start + bytes_read ) % VPAGE_SIZE
if remaining > 0 :
assert ( read_start == 0 )
except ValueError :
2021-12-27 05:07:01 +00:00
logging . error ( " key: couldn ' t decrypt vpage @ {:x} ppage @ {:x} " . format ( page_addr ) , pp_start )
2021-11-20 18:21:25 +00:00
page_addr + = VPAGE_SIZE
2021-11-22 10:26:19 +00:00
# CI check -- if it doesn't pass, it doesn't mean we've failed -- could also just be a "normal" record that doesn't have the checksum appended
check_data = self . data [ : - 4 ]
while len ( check_data ) % 4 != 0 :
check_data + = bytes ( [ 0 ] )
checksum = mm3_hash ( check_data )
refcheck = int . from_bytes ( self . data [ len ( self . data ) - 4 : ] , ' little ' )
if checksum == refcheck :
self . ci_ok = True
else :
self . ci_ok = False
2022-01-01 06:14:09 +00:00
logging . error ( ' checksum: {:x} , refchecksum: {:x} \n ' . format ( checksum , refcheck ) )
2021-11-25 19:08:08 +00:00
else :
# print('invalid key')
pass
2021-11-20 18:21:25 +00:00
2021-11-22 18:57:13 +00:00
def as_str ( self , indent = ' ' ) :
2021-11-22 10:26:19 +00:00
PRINT_LEN = 64
2021-11-23 15:33:06 +00:00
global PRINTED_FULL
2021-11-20 18:21:25 +00:00
desc = ' '
2021-11-24 19:14:29 +00:00
if self . start > 0x7e_fff02_0000 :
desc + = indent + ' Start: 0x {:x} (lg) \n ' . format ( self . start )
else :
dict = ( self . start - 0x3f_8000_0000 ) / / 0xFE_0000
pool = ( ( self . start - 0x3f_8000_0000 ) - dict * 0xFE_0000 ) / / 0xFE0
desc + = indent + ' Start: 0x {:x} | dict_index {} | pool {} \n ' . format ( self . start , dict , pool )
2021-11-22 10:26:19 +00:00
desc + = indent + ' Len: {} /0x {:x} \n ' . format ( self . len , self . reserved )
2021-11-20 18:21:25 +00:00
desc + = indent + ' Flags: '
if self . valid :
desc + = ' VALID '
if self . unresolved :
desc + = indent + ' UNRESOLVED '
2021-11-22 10:26:19 +00:00
desc + = ' | Age: {} ' . format ( self . age )
desc + = ' | CI OK: {} ' . format ( self . ci_ok )
2021-11-20 18:21:25 +00:00
desc + = ' \n '
2021-11-23 15:33:06 +00:00
if len ( self . data ) < PRINT_LEN :
2021-11-21 20:46:36 +00:00
print_len = len ( self . data )
extra = ' '
else :
2021-11-23 15:33:06 +00:00
if ( PRINTED_FULL == False ) and ( self . ci_ok == False ) :
print_len = len ( self . data )
extra = ' '
PRINTED_FULL = True
else :
print_len = PRINT_LEN
extra = ' ... '
2021-11-21 20:46:36 +00:00
desc + = indent + ' Data (hex): {} {} \n ' . format ( self . data [ : print_len ] . hex ( ) , extra )
2021-11-22 10:26:19 +00:00
desc + = indent + ' Data (txt): {} {} ' . format ( make_printable ( self . data [ : print_len ] . decode ( ' utf-8 ' , errors = ' ignore ' ) ) , extra ) + ' \n '
2021-11-20 18:21:25 +00:00
return ( desc )
2021-11-17 18:01:30 +00:00
class BasisDicts :
DICT_VSTRIDE = 0xFE_0000
2021-11-20 18:21:25 +00:00
KV_STRIDE = 127
2021-11-22 10:26:19 +00:00
MAX_NAME_LEN = 111
2021-11-17 18:01:30 +00:00
def __init__ ( self , index , v2p , disk , key , name ) :
global PAGE_SIZE
2021-11-22 10:26:19 +00:00
global VPAGE_SIZE
2021-11-17 18:01:30 +00:00
dict_header_vaddr = self . DICT_VSTRIDE * ( index + 1 )
2021-11-24 19:14:29 +00:00
self . index = index
self . vaddr = dict_header_vaddr
2021-11-22 18:57:13 +00:00
if dict_header_vaddr in v2p :
self . valid = True
pp = v2p [ dict_header_vaddr ]
self . keys = { }
try :
2021-12-27 05:07:01 +00:00
logging . debug ( ' dict decrypt @ pp {:x} , nonce: {} ' . format ( pp , disk [ pp : pp + 12 ] . hex ( ) ) )
2021-11-22 18:57:13 +00:00
cipher = AES_GCM_SIV ( key , disk [ pp : pp + 12 ] )
pt_data = cipher . decrypt ( disk [ pp + 12 : pp + PAGE_SIZE ] , basis_aad ( name ) )
# print('raw pt_data: {}'.format(pt_data[:127].hex()))
i = 0
self . journal = int . from_bytes ( pt_data [ i : i + 4 ] , ' little ' )
i + = 4
self . flags = int . from_bytes ( pt_data [ i : i + 4 ] , ' little ' )
i + = 4
self . age = int . from_bytes ( pt_data [ i : i + 4 ] , ' little ' )
i + = 4
self . num_keys = int . from_bytes ( pt_data [ i : i + 4 ] , ' little ' )
i + = 4
self . free_key_index = int . from_bytes ( pt_data [ i : i + 4 ] , ' little ' )
i + = 4
2022-01-06 10:53:01 +00:00
self . name = pt_data [ i + 1 : i + 1 + pt_data [ i ] ] . decode ( ' utf8 ' , errors = ' ignore ' )
2021-11-22 18:57:13 +00:00
i + = BasisDicts . MAX_NAME_LEN
2021-12-27 05:07:01 +00:00
logging . info ( " decrypt dict ' {} ' with {} keys and {} free_key_index " . format ( self . name , self . num_keys , self . free_key_index ) )
logging . debug ( " dict header len: {} " . format ( i - 4 ) ) # subtract 4 because of the journal
2021-11-22 18:57:13 +00:00
except ValueError :
2021-12-27 05:07:01 +00:00
logging . error ( " basisdicts: couldn ' t decrypt vpage @ {:x} ppage @ {:x} " . format ( dict_header_vaddr , v2p [ dict_header_vaddr ] ) )
2021-11-22 18:57:13 +00:00
if self . num_keys > 0 :
keys_found = 0
keys_tried = 1
2022-01-01 06:14:09 +00:00
prev_index = 128 # hack to optimize effort by only decrypting a new page when the key index resets
2021-11-22 18:57:13 +00:00
while ( keys_found < self . num_keys ) and keys_tried < 131071 :
keyindex_start_vaddr = self . DICT_VSTRIDE * ( index + 1 ) + ( keys_tried * self . KV_STRIDE )
2021-12-27 22:47:33 +00:00
#if self.name == 'dict2' and keyindex_start_vaddr < (0x1fc2fa0 + 0xfe0):
# try:
# print('keys_tried {}'.format(keys_tried))
# print('ki_vaddr {:x}'.format((keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE))
# pp = v2p[(keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE]
# print('ki_paddr {:x}'.format(pp))
# except KeyError:
# pass
2021-11-22 10:26:19 +00:00
try :
2021-11-22 18:57:13 +00:00
pp_start = v2p [ ( keyindex_start_vaddr / / VPAGE_SIZE ) * VPAGE_SIZE ]
pp_data = disk [ pp_start : pp_start + PAGE_SIZE ]
2021-12-27 22:47:33 +00:00
except KeyError :
# the page wasn't allocated, so let's just assume all the key entries are invalid, and were "tried"
keys_tried + = VPAGE_SIZE / / self . KV_STRIDE
continue
try :
key_index = 4 + keys_tried % ( VPAGE_SIZE / / self . KV_STRIDE ) * self . KV_STRIDE
2022-01-01 06:14:09 +00:00
if key_index < prev_index :
cipher = AES_GCM_SIV ( key , pp_data [ : 12 ] )
pt_data = cipher . decrypt ( pp_data [ 12 : ] , basis_aad ( name ) )
prev_index = key_index
2021-12-27 22:47:33 +00:00
#if self.name == 'dict2':
# print('key_index {:x}/{}/{:x}/{:x}'.format(key_index, keys_tried, pp, (keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE))
# print('{:x}'.format(v2p[(keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE]))
2021-11-22 18:57:13 +00:00
try :
maybe_key = KeyDescriptor ( pt_data [ key_index : key_index + self . KV_STRIDE ] , v2p , disk , key , name )
if maybe_key . valid :
self . keys [ maybe_key . name ] = maybe_key
keys_found + = 1
2021-12-27 22:47:33 +00:00
#if self.name == 'dict2':
# print('found {}: {}'.format(keys_found, maybe_key.name))
except KeyError :
logging . error ( " Key @ offset {} is missing data allocation " . format ( key_index ) )
except ValueError :
logging . error ( " key: couldn ' t decrypt vpage @ {:x} ppage @ {:x} " . format ( keyindex_start_vaddr , pp_start ) )
keys_tried + = 1
2021-11-22 18:57:13 +00:00
if keys_found < self . num_keys :
2021-12-27 05:07:01 +00:00
logging . error ( " Expected {} keys but only found {} " . format ( self . num_keys , keys_found ) )
2021-11-22 18:57:13 +00:00
self . found_all_keys = False
else :
self . found_all_keys = True
2021-12-27 22:47:33 +00:00
#if self.name == 'dict2':
# for v, p in v2p.items():
# print('map: v{:16x} | p{:8x}'.format(v, p))
2021-11-22 18:57:13 +00:00
else :
self . valid = False
2021-11-20 18:21:25 +00:00
2021-11-17 18:01:30 +00:00
def as_str ( self ) :
desc = ' '
2021-11-24 19:14:29 +00:00
desc + = ' Name: {} / index {} / vaddr: {:x} \n ' . format ( self . name , self . index , self . vaddr )
2021-11-17 18:01:30 +00:00
desc + = ' Age: {} \n ' . format ( self . age )
desc + = ' Flags: {} \n ' . format ( self . flags )
desc + = ' Key count: {} \n ' . format ( self . num_keys )
2021-11-22 10:26:19 +00:00
desc + = ' Free key index: {} \n ' . format ( self . free_key_index )
2021-11-20 18:21:25 +00:00
desc + = ' Keys: \n '
for ( name , key ) in self . keys . items ( ) :
desc + = ' {} : \n ' . format ( name )
desc + = key . as_str ( ' ' )
2021-11-17 18:01:30 +00:00
return desc
2021-11-22 10:26:19 +00:00
def ci_check ( self ) :
check_ok = True
for ( name , key ) in self . keys . items ( ) :
namecheck = name . split ( ' | ' )
if namecheck [ 1 ] != self . name :
2021-12-27 05:07:01 +00:00
logging . warning ( " Key/dict mismatch: {} / {} " . format ( key . name , self . name ) )
2021-11-22 10:26:19 +00:00
keylen = int ( namecheck [ 3 ] [ 3 : ] )
2022-01-01 07:14:44 +00:00
if keylen > 12 :
if keylen != key . len and keylen + 4 != key . len : # the second case is what happens with the length extension test, it's actually not an error
logging . warning ( " Key named len vs disk len mismatch: {} / {} " . format ( keylen , key . len ) )
else :
2022-02-15 00:45:12 +00:00
if key . len != 17 and key . len != 12 : # 12 means we weren't extended. But, the test patch data extends a 12-length key to 13, so with the checksum its final length is 17.
2022-01-01 07:14:44 +00:00
logging . warning ( " Key named len vs disk len mismatch: {} / {} (extension did not work correctly) " . format ( keylen , key . len ) )
2021-11-22 10:26:19 +00:00
if key . ci_ok == False :
2021-12-27 05:07:01 +00:00
logging . warning ( " CI failed on key {} : " . format ( name ) )
logging . warning ( key . as_str ( ' ' ) )
2021-11-22 10:26:19 +00:00
check_ok = False
if self . found_all_keys == False :
2021-12-27 05:07:01 +00:00
logging . warning ( ' Dictionary was missing keys ' )
2021-11-22 10:26:19 +00:00
check_ok = False
if check_ok :
2021-12-27 05:07:01 +00:00
logging . info ( ' Dictionary {} CI check: OK ' . format ( self . name ) )
2021-11-22 10:26:19 +00:00
else :
2021-12-27 05:07:01 +00:00
logging . error ( ' Dictionary {} CI check: FAIL ' . format ( self . name ) )
2021-11-22 10:26:19 +00:00
2021-11-17 18:01:30 +00:00
2021-11-14 20:03:55 +00:00
class Basis :
MAX_NAME_LEN = 64
def __init__ ( self , i_bytes ) :
self . bytes = i_bytes
i = 0
self . journal = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
2021-11-15 08:38:57 +00:00
self . magic = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
self . version = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
self . age = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
self . num_dicts = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
2022-01-06 10:53:01 +00:00
self . name = i_bytes [ i + 1 : i + 1 + i_bytes [ i ] ] . rstrip ( b ' \x00 ' ) . decode ( ' utf8 ' , errors = ' ignore ' )
2021-11-15 08:38:57 +00:00
i + = Basis . MAX_NAME_LEN
2021-11-16 10:45:24 +00:00
#self.prealloc_open_end = int.from_bytes(i_bytes[i:i+8], 'little')
#i += 8
#self.dict_ptr = int.from_bytes(i_bytes[i:i+8], 'little')
#i += 8
2021-11-15 08:38:57 +00:00
""" rkyv unpacker
i = 0
self . journal = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
2021-11-15 08:00:01 +00:00
self . rkyvpos = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
2021-11-14 20:03:55 +00:00
i + = 4
print ( " alloc on disk: {} " . format ( i_bytes [ i : i + 8 ] . hex ( ) ) )
self . prealloc_open_end = int . from_bytes ( i_bytes [ i : i + 8 ] , ' little ' )
i + = 8
2021-11-15 08:00:01 +00:00
self . age = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
2021-11-14 20:03:55 +00:00
self . num_dicts = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
2021-11-15 08:00:01 +00:00
self . version = int . from_bytes ( i_bytes [ i : i + 2 ] , ' little ' )
i + = 2
self . magic = int . from_bytes ( i_bytes [ i : i + 4 ] , ' little ' )
i + = 4
self . name = i_bytes [ i : i + Basis . MAX_NAME_LEN ] . rstrip ( b ' \x00 ' ) . decode ( ' utf8 ' , errors = ' ignore ' )
i + = Basis . MAX_NAME_LEN
i + = 10 # mysterious padding in the rkyv format!!
2021-11-14 20:03:55 +00:00
self . dict_ptr = int . from_bytes ( i_bytes [ i : i + 8 ] , ' little ' )
i + = 8
2021-11-15 08:38:57 +00:00
"""
2021-11-14 20:03:55 +00:00
def as_str ( self ) :
desc = ' '
desc + = ' Journal rev: {} \n ' . format ( self . journal )
2021-11-15 08:38:57 +00:00
#desc += ' Rkyv pos: {}\n'.format(self.rkyvpos)
2021-11-14 20:03:55 +00:00
desc + = ' Magic: {:x} \n ' . format ( self . magic )
2021-11-15 08:00:01 +00:00
desc + = ' Version: {:x} \n ' . format ( self . version )
desc + = ' Age: {:x} \n ' . format ( self . age )
2021-11-14 20:03:55 +00:00
desc + = ' Name: {} \n ' . format ( self . name )
2021-11-16 10:45:24 +00:00
#desc += ' Alloc: {:x}\n'.format(self.prealloc_open_end)
2021-11-14 20:03:55 +00:00
desc + = ' NumDicts: {:x} \n ' . format ( self . num_dicts )
2021-11-16 10:45:24 +00:00
#desc += ' DictPtr: {:x}\n'.format(self.dict_ptr)
2021-11-14 20:03:55 +00:00
return desc
2022-02-27 20:16:06 +00:00
def basis_aad ( name , version = 0x01_01 , dna = 0 ) :
2021-11-14 20:03:55 +00:00
name_bytes = bytearray ( name , ' utf-8 ' )
2022-01-06 10:53:01 +00:00
# name_bytes += bytearray(([0] * (Basis.MAX_NAME_LEN - len(name))))
2021-11-15 18:58:33 +00:00
name_bytes + = version . to_bytes ( 4 , ' little ' )
2021-11-14 20:03:55 +00:00
name_bytes + = dna . to_bytes ( 8 , ' little ' )
return name_bytes
2021-11-14 11:02:34 +00:00
class Pte :
PTE_LEN = 16
2021-11-14 20:03:55 +00:00
def __init__ ( self , i_bytes ) :
assert ( len ( i_bytes ) == self . PTE_LEN )
self . bytes = i_bytes
2021-11-14 11:02:34 +00:00
def len_bytes ( self ) :
return ( self . bytes . len ( ) )
def as_bytes ( self ) :
return self . bytes
def addr ( self ) :
2021-11-23 17:49:22 +00:00
return int . from_bytes ( self . bytes [ : 7 ] , ' little ' )
2021-11-14 11:02:34 +00:00
def flags ( self ) :
if self . bytes [ 6 ] == 1 :
return ' CLN '
2021-11-15 18:58:33 +00:00
elif self . bytes [ 6 ] == 2 :
return ' CHK '
elif self . bytes [ 6 ] == 3 :
return ' COK '
2021-11-14 11:02:34 +00:00
else :
return ' INV '
def nonce ( self ) :
return int . from_bytes ( self . bytes [ 8 : 12 ] , ' little ' )
def checksum ( self ) :
return int . from_bytes ( self . bytes [ 12 : ] , ' little ' )
# checks the checksum
def is_valid ( self ) :
return ( self . checksum ( ) == mm3_hash ( self . bytes [ : 12 ] , self . nonce ( ) ) )
2021-11-14 20:03:55 +00:00
def as_str ( self , ppage = None ) :
2021-11-14 11:02:34 +00:00
#print('checksum: {:08x}, hash: {:08x}'.format(self.checksum(), mm3_hash(self.bytes[:12], self.nonce())))
if ppage != None :
2021-11-14 20:03:55 +00:00
return ' {:08x} : a_ {:012x} | f_ {} | n_ {:08x} | c_ {:08x} ' . format ( ppage , self . addr ( ) , self . flags ( ) , self . nonce ( ) , self . checksum ( ) )
2021-11-14 11:02:34 +00:00
else :
2021-11-14 20:03:55 +00:00
return ' a_ {:012x} | f_ {} | n_ {:08x} | c_ {:08x} ' . format ( self . addr ( ) , self . flags ( ) , self . nonce ( ) , self . checksum ( ) )
2021-11-14 11:02:34 +00:00
2021-11-15 11:05:03 +00:00
def find_mbbb ( mbbb ) :
global MBBB_PAGES
pages = [ mbbb [ i : i + PAGE_SIZE ] for i in range ( 0 , MBBB_PAGES * PAGE_SIZE , PAGE_SIZE ) ]
2021-11-23 15:33:06 +00:00
candidates = [ ]
2021-11-15 11:05:03 +00:00
for page in pages :
if bytearray ( page [ : 16 ] ) != bytearray ( [ 0xff ] * 16 ) :
2021-11-23 15:33:06 +00:00
candidates . append ( page )
if len ( candidates ) > 1 :
2021-12-27 05:07:01 +00:00
logging . error ( " More than one MBBB found, this is an error! " )
2021-11-23 15:33:06 +00:00
else :
return candidates [ 0 ]
2021-11-15 11:05:03 +00:00
return None
def decode_pagetable ( img , entries , keys , mbbb ) :
global PAGE_SIZE
2021-11-14 11:02:34 +00:00
key_table = { }
for name , key in keys . items ( ) :
2021-12-27 05:07:01 +00:00
logging . debug ( " Pages for basis {} " . format ( name ) )
logging . debug ( " key: {} " . format ( key . hex ( ) ) )
2021-11-14 11:02:34 +00:00
cipher = AES . new ( key , AES . MODE_ECB )
2021-11-15 11:05:03 +00:00
pages = [ img [ i : i + PAGE_SIZE ] for i in range ( 0 , entries * Pte . PTE_LEN , PAGE_SIZE ) ]
page_num = 0
2021-11-14 11:02:34 +00:00
v2p_table = { }
p2v_table = { }
2021-11-15 11:05:03 +00:00
for page in pages :
if bytearray ( page [ : 16 ] ) == bytearray ( [ 0xff ] * 16 ) :
# decrypt from mbbb
2021-12-27 05:07:01 +00:00
logging . debug ( " Found a blank PTE page, falling back to MBBB! " )
2021-11-15 11:05:03 +00:00
maybe_page = find_mbbb ( mbbb )
if maybe_page != None :
page = maybe_page
else :
2021-12-27 05:07:01 +00:00
logging . warning ( " Blank entry in PTE found, but no corresponding MBBB entry found. Filesystem likely corrupted. " )
2021-11-15 11:05:03 +00:00
encrypted_ptes = [ page [ i : i + Pte . PTE_LEN ] for i in range ( 0 , PAGE_SIZE , Pte . PTE_LEN ) ]
for pte in encrypted_ptes :
maybe_pte = Pte ( cipher . decrypt ( pte ) )
if maybe_pte . is_valid ( ) :
2021-12-27 05:07:01 +00:00
logging . debug ( maybe_pte . as_str ( page_num ) )
2021-11-15 11:05:03 +00:00
p_addr = page_num * ( 4096 / / Pte . PTE_LEN )
2021-11-24 19:14:29 +00:00
if maybe_pte . addr ( ) in v2p_table :
2021-12-27 05:07:01 +00:00
logging . warning ( " duplicate V2P PTE entry, evicting {:x} : {:x} " , maybe_pte . addr ( ) , p_addr )
2021-11-15 11:05:03 +00:00
v2p_table [ maybe_pte . addr ( ) ] = p_addr
2021-11-24 19:14:29 +00:00
if p_addr in p2v_table :
2021-12-27 05:07:01 +00:00
logging . warning ( " duplicate P2V PTE entry, evicting {:x} : {:x} " , p_addr , maybe_pte . addr ( ) )
2021-11-15 11:05:03 +00:00
p2v_table [ p_addr ] = maybe_pte . addr ( )
page_num + = Pte . PTE_LEN
key_table [ name ] = [ v2p_table , p2v_table ]
2021-11-14 11:02:34 +00:00
return key_table
class PhysPage :
2021-11-14 20:03:55 +00:00
PP_LEN = 4
def __init__ ( self , i_bytes ) :
self . pp = int . from_bytes ( i_bytes [ : 4 ] , ' little ' )
2021-11-14 11:02:34 +00:00
def page_number ( self ) :
return self . pp & 0xF_FFFF
def clean ( self ) :
if ( self . pp & 0x10_0000 ) != 0 :
return True
else :
return False
def valid ( self ) :
if ( self . pp & 0x20_0000 ) != 0 :
return True
else :
return False
def space_state ( self ) :
code = ( self . pp >> 22 ) & 3
if code == 0 :
return ' FREE '
elif code == 1 :
return ' MUSE '
elif code == 2 :
return ' USED '
else :
return ' DIRT '
def journal ( self ) :
return ( self . pp >> 24 ) & 0xF
2021-11-14 20:03:55 +00:00
def as_str ( self ) :
2021-11-23 15:33:06 +00:00
return ' pp_ {:05x} | c_ {} | v_ {} | ss_ {} | j_ {:02} ' . format ( self . page_number ( ) , self . clean ( ) , self . valid ( ) , self . space_state ( ) , self . journal ( ) )
2021-11-14 11:02:34 +00:00
class Fscb :
2021-11-27 13:23:42 +00:00
def __init__ ( self , i_bytes , FASTSPACE_PAGES = 2 ) :
2021-11-14 11:02:34 +00:00
NONCE_LEN = 12
TAG_LEN = 16
PP_LEN = 4
FREE_POOL_LEN = ( ( 4096 * FASTSPACE_PAGES ) - ( NONCE_LEN + TAG_LEN ) ) / / PP_LEN
2021-11-14 20:03:55 +00:00
assert ( len ( i_bytes ) == FREE_POOL_LEN * PP_LEN )
raw_pps = [ i_bytes [ i : i + PP_LEN ] for i in range ( 0 , FREE_POOL_LEN * PP_LEN , PP_LEN ) ]
2021-11-14 11:02:34 +00:00
self . free_space = { }
for raw_pp in raw_pps :
pp = PhysPage ( raw_pp )
if pp . valid ( ) :
self . free_space [ pp . page_number ( ) * 4096 ] = pp
def at_phys_addr ( self , addr ) :
try :
return self . free_pool [ addr ]
except :
None
def print ( self ) :
for pagenum , pp in self . free_space . items ( ) :
2021-12-27 05:07:01 +00:00
logging . debug ( pp . as_str ( ) )
2021-11-14 20:03:55 +00:00
def try_replace ( self , candidate ) :
if candidate . is_valid ( ) :
pp = candidate . get_pp ( )
cur_pp = self . free_space [ pp . page_number ( ) * 4096 ]
if pp . valid ( ) and ( cur_pp . journal ( ) < pp . journal ( ) ) :
2021-12-27 05:07:01 +00:00
logging . debug ( " FSCB replace {} /with/ {} " . format ( cur_pp . as_str ( ) , pp . as_str ( ) ) )
2021-11-14 20:03:55 +00:00
self . free_space [ pp . page_number ( ) * 4096 ] = pp
2021-11-14 11:02:34 +00:00
# this is the "AAD" used to encrypt the FastSpace
2022-02-27 20:16:06 +00:00
def aad ( version = 0x01_01 , dna = 0 ) :
2021-11-15 18:58:33 +00:00
return bytearray ( [ 46 , 70 , 97 , 115 , 116 , 83 , 112 , 97 , 99 , 101 ] ) + version . to_bytes ( 4 , ' little ' ) + dna . to_bytes ( 8 , ' little ' )
2021-11-14 11:02:34 +00:00
2021-11-14 20:03:55 +00:00
class SpaceUpdate :
SU_LEN = 16
def __init__ ( self , i_bytes ) :
self . nonce = i_bytes [ : 8 ]
self . page_number = PhysPage ( i_bytes [ 8 : 12 ] )
self . checksum = int . from_bytes ( i_bytes [ 12 : ] , ' little ' )
hash = mm3_hash ( i_bytes [ : 12 ] , int . from_bytes ( i_bytes [ 4 : 8 ] , ' big ' ) )
if self . checksum == hash :
self . valid = True
else :
self . valid = False
def is_valid ( self ) :
return self . valid
def get_pp ( self ) :
return self . page_number
2021-11-14 11:02:34 +00:00
# img is clipped to just the fscb region of the overall disk image
def decode_fscb ( img , keys , FSCB_LEN_PAGES = 2 ) :
global SYSTEM_BASIS
2021-11-14 20:03:55 +00:00
fscb = None
2021-11-25 19:36:36 +00:00
fscb_count = 0
2021-11-14 11:02:34 +00:00
if SYSTEM_BASIS in keys :
key = keys [ SYSTEM_BASIS ]
pages = [ img [ i : i + 4096 ] for i in range ( 0 , len ( img ) , 4096 ) ]
space_update = [ ]
fscb_start = None
pg = 0
for page in pages :
if bytearray ( page [ : 32 ] ) == bytearray ( [ 0xff ] * 32 ) :
# page is blank
pass
2021-11-15 11:05:03 +00:00
elif bytearray ( page [ : 16 ] ) == bytearray ( [ 0xff ] * 16 ) :
2021-11-14 20:03:55 +00:00
space_update . append ( page [ 16 : ] )
2021-11-14 11:02:34 +00:00
else :
if fscb_start == None :
fscb_start = pg
pg + = 1
if fscb_start :
2021-12-27 05:07:01 +00:00
logging . debug ( " Found FSCB at {:x} " . format ( fscb_start ) )
2021-11-14 11:02:34 +00:00
fscb_enc = img [ fscb_start * 4096 : ( fscb_start + FSCB_LEN_PAGES ) * 4096 ]
2021-11-28 15:54:58 +00:00
# print("data: {}".format(fscb_enc.hex()))
2021-11-14 11:02:34 +00:00
try :
nonce = fscb_enc [ : 12 ]
2021-12-27 05:07:01 +00:00
logging . info ( " key: {} , nonce: {} " . format ( key . hex ( ) , nonce . hex ( ) ) )
2021-11-14 11:02:34 +00:00
cipher = AES_GCM_SIV ( key , nonce )
2021-12-27 05:07:01 +00:00
logging . info ( " aad: {} " . format ( Fscb . aad ( ) . hex ( ) ) )
logging . info ( " mac: {} " . format ( fscb_enc [ - 16 : ] . hex ( ) ) )
2021-11-14 11:02:34 +00:00
#print("data: {}".format(fscb_enc[12:-16].hex()))
fscb_dec = cipher . decrypt ( fscb_enc [ 12 : ] , Fscb . aad ( ) )
2021-11-26 18:19:57 +00:00
fscb = Fscb ( fscb_dec , FASTSPACE_PAGES = FSCB_LEN_PAGES )
2021-11-14 11:02:34 +00:00
except KeyError :
2021-12-27 05:07:01 +00:00
logging . error ( " couldn ' t decrypt " )
2021-11-14 11:02:34 +00:00
fscb = None
2021-11-14 20:03:55 +00:00
if fscb != None :
2021-12-27 05:07:01 +00:00
logging . debug ( " key: {} " . format ( key . hex ( ) ) )
2021-11-14 20:03:55 +00:00
cipher = AES . new ( key , AES . MODE_ECB )
for update in space_update :
entries = [ update [ i : i + SpaceUpdate . SU_LEN ] for i in range ( 0 , len ( update ) , SpaceUpdate . SU_LEN ) ]
for entry in entries :
if entry == bytearray ( [ 0xff ] * 16 ) :
break
else :
fscb . try_replace ( SpaceUpdate ( cipher . decrypt ( entry ) ) )
2021-11-25 19:36:36 +00:00
fscb_count + = 1
2021-12-27 05:07:01 +00:00
logging . info ( " Total FSCB entries found: {} " . format ( fscb_count ) )
2021-11-14 20:03:55 +00:00
return fscb
2021-11-14 11:02:34 +00:00
if __name__ == " __main__ " :
main ( )
exit ( 0 )