Add DPA Attack task files

This commit is contained in:
Manuel Thalmann 2023-12-19 19:19:01 +01:00
parent e5c5959908
commit 25291cbb54
8 changed files with 1232 additions and 0 deletions

View file

@ -27,6 +27,10 @@
"name": "AES Intrinsic", "name": "AES Intrinsic",
"path": "./aes-performance/aes-intrinsic" "path": "./aes-performance/aes-intrinsic"
}, },
{
"name": "DPA Attack",
"path": "./dpa-attack"
},
{ {
"name": "TRNG Attack", "name": "TRNG Attack",
"path": "./TRNG_attack" "path": "./TRNG_attack"

101
dpa-attack/ciphertext.txt Normal file
View file

@ -0,0 +1,101 @@
08 2b ca 4a d7 57 b2 e7 7c 6f aa 14 79 41 7f d0
be 21 f5 fc 74 8f fc f4 1b 46 0a 07 e7 b6 76 85
57 9c 34 de ad 38 97 4d 9c d2 e1 6a 83 c1 f5 3c
e9 68 d8 87 87 6d bc 9c ba ad ea b4 46 e2 89 5a
00 96 73 2c 73 50 0f 50 b5 40 0b bc 69 d6 da 96
7b 52 70 71 e8 44 9f 43 ba 8b 5d dd e8 b2 8e f1
22 9d 66 f9 b8 40 d3 0c 8f 2b 82 ed f4 02 a2 bd
52 fc 6f 22 56 5a fb fc 02 58 5d 6c 0c be 6c e8
e7 41 8e 32 54 24 c3 db 78 1a 81 1f c6 ab 36 47
fa 78 07 9b c0 e9 68 6e 30 0f d4 04 65 50 2f 79
18 eb f4 85 ad 3d 39 f3 78 8c e0 8a 0a 66 20 ab
d2 36 bb ae 17 55 e3 2c 58 86 09 4f 18 1c d5 0f
7f 84 fd f2 ae 33 3b 94 95 10 36 6c 31 c5 fe f8
9e 52 39 1b 37 28 09 75 d0 9e 4f 0b 2b 15 61 ce
5a 9a f2 78 59 0e 27 9c 1d 9b 9d ef ff 93 74 f5
38 43 54 6f 4c fc 4c d7 63 d9 e8 55 5d 83 d8 e9
24 26 45 e0 13 4d d0 68 75 f4 e7 09 b3 80 03 d0
e4 65 5b 4e 00 43 3f 04 d5 19 aa f9 a8 46 95 eb
6a 4d 48 55 84 f8 a2 fe 97 68 42 34 06 58 0a d7
b4 db 7c 6b 8b 35 b9 67 9b fe 92 54 56 51 57 f7
23 96 e6 3e 7b da d5 19 7c 11 7f 1d 83 64 b7 1d
53 a1 3c b3 85 25 f0 d8 6a 4a 48 6f 01 fb 90 25
7a 21 3f b4 40 f4 94 67 55 75 89 a2 bb 8c 60 39
a4 59 de 4e a7 db ec ce 69 c3 f5 b1 77 c4 e0 97
b5 0f 93 a2 13 1f 37 66 c8 0a c4 2d 57 ee 74 a6
e4 36 d6 cd ae ee b6 e7 70 5a a3 26 3c 65 4d 95
51 ba bc 16 e3 24 bb e0 33 b0 a5 fa 23 66 2f 91
16 37 93 3d c5 09 f7 15 65 eb a8 28 d5 f2 ac 4d
d2 35 15 41 de 26 95 41 ca 1f 44 de a2 09 d9 4a
62 76 cd b6 c5 e7 05 0e b3 5b b6 85 30 52 1c be
1b 2d a7 af 75 fc 0a a9 c8 0c 9d 05 15 9f 68 30
5d bc f4 e5 a5 ba 2e 0b 4b 67 32 e8 48 73 55 f9
a4 1f e2 58 41 4b c7 0d 27 1a cc 9a 40 85 8d ad
c6 60 01 9a e0 5f 44 c0 86 a2 98 77 f4 0c 47 7c
5a c4 12 2b 83 b4 d6 18 62 25 c1 01 ae 75 6a 5a
f6 4e fd 0d 9d 6b cc 19 26 dc 04 e1 2d 2e e7 75
19 e2 9e 4d 33 a8 d8 79 14 59 b0 56 03 84 86 0b
8d 5e 84 d8 c6 70 1c c9 9e 98 4c 2d 5c 5d 82 8b
0d 00 67 9a a2 07 a7 e8 7d 35 d3 1d 7c 05 a8 f0
c4 10 3a b0 1e fe e7 02 52 6f e8 20 6e a0 a8 dc
d1 39 5b 05 15 d9 e0 e7 a0 6d 0a 45 44 58 89 d6
7b 65 46 11 51 98 ca 37 a4 78 67 42 88 d1 18 8c
c8 56 a5 37 7c 87 12 3a 9c 0b c7 33 bc 39 43 db
46 c5 91 c9 bc 2e 1f 8f 69 b7 5e ff c8 d8 50 b4
b4 c9 4a c1 df f2 c3 e5 26 1b 75 30 f7 58 2a aa
8e a3 c3 46 c8 9f c2 4b 9b ca 7d 59 22 b1 05 42
5d bc d2 13 04 a4 1a 57 8d ac 79 e5 5e b0 80 65
9d de 40 af 72 1d d6 d3 bd 14 69 58 13 0c 70 a4
58 1d 26 71 b0 ad 08 6e 95 66 06 ab 0d b7 96 ad
dd 02 a7 b4 d4 90 8a 54 a5 62 70 56 fc 63 b5 20
a4 c4 64 fc 68 dc e2 9e d1 86 09 ef 17 2a 5c 9c
e5 2f ae d2 ea a2 d4 ad 6b cb 04 39 e2 7e 1a 0e
0a 1c 10 a9 2c 88 a1 10 e0 85 db 67 4c 69 e8 e2
95 70 e8 92 69 f7 b4 d0 f6 69 cb 66 90 61 e1 a8
be a7 36 72 e8 52 fc 3a 2d b5 9e 15 76 0e f3 36
b6 e5 c0 7f 7d 74 84 28 26 dc 21 4a d3 a5 a1 8f
c5 65 ce dc 3f dd 71 50 4e e5 36 9a 60 2d 64 50
6a ab 6a 42 67 a9 06 a2 cc b0 1b cb c1 be 49 51
09 9c 1b d6 2e c9 24 ac 0e a3 cc 5f 91 96 4e cb
18 94 95 c3 3c f2 a1 c5 25 79 4e 69 c5 8f 66 38
ae 9b 87 e6 53 46 10 ee 9f 27 85 0e e5 c7 ab 51
53 90 41 65 7e ba 29 18 2f 8a e4 b1 04 88 ac 39
1e e8 36 b9 4e 4f 2b a7 4b 59 ca eb 45 85 00 06
98 33 ae 78 0d 69 6d c3 07 11 45 b2 d2 1c 15 ce
9b 26 65 77 19 5d fb 21 ac 64 10 53 85 40 32 37
f2 c3 d0 af bf 91 6f 01 87 38 17 58 0e 03 d2 ca
34 b2 b2 43 30 75 76 9e 50 92 b9 9d ed af 11 7f
21 d1 36 0b 17 74 4f cf f7 9c bf 03 8b cd ea 2b
13 7d e7 6a f8 a0 79 36 58 ce 89 2b d5 31 14 48
2c de 78 bd bc 31 b0 74 3a 47 96 c1 06 6c 5f f5
0f bf 35 71 c1 aa 9d b9 a8 67 42 7e 9a 68 44 80
0f fa e9 df f7 37 5e 90 be 2d cf 6a 7f 7c 81 7a
3d 7b d0 30 43 9c 51 3d 26 9b d0 3f c9 06 23 37
a8 5e c4 35 65 2e 35 08 64 c7 31 ec 8c 18 dd 84
e0 ed 56 65 70 27 b9 6d 46 e9 3d 1d cf 0e bf 78
9e 62 c4 24 b4 45 af 55 bd 65 1e 9c f7 f4 3c 79
04 ea 10 d0 33 dc 9b 1f b9 35 49 6b b0 3e 21 af
da 9e 7b 0b 46 04 7a 90 8e 50 53 86 44 f3 ab 25
eb b0 fa 5b eb a3 28 1b 64 40 99 22 89 b2 9f 60
cc 03 b2 b3 e5 37 71 06 05 f6 94 0e 56 9b c5 85
1b 2b 18 49 69 6e a8 ca b0 50 df c4 6c e7 9b 51
4a f2 81 a6 8e cf e7 e8 67 95 c6 f0 f8 80 82 b9
4c b0 3b 6b 2c 1d a6 5b a6 90 ce dd 77 f7 52 3c
ab 44 cd b1 66 b0 9c fb 30 0c 64 5f 63 47 ee d5
35 aa a5 77 11 a0 e9 db a0 04 a5 33 85 56 d6 a5
c4 98 fe 7e 6c 1c cc 05 47 2b 46 78 38 09 b4 a5
f3 a0 ee 9c ec d2 05 6c a5 94 4e b3 b7 e7 1d ed
72 62 1c 21 76 34 11 9a 42 01 ef 94 c4 7b 36 33
a8 8e 36 cd 74 38 8b e1 4f 65 62 07 bf 6d 49 54
43 a4 98 a4 45 9e 56 93 bc 6f f2 51 be a0 d1 16
61 f4 bc 5c 00 48 31 c9 25 df 4c 49 8b 70 5e c7
68 f9 3b 0d 56 d8 cc c8 94 15 f3 17 08 e0 f7 af
eb 4a dd 8c 8e 26 21 08 ef 41 6a a0 2c e0 9c ce
3c c3 c5 9e bd c7 4e 9f 5c 71 80 17 25 dd 44 ca
2d 43 70 69 e3 d5 d9 d6 7f ef b4 61 81 f2 ce c2
20 e7 b5 02 2d 7c 42 83 1b 0e 97 30 c8 09 f6 e6
07 76 7b f9 68 66 70 c5 3e a0 96 88 c3 c1 14 05
9b 65 52 73 af c4 6d 6f 5b 36 87 29 aa e0 67 b5
7e d1 d6 b2 d1 27 0c 34 ee 75 d0 b7 77 fa ac c8
11 c7 11 88 24 63 3a b1 15 3f 6d 9f af 84 40 70

BIN
dpa-attack/dpa-aes-v.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

View file

@ -0,0 +1,664 @@
(* Content-type: application/mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 7.0' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 145, 7]
NotebookDataLength[ 25847, 655]
NotebookOptionsPosition[ 24625, 617]
NotebookOutlinePosition[ 25059, 635]
CellTagsIndexPosition[ 25016, 632]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
RowBox[{"SetDirectory", "[",
RowBox[{"NotebookDirectory", "[", "]"}], "]"}]], "Input"],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"read", " ", "plaintext", " ", "inputs"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"inputs", "=",
RowBox[{"StringSplit", "[",
RowBox[{"ReadList", "[",
RowBox[{"\"\<plaintext.txt\>\"", ",", "String"}], "]"}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"is", "=",
RowBox[{
RowBox[{
RowBox[{"StringTrim", "[",
RowBox[{"#", ",", "\"\<0x\>\""}], "]"}], "&"}], "/@", "inputs"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"inputs", "=",
RowBox[{"Map", "[",
RowBox[{
RowBox[{
RowBox[{"FromDigits", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}], ",", "is", ",",
RowBox[{"{", "2", "}"}]}], "]"}]}], ";"}]}]}]], "Input",
CellChangeTimes->{{3.513065164153782*^9, 3.513065164701598*^9}, {
3.51306956390343*^9, 3.513069593104686*^9}, {3.5130699021288524`*^9,
3.5130699027752953`*^9}, {3.513072041709684*^9, 3.5130720429273796`*^9}, {
3.6047750649215713`*^9, 3.6047750684215765`*^9}, {3.6047751308016634`*^9,
3.6047751699517183`*^9}, {3.604775205431768*^9, 3.604775209171773*^9}, {
3.6047753763720074`*^9, 3.604775378742011*^9}, {3.604775445462104*^9,
3.6047755065721893`*^9}, {3.60477563539237*^9, 3.604775664782411*^9}, {
3.604775717942486*^9, 3.6047757458425245`*^9}, {3.604775813222619*^9,
3.6047760178839054`*^9}, 3.604776217714185*^9, {3.611654553014601*^9,
3.611654555544605*^9}, 3.6576111564330072`*^9, {3.7204347847718115`*^9,
3.7204347932733574`*^9}}],
Cell[BoxData[{
RowBox[{"Length", "[", "inputs", "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"inputs", "[",
RowBox[{"[",
RowBox[{"1", ",", "All"}], "]"}], "]"}], "//",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}], " ",
RowBox[{"(*",
RowBox[{"debug", ":", " ",
RowBox[{"print", " ", "the", " ", "first", " ", "block"}]}],
"*)"}]}]}], "Input",
CellChangeTimes->{{3.60477614246408*^9, 3.604776148824089*^9}, {
3.7204352933027515`*^9, 3.720435305065805*^9}}],
Cell[BoxData[
RowBox[{"recLen", "=",
RowBox[{
RowBox[{"Import", "[",
RowBox[{"\"\<traceLength.txt\>\"", ",", "\"\<CSV\>\""}], "]"}], "[",
RowBox[{"[",
RowBox[{"1", ",", "1"}], "]"}], "]"}],
RowBox[{"(*",
RowBox[{
"read", " ", "length", " ", "of", " ", "one", " ", "complete", " ",
"trace"}], "*)"}]}]], "Input",
CellChangeTimes->{{3.6047725781640882`*^9, 3.6047726245241537`*^9}, {
3.6047728509444704`*^9, 3.6047728549544764`*^9}, {3.6047728903545256`*^9,
3.604772892964529*^9}, {3.60477367922663*^9, 3.6047736850566387`*^9}, {
3.6047764424945*^9, 3.6047764426545*^9}, {3.611654395614381*^9,
3.6116544204244156`*^9}, {3.6116544546444635`*^9,
3.6116545073645372`*^9}, {3.611656982271825*^9, 3.6116569830418262`*^9},
3.657611148657194*^9, 3.6576112471096745`*^9, {3.7204334339389772`*^9,
3.720433436988076*^9}, {3.720433614531517*^9, 3.7204336175115733`*^9},
3.720434584600828*^9, {3.7204348191751294`*^9, 3.7204348743819485`*^9}, {
3.7204349530964828`*^9, 3.7204349543014317`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*", " ",
RowBox[{
RowBox[{
"prepare", " ", "to", " ", "read", " ", "parts", " ", "of", " ", "traces",
"\[IndentingNewLine]", "start"}], " ", "=", " ",
RowBox[{
RowBox[{
"number", " ", "of", " ", "samples", " ", "to", " ", "skip", " ", "at",
" ", "the", " ", "start", " ", "of", " ", "each", " ", "trace",
"\[IndentingNewLine]", "len"}], " ", "=", " ",
RowBox[{
RowBox[{
"number", " ", "of", " ", "samples", " ", "to", " ", "keep", " ",
"from", " ", "each", " ", "trace", "\[IndentingNewLine]", "count"}],
" ", "=", " ",
RowBox[{
"number", " ", "of", " ", "traces", " ", "to", " ", "read"}]}]}]}], " ",
"*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"start", "=", "0"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"len", "=", "recLen"}], ";"}],
RowBox[{"(*",
RowBox[{
"CHANGE", " ", "to", " ", "the", " ", "length", " ", "of", " ", "the",
" ", "first", " ", "round"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"count", "=", "10"}], ";",
RowBox[{"(*",
RowBox[{
RowBox[{"CHANGE", " ", "to", " ",
RowBox[{"Length", "[", "inputs", "]"}]}], ";", " ",
RowBox[{"for", " ", "the", " ", "attack"}]}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"type", "=", "\"\<Byte\>\""}]}]}]}]], "Input",
CellChangeTimes->{{3.6047725781640882`*^9, 3.6047726245241537`*^9}, {
3.6047728509444704`*^9, 3.6047728549544764`*^9}, {3.6047728903545256`*^9,
3.604772892964529*^9}, {3.60477367922663*^9, 3.6047736850566387`*^9}, {
3.6047764424945*^9, 3.6047764426545*^9}, {3.611654395614381*^9,
3.6116544204244156`*^9}, {3.6116544546444635`*^9,
3.6116545073645372`*^9}, {3.611656982271825*^9, 3.6116569830418262`*^9},
3.657611148657194*^9, 3.6576112471096745`*^9, {3.7204334339389772`*^9,
3.720433436988076*^9}, {3.720433614531517*^9, 3.7204336175115733`*^9},
3.720434584600828*^9, {3.7204348191751294`*^9, 3.7204350410331383`*^9}, {
3.720435726616678*^9, 3.720435735879939*^9}, {3.7204393408982005`*^9,
3.720439433030507*^9}, 3.72043946895192*^9}],
Cell[BoxData[{
RowBox[{
RowBox[{
RowBox[{"t", "=",
RowBox[{"ConstantArray", "[",
RowBox[{"0.", ",",
RowBox[{"{",
RowBox[{"count", ",", "len"}], "}"}]}], "]"}]}], ";"}], " ",
RowBox[{"(*",
RowBox[{"prepare", " ", "empty", " ", "matrix"}], "*)"}]}], "\n",
RowBox[{
RowBox[{"f", "=",
RowBox[{"OpenRead", "[",
RowBox[{"\"\<traces.bin\>\"", ",",
RowBox[{"BinaryFormat", "\[Rule]", "True"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"For", "[",
RowBox[{
RowBox[{"i", "=", "1"}], ",",
RowBox[{"i", "\[LessEqual]", "count"}], ",",
RowBox[{"i", "++"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"BinaryReadList", "[",
RowBox[{"f", ",", "type", ",", "start"}], "]"}], ";",
RowBox[{"(*", "skip", "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"t", "[",
RowBox[{"[", "i", "]"}], "]"}], "=",
RowBox[{"N", "[",
RowBox[{"BinaryReadList", "[",
RowBox[{"f", ",", "type", ",", "len"}], "]"}], "]"}]}], ";",
RowBox[{"(*",
RowBox[{"read", " ", "interesting", " ", "part"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"BinaryReadList", "[",
RowBox[{"f", ",", "type", ",",
RowBox[{"recLen", "-",
RowBox[{"(",
RowBox[{"start", "+", "len"}], ")"}]}]}], "]"}], ";"}]}],
RowBox[{"(*", "skip", "*)"}], "\[IndentingNewLine]", "]"}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"Close", "[", "f", "]"}], ";"}]}], "Input",
CellChangeTimes->{{3.6047736543465953`*^9, 3.6047737432467194`*^9}, {
3.604773840806856*^9, 3.6047738437368603`*^9}, 3.6047763663443937`*^9, {
3.7204350706630774`*^9, 3.7204351113945007`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"Dimensions", "[", "t", "]"}],
RowBox[{"(*",
RowBox[{"dimensions", " ", "of", " ", "matrix", " ", "of", " ", "traces"}],
"*)"}]}]], "Input",
CellChangeTimes->{{3.657611200974016*^9, 3.657611203442487*^9}, {
3.720435139439351*^9, 3.7204351499092684`*^9}}],
Cell[BoxData[{
RowBox[{
RowBox[{
RowBox[{"mt", "=",
RowBox[{"Mean", "[",
RowBox[{"Transpose", "[", "t", "]"}], "]"}]}], ";"}], " ",
RowBox[{"(*",
RowBox[{"remove", " ", "mean", " ", "of", " ", "each", " ", "trace", " ",
RowBox[{"(", "detrend", ")"}]}], "*)"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"t", "=",
RowBox[{"t", "-", "mt"}]}], ";"}]}], "Input",
CellChangeTimes->{{3.513064701337701*^9, 3.5130647223900642`*^9}, {
3.5130691259617333`*^9, 3.5130691476566343`*^9}, {3.697802894461648*^9,
3.6978029010809946`*^9}, {3.720435204500185*^9, 3.720435232348072*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"ListLinePlot", "[",
RowBox[{
RowBox[{"t", "[",
RowBox[{"[", "1", "]"}], "]"}], ",",
RowBox[{"PlotRange", "\[Rule]", "All"}]}], "]"}],
RowBox[{"(*",
RowBox[{"plot", " ", "the", " ", "1", "st", " ", "trace"}],
"*)"}]}]], "Input",
CellChangeTimes->{{3.513059525407263*^9, 3.5130595320064497`*^9}, {
3.5130597927680807`*^9, 3.5130597934028053`*^9}, {3.5130607553441668`*^9,
3.513060770237745*^9}, {3.513060805087077*^9, 3.5130608247957764`*^9}, {
3.513064529241294*^9, 3.5130645554105177`*^9}, {3.5130646262800045`*^9,
3.5130646414577446`*^9}, {3.513064908861528*^9, 3.5130649090265565`*^9},
3.5130690322353106`*^9, {3.611656491884932*^9, 3.6116564951749363`*^9}, {
3.720435152775014*^9, 3.720435178730044*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"ListLinePlot", "[",
RowBox[{"t", "[",
RowBox[{"[",
RowBox[{"All", ",",
RowBox[{"1", ";;", "100"}]}], "]"}], "]"}], "]"}],
RowBox[{"(*",
RowBox[{"plot", " ", "zoomed", " ", "overlapping", " ", "trace"}],
"*)"}]}]], "Input",
CellChangeTimes->{
3.5130651127205505`*^9, 3.5130690498250046`*^9, 3.5130691784642324`*^9, {
3.7204352499893413`*^9, 3.7204352709064436`*^9}}],
Cell[BoxData[
RowBox[{"(*", " ",
RowBox[{
RowBox[{"STOP", ":", " ",
RowBox[{
"Select", " ", "the", " ", "interesting", " ", "part", " ", "of", " ",
"traces"}]}], ",", " ",
RowBox[{
RowBox[{"change", " ", "start"}], "="}], ",", " ",
RowBox[{"len", "="}], ",", " ",
RowBox[{"count", "=", " ",
RowBox[{"...", " ",
RowBox[{"above", "."}]}]}]}], " ", "*)"}]], "Input",
CellChangeTimes->{{3.720439262833274*^9, 3.7204392959636936`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
RowBox[{"**", "**", "**", "**", "**"}], "*", " ", "Define", " ", "useful",
" ", "functions"}], " ", "***********)"}], "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{"setup", " ", "AES", " ", "SBOX"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
StyleBox[
RowBox[{
RowBox[{
"ss", "=",
"\"\<0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, \
0x2B, 0xFE, 0xD7, 0xAB, 0x76,\n 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, \
0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,\n 0xB7, 0xFD, 0x93, \
0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,\
\n 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, \
0xEB, 0x27, 0xB2, 0x75,\n 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, \
0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,\n 0x53, 0xD1, 0x00, 0xED, \
0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,\n \
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, \
0x3C, 0x9F, 0xA8,\n 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, \
0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,\n 0xCD, 0x0C, 0x13, 0xEC, 0x5F, \
0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,\n 0x60, \
0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, \
0x0B, 0xDB,\n 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, \
0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,\n 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, \
0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,\n 0xBA, 0x78, \
0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, \
0x8A,\n 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, \
0xB9, 0x86, 0xC1, 0x1D, 0x9E,\n 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, \
0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,\n 0x8C, 0xA1, 0x89, \
0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, \
0x16\>\""}], ";"}],
FontSize->9], "\[IndentingNewLine]",
RowBox[{
RowBox[{"sss", "=",
RowBox[{"StringTrim", "[",
RowBox[{
RowBox[{"StringTrim", "[",
RowBox[{"StringSplit", "[",
RowBox[{"ss", ",", "\"\<,\>\""}], "]"}], "]"}], ",", "\"\<0x\>\""}],
"]"}]}], ";",
RowBox[{"(*",
RowBox[{"convert", " ", "to", " ", "hex", " ", "strings"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"SubBytes", "=",
RowBox[{"Map", "[",
RowBox[{
RowBox[{
RowBox[{"FromDigits", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}], ",", "sss"}], "]"}]}], ";",
RowBox[{"(*",
RowBox[{"convert", " ", "to", " ", "integers"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"ClearAll", "[", "SBOX", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"SBOX", "[", "a_", "]"}], ":=",
RowBox[{"SubBytes", "[",
RowBox[{"[",
RowBox[{"a", "+", "1"}], "]"}], "]"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"SBOX", "[", "0", "]"}], "\[Equal]", "16^^63",
RowBox[{"(*",
RowBox[{"test", " ", "SBOX",
RowBox[{"(", "0", ")"}]}], "*)"}]}]}]}]], "Input",
CellChangeTimes->{{3.611654569214624*^9, 3.6116546224046984`*^9},
3.720435287858545*^9, {3.7204391212751875`*^9, 3.72043914322598*^9}}],
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "HammingWeight", "]"}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"HammingWeight", "[", "x_", "]"}], ":=",
RowBox[{"DigitCount", "[",
RowBox[{"x", ",", "2", ",", "1"}], "]"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{"HammingWeight", "[", "16^^00", "]"}], "\[IndentingNewLine]",
RowBox[{"HammingWeight", "[", "16^^FF", "]"}], "\[IndentingNewLine]",
RowBox[{"HammingWeight", "[",
RowBox[{"{",
RowBox[{"16^^A3", ",", "16^^00", ",", "16^^21"}], "}"}], "]"}]}], "Input",
CellChangeTimes->{{3.6116554925799284`*^9, 3.611655515599961*^9},
3.6116556132500973`*^9, {3.611655674950184*^9, 3.6116556795801907`*^9}, {
3.6116557152702403`*^9, 3.6116557190102453`*^9}, {3.61165586224049*^9,
3.6116558682004986`*^9}, 3.611656188129399*^9, {3.720435635640376*^9,
3.7204356859751062`*^9}, {3.7204359822956524`*^9,
3.7204360272497416`*^9}, {3.720436612207198*^9, 3.7204366371002493`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
RowBox[{
"**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**",
"**"}], "*", " ", "Attack", " ", "the", " ", "first", " ", "key", " ",
RowBox[{
"byte", " ", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**",
"**", "**"}]}], "*****)"}], "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
"You", " ", "need", " ", "to", " ", "have", " ", "selected", " ", "the",
" ", "interesting", " ", "parts", " ", "of", " ", "measured", " ",
"traces", " ", "for", " ", "correlation"}], " ", "*)"}]}]], "Input",
CellChangeTimes->{{3.7204391531928515`*^9, 3.720439218197921*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"Select", " ", "the", " ", "first", " ", "byte", " ", "of", " ", "each",
" ", "input"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"d", "=",
RowBox[{"inputs", "[",
RowBox[{"[",
RowBox[{
RowBox[{"1", ";;", "count"}], ",", "1"}], "]"}], "]"}]}], ";"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"d", "[",
RowBox[{"[",
RowBox[{"1", ";;", "20"}], "]"}], "]"}], "//",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}]}]}]}]], "Input",
CellChangeTimes->{{3.720436066249104*^9, 3.720436073200232*^9}, {
3.7204361291892776`*^9, 3.720436140346721*^9}, {3.720437898533883*^9,
3.7204379207177186`*^9}, {3.7204379911764402`*^9, 3.7204380042089243`*^9}, {
3.7204380452344737`*^9, 3.7204380454299173`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Generate", " ", "key", " ", "hypotheses"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"k", "=",
RowBox[{"Range", "[",
RowBox[{"0", ",", "255"}], "]"}]}], ";"}], "\n",
RowBox[{
RowBox[{"k", "[",
RowBox[{"[",
RowBox[{"1", ";;", "20"}], "]"}], "]"}], "//",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}]}]}]}]], "Input",
CellChangeTimes->{{3.7204363069291754`*^9, 3.720436313709281*^9},
3.720436529424485*^9, {3.7204380153299246`*^9, 3.7204380488776207`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"XOR", " ", "each", " ", "data", " ", "byte", " ", "with", " ", "each",
" ", "key"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"xmat", "=",
RowBox[{"Outer", "[",
RowBox[{"BitXor", ",", "d", ",", "k"}], "]"}]}], ";"}], "\n",
RowBox[{
RowBox[{
RowBox[{"xmat", "[",
RowBox[{"[",
RowBox[{
RowBox[{"1", ";;", "10"}], ",",
RowBox[{"1", ";;", "10"}]}], "]"}], "]"}], "//", "MatrixForm"}], "//",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}]}]}]}]], "Input",
CellChangeTimes->{{3.7204378406581306`*^9, 3.720437873889809*^9}, {
3.7204380578574905`*^9, 3.72043808961254*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"Substitute", " ", "with", " ", "SBOX", " ", "all", " ", "XORed", " ",
"values"}], "*)"}], "\[IndentingNewLine]",
RowBox[{"smat", "=",
RowBox[{"Map", "[",
RowBox[{"?",
RowBox[{
RowBox[{
RowBox[{",?];", "\n",
RowBox[{"smat", "[",
RowBox[{"[",
RowBox[{
RowBox[{"1", ";;", "10"}], ",",
RowBox[{"1", ";;", "10"}]}], "]"}], "]"}]}], "//", "MatrixForm"}],
"//",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"#", ",", "16"}], "]"}], "&"}]}]}]}]}]}]], "Input",
CellChangeTimes->{{3.7204364937533484`*^9, 3.7204365216630898`*^9}, {
3.7204381098511887`*^9, 3.720438147519025*^9}, {3.7204381799849195`*^9,
3.72043818617476*^9}, 3.7204385169275365`*^9, {3.720438846096431*^9,
3.720438866277361*^9}},
EmphasizeSyntaxErrors->True],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Compute", " ", "Hamming", " ",
RowBox[{"Weights", " ", "--"}], " ", "the", " ", "matrix", " ", "of", " ",
"hypothetical", " ", "power", " ", "consumption"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"hmat", "=",
RowBox[{"Map", "[",
RowBox[{"?",
RowBox[{
RowBox[{",?];", "\n",
RowBox[{"hmat", "[",
RowBox[{"[",
RowBox[{
RowBox[{"1", ";;", "10"}], ",",
RowBox[{"1", ";;", "10"}]}], "]"}], "]"}]}], "//",
"MatrixForm"}]}]}]}]}]], "Input",
CellChangeTimes->{{3.720436564076136*^9, 3.720436573148843*^9},
3.7204366713173647`*^9, {3.720437434578974*^9, 3.7204374489522295`*^9}, {
3.720438195365692*^9, 3.720438208866293*^9}, {3.7204389027292747`*^9,
3.72043890395277*^9}, {3.7204390240388007`*^9, 3.720439046425144*^9}},
EmphasizeSyntaxErrors->True],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Compute", " ", "the", " ", "correlation", " ",
RowBox[{"matrix", " ", "--"}], " ", "correlate", " ", "the", " ",
"hypotheses", " ", "with", " ", "measured", " ", "traces"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"Dimensions", "[", "hmat", "]"}], "\[IndentingNewLine]",
RowBox[{"Dimensions", "[", "t", "]"}], "\[IndentingNewLine]",
RowBox[{"corr", "=",
RowBox[{"Correlation", "[",
RowBox[{"?",
RowBox[{
RowBox[{",?];", "\[IndentingNewLine]",
RowBox[{"corr", "[",
RowBox[{"[",
RowBox[{
RowBox[{"1", ";;", "10"}], ",",
RowBox[{"1", ";;", "5"}]}], "]"}], "]"}]}], "//",
"MatrixForm"}]}]}]}]}]}]], "Input",
CellChangeTimes->{{3.7204366947095766`*^9, 3.7204367233700914`*^9}, {
3.7204374518595214`*^9, 3.7204374574933295`*^9}, {3.720438914786463*^9,
3.720438916653968*^9}, {3.7204389933387804`*^9, 3.7204390078214912`*^9}, {
3.720439049151926*^9, 3.7204390634049435`*^9}},
EmphasizeSyntaxErrors->True],
Cell[BoxData[
RowBox[{"acorr", "=",
RowBox[{"Abs", "[",
RowBox[{
RowBox[{"?",
RowBox[{"];", "\n", "maxcorr"}]}], "=",
RowBox[{"Max", "[",
RowBox[{
RowBox[{"?",
RowBox[{"]", "\n",
RowBox[{"{",
RowBox[{"kindex", ",", "kpos"}], "}"}]}]}], "=",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"Position", "[",
RowBox[{"acorr", ",", "maxcorr"}], "]"}], "[",
RowBox[{"[", "1", "]"}], "]"}], "\[IndentingNewLine]", "key"}], "=",
RowBox[{"kindex", "-",
RowBox[{"1", "\[IndentingNewLine]",
RowBox[{"BaseForm", "[",
RowBox[{"key", ",", "16"}], "]"}]}]}]}]}]}]}]}]}]], "Input",
CellChangeTimes->{{3.7204368809288006`*^9, 3.720436892960585*^9},
3.72043825056677*^9, {3.720438930572811*^9, 3.720438933656006*^9}},
EmphasizeSyntaxErrors->True],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Find", " ", "the", " ", "correlation", " ", "peak",
RowBox[{"(", "s", ")"}], " ", "in", " ", "the", " ", "trace", " ",
"plot"}], "*)"}], "\[IndentingNewLine]",
RowBox[{"ListLinePlot", "[",
RowBox[{"corr", "[",
RowBox[{"[",
RowBox[{"?", "]],PlotRange\[Rule]All]"}]}]}]}]}]], "Input",
CellChangeTimes->{{3.720437664036378*^9, 3.720437668858079*^9}, {
3.7204389531392536`*^9, 3.7204389679561768`*^9}},
EmphasizeSyntaxErrors->True],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Break", " ", "all", " ", "key", " ",
RowBox[{"bytes", "!"}]}], "*)"}], "\[IndentingNewLine]",
RowBox[{"(*", "\[IndentingNewLine]",
RowBox[{
RowBox[{"kk", "=",
RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"For", "[",
RowBox[{
RowBox[{"ki", "=", "1"}], ",",
RowBox[{"ki", "\[LessEqual]", "16"}], ",",
RowBox[{"ki", "++"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"AppendTo", "[",
RowBox[{"kk", ",", "key"}], "]"}], ";", "\[IndentingNewLine]",
RowBox[{"Print", "[",
RowBox[{
"\"\<Found key byte\>\"", ",", " ", "\"\< at position \>\"", ",",
" ", "\"\< with correlation \>\""}], "]"}], ";"}]}],
"\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]",
RowBox[{
RowBox[{"BaseForm", "[",
RowBox[{"kk", ",", "16"}], "]"}], "\[IndentingNewLine]",
RowBox[{"FromCharacterCode", "[", "kk", "]"}]}]}], "\[IndentingNewLine]",
"*)"}]}]], "Input",
CellChangeTimes->{{3.5130696191801653`*^9, 3.5130696330894313`*^9}, {
3.513069906023134*^9, 3.513069908271037*^9}, {3.513070107142098*^9,
3.5130701234115644`*^9}, 3.513072048142866*^9, 3.6116553510297303`*^9, {
3.6116554562198772`*^9, 3.6116554859399195`*^9}, {3.6116555178099637`*^9,
3.611655619500106*^9}, {3.6116556523401523`*^9, 3.6116557420702777`*^9}, {
3.61165577646435*^9, 3.6116557933043737`*^9}, {3.611655836908444*^9,
3.6116558509384637`*^9}, {3.6116559727286634`*^9, 3.611655978018671*^9}, {
3.6116560616130557`*^9, 3.6116560931031*^9}, {3.6116561897594013`*^9,
3.6116562043994217`*^9}, {3.611656539034998*^9, 3.6116565408050003`*^9}, {
3.6977984386017427`*^9, 3.697798459144847*^9}, {3.6977985148794065`*^9,
3.6977985833439856`*^9}, {3.697798645947137*^9, 3.6977986636405888`*^9}, {
3.697801698213625*^9, 3.697801776406316*^9}, {3.697801810083218*^9,
3.697801884028393*^9}, {3.697801985851428*^9, 3.69780202420901*^9}, {
3.6978021615495586`*^9, 3.697802209294267*^9}, {3.697802468640934*^9,
3.6978024749311595`*^9}, {3.720172526518976*^9, 3.7201725707035656`*^9},
3.720433541599208*^9, {3.7204356258210487`*^9, 3.720435626626298*^9}, {
3.7204357862180295`*^9, 3.7204357878500185`*^9}, {3.7204358320926394`*^9,
3.7204358376169844`*^9}, {3.720435896349845*^9, 3.720435901189147*^9}, {
3.7204372729625654`*^9, 3.720437349472704*^9}, {3.7204383744797044`*^9,
3.720438395475066*^9}, {3.720438574027712*^9, 3.720438574545648*^9}, {
3.7204389746913157`*^9, 3.7204389843366966`*^9}}]
},
PrintingStyleEnvironment->"SlideShow",
WindowSize->{1264, 777},
WindowMargins->{{0, Automatic}, {0, Automatic}},
ShowSelection->True,
Magnification:>1.25 Inherited,
FrontEndVersion->"9.0 for Microsoft Windows (64-bit) (January 25, 2013)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)
(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[545, 20, 104, 2, 36, "Input"],
Cell[652, 24, 1604, 37, 106, "Input"],
Cell[2259, 63, 528, 14, 60, "Input"],
Cell[2790, 79, 1050, 20, 36, "Input"],
Cell[3843, 101, 2189, 48, 198, "Input"],
Cell[6035, 151, 1732, 46, 198, "Input"],
Cell[7770, 199, 302, 7, 36, "Input"],
Cell[8075, 208, 613, 14, 60, "Input"],
Cell[8691, 224, 794, 16, 36, "Input"],
Cell[9488, 242, 439, 12, 36, "Input"],
Cell[9930, 256, 481, 13, 36, "Input"],
Cell[10414, 271, 3361, 69, 543, "Input"],
Cell[13778, 342, 988, 19, 129, "Input"],
Cell[14769, 363, 697, 15, 60, "Input"],
Cell[15469, 380, 878, 24, 83, "Input"],
Cell[16350, 406, 619, 18, 83, "Input"],
Cell[16972, 426, 757, 22, 83, "Input"],
Cell[17732, 450, 901, 25, 83, "Input"],
Cell[18636, 477, 899, 22, 83, "Input"],
Cell[19538, 501, 1080, 25, 129, "Input"],
Cell[20621, 528, 874, 24, 129, "Input"],
Cell[21498, 554, 512, 12, 60, "Input"],
Cell[22013, 568, 2608, 47, 244, "Input"]
}
]
*)
(* End of internal cache information *)

View file

@ -0,0 +1,361 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Break AES using DPA with correlations\n",
"\n",
"You need:\n",
"* `plaintext.txt`: all PT blocks, (one block per line, in hex, bytes separated by spaces)\n",
"* `ciphertext.txt`: all CT blocks, (one block per line, in hex, bytes separated by spaces)\n",
"* `traceLength.txt`: how many samples per trace (one decimal number)\n",
"* `traces.bin`: raw measured traces, one byte per sample (uint8), all traces together continuously\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GEwwR12Gupsi"
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8fW8nPQ5uyEO"
},
"outputs": [],
"source": [
"# AES SBOX\n",
"sbox = np.array([\n",
" 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,\n",
" 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,\n",
" 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,\n",
" 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,\n",
" 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,\n",
" 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,\n",
" 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,\n",
" 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,\n",
" 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,\n",
" 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,\n",
" 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,\n",
" 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,\n",
" 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,\n",
" 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,\n",
" 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,\n",
" 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16\n",
" ], dtype='uint8')\n",
"\n",
"# Hamming weight lookup table\n",
"hw_table = []\n",
"for i in range(256):\n",
" s = '{0:08b}'.format(i)\n",
" hw_table.append(s.count('1'))\n",
"hw_table = np.array(hw_table, 'uint8')\n",
"\n",
"# Correlation of two matrices\n",
"def correlate(x, y):\n",
" \"\"\"\n",
" Correlate all columns from matrix x of shape (a,b)\n",
" with all columns from matrix y of shape (a,c),\n",
" creating correlation matrix C of shape (b,c).\n",
" \n",
" Originally matlab script by Jiri Bucek in NI-HWB.\n",
" \"\"\"\n",
" x = x - np.average(x, 0) # remove vertical averages\n",
" y = y - np.average(y, 0) # remove vertical averages\n",
" C = x.T @ y # (n-1) Cov(x,y)\n",
" C = C / (np.sum(x**2, 0)**(1/2))[:,np.newaxis] # divide by (n-1) Var(x)\n",
" C = C / (np.sum(y**2, 0)**(1/2)) # divide by (n-1) Var(y)\n",
" return C\n",
"\n",
"# Load PT of CT from file\n",
"def load_text(file_name):\n",
" \"\"\"\n",
" Load any text PT/CT from file containing hex strings with bytes \n",
" separated by spaces, one block per line\n",
" Output is a matrix of bytes (np.array)\n",
" \"\"\"\n",
" txt_str = open(file_name).readlines()\n",
" del txt_str[-1] #discard last empty line\n",
" #split each line into bytes and convert from hex\n",
" txt_bytes_list = list(\n",
" map(lambda line: \n",
" list(\n",
" map(lambda s: int(s, 16),\n",
" line.rstrip().split(\" \"))\n",
" ),\n",
" txt_str)\n",
" )\n",
" return np.array(txt_bytes_list, 'uint8')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "--PH16eNuz_H"
},
"outputs": [],
"source": [
"# read plaintext inputs\n",
"inputs = load_text(\"plaintext.txt\")\n",
"\n",
"# read length of one complete trace (number of samples per trace)\n",
"with open(\"traceLength.txt\", \"r\") as fin:\n",
" trace_length = int(fin.readline())\n",
"\n",
"# trim each trace - select interesting part\n",
"start = 0\n",
"len = trace_length # CHANGE to the length of the first round; \n",
"\n",
"# read traces from binary file\n",
"traces = np.fromfile(\"traces.bin\", dtype='uint8') # read as linear array\n",
"traces = np.reshape(traces, (traces.size // trace_length, trace_length)) # reshape into matrix\n",
"traces = traces[:, start:len] # select only the interesting part of each trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZVJ_Tk55u1wu"
},
"outputs": [],
"source": [
"print(inputs.shape) # dimensions of inputs\n",
"print(trace_length)\n",
"print(traces.shape) # dimensions of matrix of traces"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6hzUcHiWxyH0"
},
"outputs": [],
"source": [
"# If you feel brave enough -- interactive plots\n",
"#!pip install ipympl\n",
"#from google.colab import output\n",
"#output.enable_custom_widget_manager()\n",
"#%matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wDAUVmNOu3BP"
},
"outputs": [],
"source": [
"# Plot one trace\n",
"fig = plt.figure()\n",
"plt.plot(traces[0])\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w6boaqAQvF1G"
},
"source": [
"## **Attack the first key byte**\n",
"![Intermediate value](dpa-aes-v.png)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WaKiOUmbvbQR"
},
"outputs": [],
"source": [
"# Generate key hypotheses (all possible byte values)\n",
"keys = np.arange(start=0, stop=256, step=1, dtype='uint8')\n",
"# Select the first byte of each input block\n",
"inp = inputs[:, 0]\n",
"# XOR each data byte with each key\n",
"xmat = inp[:, np.newaxis] ^ keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Examine the inputs matrix. Does it contain the data from plaintext.txt?\n",
"print(inputs)\n",
"# What is the shape of all the operands from the previous cell?\n",
"print(inputs.shape)\n",
"print(inp.shape)\n",
"print(inp[:, np.newaxis].shape)\n",
"print(keys.shape)\n",
"print(xmat.shape)\n",
"# Do you understand the values after the XOR operation? What AES operation do they represent?\n",
"print(xmat)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "VrBZd18VwBOH"
},
"outputs": [],
"source": [
"# Substitute with SBOX all XORed values -- matrix of intermediate values\n",
"smat = sbox[?]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4GfR9BU-wT4G"
},
"outputs": [],
"source": [
"# Compute Hamming Weights -- the matrix of hypothetical power consumption\n",
"hmat = ?[?]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "J8TTPk-WwjQH"
},
"outputs": [],
"source": [
"# Compute the correlation matrix -- correlate the hypotheses with measured traces\n",
"print(hmat.shape)\n",
"print(traces.shape)\n",
"corr = correlate(?, ?)\n",
"# What is the shape and contents of the correlation matrix?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iOqbuNAKxCvP"
},
"outputs": [],
"source": [
"# Find the absolute maximum correlation\n",
"acorr = abs(?)\n",
"max_acorr = ?.max()\n",
"(k, j) = np.where(acorr == ?) # find idices of maximum\n",
"print(\"key: %d time: %d\" % (k[0], j[0]))\n",
"print(\"key: %1c, %02x\" % (k[0], k[0]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot the correlation traces for the right key byte guess and one wrong key byte guess\n",
"# Do you see the correlation peaks?\n",
"fig = plt.figure()\n",
"plt.plot(?)\n",
"plt.plot(?)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z62RVYJYzncZ"
},
"source": [
"## **Break all key bytes!**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "T7HhwO-ezpoQ"
},
"outputs": [],
"source": [
"keys = np.array(range(0, 256))\n",
"kk = np.zeros(16, dtype='uint8')\n",
"for i in range(0, 16):\n",
" inp = inputs[:, ?]\n",
" ????\n",
" kk[i] = k\n",
" print(\"%1c, %02x @ %d\" % (k[0], k[0], j[0]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **Verify the key on a PT, CT pair!**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"key_bytes = bytes(kk)\n",
"outputs = ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install pycryptodome\n",
"from Crypto.Cipher import AES\n",
"cipher = AES.new(key_bytes, AES.MODE_ECB)\n",
"??"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "dpa_student.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

101
dpa-attack/plaintext.txt Normal file
View file

@ -0,0 +1,101 @@
25 eb 8c 48 ff 89 cb 85 4f c0 90 81 cc 47 ed fc
86 19 b2 14 fe 65 92 d4 8b fc ea 9c 9d 8e 32 44
d7 d7 e9 f1 f7 de 60 56 8d e9 89 07 3f 3d 16 39
01 80 3c d1 08 d8 8d 73 af ea 79 c8 1e 47 83 c6
95 31 39 03 c4 18 f1 2b 4c 1a 34 50 6d 73 29 d2
0f 40 c4 19 6f e2 d7 87 1a 99 68 16 09 c3 e7 7e
17 7d 64 9b a5 39 53 a6 88 20 a2 0a 17 8f ef 57
19 c7 f3 5c 4a be 2e a0 d8 97 b7 41 71 4d 03 80
f8 fd cd 06 34 d5 c6 02 4c db 95 cb 07 4d c8 4b
4c 2b 14 1e 24 67 07 2d c4 39 f0 fc d2 60 0d 0a
17 7c 51 87 79 98 ca dc 94 a0 8c c1 5e 3c e9 98
52 73 61 82 ec dc 67 62 0a b6 60 e9 52 d6 c6 c2
47 e7 b0 36 0f 85 91 aa 14 76 b0 16 e5 8d f1 72
61 b5 54 0a 60 b7 3d 38 d9 95 e7 60 f9 d3 19 f1
8e 8d d4 74 2b 86 cd b8 bb 8f 18 fb 89 c2 c7 35
45 a4 65 f8 15 28 4d db b1 71 2f cd a8 ce 2d 57
90 9c ea 2d c3 74 42 ce 2e 80 9d 3f 4b 23 b5 dd
21 82 d4 53 30 36 20 fd 9c 37 d2 1f 1c de 4a 88
6d 63 20 08 54 cd 32 4f a9 40 6c d3 18 71 ea 14
2c 0f 9e db 8e f3 13 fb 9a eb 56 87 e3 35 af 6e
bc f3 22 64 e4 20 93 c3 18 d3 e5 5e a6 af e7 85
4f 3f 71 57 a0 2a ca c2 d8 e2 9e 91 44 40 bc f4
ce 91 a7 a3 d1 9c 96 26 a9 ca cd 46 99 e6 f0 30
b2 be 2c 69 b6 00 56 f7 10 13 09 70 dc 33 6f 8a
7b 44 97 0e bf 15 ae 7c 83 b8 58 d0 ae 36 cf 47
fc 0e cd 8f 99 35 d4 f9 ba d5 72 ee 9d 2c 25 96
db b6 4c 0c 78 3b 9a cc c7 d7 a7 7d 2b cc 72 26
5b 70 fd 45 e2 a1 ab 9a b8 c5 49 34 59 1b 2b 01
52 7f 5e e0 9a 54 1e 67 40 96 62 34 83 46 8c 27
e6 53 ef b0 3d c6 0d a4 17 96 c2 7a dc f9 ea 5f
35 89 a9 b9 58 6d f6 dd f4 cc 52 6a 8b 69 24 70
90 9e 73 6a d4 b9 23 a9 51 02 d5 5d a4 6d 2e 50
1b 48 1c 82 e3 8d a9 79 24 e4 a8 12 e1 a6 3f f3
2e 68 d9 e7 70 ff 65 02 62 1c ce cb ba aa 1a e8
95 3a 7f 90 dc 25 e9 ed e8 ea 0b 96 06 60 8e 64
57 cb a3 bf 7b 20 6e 5b b1 e6 7a 80 96 61 c6 a4
92 1b 9c e5 34 66 f2 de 79 32 2e 07 d5 10 e7 9d
8b bd fb fa 0e 68 1e 88 a8 b0 9e 97 c2 dc c0 e7
8c 91 fa a1 0a 1c e9 cb 85 51 85 79 2a 56 b4 39
38 4e 57 fa 75 d1 8a c8 30 f5 13 8c 99 4d 10 04
d8 76 9b b2 44 83 ba 0c 82 4c 60 60 3d 8f 64 fa
4a 8c 92 9e bb 85 90 5f 60 bc ab ec 1f eb 63 2c
18 be 4f 15 93 4d 21 04 27 7c 43 a2 a2 2a 4e d2
66 26 7a b0 7a d1 fe 05 fa e6 db f5 88 28 c5 05
07 ce 25 69 4e 21 1b b3 4d 1e ab ae 09 43 03 c3
36 61 bf e3 50 1c a5 c0 a5 dd 92 4e 67 c6 dc 34
8a a0 2e 3f 7b dd 92 a4 9d f8 27 1c 36 1d b7 67
5a 9b bc 6a 8c 91 47 5b 87 7c d3 b8 33 a0 cc 3e
66 b3 60 1e 96 05 db 5f 69 96 cc 74 6d 16 e6 92
dd 1d 12 34 7a 1f 31 e2 d3 79 dd ad eb b3 56 0f
c7 b0 9c d3 1b fd 2b 93 4f 46 58 fa d6 0b e5 c1
5d 0f b3 e2 56 2d 73 bf aa 96 6d 67 d2 a8 ac 90
11 6b 58 e6 80 f0 61 52 77 c7 f4 0e a4 1a a0 81
b0 58 f5 16 22 5e 15 c4 c3 b0 76 92 61 8b 8b 79
b4 4c 44 ad c0 59 28 07 07 f6 21 3a 07 a2 84 85
bd 76 67 b2 4c ad fe 60 95 72 7d 5c 47 dc ef 99
e9 f7 61 01 0d 74 26 d5 9b a6 d4 a1 01 fe 9f f5
71 72 26 1c cb 72 63 1f b3 42 43 a0 4a a0 e1 05
00 a5 6e 48 ab 6c 18 90 7d 60 e0 29 98 15 f9 9b
2e 23 3e 44 3e 7d 20 f9 2d 58 66 73 aa 39 99 20
68 21 f6 86 c1 33 1f 87 fb 8d 7f 83 9c 8f 1d 42
ef 3d d3 60 3a d2 e8 fa a2 1e b9 60 b1 fc 2e 53
29 14 f8 28 78 fe 6e b2 f8 f6 84 09 7c 80 da 95
fb 4a 95 f6 a1 77 a6 25 2b 67 21 5f a9 c7 45 c6
17 d6 6a 27 1f 25 26 b8 57 9c a4 42 0a d4 80 10
1e 83 fc ce 6c bb 35 79 f0 f3 76 c5 31 ca 6b ae
9b a1 48 44 27 a0 ef 61 87 21 be 0e 4d ab 3d 55
24 0c fa b5 76 1a 52 e5 fa 7f e3 0c e9 63 19 e5
81 ec a9 6b 37 37 c0 8f dc 28 3c 46 b1 b2 a0 da
14 34 26 c1 d0 b9 34 59 13 de 40 ff 2a 2f 95 61
01 59 26 fa 8c 70 12 cd cd d8 ec da d3 27 62 7f
0f 25 e6 e0 a4 62 7f f3 ee 01 20 9e e5 07 70 32
b2 fa a0 2b a9 c5 01 61 76 b3 1e 52 b1 51 d2 df
c7 fd 95 12 ae ac db 89 84 4d ff 3f 97 5f f3 bb
e5 7d 40 d2 9f 81 54 ce e9 44 36 19 d3 9b fa 38
5c 08 b7 6d 4c ae b1 0b 88 bd 4a 97 58 d9 d2 d2
05 6f aa 2e 15 b3 75 49 aa 8d 92 97 2d df 7d 34
a1 b6 8e b9 6f bd a8 f1 48 c1 16 02 d6 01 48 33
c7 70 d7 4d 3e 30 00 2a 27 03 b9 2b 35 ab ca 62
f5 61 4b bb 42 4a 5b 05 1a c3 1f 0b 83 b6 81 0d
3b 7c b8 d1 88 26 84 46 4c 49 05 cf 54 bb 1f da
07 eb 42 a0 ae cb 52 2c 40 25 af c9 da 54 0f c1
34 c5 c2 59 1c 13 eb 11 05 ed cf 8a fe 9d 91 d1
ae 07 11 f2 56 3d 40 07 1d b7 1c 74 e3 2b e2 37
e8 05 32 5c bf b4 69 a2 e2 d3 4d a5 d9 b6 04 2b
8b 1d 4c d9 a0 b2 e8 b3 aa 97 73 7d 59 2b fe 72
73 68 58 5f 1c 40 72 d6 fe 3a 58 7f 33 96 0a 6b
1f f4 09 67 86 9e 43 92 8c 49 4a 5b c1 d5 35 aa
86 89 a4 f6 d7 bb 08 b5 d7 12 6a 15 43 fd 0e 14
eb 7c d5 09 2a c2 14 6b 90 3a 4d 2a 23 4b 9c ea
55 22 ac ea f6 dc d2 88 1e 40 cd 12 a6 f4 8b b8
b4 d3 4c c4 ed 19 e8 63 45 1f 3b 3e 1d 42 7b 20
47 f9 bc fc 5d c8 9b 61 dd 44 26 3c 61 f6 09 43
4e 3f 14 a3 b7 f3 9c ad 5c b8 7a 33 04 cf cb 1f
e4 4c b4 fa 26 53 d0 6f e4 4a 46 9a e4 b9 1e 63
c0 26 92 df 29 f2 5f 39 b0 aa 71 f2 b4 96 40 c6
bd bc 6d 60 9b 00 9a 32 f6 14 fa 1a c1 65 cd e8
12 bc fd d2 e6 3b 75 71 3e f2 f7 a6 97 44 c5 bd
57 c2 13 f7 a4 11 95 cd 6a 78 f3 9a f1 a0 08 88
94 ee 44 52 36 f8 d5 7f e7 dd f7 ca 59 17 5e da

BIN
dpa-attack/traceLength.txt Normal file

Binary file not shown.

1
dpa-attack/traces.bin Normal file

File diff suppressed because one or more lines are too long