refactor set_key -> position_key

This commit is contained in:
2025-03-20 04:01:15 -05:00
parent 7b92a6b40b
commit 65f3559ef0
8 changed files with 241 additions and 247 deletions

View File

@@ -16,7 +16,7 @@ class Customer:
@classmethod
def create(cls, nkode_policy: NKodePolicy, cipher: CustomerCipher) -> 'Customer':
if nkode_policy.distinct_sets > cipher.keypad_size.numb_of_keys:
if nkode_policy.distinct_positions > cipher.keypad_size.numb_of_keys:
raise ValueError("Distinct sets cannot be greater than the number of keys")
if nkode_policy.distinct_properties > cipher.keypad_size.total_props:
raise ValueError("Distinct properties cannot be greater than the total number of properties")
@@ -76,7 +76,7 @@ class Customer:
distinct_properties = len(set(passcode_prop_idx))
if (
self.nkode_policy.min_nkode_len <= nkode_len <= self.nkode_policy.max_nkode_len and
distinct_sets >= self.nkode_policy.distinct_sets and
distinct_sets >= self.nkode_policy.distinct_positions and
distinct_properties >= self.nkode_policy.distinct_properties
):
return True

View File

@@ -10,7 +10,7 @@ class EncipheredNKode:
class NKodePolicy:
max_nkode_len: int = 10
min_nkode_len: int = 4
distinct_sets: int = 0
distinct_positions: int = 0
distinct_properties: int = 4
byte_len: int = 2 # Todo: this should change the total number of bytes an properties or set value can be
lock_out: int = 5

View File

@@ -18,8 +18,8 @@ class User:
def renew_keys(self, set_xor: np.ndarray, prop_xor: np.ndarray):
self.renew = True
self.cipher.combined_set_key = self.cipher.combined_set_key ^ set_xor
self.cipher.prop_key = self.cipher.prop_key ^ prop_xor
self.cipher.combined_position_key = self.cipher.combined_position_key ^ set_xor
self.cipher.property_key = self.cipher.property_key ^ prop_xor
def refresh_passcode(self, passcode_prop_idxs: list[int], customer_cipher: CustomerCipher):
self.cipher = UserCipher.create(

View File

@@ -9,30 +9,31 @@ from src.customer_cipher import CustomerCipher
@dataclass
class UserCipher:
prop_key: np.ndarray
combined_set_key: np.ndarray
property_key: np.ndarray
combined_position_key: np.ndarray
pass_key: np.ndarray
mask_key: np.ndarray
max_nkode_len: int
@classmethod
def create(cls, keypad_size: KeypadSize, customer_set_key: np.ndarray, max_nkode_len: int) -> 'UserCipher':
if len(customer_set_key) != keypad_size.props_per_key:
raise ValueError("Invalid set values")
user_set_key = np.random.choice(2**16,size=keypad_size.props_per_key, replace=False)
def create(cls, keypad_size: KeypadSize, customer_pos_key: np.ndarray, max_nkode_len: int) -> 'UserCipher':
if len(customer_pos_key) != keypad_size.props_per_key:
raise ValueError("Invalid position values")
user_pos_key = np.random.choice(2**16,size=keypad_size.props_per_key, replace=False)
return UserCipher(
prop_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
property_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
pass_key=np.random.choice(2 ** 16, size=max_nkode_len, replace=False),
mask_key=np.random.choice(2**16, size=max_nkode_len, replace=False),
combined_set_key=user_set_key ^ customer_set_key,
combined_position_key=user_pos_key ^ customer_pos_key,
max_nkode_len=max_nkode_len
)
def pad_user_mask(self, user_mask: np.ndarray, set_vals: np.ndarray) -> np.ndarray:
def pad_user_mask(self, user_mask: np.ndarray, pos_vals: np.ndarray) -> np.ndarray:
# TODO: replace with new method
if len(user_mask) >= self.max_nkode_len:
raise ValueError("User encoded_mask is too long")
padding_size = self.max_nkode_len - len(user_mask)
padding = np.random.choice(set_vals, size=padding_size, replace=True).astype(np.uint16)
padding = np.random.choice(pos_vals, size=padding_size, replace=True).astype(np.uint16)
return np.concatenate([user_mask, padding])
@staticmethod
@@ -90,7 +91,7 @@ class UserCipher:
passcode_cipher = self.pass_key.copy()
passcode_cipher[:passcode_len] = (
passcode_cipher[:passcode_len] ^
self.prop_key[passcode_prop_idx] ^
self.property_key[passcode_prop_idx] ^
customer_prop.property_key[passcode_prop_idx]
)
return passcode_cipher.astype(np.uint16).tobytes()
@@ -100,21 +101,21 @@ class UserCipher:
passcode_prop_idx: list[int],
customer_cipher: CustomerCipher
) -> str:
set_idxs = customer_cipher.get_passcode_position_indices_padded(passcode_prop_idx, len(self.mask_key))
ordered_set_key = self.combined_set_key[set_idxs]
ordered_customer_key = customer_cipher.position_key[set_idxs]
mask = ordered_set_key ^ ordered_customer_key ^ self.mask_key
pos_idxs = customer_cipher.get_passcode_position_indices_padded(passcode_prop_idx, len(self.mask_key))
ordered_pos_key = self.combined_position_key[pos_idxs]
ordered_customer_pos_key = customer_cipher.position_key[pos_idxs]
mask = ordered_pos_key ^ ordered_customer_pos_key ^ self.mask_key
encoded_mask = self.encode_base64_str(mask)
return encoded_mask
def decipher_mask(self, encoded_mask: str, customer_set_key: np.ndarray, passcode_len: int) -> list[int]:
def decipher_mask(self, encoded_mask: str, customer_pos_key: np.ndarray, passcode_len: int) -> list[int]:
mask = self.decode_base64_str(encoded_mask)
# user_set_key ordered by the user's nkode and padded to be length max_nkode_len
ordered_set_key = mask ^ self.mask_key
user_set_key = customer_set_key ^ self.combined_set_key
passcode_sets = []
for partial_set in ordered_set_key[:passcode_len]:
set_idx = np.where(user_set_key == partial_set)[0][0]
passcode_sets.append(int(customer_set_key[set_idx]))
return passcode_sets
# user_pos_key ordered by the user's nkode and padded to be length max_nkode_len
ordered_user_pos_key = mask ^ self.mask_key
user_pos_key = customer_pos_key ^ self.combined_position_key
passcode_position = []
for position_val in ordered_user_pos_key[:passcode_len]:
position_idx = np.where(user_pos_key == position_val)[0][0]
passcode_position.append(int(customer_pos_key[position_idx]))
return passcode_position

View File

@@ -83,16 +83,16 @@ class UserKeypad:
if not (0 <= key_numb < self.keypad_size.numb_of_keys):
raise ValueError(f"key_numb must be between 0 and {self.keypad_size.numb_of_keys - 1}")
if not (0 <= set_idx < self.keypad_size.props_per_key):
raise ValueError(f"set_indices must be between 0 and {self.keypad_size.props_per_key - 1}")
raise ValueError(f"padded_passcode_position_indices must be between 0 and {self.keypad_size.props_per_key - 1}")
keypad_prop_idx = self.keypad_matrix()
return int(keypad_prop_idx[key_numb][set_idx])
def get_prop_idxs_by_keynumb_setidx(self, key_numb: list[int], set_idx: list[int]) -> list[int]:
if len(key_numb) != len(set_idx):
raise ValueError("key_numb and set_indices must be the same length")
raise ValueError("key_numb and padded_passcode_position_indices must be the same length")
if not all(0 <= kn < self.keypad_size.numb_of_keys for kn in key_numb):
raise ValueError(f"All key_numb must be between 0 and {self.keypad_size.numb_of_keys - 1}")
if not all(0 <= si < self.keypad_size.props_per_key for si in set_idx):
raise ValueError(f"All set_indices must be between 0 and {self.keypad_size.props_per_key - 1}")
raise ValueError(f"All padded_passcode_position_indices must be between 0 and {self.keypad_size.props_per_key - 1}")
keypad_matrix = self.keypad_matrix()
return keypad_matrix[key_numb, set_idx].reshape(-1).tolist()