Skip to content

Instantly share code, notes, and snippets.

@chakpongchung
Created October 8, 2015 20:22
Show Gist options
  • Save chakpongchung/fb020e39d8a93519dec9 to your computer and use it in GitHub Desktop.
Save chakpongchung/fb020e39d8a93519dec9 to your computer and use it in GitHub Desktop.
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": "code",
"execution_count": 1064,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def column(matrix, i):\n",
" return [row[i] for row in matrix]"
]
},
{
"cell_type": "code",
"execution_count": 1065,
"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": 1066,
"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": 1067,
"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": 1068,
"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": 1069,
"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": "code",
"execution_count": 1070,
"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": [
"#add round key\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": "code",
"execution_count": 1071,
"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": 1072,
"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": "code",
"execution_count": 1073,
"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"
]
}
],
"source": [
"# genearate mixColumn table\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",
"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",
"\n",
"# print mC"
]
},
{
"cell_type": "code",
"execution_count": 1074,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# for comprison with fig 3-25\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": 1075,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# order=[0,3,2,1]\n",
"# #for comprison with fig 3-25\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": "code",
"execution_count": 1076,
"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",
"[['0x16', '0xfc', '0x63', '0x89'], ['0xfc', '0x63', '0x89', '0x16'], ['0x63', '0x89', '0x16', '0xfc'], ['0x89', '0x16', '0xfc', '0x63']]\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",
" order=order[3:]+order[:3]\n",
" resultT.append(colPrime) \n",
" \n",
"print resultT"
]
},
{
"cell_type": "code",
"execution_count": 1077,
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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