A52HackTool 1.3.0
Turnkey & easy to use tool for cracking the GSM A5/2 cipher
Data Structures | Defines | Functions
matrices_generation.c File Reference

Implementation of first attack phase: matrices dictionary generation. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "utils.h"
#include "matrices_generation.h"
#include "code.h"
#include "keygen.h"

Data Structures

struct  GenerationArgs
 Set of arguments related to a thread in a multithreaded generation context. More...

Defines

#define NULLIFY(op)   do {memset(op, 0, REGS_TOTAL_VARS*sizeof(byte));} while(0)
#define LOADR1(op, tap, x)
#define LOADR2(op, tap, x)
#define LOADR3(op, tap, x)

Functions

void matrices_generation_clockR1 (byte R1[R1_BITS][1+R1_SIMPLE_VARS])
 Clock register R1 contents.
void matrices_generation_clockR2 (byte R2[R2_BITS][1+R2_SIMPLE_VARS])
 Clock register R2 contents.
void matrices_generation_clockR3 (byte R3[R3_BITS][1+R3_SIMPLE_VARS])
 Clock register R3 contents.
void matrices_generation_clockR4 (byte R4[])
 Clock register R4 contents.
void matrices_generation_clockingUnit (byte R1[R1_BITS][1+R1_SIMPLE_VARS], byte R2[R2_BITS][1+R2_SIMPLE_VARS], byte R3[R3_BITS][1+R3_SIMPLE_VARS], byte R4[R4_BITS], int clockAll)
 Performs register clocking according to the rules of the Clocking Unit.
void matrices_generation_doubleProduct (const byte a1[], const byte a2[], byte out[])
 Performs a double product of two vectors of variables.
void matrices_generation_getOutBit (byte R1[R1_BITS][1+R1_SIMPLE_VARS], byte R2[R2_BITS][1+R2_SIMPLE_VARS], byte R3[R3_BITS][1+R3_SIMPLE_VARS], byte out[REGS_TOTAL_VARS])
 Returns the current out keystream bit (here a set of xored variables) corresponding to a particular LFSR state.
void matrices_generation_processKeystreamEqns (const byte initialR4[], byte keystream[][REGS_TOTAL_VARS], const int len)
 Generates the desired amount of keystream dependancy equations (skipping first 99cycles) considering one initial R4 state.
int matrices_generation_getRealVariableIndex (unsigned int Reg, unsigned int index)
 Processes the correct location of a variable inside a LFSR from the variable index.
byte matrices_generation_solveEquation (byte eqn[], byte R1[], byte R2[], byte R3[])
 Returns the result of the evaluation of an expression with the given values of initial variables (for test purpose)
void getR4fromIndex (const int index, byte R4[R4_BITS])
 Returns the value of LFSR R4 corresponding to a particular index in [0..TOTAL_MATRICES-1].
int matrices_generation_exportMatrices (const char *filename, const int lowindex, const int highindex)
 Thread generation method.
void * matrices_generation_launchExport (void *data)
 Thread initializer.
int exportAllMatrices (const char *filename)
 Exports all Resolution Matrices into the specified file.
int matrices_generation_test ()
 Autotests the matrices generation on a verified set.

Detailed Description

Implementation of first attack phase: matrices dictionary generation.

Author:
Nicolas Paglieri & Olivier Benjamin
Version:
File Revision #30
Date:
01/12/2011

Define Documentation

#define LOADR1 (   op,
  tap,
 
)
Value:
do {NULLIFY(op);                                                      \
                                 memcpy((op), R1[(tap)]+sizeof(byte), R1_SIMPLE_VARS*sizeof(byte));\
                                 op[REGS_TOTAL_VARS-1] = R1[(tap)][0] ^ ((x)&1);                   \
                             } while(0)
#define LOADR2 (   op,
  tap,
 
)
Value:
do {NULLIFY(op);                                                      \
                                 memcpy((op)+R1_SIMPLE_VARS, R2[(tap)]+sizeof(byte), R2_SIMPLE_VARS*sizeof(byte));\
                                 op[REGS_TOTAL_VARS-1] = R2[(tap)][0] ^ ((x)&1);                   \
                             } while(0)
#define LOADR3 (   op,
  tap,
 
)
Value:
do {NULLIFY(op);                                                      \
                                 memcpy((op)+R1_SIMPLE_VARS+R2_SIMPLE_VARS, R3[(tap)]+sizeof(byte), R3_SIMPLE_VARS*sizeof(byte));\
                                 op[REGS_TOTAL_VARS-1] = R3[(tap)][0] ^ ((x)&1);                   \
                             } while(0)

Function Documentation

int exportAllMatrices ( const char *  filename)

Exports all Resolution Matrices into the specified file.

Parameters:
[in]filenamePath of the file to export to
Returns:
0 if the export is successfull, non-zero otherwise

References BUFFER_SIZE, DEBUG, GenerationArgs::highindex, GenerationArgs::lowindex, matrices_generation_launchExport(), PROCESSING_THREADS, and THREAD_CHUNKSIZE.

Referenced by main().

void getR4fromIndex ( const int  index,
byte  R4[R4_BITS] 
)

Returns the value of LFSR R4 corresponding to a particular index in [0..TOTAL_MATRICES-1].

Parameters:
[in]indexConsidered index
[in]R4Corresponding value of R4

References R4, and R4_INITIAL_CONST_POS.

Referenced by attack_decipherSecretKey().

void matrices_generation_clockingUnit ( byte  R1[R1_BITS][1+R1_SIMPLE_VARS],
byte  R2[R2_BITS][1+R2_SIMPLE_VARS],
byte  R3[R3_BITS][1+R3_SIMPLE_VARS],
byte  R4[R4_BITS],
int  clockAll 
)

Performs register clocking according to the rules of the Clocking Unit.

Parameters:
[in,out]R1First LFSR
[in,out]R2Second LFSR
[in,out]R3Third LFSR
[in,out]R4Fourth LFSR
[in]clockAllWhen non-zero, bypass Clocking Unit decision: always clock (initialization phase)

References MAJORITY, matrices_generation_clockR1(), matrices_generation_clockR2(), matrices_generation_clockR3(), matrices_generation_clockR4(), R1, R2, R3, R4, R4_CLOCKTAP_R1, R4_CLOCKTAP_R2, and R4_CLOCKTAP_R3.

Referenced by matrices_generation_processKeystreamEqns().

void matrices_generation_clockR1 ( byte  R1[R1_BITS][1+R1_SIMPLE_VARS])

Clock register R1 contents.

Parameters:
[in,out]R1Register to clock

References R1, R1_BITS, R1_SHIFTTAP_1, R1_SHIFTTAP_2, R1_SHIFTTAP_3, R1_SHIFTTAP_4, R1_SIMPLE_VARS, and XOR_CHARARRAYS.

Referenced by matrices_generation_clockingUnit().

void matrices_generation_clockR2 ( byte  R2[R2_BITS][1+R2_SIMPLE_VARS])

Clock register R2 contents.

Parameters:
[in,out]R2Register to clock

References R2, R2_BITS, R2_SHIFTTAP_1, R2_SHIFTTAP_2, R2_SIMPLE_VARS, and XOR_CHARARRAYS.

Referenced by matrices_generation_clockingUnit().

void matrices_generation_clockR3 ( byte  R3[R3_BITS][1+R3_SIMPLE_VARS])

Clock register R3 contents.

Parameters:
[in,out]R3Register to clock

References R3, R3_BITS, R3_SHIFTTAP_1, R3_SHIFTTAP_2, R3_SHIFTTAP_3, R3_SHIFTTAP_4, R3_SIMPLE_VARS, and XOR_CHARARRAYS.

Referenced by matrices_generation_clockingUnit().

void matrices_generation_clockR4 ( byte  R4[])

Clock register R4 contents.

Parameters:
[in,out]R4Register to clock

References R4_BITS, R4_SHIFTTAP_1, and R4_SHIFTTAP_2.

Referenced by matrices_generation_clockingUnit().

void matrices_generation_doubleProduct ( const byte  a1[],
const byte  a2[],
byte  out[] 
)

Performs a double product of two vectors of variables.

We assume here that the given vectors are correctly formed, meaning that all the present variables either belong to the same LFSR simple set, or represent the constant value "1".

Parameters:
[in]a1First Vector
[in]a2Second Vector
[out]outProduct of a1 and a2

References DEBUG, MAX, MIN, R1_PROD_VARS, R1_SIMPLE_VARS, R2_PROD_VARS, R2_SIMPLE_VARS, R3_SIMPLE_VARS, REGS_SIMPLE_VARS, and REGS_TOTAL_VARS.

Referenced by matrices_generation_getOutBit().

int matrices_generation_exportMatrices ( const char *  filename,
const int  lowindex,
const int  highindex 
)

Thread generation method.

Parameters:
[in]filenamePath of the file to write the generated data to
[in]lowindexIndex to start the generation form (inclusive)
[in]highindexLast index to be processed (exclusive)
Returns:
Number of generated matrices

References BINPRODUCT_MATRIX_MATRIX, BUFFER_SIZE, CODEWORD_LENGTH, DEBUG, EQN_SYSTEM_SIZE, matrices_generation_processKeystreamEqns(), NEEDED_ENCRYPTED_MESSAGES, processFullEncodingGMatrix(), processFullEncodingHMatrix(), PROGRESSBAR, R4, R4_BITS, R4_INITIAL_CONST_POS, REGS_TOTAL_VARS, SOURCEWORD_LENGTH, SYNDROME_LENGTH, and THREAD_CHUNKSIZE.

Referenced by matrices_generation_launchExport().

void matrices_generation_getOutBit ( byte  R1[R1_BITS][1+R1_SIMPLE_VARS],
byte  R2[R2_BITS][1+R2_SIMPLE_VARS],
byte  R3[R3_BITS][1+R3_SIMPLE_VARS],
byte  out[REGS_TOTAL_VARS] 
)

Returns the current out keystream bit (here a set of xored variables) corresponding to a particular LFSR state.

Parameters:
[in]R1First LFSR
[in]R2Second LFSR
[in]R3Third LFSR
[out]outSet of variables describing the current out keystream bit

References matrices_generation_doubleProduct(), R1_BITS, R1_OUTTAP_1, R1_OUTTAP_2, R1_OUTTAP_3, R2_BITS, R2_OUTTAP_1, R2_OUTTAP_2, R2_OUTTAP_3, R3_BITS, R3_OUTTAP_1, R3_OUTTAP_2, R3_OUTTAP_3, REGS_TOTAL_VARS, and XOR_CHARARRAYS.

Referenced by matrices_generation_processKeystreamEqns().

int matrices_generation_getRealVariableIndex ( unsigned int  Reg,
unsigned int  index 
)

Processes the correct location of a variable inside a LFSR from the variable index.

Only variables were recorded in equations, thus there is a small gap in the location of the constant "1" in the LFSR. This function is mandatory to allow a transparent access to the array.

Parameters:
[in]RegConsidered register id. May be (1), (2) or (3)
[in]indexVariable index
Returns:
Fixed-up location inside the LFSR

References DEBUG, R1_INITIAL_CONST_POS, R2_INITIAL_CONST_POS, and R3_INITIAL_CONST_POS.

Referenced by matrices_generation_solveEquation().

void * matrices_generation_launchExport ( void *  data)

Thread initializer.

Parameters:
[in]dataPointer to the thread's arguments
Returns:
NULL

References GenerationArgs::filename, GenerationArgs::highindex, GenerationArgs::lowindex, and matrices_generation_exportMatrices().

Referenced by exportAllMatrices().

void matrices_generation_processKeystreamEqns ( const byte  initialR4[],
byte  keystream[][REGS_TOTAL_VARS],
const int  len 
)

Generates the desired amount of keystream dependancy equations (skipping first 99cycles) considering one initial R4 state.

Parameters:
[in]initialR4Inferred initial value of the fourth LFSR
[out]keystreamKeystream dependancy equations to be generated
[in]lenDesired amount of keystream dependancy equations

References DEBUG, matrices_generation_clockingUnit(), matrices_generation_getOutBit(), R1, R1_BITS, R1_INITIAL_CONST_POS, R1_SIMPLE_VARS, R2, R2_BITS, R2_INITIAL_CONST_POS, R2_SIMPLE_VARS, R3, R3_BITS, R3_INITIAL_CONST_POS, R3_SIMPLE_VARS, R4, R4_BITS, R4_INITIAL_CONST_POS, and REGS_TOTAL_VARS.

Referenced by matrices_generation_exportMatrices(), and matrices_generation_test().

byte matrices_generation_solveEquation ( byte  eqn[],
byte  R1[],
byte  R2[],
byte  R3[] 
)

Returns the result of the evaluation of an expression with the given values of initial variables (for test purpose)

Parameters:
[in]eqnConsidered expression
[in]R1Contents of the first LFSR
[in]R2Contents of the second LFSR
[in]R3Contents of the third LFSR
Returns:
Value of the expression described by eqn, after substituting LFSR values for variables

References matrices_generation_getRealVariableIndex(), R1_SIMPLE_VARS, R2_SIMPLE_VARS, R3_SIMPLE_VARS, REGS_SIMPLE_VARS, and REGS_TOTAL_VARS.

Referenced by matrices_generation_test().

int matrices_generation_test ( )

Autotests the matrices generation on a verified set.

Returns:
0 if the test is successfull, non-zero otherwise

References DEBUG, FRAMEID_BITS, getKeystream(), keysetup(), matrices_generation_processKeystreamEqns(), matrices_generation_solveEquation(), R1, R1_BITS, R2, R2_BITS, R3, R3_BITS, R4_BITS, REGS_TOTAL_VARS, and SECRETKEY_BITS.

Referenced by main().