diff --git a/NIST/SECURE_LCB_FILE_GENERATION.ipynb b/NIST/SECURE_LCB_FILE_GENERATION.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..bbb848c58ace683c119cc38e79cde683af94e7a1 --- /dev/null +++ b/NIST/SECURE_LCB_FILE_GENERATION.ipynb @@ -0,0 +1,568 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "#1#Header\n", + "import csv\n", + "import numpy as np\n", + "import os \n", + "from os import urandom\n", + "from keras.models import model_from_json" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [], + "source": [ + "#2#Defining Global Variables\n", + "num_rounds = 20\n", + "m = 0\n", + "o = 0\n", + "counter = 0\n", + "k_int = 0\n", + "k_int1 = 0" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [], + "source": [ + "#3#Defining WORDSIZE\n", + "def WORD_SIZE():\n", + " return(16);" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [], + "source": [ + "#4#Defining S-Box\n", + "s_box_mapping_np = np.array([12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2], dtype=np.uint8)\n", + "\n", + "def s_box(input_bits):\n", + " input_bits_int = int(input_bits)\n", + " output_bits_int = s_box_mapping_np[input_bits_int]\n", + " return output_bits_int" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [], + "source": [ + "#5#Defining P-Box\n", + "def decimal_to_binary_list(value, num_bits=4):\n", + " return np.array([int(x) for x in format(value, f'0{num_bits}b')], dtype=np.uint8)\n", + "\n", + "def p_box(c_decimal, d_decimal, x_decimal, y_decimal):\n", + " c = decimal_to_binary_list(c_decimal)\n", + " d = decimal_to_binary_list(d_decimal)\n", + " x = decimal_to_binary_list(x_decimal)\n", + " y = decimal_to_binary_list(y_decimal)\n", + " \n", + " e = np.zeros(16, dtype=np.uint8)\n", + "\n", + " e[0] = d[3]\n", + " e[1] = y[1]\n", + " e[2] = c[1]\n", + " e[3] = x[0]\n", + " e[4] = x[3]\n", + " e[5] = y[2]\n", + " e[6] = c[2]\n", + " e[7] = d[1]\n", + " e[8] = d[0]\n", + " e[9] = x[2]\n", + " e[10] = y[3]\n", + " e[11] = c[0]\n", + " e[12] = c[3]\n", + " e[13] = d[2]\n", + " e[14] = x[1]\n", + " e[15] = y[0]\n", + "\n", + " return e" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [], + "source": [ + "#6#Defining L-Box\n", + "def l_box(f):\n", + "\n", + " h = np.zeros(16, dtype=np.uint8)\n", + " h[0] = f[0]\n", + " h[1] = f[8]\n", + " h[2] = f[7]\n", + " h[3] = f[15]\n", + " h[4] = f[1]\n", + " h[5] = f[9]\n", + " h[6] = f[6]\n", + " h[7] = f[14]\n", + " h[8] = f[2]\n", + " h[9] = f[10]\n", + " h[10] = f[5]\n", + " h[11] = f[13]\n", + " h[12] = f[3]\n", + " h[13] = f[11]\n", + " h[14] = f[4]\n", + " h[15] = f[12]\n", + " #print(\"H:\", h)\n", + " return h" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [], + "source": [ + "#7#Defining F-function for Right Side of Plaintext\n", + "def binary_array_to_integer(output):\n", + " int_output = ''.join(map(str, output))\n", + " return int(int_output, 2)\n", + "\n", + "def to_binary(value, bits):\n", + " return format(value, f'0{bits}b')\n", + "\n", + "def f_function(x, key, d):\n", + " q=0\n", + " global m, counter, k_int\n", + " #print(\"X:\", x)\n", + " if isinstance(x, int):\n", + " x = [x]\n", + " input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n", + " for i, val in enumerate(x):\n", + " input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n", + " #print(\"F_FUNCTION\")\n", + " #print(input_parts)\n", + " s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n", + " #print(\"S-box:\", s_box_outputs)\n", + " p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n", + " for i in range(len(x)):\n", + " p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n", + " #print(\"P-box:\", p_box_outputs)\n", + " final_outputs = np.zeros(len(x), dtype=np.uint32)\n", + " #print(len(x))\n", + " for i in range(len(x)):\n", + " #print(len(x))\n", + " final_output = np.array(l_box(p_box_outputs[i][0]))\n", + " k = key[q][(m+1) % 4]\n", + " #print(\"final_output:\", final_output)\n", + " #print(\"Key:\", k)\n", + " if (counter > 1):\n", + " #print(\"counter:\", counter)\n", + " k_bin, k_int = subsequent_key(k_int)\n", + " #print(\"Key in binary:\", k_bin)\n", + " #print(\"k in int\", k_int)\n", + " output = final_output ^ k_bin\n", + " else:\n", + " k = to_binary(k,16)\n", + " k = np.array([int(bit) for bit in k])\n", + " #print(\"k\", k)\n", + " output = final_output ^ k\n", + " #print(\"XORING output:\", output)\n", + " output = binary_array_to_integer(output)\n", + " final_outputs[i] = output\n", + " q +=1 \n", + " #print(\"Final output:\", final_outputs)\n", + " if (m < 2):\n", + " m +=2\n", + " else:\n", + " m = 0\n", + " \n", + " #print(\"_______________________________________________________________\")\n", + " return final_outputs" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [], + "source": [ + "#8#Key Generation Algorithm\n", + "def to_binary(value, bits):\n", + " return format(value, f'0{bits}b')\n", + "\n", + "def binary_array_to_integer(output):\n", + " int_output = ''.join(map(str, output))\n", + " return int(int_output, 2)\n", + "\n", + "def subsequent_key(x):\n", + " #x = [x]\n", + " if isinstance(x, int):\n", + " x = [x]\n", + " #print(\"sub key\", x)\n", + " input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n", + " for i, val in enumerate(x):\n", + " input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n", + " #print(\"input_part\", input_parts)\n", + " s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n", + " #print(\"S-box:\", s_box_outputs)\n", + " p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n", + " for i in range(len(x)):\n", + " p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n", + " #print(\"P-box:\", p_box_outputs)\n", + " bin_output = np.zeros(len(x), dtype=np.uint16)\n", + " final_output = np.zeros(len(x), dtype=np.uint16)\n", + " for i in range(len(x)):\n", + " bin_output = np.array(l_box(p_box_outputs[i][0]))\n", + " #print(bin_output)\n", + " #final_outputs[i] = final_output\n", + " output = binary_array_to_integer(bin_output)\n", + " #print(output)\n", + " final_output[i] = output\n", + " \n", + " #print(\"final_outputs:\", final_outputs)\n", + " return bin_output, final_output" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [], + "source": [ + "#9#Defining F-function for Left Side of Plaintext\n", + "def binary_array_to_integer(output):\n", + " int_output = ''.join(map(str, output))\n", + " return int(int_output, 2)\n", + "\n", + "def ff_function(x, key, d):\n", + " q=0\n", + " global o, counter, k_int1\n", + " if isinstance(x, int):\n", + " x = [x]\n", + " \n", + " input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n", + " for i, val in enumerate(x):\n", + " input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n", + " #print(\"FF_FUNCTION\")\n", + " #print(input_parts)\n", + " s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n", + " #print(\"S-box:\", s_box_outputs)\n", + " p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n", + " for i in range(len(x)):\n", + " p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n", + " #print(\"P-box:\", p_box_outputs)\n", + " final_outputs = np.zeros(len(x), dtype=np.uint32)\n", + " #print(len(x))\n", + " for i in range(len(x)):\n", + " #print(len(x))\n", + " final_output = np.array(l_box(p_box_outputs[i][0]))\n", + " k = key[q][o % 4]\n", + " #print(\"final_output:\", final_output)\n", + " #print(\"Key in int:\", k)\n", + " if (counter > 1):\n", + " k_bin, k_int1 = subsequent_key(k_int1)\n", + " #print(\"Key in binary:\", k_bin)\n", + " #print(\"k\", k_int)\n", + " output = final_output ^ k_bin\n", + " else:\n", + " k = to_binary(k,16)\n", + " k = np.array([int(bit) for bit in k])\n", + " #print(\"k\", k)\n", + " output = final_output ^ k\n", + " #print(\"XORING output:\", output)\n", + " output = binary_array_to_integer(output)\n", + " final_outputs[i] = output\n", + " q +=1 \n", + " counter += 1\n", + " #print(\"Final output:\", final_outputs)\n", + " if (o < 2):\n", + " o +=2\n", + " else:\n", + " o = 0\n", + " #print(\"_______________________________________________________________\")\n", + " return final_outputs" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [], + "source": [ + "#9#Convert the ciphertext pairs into Binary array\n", + "def convert_to_binary(row):\n", + " bin_array = np.zeros(32, dtype=np.uint8)\n", + " binary_str = format(row[0], '016b') + format(row[1], '016b')\n", + " for i, b in enumerate(binary_str):\n", + " bin_array[i] = int(b)\n", + " return bin_array" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [], + "source": [ + "#10#Encryption Function\n", + "def lcb_encrypt(plaintext, key, rounds, d):\n", + " \n", + " left_plaintext = np.uint16(plaintext[0])\n", + " right_plaintext = np.uint16(plaintext[1])\n", + " L, R = left_plaintext, right_plaintext\n", + "\n", + " n = 0\n", + " \n", + " while n < rounds:\n", + " L, R = f_function(R, key, d), ff_function(L, key, d)\n", + " n += 1\n", + " print(\"Encryption done per round\") \n", + " #print(rounds)\n", + " #print(n)\n", + " return (L, R)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [], + "source": [ + "#11#Function for generation of keys\n", + "import random\n", + "\n", + "def generate_hex_keys(num_keys, length=16):\n", + " hex_chars = \"0123456789ABCDEF\"\n", + " keys_str = [\"\".join(random.choices(hex_chars, k=length)) for _ in range(num_keys)]\n", + "\n", + " return keys_str\n", + "\n", + "def generate_round_keys(num_keys):\n", + " random_keys_hex = generate_hex_keys(num_keys)\n", + " #random_keys_hex = ['D63A529ECC92D353', '563A529ECC92D353', '163A529ECC92D353', 'D67AD296CC92DB53', '76BA569EDC9BD353']\n", + " #random_keys_hex = ['163A529D687529EC']\n", + " round_keys = []\n", + " \n", + " for random_key_hex in random_keys_hex:\n", + " random_key = int(random_key_hex, 16)\n", + "\n", + " K1 = (random_key >> 48) & 0xFFFF\n", + " K2 = (random_key >> 32) & 0xFFFF\n", + " K3 = (random_key >> 16) & 0xFFFF\n", + " K4 = random_key & 0xFFFF\n", + " \n", + " #k1_bin = to_binary(K1, 16)\n", + " #k2_bin = to_binary(K2, 16)\n", + " #k3_bin = to_binary(K3, 16)\n", + " #k4_bin = to_binary(K4, 16)\n", + "\n", + " #k1_np_array = np.array([int(bit) for bit in k1_bin])\n", + " #k2_np_array = np.array([int(bit) for bit in k2_bin])\n", + " #k3_np_array = np.array([int(bit) for bit in k3_bin])\n", + " #k4_np_array = np.array([int(bit) for bit in k4_bin])\n", + "\n", + " round_key = np.array([K1, K2, K3, K4])\n", + " round_keys.append(round_key)\n", + " round_key = np.array(round_keys)\n", + " #print(\"Key generation done:\", round_keys)\n", + " return round_key" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "metadata": {}, + "outputs": [], + "source": [ + "#12#Make dataset\n", + "\n", + "def make_train_data(n, nr, var):\n", + " global counter\n", + "\n", + " nonce = np.frombuffer(urandom(2), dtype=np.uint16);\n", + " cownter = np.arange(0,1023, dtype=np.uint16)\n", + " input = (nonce.astype(np.uint32) << 16) | cownter.astype(np.uint32)\n", + " plaintext = np.frombuffer(urandom(4*n), dtype=np.uint32);\n", + " #plaintext = [0xEED4B555]\n", + " #plaintext = [0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6]\n", + " plain0l = np.empty(n, dtype=np.uint16)\n", + " plain0r = np.empty(n, dtype=np.uint16)\n", + " plaintext0l = np.frombuffer(urandom(2*n), dtype=np.uint16)\n", + " plaintext0r = np.frombuffer(urandom(2*n), dtype=np.uint16)\n", + " \n", + " for i in range(n):\n", + " plain0l[i] = (input[i] >> 16) & 0xffff\n", + " plain0r[i] = input[i] & 0xffff\n", + " \n", + " round_keys = generate_round_keys(1)\n", + " \n", + " round_key = np.repeat(round_keys, 1024, axis=0)\n", + " \n", + " ctdata0l, ctdata0r = lcb_encrypt((plain0l, plain0r), round_key, nr, n)\n", + " ciphertext0l = ctdata0l ^ plaintext0l\n", + " print(ciphertext0l[0])\n", + " ciphertext0r = ctdata0r ^ plaintext0r\n", + " print(ciphertext0r[0])\n", + " ctdata = np.vstack((ciphertext0l, ciphertext0r)).T\n", + " \n", + " X = np.array([convert_to_binary(row) for row in ctdata])\n", + " print(X[0])\n", + " X = X.reshape(1, -1)\n", + " \n", + " filename = f\"output_{var}.txt\"\n", + "\n", + "# Open the file in write mode (overwrites existing file or creates a new one)\n", + " with open(filename, 'w') as file:\n", + " # Convert the array to a string without spaces\n", + " X_string = ''.join(X.astype(str)[0])\n", + "\n", + " # Write the string to the file\n", + " file.write(X_string)\n", + " \n", + " \"\"\"\n", + " with open(\"Dataset_NewP.csv\", \"w\", newline='') as f:\n", + " writer = csv.writer(f)\n", + " writer.writerow([\"plain0l\", \"plain0r\", \"plain1l\", \"plain1r\",\"Y\"])\n", + " for i in range(n):\n", + " writer.writerow([plain0l[i], plain0r[i], plain1l[i], plain1r[i],Y[i]])\n", + "\n", + " with open(\"Dataset_NewC.csv\", \"w\", newline='') as f:\n", + " writer = csv.writer(f)\n", + " writer.writerow([\"ctdata0l\", \"ctdata0r\", \"ctdata1l\", \"ctdata1r\",\"Y\"])\n", + " for i in range(n):\n", + " writer.writerow([ctdata0l[i], ctdata0r[i], ctdata1l[i], ctdata1r[i],Y[i]])\n", + " \"\"\"\n", + " return(X);" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Encryption done per round\n", + "25812\n", + "59196\n", + "[0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 0]\n", + "Encryption done per round\n", + "65099\n", + "11722\n", + "[1 1 1 1 1 1 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 1 1 0 0 1 0 1 0]\n", + "Encryption done per round\n", + "35646\n", + "42735\n", + "[1 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 1]\n", + "Encryption done per round\n", + "1385\n", + "17212\n", + "[0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0]\n", + "Encryption done per round\n", + "53116\n", + "9840\n", + "[1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0]\n", + "Encryption done per round\n", + "46666\n", + "41535\n", + "[1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1]\n", + "Encryption done per round\n", + "56968\n", + "29415\n", + "[1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 1]\n", + "Encryption done per round\n", + "19138\n", + "32283\n", + "[0 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 1]\n", + "Encryption done per round\n", + "29708\n", + "61356\n", + "[0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0]\n", + "Encryption done per round\n", + "60847\n", + "3956\n", + "[1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0]\n" + ] + } + ], + "source": [ + "for i in range (1000000):\n", + " make_train_data(1023,20,i)" + ] + }, + { + "cell_type": "code", + "execution_count": 136, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the number of files and bits per row\n", + "num_files = 1000000\n", + "bits_per_row = 32736\n", + "\n", + "# Initialize the merged array\n", + "merged_array = []\n", + "\n", + "# Iterate over the files and read the content\n", + "for i in range(num_files):\n", + " file_name = f\"output_{i}.txt\" # Adjust the file name pattern if needed\n", + " i +=1\n", + " with open(file_name, 'r') as file:\n", + " content = file.read()\n", + " merged_array.append(content)\n", + "\n", + "# Convert the merged array into a single string with newline characters\n", + "merged_string = '\\n'.join(merged_array)\n", + "\n", + "# Write the merged string to the output file\n", + "output_file = \"merged_output.txt\"\n", + "with open(output_file, 'w') as file:\n", + " file.write(merged_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}