refactor; rename alpha to prop

This commit is contained in:
2025-03-10 10:05:49 -05:00
parent 571268b86a
commit dd0b496a21
7 changed files with 33 additions and 38 deletions

View File

@@ -165,7 +165,7 @@ set_key = generate_random_nonrepeating_list(keypad_size.attrs_per_key, max_numb=
set_key = xor_lists(set_key, customer_attr.set_vals)
UserCipherKeys(
alpha_key=generate_random_nonrepeating_list(keypad_size.attrs_per_key * keypad_size.numb_of_keys, max_numb=2**(8*numb_of_bytes)),
prop_key=generate_random_nonrepeating_list(keypad_size.attrs_per_key * keypad_size.numb_of_keys, max_numb=2**(8*numb_of_bytes)),
pass_key=generate_random_nonrepeating_list(max_nkode_len, max_numb=2**(8*numb_of_bytes)),
mask_key=generate_random_nonrepeating_list(max_nkode_len, max_numb=2**(8*numb_of_bytes)),
set_key=set_key,
@@ -177,7 +177,7 @@ UserCipherKeys(
##### User Cipher Keys Values
```
user_keys = UserCipherKeys(
alpha_key = {{ user_keys.alpha_key }},
prop_key = {{ user_keys.prop_key }},
pass_key = {{ user_keys.pass_key }},
mask_key = {{ user_keys.mask_key }},
set_key = {{ user_keys.set_key }},
@@ -233,12 +233,12 @@ Mask: {{ enciphered_nkode.mask }}
#### Passcode Enciphering and Hashing
- ciphered_customer_attr = alpha_key ^ customer_attr
- ciphered_customer_attr = prop_key ^ customer_attr
- ciphered_passcode_i = pass_key_i ^ ciphered_customer_attr_i
- code = hash(ciphered_passcode, salt)
```
ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.alpha_key)
ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.prop_key)
passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in passcode]
pad_len = customer.nkode_policy.max_nkode_len - passcode_len
@@ -389,17 +389,17 @@ sets_xor = xor_lists(new_sets, old_sets)
for user in customer.users.values():
user.renew = True
user.user_keys.set_key = xor_lists(user.user_keys.set_key, sets_xor)
user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)
user.user_keys.prop_key = xor_lists(user.user_keys.prop_key, attrs_xor)
```
##### User Alpha Key
The user's alpha key was a randomly generated list of length `numb_of_keys * attr_per_key`.
Now each value in the alpha key is `alpha_key_i = old_alpha_key_i ^ new_attr_i ^ old_attr_i`.
Recall in the login process, `ciphered_customer_attrs = alpha_key ^ customer_attr`.
##### User prop Key
The user's prop key was a randomly generated list of length `numb_of_keys * attr_per_key`.
Now each value in the prop key is `prop_key_i = old_prop_key_i ^ new_attr_i ^ old_attr_i`.
Recall in the login process, `ciphered_customer_attrs = prop_key ^ customer_attr`.
Since the customer_attr is now the new value, it gets canceled out, leaving:
```
new_alpha_key = old_alpha_key ^ old_attr ^ new_attr
ciphered_customer_attrs = new_alpha_key ^ new_attr
ciphered_customer_attrs = old_alpha_key ^ old_attr # since new_attr cancel out
new_prop_key = old_prop_key ^ old_attr ^ new_attr
ciphered_customer_attrs = new_prop_key ^ new_attr
ciphered_customer_attrs = old_prop_key ^ old_attr # since new_attr cancel out
```
Using the new customer attributes, we can validate the user's login attempt with the same hash.

View File

@@ -2,7 +2,7 @@ from jinja2 import Environment, FileSystemLoader
import os
from src.nkode_api import NKodeAPI
from src.models import NKodePolicy, KeypadSize, EncipheredNKode
from src.user_cipher_keys import UserCipher
from src.user_cipher import UserCipher
from src.utils import list_to_matrix, matrix_transpose, xor_lists
from secrets import choice
from string import ascii_lowercase
@@ -101,7 +101,7 @@ if __name__ == "__main__":
ciphered_mask = xor_lists(ciphered_mask, user_keys.mask_key)
mask = user_keys.encode_base64_str(ciphered_mask)
ciphered_customer_attrs = xor_lists(customer.customer_cipher.prop_key, user_keys.alpha_key)
ciphered_customer_attrs = xor_lists(customer.customer_cipher.prop_key, user_keys.prop_key)
passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in user_passcode]
pad_len = customer.nkode_policy.max_nkode_len - passcode_len
@@ -176,7 +176,7 @@ if __name__ == "__main__":
for user in customer.users.values():
user.renew = True
user.user_keys.set_key = xor_lists(user.user_keys.set_key, sets_xor)
user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)
user.user_keys.prop_key = xor_lists(user.user_keys.prop_key, attrs_xor)
"""
REFRESH USER KEYS

View File

@@ -1,6 +1,5 @@
from dataclasses import dataclass
from typing import ClassVar
from src.models import KeypadSize
from src.utils import generate_random_nonrepeating_list

View File

@@ -5,7 +5,7 @@ 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 UserCipher
from src.user_cipher import UserCipher
from src.user_signup_session import UserSignupSession
from src.user_keypad import UserKeypad
from src.customer_cipher import CustomerCipher

View File

@@ -1,7 +1,7 @@
from dataclasses import dataclass, field
from src.models import EncipheredNKode
from src.customer_cipher import CustomerCipher
from src.user_cipher_keys import UserCipher
from src.user_cipher import UserCipher
from src.user_keypad import UserKeypad
from src.utils import xor_lists
@@ -17,7 +17,7 @@ class User:
def renew_keys(self, sets_xor: list[int], attrs_xor: list[int]):
self.renew = True
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)
self.user_keys.prop_key = xor_lists(self.user_keys.prop_key, attrs_xor)
def refresh_passcode(self, passcode_attr_idx: list[int], customer_attributes: CustomerCipher):
self.user_keys = UserCipher.create(

View File

@@ -9,7 +9,7 @@ from src.utils import generate_random_nonrepeating_list, xor_lists, int_array_to
@dataclass
class UserCipher:
alpha_key: list[int]
prop_key: list[int]
set_key: list[int]
pass_key: list[int]
mask_key: list[int]
@@ -25,7 +25,7 @@ class UserCipher:
set_key = xor_lists(set_key, set_values)
return UserCipher(
alpha_key=generate_random_nonrepeating_list(keypad_size.props_per_key * keypad_size.numb_of_keys),
prop_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,
@@ -63,14 +63,14 @@ class UserCipher:
def encipher_nkode(
self,
passcode_attr_idx: list[int],
customer_attributes: CustomerCipher
passcode_prop_idx: list[int],
customer_cipher: CustomerCipher
) -> EncipheredNKode:
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)
passcode_attrs = [customer_cipher.prop_key[idx] for idx in passcode_prop_idx]
passcode_sets = [customer_cipher.get_prop_set_val(attr) for attr in passcode_attrs]
mask = self.encipher_mask(passcode_sets, customer_cipher)
code = self.encipher_salt_hash_code(passcode_prop_idx, customer_cipher)
return EncipheredNKode(
code=code,
mask=mask
@@ -78,19 +78,15 @@ class UserCipher:
def encipher_salt_hash_code(
self,
passcode_attr_idx: list[int],
customer_attributes: CustomerCipher,
passcode_prop_idx: list[int],
customer_prop: CustomerCipher,
) -> str:
passcode_len = len(passcode_attr_idx)
passcode_attrs = [customer_attributes.prop_key[idx] for idx in passcode_attr_idx]
passcode_len = len(passcode_prop_idx)
passcode_attrs = [customer_prop.prop_key[idx] for idx in passcode_prop_idx]
passcode_cipher = self.pass_key.copy()
for idx in range(passcode_len):
attr_idx = passcode_attr_idx[idx]
alpha = self.alpha_key[attr_idx]
attr_val = passcode_attrs[idx]
passcode_cipher[idx] ^= alpha ^ attr_val
attr_idx = passcode_prop_idx[idx]
passcode_cipher[idx] ^= self.prop_key[attr_idx] ^ passcode_attrs[idx]
return self._hash_passcode(passcode_cipher)
def encipher_mask(

View File

@@ -1,7 +1,7 @@
import pytest
from src.models import KeypadSize
from src.user_cipher_keys import UserCipher, CustomerCipher
from src.user_cipher import UserCipher, CustomerCipher
from src.utils import generate_random_nonrepeating_list