# Code for a exchange protocol

Hi I want to insert a exchange protocol in my lab report. The protocol looks something like this :

How can I write such protocol in latex?

This is really quick and dirty, put illustrates hoot set such a picture with TikZ. Note that I coded all the positions manually -- which is a bit cumbersome if you have to draw multiple diagram with different node configurations etc. The right way would probably to use the positioning library or something else to let TikZ choose the position automatically. You may also check out the answer to this thread, which Ignasi linked to in their comment above.

However, here is a code that produces a TikZ picture similar to the one you posted. (Again, there certainly are much more elegant ways to achieve the same or a better result.)

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [draw] (I) at (0,5) {Init. $I$};
\node [draw] (R) at (8,5) {Resp. $R$};
\node [draw] (K1) at (0,2) {$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$};
\node [draw] (K2) at (8,2) {$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$};
\node  at (4,4.5) {$\texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_i}|g^x,|n_i|\textcolor{red}{\textit{info}_i})$};
\node  at (4,4) {$\texttt{SA\_INIT}(\textcolor{red}{ck})$};
\node  at (4,3.5) {$m_1 = \texttt{SA\_INIT}(\textcolor{red}{ck}|\textcolor{red}{\texttt{SA}_i}|g^x|n_i|\textcolor{red}{\textit{info}_i})$};
\node  at (4,3.0) {$m_2 = \texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_r}|g^y|n_r|\textcolor{red}{\textit{info}_r})$};
\node at (4,1.0) {$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_i}|\textsf{sign}(sk_I,\textsc{hash}(m_I|n_r|\textsf{mac}(k_i,\texttt{ID}_i))))]^{k_1}$};
\node at (4,0.5) {$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_r}|\textsf{sign}(sk_R,\textsc{hash}(m_R|n_i|\textsf{mac}(k_r,\texttt{ID}_i))))]^{k_2}$};
\draw (I) -- (K1);
\draw (R) -- (K2);
\draw [->] (0,4.25) -- (8,4.25);
\draw [<-] (0,3.75) -- (8,3.75);
\draw [->] (0,3.25) -- (8,3.25);
\draw [<-] (0,2.75) -- (8,2.75);
\draw [->] (0,0.75) -- (8,0.75);
\draw [<-] (0,0.25) -- (8,0.25);
\node (B1) [fill,color=black,text width=1.2cm] at (0,-0.5) {};
\node (B2) [fill,color=black,text width=1.2cm] at (8,-0.5) {};
\draw (K1) -- (B1);
\draw (K2) -- (B2);
\end{tikzpicture}
\end{document}


Your example with msc. Compile it with xelatex because msc uses pstricks.

\documentclass{article}
\usepackage{msc}
\begin{document}
\setlength{\instdist}{8.5cm}
\setlength{\envinstdist}{3cm}
\begin{msc}{}
\declinst{I}{}{Init. $I$}
\declinst{R}{}{Resp. $R$}
\mess{$\texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_i}|g^x,|n_i|\textcolor{red}{\textit{info}_i})$}{I}{R}
\nextlevel
\mess{$\texttt{SA\_INIT}(\textcolor{red}{ck})$}{R}{I}
\nextlevel
\mess{$m_1 = \texttt{SA\_INIT}(\textcolor{red}{ck}|\textcolor{red}{\texttt{SA}_i}|g^x|n_i|\textcolor{red}{\textit{info}_i})$}{I}{R}
\nextlevel
\mess{$m_2 = \texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_r}|g^y|n_r|\textcolor{red}{\textit{info}_r})$}{R}{I}
\nextlevel
\action*{\small{$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$}}{I}
\action*{\small{$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$}}{R}
\nextlevel[3]
\mess{$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_i}|\textsf{sign}(sk_I,\textsc{hash}(m_I|n_r|\textsf{mac}(k_i,\texttt{ID}_i))))]^{k_1}$}{I}{R}
\nextlevel
\mess{$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_r}|\textsf{sign}(sk_R,\textsc{hash}(m_R|n_i|\textsf{mac}(k_r,\texttt{ID}_i))))]^{k_2}$}{R}{I}
\end{msc}
\end{document}