-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes.sty
156 lines (134 loc) · 5.68 KB
/
aes.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{aes}[2022/10/06 Utils for illustrating AES]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% AES LIBRARY FOR TIKZ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Utilities for drawing AES rounds and states %
% Author: Maria Eichlseder %
% Usage: \usepackage{aes} %
% Dependency: tikzlibrarycipher.code.tex %
% https://extgit.iaik.tugraz.at/meichlseder/tikz %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% PREREQUISITES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{amsmath,amssymb}
\RequirePackage{tikz}
\RequirePackage{xspace}
\usetikzlibrary{calc,cipher}
\usetikzlibrary{patterns}
%%% GENERAL CRYPTO UTILS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\providecommand{\cipher}[1]{\textsf{#1}}
\providecommand{\SB}{\cipher{SB}\xspace}
\providecommand{\AK}{\cipher{AK}\xspace}
\providecommand{\SR}{\cipher{SR}\xspace}
\providecommand{\MC}{\cipher{MC}\xspace}
\providecommand{\SubBytes}{\cipher{SubBytes}\xspace}
\providecommand{\AddRoundKey}{\cipher{AddRoundKey}\xspace}
\providecommand{\ShiftRows}{\cipher{ShiftRows}\xspace}
\providecommand{\MixColumns}{\cipher{MixColumns}\xspace}
%%% COLORS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\colorlet{diff}{blue!50}
\colorlet{free}{green!30}
\colorlet{oper}{white}
\providecommand{\ActCell}[2]{%
\FillCell{#1} % optional parameter for fill options
\Cell{#1}{\color{white}#2} % write content
}
\providecommand{\HighlightTweakey}[1][]{\draw[line width=1.5pt,#1] (0,0) rectangle +(4,-2);}
%%% UTILS & OPTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
state/.style={inner sep=-.5pt},
stateopts/.style={scale=.3},
fillopts/.style={diff},
markopts/.style={marc,ultra thick},
cellopts/.style={font=\footnotesize},
box/.append style={fill=oper},
>=latex
}
\providecommand{\stateScale}{.3} % TODO derive!
%%% AES ROUND FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\AesInit}{
\coordinate (init); % beginning of current line
\AesInitCoordinates
}
\newcommand{\AesInitCoordinates}{
% called by AesInit and AesNewLine to initialize state coordinates based on (init)
\draw (init) coordinate (here); % current x-coordinate of state
\draw (here) node (S) {}
+(0,.6) coordinate (lbl)
++(0,1) node (T) {};
\draw (S.east) coordinate (Shere)
(T.east) coordinate (There);
\draw (here) +(.75,0) coordinate(here);
\foreach \r/\offset in {0/.45,1/.15,2/-.15,3/-.45} {
\draw (here) + (0,\offset) coordinate (S\r);
}
\draw (S) +(-.25,-.9) coordinate (Shook); % for \AesNewLine
}
\newcommand{\AesNewLine}[2][]{
% #1 = round number (optional)
% #2 = state after mixcols (repeated at beginning of next line)
% draw and connect final state
\draw (Shere.east) coordinate (Sprev);
\draw (S-|here) node[state] (Shere) {\State{#2}};
\draw (Shere|-lbl) node[above,inner sep=1pt] {$X_{#1}$}; % adapt label here
\draw (init) +(0,-2.0) coordinate (init);
\draw (Shere.east) coordinate (Send);
\draw (Shook) coordinate (Thook);
\AesInitCoordinates
\draw[->,rounded corners=2pt] (Send) -- +(.25,0) |- (Thook) |- (init) -- +(.15,0);
}
\newcommand{\AesFin}[2][]{
% #1 = round number (optional)
% #2 = final state
\draw (Shere.east) coordinate (Sprev);
\draw (S-|here) node[state] (Shere) {\State{#2}};
\draw (Shere|-lbl) node[above,inner sep=1pt] {$X_{#1}$}; % adapt label here
}
\newcommand{\AesStep}[3]{
% #1 = function
% #2 = state content
% #3 = label
\draw (here) +(2.0,0) coordinate(here);
\draw (Shere.east) coordinate (Sprev);
\draw (S-|here) node[state] (Shere) {\State{#2}};
\draw (Shere|-lbl) node[above,inner sep=1pt] {#3};
\draw[->] (Sprev) -- node[above] {#1} (Shere.west|-Sprev);
% \draw (Shere) ++(1,0) node[box,minimum height=1.2cm,minimum width=.42cm,font=\scriptsize,align=center] (Shere) {\SC\\\AC};
}
\newcommand{\AesRound}[6][]{
% a modified AES round that *starts* with addroundkey
% #1 = round number (optional)
% #2 = state input
% #3 = round key
% #4 = state after AddRoundKey
% #5 = state after SubBytes
% #6 = state after ShiftRows
% state after MixColumns is part of the next round or AesFin
%
% round input:
\draw (Shere.east) coordinate (Sprev);
\draw (S-|here) node[state] (Shere) {\State{#2}};
\draw (Shere|-lbl) node[above,inner sep=1pt] {$X_{#1}$};
% AddRoundTweakey:
\draw (Shere.east) coordinate (Sprev);
\draw (here.east) ++(1.5,1.5*\stateScale) coordinate[xor] (Txor);
\draw (Sprev) ++(1.5+\stateScale,0) coordinate (Tfin);
\draw[->,rounded corners=1pt] (Sprev) -- +(.5*\stateScale,0) |- (Txor);
\draw[->,rounded corners=1pt] (Txor) -- +(2.5*\stateScale,0) |- (Tfin);
\draw (Txor|-lbl) node[above,inner sep=1pt] {$K_{#1}$}; % adapt label here
\draw (Txor.east) coordinate (Shere);
\draw (There.east) coordinate (Tprev);
\draw (Txor.center) ++(0,-2.5*\stateScale) node[state] (There) {\State{#3}};
\draw (There.north) -- (Txor);
\draw (here) +(3,0) coordinate(here);
\draw (Shere.east) coordinate (Sprev);
\draw (S-|here) node[state] (Shere) {\State{#4}};
\draw (Shere|-lbl) node[above,inner sep=1pt] {$Y_{#1}$}; % adapt label here
% SubCells, ShiftRows:
\AesStep{\SB}{#5}{$Z_{#1}$} % adapt label here
\AesStep{\SR}{#6}{$W_{#1}$} % adapt label here
\draw (here) +(2,0) coordinate(here);
\draw[->] (Shere.east) -- node[above] {\MC} +(.8,0);
% adapt hook depth
\draw (S) +(-.25,-1) coordinate (Shook); % for \AesNewLine
}
\endinput