speed up the analysis tool so it's a bit more practical to use in CI

This commit is contained in:
bunnie 2022-01-01 14:14:09 +08:00
parent 656f17f621
commit c02a8933ef
2 changed files with 33 additions and 17 deletions

View file

@ -288,7 +288,7 @@ class KeyDescriptor:
self.ci_ok = True self.ci_ok = True
else: else:
self.ci_ok = False self.ci_ok = False
logging.debug('checksum: {:x}, refchecksum: {:x}\n'.format(checksum, refcheck)) logging.error('checksum: {:x}, refchecksum: {:x}\n'.format(checksum, refcheck))
else: else:
# print('invalid key') # print('invalid key')
pass pass
@ -370,6 +370,7 @@ class BasisDicts:
if self.num_keys > 0: if self.num_keys > 0:
keys_found = 0 keys_found = 0
keys_tried = 1 keys_tried = 1
prev_index = 128 # hack to optimize effort by only decrypting a new page when the key index resets
while (keys_found < self.num_keys) and keys_tried < 131071: while (keys_found < self.num_keys) and keys_tried < 131071:
keyindex_start_vaddr = self.DICT_VSTRIDE * (index + 1) + (keys_tried * self.KV_STRIDE) keyindex_start_vaddr = self.DICT_VSTRIDE * (index + 1) + (keys_tried * self.KV_STRIDE)
#if self.name == 'dict2' and keyindex_start_vaddr < (0x1fc2fa0 + 0xfe0): #if self.name == 'dict2' and keyindex_start_vaddr < (0x1fc2fa0 + 0xfe0):
@ -388,9 +389,11 @@ class BasisDicts:
keys_tried += VPAGE_SIZE // self.KV_STRIDE keys_tried += VPAGE_SIZE // self.KV_STRIDE
continue continue
try: try:
cipher = AES_GCM_SIV(key, pp_data[:12])
pt_data = cipher.decrypt(pp_data[12:], basis_aad(name))
key_index = 4 + keys_tried % (VPAGE_SIZE // self.KV_STRIDE) * self.KV_STRIDE key_index = 4 + keys_tried % (VPAGE_SIZE // self.KV_STRIDE) * self.KV_STRIDE
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
#if self.name == 'dict2': #if self.name == 'dict2':
# print('key_index {:x}/{}/{:x}/{:x}'.format(key_index, keys_tried, pp, (keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE)) # 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])) # print('{:x}'.format(v2p[(keyindex_start_vaddr // VPAGE_SIZE) * VPAGE_SIZE]))

View file

@ -15,6 +15,7 @@
# Rust crate, so, in the worst case, we just get some invalid diagnostic results. # Rust crate, so, in the worst case, we just get some invalid diagnostic results.
import Crypto.Cipher.AES as AES import Crypto.Cipher.AES as AES
import pyaesni
import binascii import binascii
import six import six
import struct import struct
@ -30,19 +31,20 @@ class Field(object):
@staticmethod @staticmethod
def add(x, y): def add(x, y):
assert x < (1 << 128) #assert x < (1 << 128) # in pursuit of great performance we take great risks :-P
assert y < (1 << 128) #assert y < (1 << 128)
return x ^ y return x ^ y
@staticmethod @staticmethod
def mul(x, y): def mul(x, y):
assert x < (1 << 128), x #assert x < (1 << 128), x
assert y < (1 << 128), y #assert y < (1 << 128), y
res = 0 res = 0
for bit in range(128): for bit in range(128):
if (y >> bit) & 1: if (y >> bit) & 1:
res ^= (2 ** bit) * x #res ^= (2 ** bit) * x
res ^= (1 << bit) * x
return Field.mod(res, Field._MOD) return Field.mod(res, Field._MOD)
@ -154,14 +156,20 @@ def le_uint64(i):
class AES_GCM_SIV(object): class AES_GCM_SIV(object):
def __init__(self, key_gen_key, nonce): def __init__(self, key_gen_key, nonce):
aes_obj = AES.new(key_gen_key, AES.MODE_ECB) #aes_obj = AES.new(key_gen_key, AES.MODE_ECB)
msg_auth_key = aes_obj.encrypt(le_uint32(0) + nonce)[0:8] + \ #msg_auth_key = aes_obj.encrypt(le_uint32(0) + nonce)[0:8] + \
aes_obj.encrypt(le_uint32(1) + nonce)[0:8] # aes_obj.encrypt(le_uint32(1) + nonce)[0:8]
msg_enc_key = aes_obj.encrypt(le_uint32(2) + nonce)[0:8] + \ msg_auth_key = pyaesni.cbc256_encrypt(le_uint32(0) + nonce, key_gen_key, bytearray(16))[0:8] + \
aes_obj.encrypt(le_uint32(3) + nonce)[0:8] pyaesni.cbc256_encrypt((le_uint32(1) + nonce), key_gen_key, bytearray(16))[0:8]
#msg_enc_key = aes_obj.encrypt(le_uint32(2) + nonce)[0:8] + \
# aes_obj.encrypt(le_uint32(3) + nonce)[0:8]
msg_enc_key = pyaesni.cbc256_encrypt(le_uint32(2) + nonce, key_gen_key, bytearray(16))[0:8] + \
pyaesni.cbc256_encrypt(le_uint32(3) + nonce, key_gen_key, bytearray(16))[0:8]
if len(key_gen_key) == 32: if len(key_gen_key) == 32:
msg_enc_key += aes_obj.encrypt(le_uint32(4) + nonce)[0:8] + \ #msg_enc_key += aes_obj.encrypt(le_uint32(4) + nonce)[0:8] + \
aes_obj.encrypt(le_uint32(5) + nonce)[0:8] # aes_obj.encrypt(le_uint32(5) + nonce)[0:8]
msg_enc_key += pyaesni.cbc256_encrypt(le_uint32(4) + nonce, key_gen_key, bytearray(16))[0:8] + \
pyaesni.cbc256_encrypt(le_uint32(5) + nonce, key_gen_key, bytearray(16))[0:8]
self.msg_auth_key = msg_auth_key self.msg_auth_key = msg_auth_key
self.msg_enc_key = msg_enc_key self.msg_enc_key = msg_enc_key
self.nonce = nonce self.nonce = nonce
@ -175,7 +183,11 @@ class AES_GCM_SIV(object):
block = initial_block block = initial_block
output = b'' output = b''
while len(inp) > 0: while len(inp) > 0:
keystream_block = AES.new(key, AES.MODE_ECB).encrypt(block) #keystream_block = AES.new(key, AES.MODE_ECB).encrypt(block)
keystream_block = pyaesni.cbc256_encrypt(block, key, bytearray(16))
#print("aes")
#print(binascii.hexlify(keystream_block))
#print(binascii.hexlify(keystream_block_a))
block = le_uint32((read_le_uint32(block[0:4]) + 1) & 0xffffffff) + block[4:] block = le_uint32((read_le_uint32(block[0:4]) + 1) & 0xffffffff) + block[4:]
todo = min(len(inp), len(keystream_block)) todo = min(len(inp), len(keystream_block))
for j in range(todo): for j in range(todo):
@ -224,7 +236,8 @@ class AES_GCM_SIV(object):
# Polyval/tag calculation # Polyval/tag calculation
S_s = self._polyval_calc(plaintext, additional_data) S_s = self._polyval_calc(plaintext, additional_data)
tag = AES.new(self.msg_enc_key).encrypt(bytes(S_s)) #tag = AES.new(self.msg_enc_key).encrypt(bytes(S_s))
tag = pyaesni.cbc256_encrypt(bytes(S_s), self.msg_enc_key, bytearray(16))
# Encrypt # Encrypt
counter_block = bytearray(tag) counter_block = bytearray(tag)