refactor set_key -> position_key

This commit is contained in:
2025-03-19 09:34:02 -05:00
parent cfef58613c
commit 7b92a6b40b
10 changed files with 511 additions and 407 deletions

View File

@@ -1,5 +1,8 @@
from dataclasses import dataclass
from uuid import UUID, uuid4
import numpy as np
from src.customer_cipher import CustomerCipher
from src.models import NKodePolicy
from src.user import User
@@ -38,8 +41,8 @@ class Customer:
passcode_len = len(selected_keys)
user = self.users[username]
passcode_set_vals = user.cipher.decipher_mask(
user.enciphered_passcode.mask, self.cipher.set_key, passcode_len)
set_vals_idx = [self.cipher.get_set_index(set_val) for set_val in passcode_set_vals]
user.enciphered_passcode.mask, self.cipher.position_key, passcode_len)
set_vals_idx = [self.cipher.get_position_index(set_val) for set_val in passcode_set_vals]
presumed_property_idxs = user.user_keypad.get_prop_idxs_by_keynumb_setidx(selected_keys, set_vals_idx)
if not user.cipher.compare_nkode(presumed_property_idxs, self.cipher,user.enciphered_passcode.code):
return False
@@ -50,11 +53,11 @@ class Customer:
return True
def renew_keys(self) -> bool:
old_props = self.cipher.prop_key.copy()
old_sets = self.cipher.set_key.copy()
old_props = self.cipher.property_key.copy()
old_sets = self.cipher.position_key.copy()
self.cipher.renew()
new_props = self.cipher.prop_key
new_sets = self.cipher.set_key
new_props = self.cipher.property_key
new_sets = self.cipher.position_key
props_xor = new_props ^ old_props
set_xor = new_sets ^ old_sets
@@ -65,9 +68,10 @@ class Customer:
def valid_new_nkode(self, passcode_prop_idx: list[int]) -> bool:
nkode_len = len(passcode_prop_idx)
passcode_set_values = [
self.cipher.get_prop_set_val(int(self.cipher.prop_key[prop_idx])) for prop_idx in passcode_prop_idx
]
#passcode_set_values = [
# self.cipher.get_prop_set_val(int(self.cipher.property_key[prop_idx])) for prop_idx in passcode_prop_idx
#]
passcode_set_values = self.cipher.get_props_position_vals(passcode_prop_idx)
distinct_sets = len(set(passcode_set_values))
distinct_properties = len(set(passcode_prop_idx))
if (
@@ -77,3 +81,5 @@ class Customer:
):
return True
return False

View File

@@ -6,16 +6,13 @@ from src.models import KeypadSize
@dataclass
class CustomerCipher:
prop_key: np.ndarray
set_key: np.ndarray
property_key: np.ndarray
position_key: np.ndarray
keypad_size: KeypadSize
MAX_KEYS: ClassVar[int] = 256
MAX_PROP_PER_KEY: ClassVar[int] = 256
def __post_init__(self):
self.check_keys_vs_props()
def check_keys_vs_props(self) -> None:
if self.keypad_size.is_dispersable:
raise ValueError("number of keys must be less than the number of "
"properties per key to be dispersion resistant")
@@ -24,28 +21,40 @@ class CustomerCipher:
def create(cls, keypad_size: KeypadSize) -> 'CustomerCipher':
if keypad_size.numb_of_keys > cls.MAX_KEYS or keypad_size.props_per_key > cls.MAX_PROP_PER_KEY:
raise ValueError(f"Keys and properties per key must not exceed {cls.MAX_KEYS}")
# Using numpy to generate non-repeating random integers
prop_key = np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False)
set_key = np.random.choice(2 ** 16, size=keypad_size.props_per_key, replace=False)
pos_key = np.random.choice(2 ** 16, size=keypad_size.props_per_key, replace=False)
return cls(
prop_key=prop_key,
set_key=set_key,
property_key=prop_key,
position_key=pos_key,
keypad_size=keypad_size,
)
def renew(self):
self.prop_key = np.random.choice(2 ** 16, size=self.keypad_size.total_props, replace=False)
self.set_key = np.random.choice(2 ** 16, size=self.keypad_size.props_per_key, replace=False)
self.property_key = np.random.choice(2 ** 16, size=self.keypad_size.total_props, replace=False)
self.position_key = np.random.choice(2 ** 16, size=self.keypad_size.props_per_key, replace=False)
def get_prop_set_val(self, prop: int) -> int:
assert np.isin(prop, self.prop_key)
prop_idx = np.where(self.prop_key == prop)[0][0]
set_idx = prop_idx % self.keypad_size.props_per_key
return int(self.set_key[set_idx])
def get_props_position_vals(self, props: np.ndarray | list[int]) -> np.ndarray:
if not all([prop in self.property_key for prop in props]):
raise ValueError("Property values must be within valid range")
pos_vals = [self._get_prop_position_val(prop) for prop in props]
return np.array(pos_vals)
def get_set_index(self, set_val: int) -> int:
if not np.isin(set_val, self.set_key):
raise ValueError(f"Set value {set_val} not found in set values")
return int(np.where(self.set_key == set_val)[0][0])
def _get_prop_position_val(self, prop: int) -> int:
assert prop in self.property_key
prop_idx = np.where(self.property_key == prop)[0][0]
pos_idx = prop_idx % self.keypad_size.props_per_key
return int(self.position_key[pos_idx])
def get_position_index(self, pos_val: int) -> int:
if not np.isin(pos_val, self.position_key):
raise ValueError(f"Position value {pos_val} not found in customer cipher position_key")
return int(np.where(self.position_key == pos_val)[0][0])
def get_passcode_position_indices_padded(self, passcode_indices: list[int], max_nkode_len: int) -> list[int]:
if not all(0 <= idx < self.keypad_size.total_props for idx in passcode_indices):
raise ValueError("invalid passcode index")
pos_indices = [idx % self.keypad_size.props_per_key for idx in passcode_indices]
pad_len = max_nkode_len - len(passcode_indices)
pad = np.random.choice(self.keypad_size.props_per_key, pad_len, replace=True)
return pos_indices + pad.tolist()

View File

@@ -28,4 +28,4 @@ class KeypadSize:
@property
def is_dispersable(self) -> bool:
return self.props_per_key <= self.numb_of_keys
return self.props_per_key <= self.numb_of_keys

View File

@@ -74,7 +74,7 @@ class NKodeAPI:
passcode = self.signup_sessions[session_id].deduce_passcode(confirm_key_entry)
new_user_keys = UserCipher.create(
customer.cipher.keypad_size,
customer.cipher.set_key,
customer.cipher.position_key,
customer.nkode_policy.max_nkode_len
)
enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.cipher)

View File

@@ -24,7 +24,7 @@ class User:
def refresh_passcode(self, passcode_prop_idxs: list[int], customer_cipher: CustomerCipher):
self.cipher = UserCipher.create(
customer_cipher.keypad_size,
customer_cipher.set_key,
customer_cipher.position_key,
self.cipher.max_nkode_len
)
self.enciphered_passcode = self.cipher.encipher_nkode(passcode_prop_idxs, customer_cipher)

View File

@@ -19,9 +19,7 @@ class UserCipher:
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)
return UserCipher(
prop_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),
@@ -93,7 +91,7 @@ class UserCipher:
passcode_cipher[:passcode_len] = (
passcode_cipher[:passcode_len] ^
self.prop_key[passcode_prop_idx] ^
customer_prop.prop_key[passcode_prop_idx]
customer_prop.property_key[passcode_prop_idx]
)
return passcode_cipher.astype(np.uint16).tobytes()
@@ -102,12 +100,10 @@ class UserCipher:
passcode_prop_idx: list[int],
customer_cipher: CustomerCipher
) -> str:
customer_props = customer_cipher.prop_key[passcode_prop_idx]
customer_sets = [customer_cipher.get_prop_set_val(prop) for prop in customer_props]
padded_customer_sets = self.pad_user_mask(np.array(customer_sets), customer_cipher.set_key)
set_idx = [customer_cipher.get_set_index(set_val) for set_val in padded_customer_sets]
ordered_set_key = self.combined_set_key[set_idx]
mask = ordered_set_key ^ padded_customer_sets ^ self.mask_key
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
encoded_mask = self.encode_base64_str(mask)
return encoded_mask

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_idx must be between 0 and {self.keypad_size.props_per_key - 1}")
raise ValueError(f"set_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_idx must be the same length")
raise ValueError("key_numb and set_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_idx must be between 0 and {self.keypad_size.props_per_key - 1}")
raise ValueError(f"All set_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()