rename functions and attributes
This commit is contained in:
22
nkode_api.py
22
nkode_api.py
@@ -3,24 +3,24 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
from src.customer import Customer
|
from src.customer import Customer
|
||||||
from src.models import NKodePolicy
|
from src.models import NKodePolicy
|
||||||
from src.session_cache import SessionCacheModel
|
from src.session_cache import SessionCache
|
||||||
from src.user_cipher_keys import UserCipherKeys
|
from src.user_cipher_keys import UserCipherKeys
|
||||||
from src.user_db_model import UserDBModel
|
from src.user import User
|
||||||
from src.user_interface import UserInterface
|
from src.user_interface import UserInterface
|
||||||
from src.customer_interface import CustomerInterface
|
from src.customer_interface import CustomerInterface
|
||||||
|
|
||||||
|
|
||||||
class NKodeAPI(BaseModel):
|
class NKodeAPI(BaseModel):
|
||||||
customers: dict[UUID, Customer] = {}
|
customers: dict[UUID, Customer] = {}
|
||||||
sessions: dict[UUID, SessionCacheModel] = {}
|
sessions: dict[UUID, SessionCache] = {}
|
||||||
|
|
||||||
def generate_index_interface(self, customer_id: UUID) -> tuple[UUID, list[int]]:
|
def generate_index_interface(self, customer_id: UUID) -> tuple[UUID, list[int]]:
|
||||||
assert (customer_id in self.customers.keys())
|
assert (customer_id in self.customers.keys())
|
||||||
customer = self.customers[customer_id]
|
customer = self.customers[customer_id]
|
||||||
set_interface = UserInterface.new(customer.interface.attrs_per_key, customer.interface.numb_of_keys)
|
set_interface = UserInterface.new(customer.interface.attrs_per_key, customer.interface.numb_of_keys)
|
||||||
new_session = SessionCacheModel(
|
new_session = SessionCache(
|
||||||
session_id=uuid4(),
|
session_id=uuid4(),
|
||||||
set_interface=set_interface.interface_index,
|
set_interface=set_interface.attr_indices,
|
||||||
customer_id=customer_id,
|
customer_id=customer_id,
|
||||||
)
|
)
|
||||||
self.sessions[new_session.session_id] = new_session
|
self.sessions[new_session.session_id] = new_session
|
||||||
@@ -35,7 +35,7 @@ class NKodeAPI(BaseModel):
|
|||||||
assert (username in customer.users.keys())
|
assert (username in customer.users.keys())
|
||||||
user = customer.users[username]
|
user = customer.users[username]
|
||||||
user.user_interface.shuffle_interface()
|
user.user_interface.shuffle_interface()
|
||||||
return user.user_interface.interface_index
|
return user.user_interface.attr_indices
|
||||||
|
|
||||||
def set_nkode(
|
def set_nkode(
|
||||||
self, username: str, customer_id: UUID,
|
self, username: str, customer_id: UUID,
|
||||||
@@ -50,14 +50,14 @@ class NKodeAPI(BaseModel):
|
|||||||
attrs_per_key = customer.interface.attrs_per_key
|
attrs_per_key = customer.interface.attrs_per_key
|
||||||
assert (all(0 <= key <= numb_of_keys for key in key_selection))
|
assert (all(0 <= key <= numb_of_keys for key in key_selection))
|
||||||
set_interface = UserInterface(
|
set_interface = UserInterface(
|
||||||
interface_index=session.set_interface,
|
attr_indices=session.set_interface,
|
||||||
attrs_per_key=attrs_per_key,
|
attrs_per_key=attrs_per_key,
|
||||||
numb_of_keys=numb_of_keys,
|
numb_of_keys=numb_of_keys,
|
||||||
)
|
)
|
||||||
set_interface.disperse_interface()
|
set_interface.disperse_interface()
|
||||||
session.username = username
|
session.username = username
|
||||||
session.set_key_entry = key_selection
|
session.set_key_entry = key_selection
|
||||||
session.confirm_interface = set_interface.interface_index
|
session.confirm_interface = set_interface.attr_indices
|
||||||
self.sessions[session_id] = session
|
self.sessions[session_id] = session
|
||||||
return self.sessions[session_id].confirm_interface
|
return self.sessions[session_id].confirm_interface
|
||||||
|
|
||||||
@@ -76,14 +76,14 @@ class NKodeAPI(BaseModel):
|
|||||||
set_values = customer.interface.set_vals
|
set_values = customer.interface.set_vals
|
||||||
if not customer.valid_new_nkode(passcode):
|
if not customer.valid_new_nkode(passcode):
|
||||||
return False
|
return False
|
||||||
new_user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_values)
|
new_user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_values, customer.nkode_policy.max_nkode_len)
|
||||||
enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.interface)
|
enciphered_passcode = new_user_keys.encipher_nkode(passcode, customer.interface)
|
||||||
new_user = UserDBModel(
|
new_user = User(
|
||||||
username=username,
|
username=username,
|
||||||
enciphered_passcode=enciphered_passcode,
|
enciphered_passcode=enciphered_passcode,
|
||||||
user_keys=new_user_keys,
|
user_keys=new_user_keys,
|
||||||
user_interface=UserInterface(
|
user_interface=UserInterface(
|
||||||
interface_index=self.sessions[session_id].confirm_interface,
|
attr_indices=self.sessions[session_id].confirm_interface,
|
||||||
attrs_per_key=attrs_per_key,
|
attrs_per_key=attrs_per_key,
|
||||||
numb_of_keys=numb_of_keys,
|
numb_of_keys=numb_of_keys,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from uuid import UUID
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from src.customer_interface import CustomerInterface
|
from src.customer_interface import CustomerInterface
|
||||||
from src.models import NKodePolicy
|
from src.models import NKodePolicy
|
||||||
from src.user_db_model import UserDBModel
|
from src.user import User
|
||||||
from src.utils import xor_lists
|
from src.utils import xor_lists
|
||||||
|
|
||||||
|
|
||||||
@@ -10,9 +10,9 @@ class Customer(BaseModel):
|
|||||||
customer_id: UUID
|
customer_id: UUID
|
||||||
nkode_policy: NKodePolicy
|
nkode_policy: NKodePolicy
|
||||||
interface: CustomerInterface
|
interface: CustomerInterface
|
||||||
users: dict[str, UserDBModel]
|
users: dict[str, User]
|
||||||
|
|
||||||
def add_new_user(self, user: UserDBModel):
|
def add_new_user(self, user: User):
|
||||||
self.users[user.username] = user
|
self.users[user.username] = user
|
||||||
|
|
||||||
def valid_key_entry(self, username, selected_keys) -> bool:
|
def valid_key_entry(self, username, selected_keys) -> bool:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from uuid import UUID
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class SessionCacheModel(BaseModel):
|
class SessionCache(BaseModel):
|
||||||
session_id: UUID
|
session_id: UUID
|
||||||
set_interface: list[int] | None = None
|
set_interface: list[int] | None = None
|
||||||
confirm_interface: list[int] | None = None
|
confirm_interface: list[int] | None = None
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from src.user_interface import UserInterface
|
|||||||
from src.utils import xor_lists
|
from src.utils import xor_lists
|
||||||
|
|
||||||
|
|
||||||
class UserDBModel(BaseModel):
|
class User(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
enciphered_passcode: EncipheredNKode
|
enciphered_passcode: EncipheredNKode
|
||||||
user_keys: UserCipherKeys
|
user_keys: UserCipherKeys
|
||||||
@@ -23,8 +23,8 @@ class UserDBModel(BaseModel):
|
|||||||
self.user_keys = UserCipherKeys.new(
|
self.user_keys = UserCipherKeys.new(
|
||||||
customer_interface.numb_of_keys,
|
customer_interface.numb_of_keys,
|
||||||
customer_interface.attrs_per_key,
|
customer_interface.attrs_per_key,
|
||||||
customer_interface.set_vals
|
customer_interface.set_vals,
|
||||||
|
self.user_keys.max_nkode_len
|
||||||
)
|
)
|
||||||
self.enciphered_passcode = self.user_keys.encipher_nkode(passcode_attr_idx, customer_interface)
|
self.enciphered_passcode = self.user_keys.encipher_nkode(passcode_attr_idx, customer_interface)
|
||||||
self.renew = False
|
self.renew = False
|
||||||
@@ -15,10 +15,10 @@ class UserCipherKeys(BaseModel):
|
|||||||
pass_key: list[int]
|
pass_key: list[int]
|
||||||
mask_key: list[int]
|
mask_key: list[int]
|
||||||
salt: bytes
|
salt: bytes
|
||||||
max_nkode_len: int = 10
|
max_nkode_len: int
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int]):
|
def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int], max_nkode_len: int):
|
||||||
assert len(set_values) == attrs_per_key
|
assert len(set_values) == attrs_per_key
|
||||||
|
|
||||||
set_key = generate_random_nonrepeating_list(attrs_per_key)
|
set_key = generate_random_nonrepeating_list(attrs_per_key)
|
||||||
@@ -26,10 +26,11 @@ class UserCipherKeys(BaseModel):
|
|||||||
|
|
||||||
return UserCipherKeys(
|
return UserCipherKeys(
|
||||||
alpha_key=generate_random_nonrepeating_list(attrs_per_key * numb_of_keys),
|
alpha_key=generate_random_nonrepeating_list(attrs_per_key * numb_of_keys),
|
||||||
pass_key=generate_random_nonrepeating_list(numb_of_keys),
|
pass_key=generate_random_nonrepeating_list(max_nkode_len),
|
||||||
mask_key=generate_random_nonrepeating_list(numb_of_keys),
|
mask_key=generate_random_nonrepeating_list(max_nkode_len),
|
||||||
set_key=set_key,
|
set_key=set_key,
|
||||||
salt=bcrypt.gensalt(),
|
salt=bcrypt.gensalt(),
|
||||||
|
max_nkode_len=max_nkode_len
|
||||||
)
|
)
|
||||||
|
|
||||||
def pad_user_mask(self, user_mask: list[int], set_vals: list[int]) -> list[int]:
|
def pad_user_mask(self, user_mask: list[int], set_vals: list[int]) -> list[int]:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from src.utils import list_to_matrix, secure_fisher_yates_shuffle, matrix_to_lis
|
|||||||
|
|
||||||
|
|
||||||
class UserInterface(BaseModel):
|
class UserInterface(BaseModel):
|
||||||
interface_index: list[int]
|
attr_indices: list[int]
|
||||||
attrs_per_key: int
|
attrs_per_key: int
|
||||||
numb_of_keys: int
|
numb_of_keys: int
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ class UserInterface(BaseModel):
|
|||||||
def new(attrs_per_key: int, numb_of_keys: int):
|
def new(attrs_per_key: int, numb_of_keys: int):
|
||||||
# Todo: this a hack do a proper random interface
|
# Todo: this a hack do a proper random interface
|
||||||
interface = UserInterface(
|
interface = UserInterface(
|
||||||
interface_index=list(range(attrs_per_key * numb_of_keys)),
|
attr_indices=list(range(attrs_per_key * numb_of_keys)),
|
||||||
attrs_per_key=attrs_per_key,
|
attrs_per_key=attrs_per_key,
|
||||||
numb_of_keys=numb_of_keys,
|
numb_of_keys=numb_of_keys,
|
||||||
)
|
)
|
||||||
@@ -22,10 +22,10 @@ class UserInterface(BaseModel):
|
|||||||
return interface
|
return interface
|
||||||
|
|
||||||
def disperse_interface(self):
|
def disperse_interface(self):
|
||||||
user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key)
|
user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key)
|
||||||
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
||||||
dispersed_interface = self._random_attribute_rotation(shuffled_keys, list(range(self.attrs_per_key)))
|
dispersed_interface = self._random_attribute_rotation(shuffled_keys, list(range(self.attrs_per_key)))
|
||||||
self.interface_index = matrix_to_list(dispersed_interface)
|
self.attr_indices = matrix_to_list(dispersed_interface)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def matrix_transpose(interface: list[list[int]]) -> list[list[int]]:
|
def matrix_transpose(interface: list[list[int]]) -> list[list[int]]:
|
||||||
@@ -37,7 +37,7 @@ class UserInterface(BaseModel):
|
|||||||
# randomly shuffle half the sets. if attrs_per_key is odd, randomly add one 50% of the time
|
# 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.attrs_per_key & 1) == 1 else 0
|
numb_of_selected_sets += choice([0, 1]) if (self.attrs_per_key & 1) == 1 else 0
|
||||||
selected_sets = secure_fisher_yates_shuffle(list(range(self.attrs_per_key)))[:numb_of_selected_sets]
|
selected_sets = secure_fisher_yates_shuffle(list(range(self.attrs_per_key)))[:numb_of_selected_sets]
|
||||||
user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key)
|
user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key)
|
||||||
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
||||||
interface_by_sets = []
|
interface_by_sets = []
|
||||||
for idx, attrs in enumerate(self.matrix_transpose(shuffled_keys)):
|
for idx, attrs in enumerate(self.matrix_transpose(shuffled_keys)):
|
||||||
@@ -45,7 +45,7 @@ class UserInterface(BaseModel):
|
|||||||
interface_by_sets.append(secure_fisher_yates_shuffle(attrs))
|
interface_by_sets.append(secure_fisher_yates_shuffle(attrs))
|
||||||
else:
|
else:
|
||||||
interface_by_sets.append(attrs)
|
interface_by_sets.append(attrs)
|
||||||
self.interface_index = matrix_to_list(self.matrix_transpose(interface_by_sets))
|
self.attr_indices = matrix_to_list(self.matrix_transpose(interface_by_sets))
|
||||||
|
|
||||||
def _random_attribute_rotation(self, user_interface: list[list[int]], selected_sets: list[int]) -> list[list[int]]:
|
def _random_attribute_rotation(self, user_interface: list[list[int]], selected_sets: list[int]) -> list[list[int]]:
|
||||||
attr_rotation = secure_fisher_yates_shuffle(list(range(self.numb_of_keys)))[:self.attrs_per_key]
|
attr_rotation = secure_fisher_yates_shuffle(list(range(self.numb_of_keys)))[:self.attrs_per_key]
|
||||||
@@ -59,7 +59,7 @@ class UserInterface(BaseModel):
|
|||||||
return self.matrix_transpose(transposed_user_interface)
|
return self.matrix_transpose(transposed_user_interface)
|
||||||
|
|
||||||
def attribute_adjacency_graph(self) -> dict[int, set[int]]:
|
def attribute_adjacency_graph(self) -> dict[int, set[int]]:
|
||||||
user_interface_keypad = list_to_matrix(self.interface_index, self.attrs_per_key)
|
user_interface_keypad = list_to_matrix(self.attr_indices, self.attrs_per_key)
|
||||||
graph = {}
|
graph = {}
|
||||||
for key in user_interface_keypad:
|
for key in user_interface_keypad:
|
||||||
for attr in key:
|
for attr in key:
|
||||||
@@ -70,5 +70,5 @@ class UserInterface(BaseModel):
|
|||||||
|
|
||||||
def get_key_attr_idxs(self, key_numb: int) -> list[int]:
|
def get_key_attr_idxs(self, key_numb: int) -> list[int]:
|
||||||
assert (0 <= key_numb < self.numb_of_keys)
|
assert (0 <= key_numb < self.numb_of_keys)
|
||||||
keypad_attr_idx = list_to_matrix(self.interface_index, self.attrs_per_key)
|
keypad_attr_idx = list_to_matrix(self.attr_indices, self.attrs_per_key)
|
||||||
return keypad_attr_idx[key_numb]
|
return keypad_attr_idx[key_numb]
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ from src.user_interface import UserInterface
|
|||||||
def test_attr_set_idx(numb_of_keys, attrs_per_key):
|
def test_attr_set_idx(numb_of_keys, attrs_per_key):
|
||||||
user_interface = UserInterface.new(attrs_per_key, numb_of_keys)
|
user_interface = UserInterface.new(attrs_per_key, numb_of_keys)
|
||||||
for attr_idx in range(70):
|
for attr_idx in range(70):
|
||||||
user_interface_idx = user_interface.interface_index[attr_idx]
|
user_interface_idx = user_interface.attr_indices[attr_idx]
|
||||||
|
|
||||||
assert (attr_idx % attrs_per_key == user_interface_idx % attrs_per_key)
|
assert (attr_idx % attrs_per_key == user_interface_idx % attrs_per_key)
|
||||||
|
|||||||
@@ -16,20 +16,21 @@ def test_encode_decode_base64(passcode_len):
|
|||||||
assert (len(data) == len(decoded))
|
assert (len(data) == len(decoded))
|
||||||
assert (all(data[idx] == decoded[idx] for idx in range(passcode_len)))
|
assert (all(data[idx] == decoded[idx] for idx in range(passcode_len)))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"numb_of_keys,attrs_per_key",
|
"numb_of_keys,attrs_per_key,max_nkode_len",
|
||||||
[
|
[
|
||||||
(10, 7,)
|
(10, 7,10)
|
||||||
])
|
])
|
||||||
def test_decode_mask(numb_of_keys, attrs_per_key):
|
def test_decode_mask(numb_of_keys, attrs_per_key, max_nkode_len):
|
||||||
customer = CustomerInterface.new(numb_of_keys, attrs_per_key)
|
customer = CustomerInterface.new(numb_of_keys, attrs_per_key)
|
||||||
passcode_entry = generate_random_nonrepeating_list(numb_of_keys*attrs_per_key, max_val=70)[:4]
|
passcode_entry = generate_random_nonrepeating_list(numb_of_keys * attrs_per_key, max_val=70)[:4]
|
||||||
passcode_values = [customer.attr_vals[idx] for idx in passcode_entry]
|
passcode_values = [customer.attr_vals[idx] for idx in passcode_entry]
|
||||||
set_vals = customer.set_vals
|
set_vals = customer.set_vals
|
||||||
user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_vals)
|
user_keys = UserCipherKeys.new(numb_of_keys, attrs_per_key, set_vals, max_nkode_len)
|
||||||
passcode = user_keys.encipher_nkode(passcode_entry, customer)
|
passcode = user_keys.encipher_nkode(passcode_entry, customer)
|
||||||
|
|
||||||
orig_passcode_set_vals = [customer.get_attr_set_val(attr) for attr in passcode_values]
|
orig_passcode_set_vals = [customer.get_attr_set_val(attr) for attr in passcode_values]
|
||||||
passcode_set_vals = user_keys.decipher_mask(passcode.mask, set_vals, len(passcode_entry))
|
passcode_set_vals = user_keys.decipher_mask(passcode.mask, set_vals, len(passcode_entry))
|
||||||
assert(len(passcode_set_vals) == len(orig_passcode_set_vals))
|
assert (len(passcode_set_vals) == len(orig_passcode_set_vals))
|
||||||
assert(all(orig_passcode_set_vals[idx] == passcode_set_vals[idx] for idx in range(len(passcode_set_vals))))
|
assert (all(orig_passcode_set_vals[idx] == passcode_set_vals[idx] for idx in range(len(passcode_set_vals))))
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ def test_shuffle_attrs(user_interface):
|
|||||||
- every attribute is adjacent to every other attribute with uniform distribution
|
- every attribute is adjacent to every other attribute with uniform distribution
|
||||||
- the order in which the attributes move from key to key is random (i.e. the distance traveled is uniform)
|
- the order in which the attributes move from key to key is random (i.e. the distance traveled is uniform)
|
||||||
"""
|
"""
|
||||||
pre_shuffle_interface = user_interface.interface_index
|
pre_shuffle_interface = user_interface.attr_indices
|
||||||
user_interface.shuffle_interface()
|
user_interface.shuffle_interface()
|
||||||
post_shuffle_interface = user_interface.interface_index
|
post_shuffle_interface = user_interface.attr_indices
|
||||||
for i in range(1000):
|
for i in range(1000):
|
||||||
assert (not all(
|
assert (not all(
|
||||||
post_shuffle_interface[idx] == pre_shuffle_interface[idx] for idx in range(len(post_shuffle_interface))
|
post_shuffle_interface[idx] == pre_shuffle_interface[idx] for idx in range(len(post_shuffle_interface))
|
||||||
|
|||||||
Reference in New Issue
Block a user