\doxysection{src/gsm/comp128.c File Reference}
\label{comp128_8c}\index{src/gsm/comp128.c@{src/gsm/comp128.c}}


COMP128 v1; common/old GSM Authentication Algorithm (A3/\+A8).  


{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$stdint.\+h$>$}\newline
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
static void \textbf{ \+\_\+comp128\+\_\+compression\+\_\+round} (uint8\+\_\+t $\ast$\textbf{ x}, int \textbf{ n}, const uint8\+\_\+t $\ast$tbl)
\item 
static void \textbf{ \+\_\+comp128\+\_\+compression} (uint8\+\_\+t $\ast$\textbf{ x})
\item 
static void \textbf{ \+\_\+comp128\+\_\+bitsfrombytes} (uint8\+\_\+t $\ast$\textbf{ x}, uint8\+\_\+t $\ast$bits)
\item 
static void \textbf{ \+\_\+comp128\+\_\+permutation} (uint8\+\_\+t $\ast$\textbf{ x}, uint8\+\_\+t $\ast$bits)
\item 
void \textbf{ comp128v1} (const uint8\+\_\+t $\ast$ki, const uint8\+\_\+t $\ast$rand, uint8\+\_\+t $\ast$\textbf{ sres}, uint8\+\_\+t $\ast$kc)
\begin{DoxyCompactList}\small\item\em Perform COMP128v1 algorithm. \end{DoxyCompactList}\item 
void \textbf{ comp128} (const uint8\+\_\+t $\ast$ki, const uint8\+\_\+t $\ast$rand, uint8\+\_\+t $\ast$\textbf{ sres}, uint8\+\_\+t $\ast$kc)
\begin{DoxyCompactList}\small\item\em Perform COMP128v1 algorithm. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
static const uint8\+\_\+t \textbf{ table\+\_\+0} [512]
\item 
static const uint8\+\_\+t \textbf{ table\+\_\+1} [256]
\item 
static const uint8\+\_\+t \textbf{ table\+\_\+2} [128]
\item 
static const uint8\+\_\+t \textbf{ table\+\_\+3} [64]
\item 
static const uint8\+\_\+t \textbf{ table\+\_\+4} [32]
\item 
static const uint8\+\_\+t $\ast$ \textbf{ \+\_\+comp128\+\_\+table} [5] = \{ \textbf{ table\+\_\+0}, \textbf{ table\+\_\+1}, \textbf{ table\+\_\+2}, \textbf{ table\+\_\+3}, \textbf{ table\+\_\+4} \}
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
COMP128 v1; common/old GSM Authentication Algorithm (A3/\+A8). 

This code is inspired by original code from \+: Marc Briceno {\texttt{ marc@scard.\+org}}, Ian Goldberg {\texttt{ iang@cs.\+berkeley.\+edu}}, and David Wagner {\texttt{ daw@cs.\+berkeley.\+edu}}

But it has been fully rewritten from various PDFs found online describing the algorithm because the licence of the code referenced above was unclear. A comment snippet from the original code is included below, it describes where the doc came from and how the algorithm was reverse engineered.

This code derived from a leaked document from the GSM standards. Some missing pieces were filled in by reverse-\/engineering a working SIM. We have verified that this is the correct COMP128 algorithm.

The first page of the document identifies it as \begin{DoxyVerb}    _Technical Information: GSM System Security Study_.
    10-1617-01, 10th June 1988.
\end{DoxyVerb}
 The bottom of the title page is marked \begin{DoxyVerb}    Racal Research Ltd.
    Worton Drive, Worton Grange Industrial Estate,
    Reading, Berks. RG2 0SB, England.
    Telephone: Reading (0734) 868601   Telex: 847152
\end{DoxyVerb}
 The relevant bits are in Part I, Section 20 (pages 66--67). Enjoy!

Note\+: There are three typos in the spec (discovered by reverse-\/engineering).
\begin{DoxyItemize}
\item First, \char`\"{}z = (2 $\ast$ x[n] + x[n]) mod 2$^\wedge$(9-\/j)\char`\"{} should clearly read \char`\"{}z = (2 $\ast$ x[m] + x[n]) mod 2$^\wedge$(9-\/j)\char`\"{}.
\item Second, the \char`\"{}k\char`\"{} loop in the \char`\"{}\+Form bits from bytes\char`\"{} section is severely botched\+: the k index should run only from 0 to 3, and clearly the range on \char`\"{}the (8-\/k)th bit of byte j\char`\"{} is also off (should be 0..7, not 1..8, to be consistent with the subsequent section).
\item Third, SRES is taken from the first 8 nibbles of x[], not the last 8 as claimed in the document. (And the document doesn\textquotesingle{}t specify how Kc is derived, but that was also easily discovered with reverse engineering.) All of these typos have been corrected in the following code.
\end{DoxyItemize}