Skip to content

Instantly share code, notes, and snippets.

@chakpongchung
Last active October 10, 2015 20:44
Show Gist options
  • Select an option

  • Save chakpongchung/88d880bb69b676ec8a07 to your computer and use it in GitHub Desktop.

Select an option

Save chakpongchung/88d880bb69b676ec8a07 to your computer and use it in GitHub Desktop.
Assignment 2 of Network Security ,taught by Dr.Hussein Abdel-Wahab
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This code implements the first round of IDEA.\n",
"This python implementation follows the code notation of \n",
"1. Dr.Wahab's lecture \n",
"and \n",
"2. http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf\n",
"\n",
"In order to avoid heavy computation of finite field operation and use table lookup and XOR mainly, we need the formula to geneate the mix-column table in p88 of \n",
"Network Security :private communication in a publc world\n",
"\n",
"To see the formula, look at the definition of \"mC\" below. I hope future students will like the formula"
]
},
{
"cell_type": "code",
"execution_count": 1224,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def column(matrix, i):\n",
" return [row[i] for row in matrix]"
]
},
{
"cell_type": "code",
"execution_count": 1225,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s = [[0 for x in range(4)] for x in range(4)]\n",
"keyarray = [[0 for x in range(4)] for x in range(4)]"
]
},
{
"cell_type": "code",
"execution_count": 1226,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n",
"[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n"
]
}
],
"source": [
"print s\n",
"print keyarray"
]
},
{
"cell_type": "code",
"execution_count": 1227,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['00', '00', '00', '00'], ['00', '00', '00', '00'], ['00', '00', '00', '00'], ['00', '00', '00', '00']]\n"
]
}
],
"source": [
"# Fall 2014, 1st round of AES-128 if\n",
"# The input data block is: FFFF FFFF FFFF FFFF 0000 0000 0000 0000\n",
"# The encryption key is: FFFF FFFF 0000 0000 0000 0000 FFFF FFFF\n",
"# input='FFFF FFFF FFFF FFFF 0000 0000 0000 0000'.replace(' ','')\n",
"# key='FFFF FFFF 0000 0000 0000 0000 FFFF FFFF'.replace(' ','')\n",
"\n",
"# Fall 2015, 1st round of AES-128 if\n",
"# The input data block is: FFFF FFFF FFFF FFFF 0000 0000 0000 0000\n",
"# The encryption key is: 0000 0000 0000 0000 0000 0000 0000 0000\n",
"input='FFFF FFFF FFFF FFFF 0000 0000 0000 0000'.replace(' ','')\n",
"key='0000 0000 0000 0000 0000 0000 0000 0000'.replace(' ','')\n",
"\n",
"for i in range(4):\n",
" for j in range(4):\n",
" keyarray[j][i] = key[8*i+2*j:8*i+2*j+2]\n",
"print keyarray"
]
},
{
"cell_type": "code",
"execution_count": 1228,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['FF', 'FF', '00', '00'], ['FF', 'FF', '00', '00'], ['FF', 'FF', '00', '00'], ['FF', 'FF', '00', '00']]\n"
]
}
],
"source": [
"for i in range(4):\n",
" for j in range(4):\n",
" s[j][i] = input[8*i+2*j:8*i+2*j+2]\n",
"print s \n"
]
},
{
"cell_type": "code",
"execution_count": 1229,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"63\n"
]
}
],
"source": [
"sBox=[['63','7c','77','7b','f2','6b','6f','c5','30','01','67','2b','fe','d7','ab','76'],\n",
"['ca','82','c9','7d','fa','59','47','f0','ad','d4','a2','af','9c','a4','72','c0'],\n",
"['b7','fd','93','26','36','3f','f7','cc','34','a5','e5','f1','71','d8','31','15'],\n",
"['04','c7','23','c3','18','96','05','9a','07','12','80','e2','eb','27','b2','75'],\n",
"['09','83','2c','1a','1b','6e','5a','a0','52','3b','d6','b3','29','e3','2f','84'],\n",
"['53','d1','00','ed','20','fc','b1','5b','6a','cb','be','39','4a','4c','58','cf'],\n",
"['d0','ef','aa','fb','43','4d','33','85','45','f9','02','7f','50','3c','9f','a8'],\n",
"['51','a3','40','8f','92','9d','38','f5','bc','b6','da','21','10','ff','f3','d2'],\n",
"['cd','0c','13','ec','5f','97','44','17','c4','a7','7e','3d','64','5d','19','73'],\n",
"['60','81','4f','dc','22','2a','90','88','46','ee','b8','14','de','5e','0b','db'],\n",
"['e0','32','3a','0a','49','06','24','5c','c2','d3','ac','62','91','95','e4','79'],\n",
"['e7','c8','37','6d','8d','d5','4e','a9','6c','56','f4','ea','65','7a','ae','08'],\n",
"['ba','78','25','2e','1c','a6','b4','c6','e8','dd','74','1f','4b','bd','8b','8a'],\n",
"['70','3e','b5','66','48','03','f6','0e','61','35','57','b9','86','c1','1d','9e'],\n",
"['e1','f8','98','11','69','d9','8e','94','9b','1e','87','e9','ce','55','28','df'],\n",
"['8c','a1','89','0d','bf','e6','42','68','41','99','2d','0f','b0','54','bb','16']]\n",
"\n",
"print sBox[0][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#AddRoundKey()"
]
},
{
"cell_type": "code",
"execution_count": 1230,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['ff', 'ff', '00', '00'], ['ff', 'ff', '00', '00'], ['ff', 'ff', '00', '00'], ['ff', 'ff', '00', '00']]\n"
]
}
],
"source": [
"\n",
"for i in range(4):\n",
" for j in range(4):\n",
" s[j][i] = int(s[j][i],16) ^ int(keyarray[j][i],16)\n",
" s[j][i]='{:02x}'.format(s[j][i])\n",
"print s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#SubBytes()"
]
},
{
"cell_type": "code",
"execution_count": 1231,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['16', '16', '63', '63'], ['16', '16', '63', '63'], ['16', '16', '63', '63'], ['16', '16', '63', '63']]\n"
]
}
],
"source": [
"for i in range(4):\n",
" for j in range(4):\n",
" s[j][i] =sBox[ int(s[j][i][0], 16) ][ int(s[j][i][1], 16) ]\n",
" \n",
"print s"
]
},
{
"cell_type": "code",
"execution_count": 1232,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['16', '16', '63', '63']\n"
]
}
],
"source": [
"s[1]=s[1][1:]+s[1][:1]\n",
"s[2]=s[2][2:]+s[2][:2]\n",
"s[3]=s[3][3:]+s[3][:3]\n",
"print column(s,0)\n",
"# int(''.join(s[1]),16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" #how to genearate mixColumn table\n",
" \n",
" shiny point of this implementation.\n",
" Please look at the formula below\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1233,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[225, 253, 253, 28]\n",
"[231, 254, 254, 25]\n",
"[229, 255, 255, 26]\n",
"[1, 141, 141, 140]\n",
"[198, 99, 99, 165]\n",
"\n"
]
}
],
"source": [
"# \n",
"# genearate mixColumn table, shiny point of this code\n",
"# look at the formula below\n",
"# \n",
"mC = [[ [] for x in range(16)] for x in range(16)]\n",
"\n",
"for i in range(16):\n",
" for j in range(16):\n",
" mC[i][j].append( (i*16+j)*2 )\n",
" if mC[i][j][0]>=256:\n",
" mC[i][j][0]=(mC[i][j][0]^int('1b',16))^int('100',16)\n",
" mC[i][j].append(i*16+j)\n",
" mC[i][j].append(i*16+j)\n",
" mC[i][j].append( (mC[i][j][0]^mC[i][j][1]) )\n",
"# testing the mC\n",
"print mC[15][13]\n",
"print mC[15][14]\n",
"print mC[15][15]\n",
"print mC[8][13]\n",
"print mC[6][3]\n",
"print \n",
"# print mC"
]
},
{
"cell_type": "code",
"execution_count": 1234,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# for comprison with fig 3-25 in the book\n",
"# xorTable=[]\n",
"# a=['2b','d4','de','ad']\n",
"# for i in range(4):\n",
"# r=int(a[i][0],16)\n",
"# c=int(a[i][1],16)\n",
"# # [int(x, 16) for x in L]\n",
"# # xorTable.append([hex(x) for x in mC[r][c]])\n",
"# xorTable.append( mC[r][c] )\n",
"# print xorTable"
]
},
{
"cell_type": "code",
"execution_count": 1235,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# order=[0,3,2,1]\n",
"# #for comprison with fig 3-25 in the book\n",
"# colPrime=[]\n",
"# # print hex(xorTable[0][order[0]]^xorTable[1][order[1]]^xorTable[2][order[2]]^xorTable[3][order[3]])\n",
"# colPrime.append(hex(xorTable[0][order[0]]\n",
"# ^xorTable[1][order[1]]\n",
"# ^xorTable[2][order[2]]\n",
"# ^xorTable[3][order[3]]))\n",
"# order=order[3:]+order[:3] \n",
"# print colPrime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#ShiftRows() and MixColumns()\n"
]
},
{
"cell_type": "code",
"execution_count": 1236,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['16', '16', '63', '63']\n",
"['16', '63', '63', '16']\n",
"['63', '63', '16', '16']\n",
"['63', '16', '16', '63']\n"
]
}
],
"source": [
"# resultT is a list of list, where the first list is the first column of the result(stateArray),\n",
"# so resultT is transpose(result), or result is transpose(resultT)\n",
"resultT=[]\n",
"for j in range(4):\n",
" xorTable=[]\n",
" a=column(s,j)\n",
" print a\n",
"# prepare the xorTable to be XORed for the columnPrime result\n",
" for i in range(4):\n",
" r=int(a[i][0],16)\n",
" c=int(a[i][1],16)\n",
" item=mC[r][c]\n",
" item=[int(hex(x)[2:],16 )for x in item]\n",
" xorTable.append( item )\n",
"# get the columnPrime using the XOR order, order will be changed each time\n",
"# the final order permutation is useless in this case\n",
" order=[0,3,2,1]\n",
" colPrime=[]\n",
" for r in range(4):\n",
" colPrime.append(hex(xorTable[0][order[0]]\n",
" ^xorTable[1][order[1]]\n",
" ^xorTable[2][order[2]]\n",
" ^xorTable[3][order[3]]))\n",
"# ShiftRows()\n",
" order=order[3:]+order[:3]\n",
" resultT.append(colPrime) \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Final answer given the input for the first round of AES-128"
]
},
{
"cell_type": "code",
"execution_count": 1237,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"the final answer given the input \n",
"FFFFFFFFFFFFFFFF0000000000000000\n",
"and key \n",
"00000000000000000000000000000000\n",
" is: \n",
"[['0x16', '0xfc', '0x63', '0x89'], ['0xfc', '0x63', '0x89', '0x16'], ['0x63', '0x89', '0x16', '0xfc'], ['0x89', '0x16', '0xfc', '0x63']]\n"
]
}
],
"source": [
"# column-wise result, in which each row in resultT is result in column\n",
"# since the state array is ordered column by column, so it can be read row by row in order in resultT\n",
"print\n",
"print 'the final answer given the input '\n",
"print input\n",
"print 'and key '\n",
"print key\n",
"print ' is: '\n",
"print resultT"
]
},
{
"cell_type": "code",
"execution_count": 1238,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# for simplicity, we can also do it column by column to get the final answer here \n",
"\n",
"\n",
"# order=[0,3,2,1]\n",
"# colPrime=[]\n",
"# for r in range(4):\n",
"# colPrime.append(hex(xorTable[0][order[0]]\n",
"# ^xorTable[1][order[1]]\n",
"# ^xorTable[2][order[2]]\n",
"# ^xorTable[3][order[3]]))\n",
"# order=order[3:]+order[:3]\n",
"\n",
"# colPrime.append(hex(xorTable[0][order[0]]\n",
"# ^xorTable[1][order[1]]\n",
"# ^xorTable[2][order[2]]\n",
"# ^xorTable[3][order[3]]))\n",
"# order=order[3:]+order[:3]\n",
"\n",
"# colPrime.append(hex(xorTable[0][order[0]]\n",
"# ^xorTable[1][order[1]]\n",
"# ^xorTable[2][order[2]]\n",
"# ^xorTable[3][order[3]]))\n",
"# order=order[3:]+order[:3]\n",
"\n",
"# colPrime.append(hex(xorTable[0][order[0]]\n",
"# ^xorTable[1][order[1]]\n",
"# ^xorTable[2][order[2]]\n",
"# ^xorTable[3][order[3]]))\n",
"# order=order[3:]+order[:3]\n",
"# print colPrime"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment