From c8c80e2b37c948e638eea6d41decc44767fefe79 Mon Sep 17 00:00:00 2001 From: Donovan Date: Thu, 1 Aug 2024 14:17:43 -0500 Subject: [PATCH] add renew to tutorial --- nkode_tutorial.ipynb | 329 ++++++++++++++++++++++++++++++------------- src/customer.py | 12 +- 2 files changed, 237 insertions(+), 104 deletions(-) diff --git a/nkode_tutorial.ipynb b/nkode_tutorial.ipynb index 4efc752..699f066 100644 --- a/nkode_tutorial.ipynb +++ b/nkode_tutorial.ipynb @@ -12,12 +12,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.394441Z", - "start_time": "2024-08-01T17:32:43.392271Z" + "end_time": "2024-08-01T19:17:14.809230Z", + "start_time": "2024-08-01T19:17:14.807153Z" } }, "outputs": [], - "execution_count": 41 + "execution_count": 90 }, { "cell_type": "code", @@ -38,12 +38,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.447194Z", - "start_time": "2024-08-01T17:32:43.444212Z" + "end_time": "2024-08-01T19:17:14.881089Z", + "start_time": "2024-08-01T19:17:14.877818Z" } }, "outputs": [], - "execution_count": 42 + "execution_count": 91 }, { "cell_type": "code", @@ -53,12 +53,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.450349Z", - "start_time": "2024-08-01T17:32:43.448288Z" + "end_time": "2024-08-01T19:17:14.887878Z", + "start_time": "2024-08-01T19:17:14.885951Z" } }, "outputs": [], - "execution_count": 43 + "execution_count": 92 }, { "cell_type": "markdown", @@ -98,12 +98,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.624648Z", - "start_time": "2024-08-01T17:32:43.451407Z" + "end_time": "2024-08-01T19:17:15.071180Z", + "start_time": "2024-08-01T19:17:14.890419Z" } }, "outputs": [], - "execution_count": 44 + "execution_count": 93 }, { "cell_type": "markdown", @@ -137,8 +137,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.627977Z", - "start_time": "2024-08-01T17:32:43.625820Z" + "end_time": "2024-08-01T19:17:15.075131Z", + "start_time": "2024-08-01T19:17:15.072535Z" } }, "outputs": [ @@ -146,17 +146,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Sets: [21921, 7738, 57204, 44555, 38162, 22025]\n", + "Customer Sets: [27169, 36659, 54746, 56159, 47007, 38654]\n", "Keypad View\n", - "Key 0: [25764, 64003, 42138, 9976, 26896, 36991]\n", - "Key 1: [53269, 22932, 2731, 14908, 55210, 40777]\n", - "Key 2: [11304, 9784, 26683, 48512, 24904, 42130]\n", - "Key 3: [47297, 63193, 31705, 44, 46268, 28475]\n", - "Key 4: [38192, 28529, 11254, 19824, 47753, 34896]\n" + "Key 0: [63421, 44364, 4387, 12321, 14840, 38251]\n", + "Key 1: [42757, 19108, 4797, 54866, 10010, 33108]\n", + "Key 2: [61136, 42782, 46073, 53243, 53024, 53858]\n", + "Key 3: [44676, 54825, 25002, 7458, 37684, 16872]\n", + "Key 4: [65393, 10661, 14854, 8995, 64876, 36070]\n" ] } ], - "execution_count": 45 + "execution_count": 94 }, { "cell_type": "markdown", @@ -180,8 +180,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.631493Z", - "start_time": "2024-08-01T17:32:43.628798Z" + "end_time": "2024-08-01T19:17:15.078753Z", + "start_time": "2024-08-01T19:17:15.076083Z" } }, "outputs": [ @@ -190,16 +190,16 @@ "output_type": "stream", "text": [ "Set to Attribute Map:\n", - "21921: [25764, 53269, 11304, 47297, 38192]\n", - "7738: [64003, 22932, 9784, 63193, 28529]\n", - "57204: [42138, 2731, 26683, 31705, 11254]\n", - "44555: [9976, 14908, 48512, 44, 19824]\n", - "38162: [26896, 55210, 24904, 46268, 47753]\n", - "22025: [36991, 40777, 42130, 28475, 34896]\n" + "27169: [63421, 42757, 61136, 44676, 65393]\n", + "36659: [44364, 19108, 42782, 54825, 10661]\n", + "54746: [4387, 4797, 46073, 25002, 14854]\n", + "56159: [12321, 54866, 53243, 7458, 8995]\n", + "47007: [14840, 10010, 53024, 37684, 64876]\n", + "38654: [38251, 33108, 53858, 16872, 36070]\n" ] } ], - "execution_count": 46 + "execution_count": 95 }, { "cell_type": "markdown", @@ -229,27 +229,27 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.636317Z", - "start_time": "2024-08-01T17:32:43.633201Z" + "end_time": "2024-08-01T19:17:15.083395Z", + "start_time": "2024-08-01T19:17:15.080158Z" } }, "outputs": [ { "data": { "text/plain": [ - "[[18, 11, 22, 2, 19],\n", - " [0, 23, 28, 8, 25],\n", - " [6, 5, 10, 26, 13],\n", - " [12, 17, 4, 14, 1],\n", - " [24, 29, 16, 20, 7]]" + "[[25, 14, 15, 23, 28],\n", + " [13, 26, 21, 17, 4],\n", + " [19, 20, 27, 29, 10],\n", + " [1, 8, 3, 5, 22],\n", + " [7, 2, 9, 11, 16]]" ] }, - "execution_count": 47, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 47 + "execution_count": 96 }, { "cell_type": "markdown", @@ -275,8 +275,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.639361Z", - "start_time": "2024-08-01T17:32:43.637070Z" + "end_time": "2024-08-01T19:17:15.087201Z", + "start_time": "2024-08-01T19:17:15.084377Z" } }, "outputs": [ @@ -285,18 +285,18 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [18, 11, 22, 2, 19]\n", - "Key 1: [0, 23, 28, 8, 25]\n", - "Key 2: [6, 5, 10, 26, 13]\n", - "Key 3: [12, 17, 4, 14, 1]\n", - "Key 4: [24, 29, 16, 20, 7]\n", - "User Passcode: [18, 11, 22, 2]\n", + "Key 0: [25, 14, 15, 23, 28]\n", + "Key 1: [13, 26, 21, 17, 4]\n", + "Key 2: [19, 20, 27, 29, 10]\n", + "Key 3: [1, 8, 3, 5, 22]\n", + "Key 4: [7, 2, 9, 11, 16]\n", + "User Passcode: [25, 14, 15, 23]\n", "Selected Keys\n", "[0, 0, 0, 0]\n" ] } ], - "execution_count": 48 + "execution_count": 97 }, { "cell_type": "code", @@ -309,8 +309,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:43.642851Z", - "start_time": "2024-08-01T17:32:43.640093Z" + "end_time": "2024-08-01T19:17:15.090959Z", + "start_time": "2024-08-01T19:17:15.088160Z" } }, "outputs": [ @@ -319,17 +319,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [6, 29, 4, 8, 19]\n", - "Key 1: [12, 5, 22, 20, 25]\n", - "Key 2: [18, 17, 28, 26, 7]\n", - "Key 3: [0, 11, 16, 14, 13]\n", - "Key 4: [24, 23, 10, 2, 1]\n", + "Key 0: [1, 14, 21, 11, 10]\n", + "Key 1: [19, 2, 15, 5, 4]\n", + "Key 2: [13, 8, 9, 29, 28]\n", + "Key 3: [25, 20, 3, 17, 16]\n", + "Key 4: [7, 26, 27, 23, 22]\n", "Selected Keys\n", - "[2, 3, 1, 4]\n" + "[3, 0, 1, 4]\n" ] } ], - "execution_count": 49 + "execution_count": 98 }, { "cell_type": "code", @@ -341,8 +341,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:44.271378Z", - "start_time": "2024-08-01T17:32:43.643592Z" + "end_time": "2024-08-01T19:17:15.725645Z", + "start_time": "2024-08-01T19:17:15.091674Z" } }, "outputs": [ @@ -354,7 +354,7 @@ ] } ], - "execution_count": 50 + "execution_count": 99 }, { "metadata": {}, @@ -373,8 +373,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:32:44.606104Z", - "start_time": "2024-08-01T17:32:44.272228Z" + "end_time": "2024-08-01T19:17:16.066633Z", + "start_time": "2024-08-01T19:17:15.726507Z" } }, "cell_type": "code", @@ -392,7 +392,7 @@ "passcode_server_set = [customer.attributes.get_attr_set_val(attr) for attr in passcode_server_attr]" ], "outputs": [], - "execution_count": 51 + "execution_count": 100 }, { "metadata": {}, @@ -414,8 +414,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:32:44.609542Z", - "start_time": "2024-08-01T17:32:44.606956Z" + "end_time": "2024-08-01T19:17:16.070242Z", + "start_time": "2024-08-01T19:17:16.067409Z" } }, "cell_type": "code", @@ -431,7 +431,7 @@ "mask = user_keys.encode_base64_str(ciphered_mask)" ], "outputs": [], - "execution_count": 52 + "execution_count": 101 }, { "metadata": {}, @@ -448,8 +448,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:32:44.916167Z", - "start_time": "2024-08-01T17:32:44.611593Z" + "end_time": "2024-08-01T19:17:16.378573Z", + "start_time": "2024-08-01T19:17:16.072571Z" } }, "cell_type": "code", @@ -473,13 +473,13 @@ "code = hashed_data.decode(\"utf-8\")" ], "outputs": [], - "execution_count": 53 + "execution_count": 102 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:32:44.919048Z", - "start_time": "2024-08-01T17:32:44.916981Z" + "end_time": "2024-08-01T19:17:16.381295Z", + "start_time": "2024-08-01T19:17:16.379384Z" } }, "cell_type": "code", @@ -492,7 +492,7 @@ ")" ], "outputs": [], - "execution_count": 54 + "execution_count": 103 }, { "cell_type": "markdown", @@ -518,8 +518,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:45.225994Z", - "start_time": "2024-08-01T17:32:44.919967Z" + "end_time": "2024-08-01T19:17:16.687966Z", + "start_time": "2024-08-01T19:17:16.382136Z" } }, "outputs": [ @@ -528,17 +528,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [24, 7, 14, 21, 10, 29]\n", - "Key 1: [6, 13, 26, 9, 28, 5]\n", - "Key 2: [0, 25, 20, 3, 22, 23]\n", - "Key 3: [18, 19, 2, 27, 16, 11]\n", - "Key 4: [12, 1, 8, 15, 4, 17]\n", - "Selected Keys: [3, 3, 2, 3]\n", + "Key 0: [0, 7, 20, 3, 10, 29]\n", + "Key 1: [6, 25, 26, 15, 4, 17]\n", + "Key 2: [12, 19, 14, 21, 28, 23]\n", + "Key 3: [18, 1, 2, 9, 16, 11]\n", + "Key 4: [24, 13, 8, 27, 22, 5]\n", + "Selected Keys: [1, 2, 1, 2]\n", "True\n" ] } ], - "execution_count": 55 + "execution_count": 104 }, { "metadata": {}, @@ -570,8 +570,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:35:30.015361Z", - "start_time": "2024-08-01T17:35:30.010731Z" + "end_time": "2024-08-01T19:17:16.691445Z", + "start_time": "2024-08-01T19:17:16.688722Z" } }, "cell_type": "code", @@ -593,11 +593,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "[21921, 22025, 38162, 57204]\n" + "[36659, 54746, 56159, 38654]\n" ] } ], - "execution_count": 60 + "execution_count": 105 }, { "metadata": {}, @@ -607,8 +607,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-01T17:35:31.615135Z", - "start_time": "2024-08-01T17:35:31.307978Z" + "end_time": "2024-08-01T19:17:16.694718Z", + "start_time": "2024-08-01T19:17:16.692225Z" } }, "cell_type": "code", @@ -622,9 +622,7 @@ " selected_attr_idx = customer.users[username].user_interface.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)\n", " presumed_selected_attributes_idx.append(selected_attr_idx)\n", "\n", - "enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n", - "\n", - "print(enciphered_nkode == user.enciphered_passcode.code)" + "print(user_passcode == presumed_selected_attributes_idx)" ], "outputs": [ { @@ -635,31 +633,166 @@ ] } ], - "execution_count": 61 + "execution_count": 106 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Compare Enciphered Passcodes" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-01T19:17:17.000271Z", + "start_time": "2024-08-01T19:17:16.695443Z" + } + }, + "cell_type": "code", + "source": [ + "enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n", + "\n", + "print(enciphered_nkode == user.enciphered_passcode.code)\n" + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "execution_count": 107 }, { "cell_type": "markdown", "source": [ "#### Renew Keys\n", "1. Renew Customer Keys\n", - "2. Renew User Keys" + "2. Renew User Keys\n", + "3. Refresh User on Login\n", + "\n" ], "metadata": { "collapsed": false } }, { - "cell_type": "code", - "source": [], "metadata": { - "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-01T17:32:45.239562Z", - "start_time": "2024-08-01T17:32:45.239409Z" + "end_time": "2024-08-01T19:17:18.107245Z", + "start_time": "2024-08-01T19:17:17.001318Z" } }, + "cell_type": "code", + "source": [ + "def print_user_enciphered_code():\n", + " mask = api.customers[customer_id].users[username].enciphered_passcode.mask\n", + " code = api.customers[customer_id].users[username].enciphered_passcode.code\n", + " print(f\"mask: {mask}, code: {code}\")\n", + "\n", + "print_user_enciphered_code() \n", + "api.renew_keys(customer_id)\n", + "print_user_enciphered_code()\n", + "\n", + "login_interface = api.get_login_interface(username, customer_id)\n", + "selected_keys_login = select_keys_with_passcode_values(user_passcode, login_interface, keypad_size.attrs_per_key)\n", + "success = api.login(customer_id, username, selected_keys_login)\n", + "print(success)\n", + "print_user_enciphered_code()" + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mask: AH3tBMcj4vYN+WBQa4Wgwe3KDgE=, code: $2b$12$2w4nllgf0wnWMsdTYL4HNuxVN5A5AzqP5X2Xraj0ijKCAjM3kRlLm\n", + "mask: AH3tBMcj4vYN+WBQa4Wgwe3KDgE=, code: $2b$12$2w4nllgf0wnWMsdTYL4HNuxVN5A5AzqP5X2Xraj0ijKCAjM3kRlLm\n", + "True\n", + "mask: pbB0tDKAFPtJUEQjyUZBdSCRsIc=, code: $2b$12$KW3WJQ2YNsbmyWJ6Rq32eed4/GJ6howBNsKA/sUhFOHRf.sIW2Pz6\n" + ] + } + ], + "execution_count": 108 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "#### Renew Customer Keys\n", + "- Get old attributes and sets\n", + "- Replace attributes and sets" + ] + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-01T19:17:18.281874Z", + "start_time": "2024-08-01T19:17:18.108020Z" + } + }, + "cell_type": "code", + "source": [ + "old_attrs = customer.attributes.attr_vals.copy()\n", + "old_sets = customer.attributes.set_vals.copy()\n", + "customer.attributes.renew()\n", + "new_attrs = customer.attributes.attr_vals\n", + "new_sets = customer.attributes.set_vals" + ], "outputs": [], - "execution_count": null + "execution_count": 109 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Renew User\n", + "\n" + ] + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-01T19:17:18.285195Z", + "start_time": "2024-08-01T19:17:18.282828Z" + } + }, + "cell_type": "code", + "source": [ + "attrs_xor = xor_lists(new_attrs, old_attrs)\n", + "sets_xor = xor_lists(new_sets, old_sets)\n", + "for user in customer.users.values():\n", + " user.renew = True\n", + " user.user_keys.set_key = xor_lists(user.user_keys.set_key, sets_xor)\n", + " user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)" + ], + "outputs": [], + "execution_count": 110 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Refresh User Keys" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-01T19:17:18.921478Z", + "start_time": "2024-08-01T19:17:18.286363Z" + } + }, + "cell_type": "code", + "source": [ + "user.user_keys = UserCipherKeys.new(\n", + " customer.attributes.keypad_size,\n", + " customer.attributes.set_vals,\n", + " user.user_keys.max_nkode_len\n", + ")\n", + "user.enciphered_passcode = user.user_keys.encipher_nkode(presumed_selected_attributes_idx, customer.attributes)\n", + "user.renew = False" + ], + "outputs": [], + "execution_count": 111 } ], "metadata": { diff --git a/src/customer.py b/src/customer.py index 7db7626..8ee9afb 100644 --- a/src/customer.py +++ b/src/customer.py @@ -44,14 +44,14 @@ class Customer(BaseModel): return True def renew_keys(self) -> bool: - attrs_before = self.attributes.attr_vals.copy() - sets_before = self.attributes.set_vals.copy() + old_attrs = self.attributes.attr_vals.copy() + old_sets = self.attributes.set_vals.copy() self.attributes.renew() - attrs_after = self.attributes.attr_vals - sets_after = self.attributes.set_vals + new_attrs = self.attributes.attr_vals + new_sets = self.attributes.set_vals - attrs_xor = xor_lists(attrs_after, attrs_before) - set_xor = xor_lists(sets_after, sets_before) + attrs_xor = xor_lists(new_attrs, old_attrs) + set_xor = xor_lists(new_sets, old_sets) for user in self.users.values(): user.renew_keys(set_xor, attrs_xor) return True