refactor; rename classes and methods
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.customer_cipher import CustomerCipher
|
||||
from src.models import NKodePolicy
|
||||
from src.user import User
|
||||
from src.utils import xor_lists
|
||||
@@ -9,7 +9,7 @@ from src.utils import xor_lists
|
||||
class Customer:
|
||||
customer_id: UUID
|
||||
nkode_policy: NKodePolicy
|
||||
attributes: CustomerAttributes
|
||||
customer_cipher: CustomerCipher
|
||||
users: dict[str, User]
|
||||
|
||||
# TODO: validate policy and keypad size don't conflict
|
||||
@@ -23,16 +23,16 @@ class Customer:
|
||||
if username not in self.users:
|
||||
raise ValueError(f"User '{username}' does not exist")
|
||||
|
||||
keypad_size = self.attributes.keypad_size.numb_of_keys
|
||||
if not all(0 <= key_idx < keypad_size for key_idx in selected_keys):
|
||||
raise ValueError(f"Invalid key indices. Must be between 0 and {keypad_size - 1}")
|
||||
numb_of_keys = self.customer_cipher.keypad_size.numb_of_keys
|
||||
if not all(0 <= key_idx < numb_of_keys for key_idx in selected_keys):
|
||||
raise ValueError(f"Invalid key indices. Must be between 0 and {numb_of_keys - 1}")
|
||||
|
||||
passcode_len = len(selected_keys)
|
||||
user = self.users[username]
|
||||
|
||||
passcode_set_vals = user.user_keys.decipher_mask(
|
||||
user.enciphered_passcode.mask, self.attributes.set_vals, passcode_len)
|
||||
set_vals_idx = [self.attributes.get_set_index(set_val) for set_val in passcode_set_vals]
|
||||
user.enciphered_passcode.mask, self.customer_cipher.set_key, passcode_len)
|
||||
set_vals_idx = [self.customer_cipher.get_set_index(set_val) for set_val in passcode_set_vals]
|
||||
|
||||
presumed_selected_attributes_idx = []
|
||||
for idx in range(passcode_len):
|
||||
@@ -41,20 +41,20 @@ class Customer:
|
||||
selected_attr_idx = user.user_keypad.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)
|
||||
presumed_selected_attributes_idx.append(selected_attr_idx)
|
||||
|
||||
enciphered_attr = user.user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, self.attributes)
|
||||
enciphered_attr = user.user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, self.customer_cipher)
|
||||
if enciphered_attr != user.enciphered_passcode.code:
|
||||
return False
|
||||
|
||||
if user.renew:
|
||||
user.refresh_passcode(presumed_selected_attributes_idx, self.attributes)
|
||||
user.refresh_passcode(presumed_selected_attributes_idx, self.customer_cipher)
|
||||
return True
|
||||
|
||||
def renew_keys(self) -> bool:
|
||||
old_attrs = self.attributes.attr_vals.copy()
|
||||
old_sets = self.attributes.set_vals.copy()
|
||||
self.attributes.renew()
|
||||
new_attrs = self.attributes.attr_vals
|
||||
new_sets = self.attributes.set_vals
|
||||
old_attrs = self.customer_cipher.prop_key.copy()
|
||||
old_sets = self.customer_cipher.set_key.copy()
|
||||
self.customer_cipher.renew()
|
||||
new_attrs = self.customer_cipher.prop_key
|
||||
new_sets = self.customer_cipher.set_key
|
||||
|
||||
attrs_xor = xor_lists(new_attrs, old_attrs)
|
||||
set_xor = xor_lists(new_sets, old_sets)
|
||||
@@ -66,7 +66,7 @@ class Customer:
|
||||
def valid_new_nkode(self, passcode_attr_idx: list[int]) -> bool:
|
||||
nkode_len = len(passcode_attr_idx)
|
||||
passcode_set_values = [
|
||||
self.attributes.get_attr_set_val(self.attributes.attr_vals[attr_idx]) for attr_idx in passcode_attr_idx
|
||||
self.customer_cipher.get_prop_set_val(self.customer_cipher.prop_key[attr_idx]) for attr_idx in passcode_attr_idx
|
||||
]
|
||||
distinct_sets = len(set(passcode_set_values))
|
||||
distinct_attributes = len(set(passcode_attr_idx))
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import ClassVar
|
||||
|
||||
from src.models import KeypadSize
|
||||
from src.utils import generate_random_nonrepeating_list
|
||||
|
||||
|
||||
@dataclass
|
||||
class CustomerAttributes:
|
||||
attr_vals: list[int]
|
||||
set_vals: list[int]
|
||||
keypad_size: KeypadSize
|
||||
MAX_KEYS: ClassVar[int] = 256
|
||||
MAX_ATTRS_PER_KEY: ClassVar[int] = 256
|
||||
|
||||
def __post_init__(self):
|
||||
self.check_keys_vs_attrs()
|
||||
|
||||
def check_keys_vs_attrs(self) -> None:
|
||||
if self.keypad_size.is_dispersable:
|
||||
raise ValueError("number of keys must be less than the number of "
|
||||
"attributes per key to be dispersion resistant")
|
||||
|
||||
@classmethod
|
||||
def create(cls, keypad_size: KeypadSize) -> 'CustomerAttributes':
|
||||
if keypad_size.numb_of_keys > cls.MAX_KEYS or keypad_size.attrs_per_key > cls.MAX_ATTRS_PER_KEY:
|
||||
raise ValueError(f"Keys and attributes per key must not exceed {cls.MAX_KEYS}")
|
||||
return cls(
|
||||
attr_vals=generate_random_nonrepeating_list(keypad_size.numb_of_attrs),
|
||||
set_vals=generate_random_nonrepeating_list(keypad_size.attrs_per_key),
|
||||
keypad_size=keypad_size,
|
||||
)
|
||||
|
||||
def renew(self):
|
||||
self.attr_vals = generate_random_nonrepeating_list(self.keypad_size.numb_of_attrs)
|
||||
self.set_vals = generate_random_nonrepeating_list(self.keypad_size.attrs_per_key)
|
||||
|
||||
def get_attr_set_val(self, attr: int) -> int:
|
||||
assert (attr in self.attr_vals)
|
||||
attr_idx = self.attr_vals.index(attr)
|
||||
set_idx = attr_idx % self.keypad_size.attrs_per_key
|
||||
return self.set_vals[set_idx]
|
||||
|
||||
def get_set_index(self, set_val: int) -> int:
|
||||
if set_val not in self.set_vals:
|
||||
raise ValueError(f"Set value {set_val} not found in set values")
|
||||
return self.set_vals.index(set_val)
|
||||
47
src/customer_cipher.py
Normal file
47
src/customer_cipher.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import ClassVar
|
||||
|
||||
from src.models import KeypadSize
|
||||
from src.utils import generate_random_nonrepeating_list
|
||||
|
||||
|
||||
@dataclass
|
||||
class CustomerCipher:
|
||||
prop_key: list[int]
|
||||
set_key: list[int]
|
||||
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")
|
||||
|
||||
@classmethod
|
||||
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}")
|
||||
return cls(
|
||||
prop_key=generate_random_nonrepeating_list(keypad_size.numb_of_props),
|
||||
set_key=generate_random_nonrepeating_list(keypad_size.props_per_key),
|
||||
keypad_size=keypad_size,
|
||||
)
|
||||
|
||||
def renew(self):
|
||||
self.prop_key = generate_random_nonrepeating_list(self.keypad_size.numb_of_props)
|
||||
self.set_key = generate_random_nonrepeating_list(self.keypad_size.props_per_key)
|
||||
|
||||
def get_prop_set_val(self, prop: int) -> int:
|
||||
assert (prop in self.prop_key)
|
||||
prop_idx = self.prop_key.index(prop)
|
||||
set_idx = prop_idx % self.keypad_size.props_per_key
|
||||
return self.set_key[set_idx]
|
||||
|
||||
def get_set_index(self, set_val: int) -> int:
|
||||
if set_val not in self.set_key:
|
||||
raise ValueError(f"Set value {set_val} not found in set values")
|
||||
return self.set_key.index(set_val)
|
||||
@@ -19,13 +19,13 @@ class NKodePolicy:
|
||||
|
||||
@dataclass
|
||||
class KeypadSize:
|
||||
attrs_per_key: int
|
||||
props_per_key: int
|
||||
numb_of_keys: int
|
||||
|
||||
@property
|
||||
def numb_of_attrs(self) -> int:
|
||||
return self.attrs_per_key * self.numb_of_keys
|
||||
def numb_of_props(self) -> int:
|
||||
return self.props_per_key * self.numb_of_keys
|
||||
|
||||
@property
|
||||
def is_dispersable(self) -> bool:
|
||||
return self.attrs_per_key <= self.numb_of_keys
|
||||
return self.props_per_key <= self.numb_of_keys
|
||||
@@ -5,10 +5,10 @@ from typing import Dict, List, Tuple
|
||||
from src.customer import Customer
|
||||
from src.models import NKodePolicy, KeypadSize
|
||||
from src.user import User
|
||||
from src.user_cipher_keys import UserCipherKeys
|
||||
from src.user_cipher_keys import UserCipher
|
||||
from src.user_signup_session import UserSignupSession
|
||||
from src.user_keypad import UserKeypad
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.customer_cipher import CustomerCipher
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -19,7 +19,7 @@ class NKodeAPI:
|
||||
def create_new_customer(self, keypad_size: KeypadSize, nkode_policy: NKodePolicy) -> UUID:
|
||||
new_customer = Customer(
|
||||
customer_id=uuid4(),
|
||||
attributes=CustomerAttributes.create(keypad_size),
|
||||
customer_cipher=CustomerCipher.create(keypad_size),
|
||||
users={},
|
||||
nkode_policy=nkode_policy
|
||||
)
|
||||
@@ -30,7 +30,7 @@ class NKodeAPI:
|
||||
if customer_id not in self.customers.keys():
|
||||
raise ValueError(f"Customer with ID '{customer_id}' does not exist")
|
||||
customer = self.customers[customer_id]
|
||||
login_keypad = UserKeypad.create(customer.attributes.keypad_size)
|
||||
login_keypad = UserKeypad.create(customer.customer_cipher.keypad_size)
|
||||
set_keypad = login_keypad.sign_up_keypad()
|
||||
new_session = UserSignupSession(
|
||||
session_id=uuid4(),
|
||||
@@ -75,12 +75,12 @@ class NKodeAPI:
|
||||
raise AssertionError(f"Username mismatch: {username} vs {session.username}")
|
||||
customer = self.customers[customer_id]
|
||||
passcode = self.signup_sessions[session_id].deduce_passcode(confirm_key_entry)
|
||||
new_user_keys = UserCipherKeys.create(
|
||||
customer.attributes.keypad_size,
|
||||
customer.attributes.set_vals,
|
||||
new_user_keys = UserCipher.create(
|
||||
customer.customer_cipher.keypad_size,
|
||||
customer.customer_cipher.set_key,
|
||||
customer.nkode_policy.max_nkode_len
|
||||
)
|
||||
enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.attributes)
|
||||
enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.customer_cipher)
|
||||
new_user = User(
|
||||
username=username,
|
||||
enciphered_passcode=enciphered_passcode,
|
||||
|
||||
12
src/user.py
12
src/user.py
@@ -1,7 +1,7 @@
|
||||
from dataclasses import dataclass, field
|
||||
from src.models import EncipheredNKode
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.user_cipher_keys import UserCipherKeys
|
||||
from src.customer_cipher import CustomerCipher
|
||||
from src.user_cipher_keys import UserCipher
|
||||
from src.user_keypad import UserKeypad
|
||||
from src.utils import xor_lists
|
||||
|
||||
@@ -10,7 +10,7 @@ from src.utils import xor_lists
|
||||
class User:
|
||||
username: str
|
||||
enciphered_passcode: EncipheredNKode
|
||||
user_keys: UserCipherKeys
|
||||
user_keys: UserCipher
|
||||
user_keypad: UserKeypad
|
||||
renew: bool = field(default=False)
|
||||
|
||||
@@ -19,10 +19,10 @@ class User:
|
||||
self.user_keys.set_key = xor_lists(self.user_keys.set_key, sets_xor)
|
||||
self.user_keys.alpha_key = xor_lists(self.user_keys.alpha_key, attrs_xor)
|
||||
|
||||
def refresh_passcode(self, passcode_attr_idx: list[int], customer_attributes: CustomerAttributes):
|
||||
self.user_keys = UserCipherKeys.create(
|
||||
def refresh_passcode(self, passcode_attr_idx: list[int], customer_attributes: CustomerCipher):
|
||||
self.user_keys = UserCipher.create(
|
||||
customer_attributes.keypad_size,
|
||||
customer_attributes.set_vals,
|
||||
customer_attributes.set_key,
|
||||
self.user_keys.max_nkode_len
|
||||
)
|
||||
self.enciphered_passcode = self.user_keys.encipher_nkode(passcode_attr_idx, customer_attributes)
|
||||
|
||||
@@ -4,11 +4,11 @@ from dataclasses import dataclass
|
||||
import bcrypt
|
||||
from secrets import choice
|
||||
from src.models import EncipheredNKode, KeypadSize
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.customer_cipher import CustomerCipher
|
||||
from src.utils import generate_random_nonrepeating_list, xor_lists, int_array_to_bytes
|
||||
|
||||
@dataclass
|
||||
class UserCipherKeys:
|
||||
class UserCipher:
|
||||
alpha_key: list[int]
|
||||
set_key: list[int]
|
||||
pass_key: list[int]
|
||||
@@ -17,15 +17,15 @@ class UserCipherKeys:
|
||||
max_nkode_len: int
|
||||
|
||||
@classmethod
|
||||
def create(cls, keypad_size: KeypadSize, set_values: list[int], max_nkode_len: int) -> 'UserCipherKeys':
|
||||
if len(set_values) != keypad_size.attrs_per_key:
|
||||
def create(cls, keypad_size: KeypadSize, set_values: list[int], max_nkode_len: int) -> 'UserCipher':
|
||||
if len(set_values) != keypad_size.props_per_key:
|
||||
raise ValueError("Invalid set values")
|
||||
|
||||
set_key = generate_random_nonrepeating_list(keypad_size.attrs_per_key)
|
||||
set_key = generate_random_nonrepeating_list(keypad_size.props_per_key)
|
||||
set_key = xor_lists(set_key, set_values)
|
||||
|
||||
return UserCipherKeys(
|
||||
alpha_key=generate_random_nonrepeating_list(keypad_size.attrs_per_key * keypad_size.numb_of_keys),
|
||||
return UserCipher(
|
||||
alpha_key=generate_random_nonrepeating_list(keypad_size.props_per_key * keypad_size.numb_of_keys),
|
||||
pass_key=generate_random_nonrepeating_list(max_nkode_len),
|
||||
mask_key=generate_random_nonrepeating_list(max_nkode_len),
|
||||
set_key=set_key,
|
||||
@@ -64,11 +64,11 @@ class UserCipherKeys:
|
||||
def encipher_nkode(
|
||||
self,
|
||||
passcode_attr_idx: list[int],
|
||||
customer_attributes: CustomerAttributes
|
||||
customer_attributes: CustomerCipher
|
||||
) -> EncipheredNKode:
|
||||
|
||||
passcode_attrs = [customer_attributes.attr_vals[idx] for idx in passcode_attr_idx]
|
||||
passcode_sets = [customer_attributes.get_attr_set_val(attr) for attr in passcode_attrs]
|
||||
passcode_attrs = [customer_attributes.prop_key[idx] for idx in passcode_attr_idx]
|
||||
passcode_sets = [customer_attributes.get_prop_set_val(attr) for attr in passcode_attrs]
|
||||
mask = self.encipher_mask(passcode_sets, customer_attributes)
|
||||
code = self.encipher_salt_hash_code(passcode_attr_idx, customer_attributes)
|
||||
return EncipheredNKode(
|
||||
@@ -79,10 +79,10 @@ class UserCipherKeys:
|
||||
def encipher_salt_hash_code(
|
||||
self,
|
||||
passcode_attr_idx: list[int],
|
||||
customer_attributes: CustomerAttributes,
|
||||
customer_attributes: CustomerCipher,
|
||||
) -> str:
|
||||
passcode_len = len(passcode_attr_idx)
|
||||
passcode_attrs = [customer_attributes.attr_vals[idx] for idx in passcode_attr_idx]
|
||||
passcode_attrs = [customer_attributes.prop_key[idx] for idx in passcode_attr_idx]
|
||||
|
||||
passcode_cipher = self.pass_key.copy()
|
||||
|
||||
@@ -96,9 +96,9 @@ class UserCipherKeys:
|
||||
def encipher_mask(
|
||||
self,
|
||||
passcode_sets: list[int],
|
||||
customer_attributes: CustomerAttributes
|
||||
customer_attributes: CustomerCipher
|
||||
) -> str:
|
||||
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_attributes.set_vals)
|
||||
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_attributes.set_key)
|
||||
set_idx = [customer_attributes.get_set_index(set_val) for set_val in padded_passcode_sets]
|
||||
mask_set_keys = [self.set_key[idx] for idx in set_idx]
|
||||
ciphered_mask = xor_lists(mask_set_keys, padded_passcode_sets)
|
||||
|
||||
@@ -11,7 +11,7 @@ class UserKeypad:
|
||||
@classmethod
|
||||
def create(cls, keypad_size: KeypadSize) -> 'UserKeypad':
|
||||
keypad = UserKeypad(
|
||||
keypad=list(range(keypad_size.numb_of_attrs)),
|
||||
keypad=list(range(keypad_size.numb_of_props)),
|
||||
keypad_size=keypad_size
|
||||
)
|
||||
keypad.random_keypad_shuffle()
|
||||
@@ -30,12 +30,12 @@ class UserKeypad:
|
||||
keypad=matrix_to_list(keypad_matrix),
|
||||
keypad_size=KeypadSize(
|
||||
numb_of_keys=self.keypad_size.numb_of_keys,
|
||||
attrs_per_key=self.keypad_size.numb_of_keys
|
||||
props_per_key=self.keypad_size.numb_of_keys
|
||||
)
|
||||
)
|
||||
|
||||
def keypad_matrix(self) -> list[list[int]]:
|
||||
return list_to_matrix(self.keypad, self.keypad_size.attrs_per_key)
|
||||
return list_to_matrix(self.keypad, self.keypad_size.props_per_key)
|
||||
|
||||
def random_keypad_shuffle(self):
|
||||
keypad_view = self.keypad_matrix()
|
||||
@@ -48,11 +48,11 @@ class UserKeypad:
|
||||
def disperse_keypad(self):
|
||||
if not self.keypad_size.is_dispersable:
|
||||
raise ValueError("Keypad size is not dispersable")
|
||||
user_keypad_matrix = list_to_matrix(self.keypad, self.keypad_size.attrs_per_key)
|
||||
user_keypad_matrix = list_to_matrix(self.keypad, self.keypad_size.props_per_key)
|
||||
shuffled_keys = secure_fisher_yates_shuffle(user_keypad_matrix)
|
||||
|
||||
attr_rotation = secure_fisher_yates_shuffle(list(range(self.keypad_size.numb_of_keys)))[
|
||||
:self.keypad_size.attrs_per_key]
|
||||
:self.keypad_size.props_per_key]
|
||||
dispersed_keypad = self.random_attribute_rotation(
|
||||
shuffled_keys,
|
||||
attr_rotation,
|
||||
@@ -61,10 +61,10 @@ class UserKeypad:
|
||||
|
||||
def partial_keypad_shuffle(self):
|
||||
# TODO: this should be split shuffle
|
||||
numb_of_selected_sets = self.keypad_size.attrs_per_key // 2
|
||||
# randomly shuffle half the sets. if attrs_per_key is odd, randomly add one 50% of the time
|
||||
numb_of_selected_sets += choice([0, 1]) if (self.keypad_size.attrs_per_key & 1) == 1 else 0
|
||||
selected_sets = secure_fisher_yates_shuffle(list(range(self.keypad_size.attrs_per_key)))[:numb_of_selected_sets]
|
||||
numb_of_selected_sets = self.keypad_size.props_per_key // 2
|
||||
# randomly shuffle half the sets. if props_per_key is odd, randomly add one 50% of the time
|
||||
numb_of_selected_sets += choice([0, 1]) if (self.keypad_size.props_per_key & 1) == 1 else 0
|
||||
selected_sets = secure_fisher_yates_shuffle(list(range(self.keypad_size.props_per_key)))[:numb_of_selected_sets]
|
||||
user_keypad_matrix = self.keypad_matrix()
|
||||
shuffled_keys = secure_fisher_yates_shuffle(user_keypad_matrix)
|
||||
keypad_by_sets = []
|
||||
@@ -100,7 +100,7 @@ class UserKeypad:
|
||||
def get_attr_idx_by_keynumb_setidx(self, key_numb: int, set_idx: int) -> int:
|
||||
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.attrs_per_key):
|
||||
raise ValueError(f"set_idx must be between 0 and {self.keypad_size.attrs_per_key - 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}")
|
||||
keypad_attr_idx = self.keypad_matrix()
|
||||
return keypad_attr_idx[key_numb][set_idx]
|
||||
|
||||
@@ -19,7 +19,7 @@ class UserSignupSession(BaseModel):
|
||||
def deduce_passcode(self, confirm_key_entry: list[int]) -> list[int]:
|
||||
if not all(0 <= key <= self.keypad_size.numb_of_keys for key in confirm_key_entry):
|
||||
raise ValueError("Key values must be within valid range")
|
||||
attrs_per_key = self.keypad_size.attrs_per_key
|
||||
attrs_per_key = self.keypad_size.props_per_key
|
||||
set_key_entry = self.set_key_entry
|
||||
if len(set_key_entry) != len(confirm_key_entry):
|
||||
raise ValueError("Key entry lengths must match")
|
||||
|
||||
Reference in New Issue
Block a user