A52HackTool 1.3.0
Turnkey & easy to use tool for cracking the GSM A5/2 cipher
Defines | Typedefs | Functions
utils.h File Reference

Utilitary tools & Macros. More...

#include <sys/time.h>

Go to the source code of this file.

Defines

#define DEBUG_INFO   0
 Debug Flag. Set to non-zero to activate debug info display.
#define MIN(a, b)   (((a)<(b)) ? (a) : (b))
 Standard min math function, returns the minimum of a and b.
#define MAX(a, b)   (((a)>(b)) ? (a) : (b))
 Standard max math function, returns the maximum of a and b.
#define MSBIT(i)   (((i) & 0x80000000) >> 31)
 Returns the most significant bit of a given 32bit-Integer i.
#define SET_INTARRAY_BIT(a, i, bit)
 Sets the i-th bit of the compact int storage array a (32bit per int) to the value bit.
#define GET_INTARRAY_BIT(a, i)   (((int)(i)<0) ? 0 : ((a)[(i)/32] >> (31-((i)%32)))&1)
 Gets the i-th bit of the compact int storage array a (32bit per int)
#define SET_CHARARRAY_BIT(a, i, bit)
 Sets the i-th bit of the compact byte storage array a (8bit per byte) to the value bit.
#define GET_CHARARRAY_BIT(a, i)   (((int)(i)<0) ? 0 : ((a)[(i)/8] >> (7-((i)%8)))&1)
 Gets the i-th bit of the compact byte storage array a (8bit per byte)
#define XOR_CHARARRAYS(V1, V2, len)
 XOR operation on two byte vectors V1 & V2 of length len, returning the result in V1.
#define MAJORITY(bit1, bit2, bit3)   ( ( ((bit1)!=0) + ((bit2)!=0) + ((bit3)!=0) >= 2 ) & 1 )
 Returns 1 if and only if at least 2 bits out of 3 are non-zero.
#define DUMP_CHAR_MATRIX(M, lines, cols, name)
 Displays a char matrix M of lines lines and cols columns, labelled name.
#define DUMP_CHAR_VECTOR(V, len, name)
 Displays a char vector V of len elements, labelled name (single-line display)
#define DUMP_INT_VECTOR(V, len, name)
 Displays a int vector V of len elements, labelled name (multi-line display)
#define DEBUG(msg,...)
 Displays some debug (includes file name and line, along with parent function)
#define DEBUG_LF
 Simply prints a new line.
#define CHAR_IDENTITY(I, size)
 Initializes Matrix I to Identity matrix of size size.
#define BINPRODUCT_MATRIX_MATRIX(M1, M2, PROD, PROD_lines, PROD_cols, depth)
 Processes the result of binary char matrix multiplication (M1 × M2) into PROD. PROD_lines represent the result line size, PROD_cols represent the result col size, depth represent the common dimension of M1 and M2.
#define BINPRODUCT_MATRIX_VECTOR(M, V, PROD, M_lines, M_cols)
 Processes the result of binary char matrix vector multiplication (M × V) into PROD. M_lines represent the matrix line size, M_cols represent the matrix col size.
#define BINPRODUCT_VECTOR_MATRIX(V, M, PROD, M_lines, M_cols)
 Processes the result of binary char vector matrix multiplication (V × M) into PROD. M_lines represent the matrix line size, M_cols represent the matrix col size.
#define CHAR_VECTOR_TO_INT_VECTOR(CHAR_V, INT_V, CHAR_V_size)
 Translates a sparse-byte representation (1bit per byte) into a full-integer one (32bit per int)
#define INT_VECTOR_TO_CHAR_VECTOR(INT_V, CHAR_V, CHAR_V_size)
 Translates a full-integer representation (32bit per int) into a sparse-byte one (1bit per byte)
#define BIT_VECTOR_TO_BYTE_VECTOR(BIT_V, BYTE_V, BIT_V_size)
 Translates a sparse-byte representation (1bit per byte) into a full-byte one (8bit per byte)
#define BYTE_VECTOR_TO_BIT_VECTOR(BYTE_V, BIT_V, BIT_V_size)
 Translates a full-byte representation (8bit per byte) into a sparse-byte one (1bit per byte)
#define PROGRESSBAR(percent)
 Displays a console progress bar (single line, with ending LF)
#define PROGRESSBAR_NOLF(percent)
 Displays a console progress bar (single line, without ending LF)
#define CLEARLINE
 Erases the last 80 characters on the current console line.

Typedefs

typedef unsigned char byte
 Type byte is defined here to improve portability.

Functions

int stringToByteArray (char *s, byte a[], unsigned int len)
 Translates a string composed of "0"s or "1"s into a byte array.
int fileExists (const char *filename)
 Checks the accessibility of a file for reading.
long long timeval_diff (struct timeval *difference, struct timeval *end_time, struct timeval *start_time)
 Computes the difference between two time values.

Detailed Description

Utilitary tools & Macros.

Author:
Nicolas Paglieri & Olivier Benjamin
Version:
File Revision #20
Date:
24/06/2011

Define Documentation

#define BINPRODUCT_MATRIX_MATRIX (   M1,
  M2,
  PROD,
  PROD_lines,
  PROD_cols,
  depth 
)
Value:
do {                                                                                 \
            unsigned int __PROD_lines__ = (PROD_lines);                                      \
            unsigned int __PROD_cols__ = (PROD_cols);                                        \
            unsigned int __depth__ = (depth);                                                \
            memset((PROD), 0, __PROD_lines__*__PROD_cols__*sizeof(byte));                    \
            for (unsigned int __line__=0 ; __line__<__PROD_lines__ ; ++__line__) {           \
                for (unsigned int __col__=0 ; __col__<__PROD_cols__ ; ++__col__) {           \
                    for (unsigned int __i__=0 ; __i__<__depth__ ; ++__i__) {                 \
                        (PROD)[__line__][__col__] ^= ((M1)[__line__][__i__])                 \
                                                 & ((M2)[__i__][__col__])  & 1;              \
                    }                                                                        \
                }                                                                            \
            }                                                                                \
        } while (0)

Processes the result of binary char matrix multiplication (M1 × M2) into PROD. PROD_lines represent the result line size, PROD_cols represent the result col size, depth represent the common dimension of M1 and M2.

Referenced by matrices_generation_exportMatrices(), and processFullEncodingHMatrix().

#define BINPRODUCT_MATRIX_VECTOR (   M,
  V,
  PROD,
  M_lines,
  M_cols 
)
Value:
do {                                                                                 \
            unsigned int __M_lines__ = (M_lines);                                            \
            unsigned int __M_cols__ = (M_cols);                                              \
            memset((PROD), 0, __M_lines__*sizeof(byte));                                     \
            for (unsigned int __line__=0 ; __line__<__M_lines__ ; ++__line__) {              \
                for (unsigned int __i__=0 ; __i__<__M_cols__ ; ++__i__) {                    \
                    (PROD)[__line__] ^= ((M)[__line__][__i__]) & ((V)[__i__]) & 1;           \
                }                                                                            \
            }                                                                                \
        } while (0)

Processes the result of binary char matrix vector multiplication (M × V) into PROD. M_lines represent the matrix line size, M_cols represent the matrix col size.

Referenced by attack_decipherSecretKey().

#define BINPRODUCT_VECTOR_MATRIX (   V,
  M,
  PROD,
  M_lines,
  M_cols 
)
Value:
do {                                                                                 \
            unsigned int __M_lines__ = (M_lines);                                            \
            unsigned int __M_cols__ = (M_cols);                                              \
            memset((PROD), 0, __M_cols__*sizeof(byte));                                      \
            for (unsigned int __col__=0 ; __col__<__M_cols__ ; ++__col__) {                  \
                for (unsigned int __i__=0 ; __i__<__M_lines__ ; ++__i__) {                   \
                    (PROD)[__col__] ^= ((M)[__i__][__col__]) & ((V)[__i__]) & 1;             \
                }                                                                            \
            }                                                                                \
        } while (0)

Processes the result of binary char vector matrix multiplication (V × M) into PROD. M_lines represent the matrix line size, M_cols represent the matrix col size.

Referenced by attack_test(), code_test(), and main().

#define BIT_VECTOR_TO_BYTE_VECTOR (   BIT_V,
  BYTE_V,
  BIT_V_size 
)
Value:
do {                                                                                 \
            unsigned int __BIT_V_size__ = (BIT_V_size);                                      \
            memset((BYTE_V), 0, ((__BIT_V_size__+7)/8)*sizeof(byte));                        \
            for (unsigned int __k__=0 ; __k__<__BIT_V_size__ ; ++__k__) {                    \
                SET_CHARARRAY_BIT((BYTE_V), __k__, ((BIT_V)[__k__]));                        \
            }                                                                                \
        } while (0)

Translates a sparse-byte representation (1bit per byte) into a full-byte one (8bit per byte)

Referenced by main().

#define BYTE_VECTOR_TO_BIT_VECTOR (   BYTE_V,
  BIT_V,
  BIT_V_size 
)
Value:
do {                                                                                 \
            unsigned int __BIT_V_size__ = (BIT_V_size);                                      \
            memset((BIT_V), 0, __BIT_V_size__*sizeof(byte));                                 \
            for (unsigned int __k__=0 ; __k__<__BIT_V_size__ ; ++__k__) {                    \
                (BIT_V)[__k__] = GET_CHARARRAY_BIT((BYTE_V), __k__);                         \
            }                                                                                \
        } while (0)

Translates a full-byte representation (8bit per byte) into a sparse-byte one (1bit per byte)

Referenced by main().

#define CHAR_IDENTITY (   I,
  size 
)
Value:
do {                                                                                      \
        unsigned int __size__ = (size);                                                       \
        memset((I), 0, __size__*__size__*sizeof(char));                                       \
        for(unsigned int __i__=0 ; __i__<__size__ ; ++__i__){                                 \
            (I)[__i__][__i__]=1;                                                              \
        }                                                                                     \
     } while(0)

Initializes Matrix I to Identity matrix of size size.

Referenced by InvertMatrixL(), and processFullEncodingGSystematicMatrix().

#define CHAR_VECTOR_TO_INT_VECTOR (   CHAR_V,
  INT_V,
  CHAR_V_size 
)
Value:
do {                                                                                 \
            unsigned int __CHAR_V_size__ = (CHAR_V_size);                                    \
            memset((INT_V), 0, ((__CHAR_V_size__+31)/32)*sizeof(unsigned int));              \
            for (unsigned int __k__=0 ; __k__<__CHAR_V_size__ ; ++__k__) {                   \
                SET_INTARRAY_BIT((INT_V), __k__, ((CHAR_V)[__k__]));                         \
            }                                                                                \
        } while (0)

Translates a sparse-byte representation (1bit per byte) into a full-integer one (32bit per int)

#define CLEARLINE
Value:
do {                                                                                \
            for (int __c__=0 ; __c__<80 ; ++__c__) printf("\b");                            \
        } while (0)

Erases the last 80 characters on the current console line.

Referenced by main().

#define DEBUG (   msg,
  ... 
)
Value:
do                                                                                        \
        printf("[%s: %s, l.%u] " msg "\n", __FILE__,                                          \
                                           __func__,                                          \
                                           __LINE__,                                          \
                                           ##__VA_ARGS__);                                    \
    while (0)

Displays some debug (includes file name and line, along with parent function)

Referenced by attack(), attack_test(), code_test(), exportAllMatrices(), initializeRAM(), keygen_clockReg(), keygen_test(), keysetup_reverse_test(), matrices_generation_doubleProduct(), matrices_generation_exportMatrices(), matrices_generation_getRealVariableIndex(), matrices_generation_processKeystreamEqns(), matrices_generation_test(), and reverseKeysetup().

#define DEBUG_LF
Value:
do                                                                                        \
        printf("\n");                                                                         \
    while (0)

Simply prints a new line.

Referenced by attack_test().

#define DUMP_CHAR_MATRIX (   M,
  lines,
  cols,
  name 
)
Value:
do {                                                                                  \
            unsigned int __lines__ = (lines);                                                 \
            unsigned int __cols__  = (cols);                                                  \
            printf("[%s: %s, l.%u] Matrix " name "\n", __FILE__,  __func__, __LINE__);        \
            for (unsigned int __i__=0 ; __i__<__lines__ ; ++__i__) {                          \
                for (unsigned int __j__=0 ; __j__<__cols__ ; ++__j__) {                       \
                    printf("%d", (M)[__i__][__j__]);                                          \
                }                                                                             \
                printf("\n");                                                                 \
            }                                                                                 \
        } while (0)

Displays a char matrix M of lines lines and cols columns, labelled name.

#define DUMP_CHAR_VECTOR (   V,
  len,
  name 
)
Value:
do {                                                                                  \
            unsigned int __len__ = (len);                                                     \
            printf("[%s: %s, l.%u] Vector " name "\n", __FILE__,  __func__, __LINE__);        \
            for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
                printf("%d", (V)[__i__]);                                                     \
            }                                                                                 \
            printf("\n");                                                                     \
        } while (0)

Displays a char vector V of len elements, labelled name (single-line display)

Referenced by attack(), attack_test(), code_test(), keysetup_reverse_test(), and reverseFire().

#define DUMP_INT_VECTOR (   V,
  len,
  name 
)
Value:
do {                                                                                  \
            unsigned int __len__ = (len);                                                     \
            printf("[%s: %s, l.%u] Vector " name "\n", __FILE__,  __func__, __LINE__);        \
            for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
                printf("%08X\n", (V)[__i__]);                                                 \
            }                                                                                 \
        } while (0)

Displays a int vector V of len elements, labelled name (multi-line display)

#define INT_VECTOR_TO_CHAR_VECTOR (   INT_V,
  CHAR_V,
  CHAR_V_size 
)
Value:
do {                                                                                 \
            unsigned int __CHAR_V_size__ = (CHAR_V_size);                                    \
            memset((CHAR_V), 0, __CHAR_V_size__*sizeof(byte));                               \
            for (unsigned int __k__=0 ; __k__<__CHAR_V_size__ ; ++__k__) {                   \
                (CHAR_V)[__k__] = GET_INTARRAY_BIT((INT_V), __k__);                          \
            }                                                                                \
        } while (0)

Translates a full-integer representation (32bit per int) into a sparse-byte one (1bit per byte)

#define PROGRESSBAR (   percent)
Value:
do {                                                                                \
            printf("[");                                                                    \
            unsigned int __percent__ = (percent);                                           \
            unsigned int __steps__   = __percent__*20/100;                                  \
            for (unsigned int __i__=0 ; __i__<__steps__ ; ++__i__)                          \
                printf("=");                                                                \
            for (unsigned int __i__=0 ; __i__<20-__steps__ ; ++__i__)                       \
                printf("-");                                                                \
            printf("] %d%%\n", __percent__);                                                \
        } while (0)

Displays a console progress bar (single line, with ending LF)

Referenced by attack_decipherSecretKey(), and matrices_generation_exportMatrices().

#define PROGRESSBAR_NOLF (   percent)
Value:
do {                                                                                \
            printf("[");                                                                    \
            unsigned int __percent__ = (percent);                                           \
            unsigned int __steps__   = __percent__*20/100;                                  \
            for (unsigned int __i__=0 ; __i__<__steps__ ; ++__i__)                          \
                printf("=");                                                                \
            for (unsigned int __i__=0 ; __i__<20-__steps__ ; ++__i__)                       \
                printf("-");                                                                \
            printf("] %d%%", __percent__);                                                  \
        } while (0)

Displays a console progress bar (single line, without ending LF)

Referenced by main().

#define SET_CHARARRAY_BIT (   a,
  i,
  bit 
)
Value:
do {                                                                                  \
            unsigned int __i__ = (i);                                                         \
            byte __tmp__ = (bit)&1;                                                           \
            (a)[__i__/8] &= ~(1 << (7-(__i__%8)));                                            \
            (a)[__i__/8] |= ((__tmp__) << (7-(__i__%8)));                                     \
        } while (0)

Sets the i-th bit of the compact byte storage array a (8bit per byte) to the value bit.

#define SET_INTARRAY_BIT (   a,
  i,
  bit 
)
Value:
do {                                                                                  \
            unsigned int __i__ = (i);                                                         \
            byte __tmp__ = (bit)&1;                                                           \
            (a)[__i__/32] &= ~(1 << (31-(__i__%32)));                                         \
            (a)[__i__/32] |= ((__tmp__) << (31-(__i__%32)));                                  \
        } while (0)

Sets the i-th bit of the compact int storage array a (32bit per int) to the value bit.

#define XOR_CHARARRAYS (   V1,
  V2,
  len 
)
Value:
do {                                                                                  \
            unsigned int __len__ = (len);                                                     \
            for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
                (V1)[__i__] ^= (V2)[__i__];                                                   \
            }                                                                                 \
        } while (0)

XOR operation on two byte vectors V1 & V2 of length len, returning the result in V1.

Referenced by attack_test(), keysetup_reverse_clockRegs(), main(), matrices_generation_clockR1(), matrices_generation_clockR2(), matrices_generation_clockR3(), matrices_generation_getOutBit(), and reverseKeysetup().


Function Documentation

int fileExists ( const char *  filename)

Checks the accessibility of a file for reading.

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

Referenced by main().

int stringToByteArray ( char *  s,
byte  a[],
unsigned int  len 
)

Translates a string composed of "0"s or "1"s into a byte array.

Parameters:
[in]sInput string
[out]aArray to construct
[in]lenLength of the string
Returns:
0 if the translation has been successful, non-zero otherwise

Referenced by main().

long long timeval_diff ( struct timeval *  difference,
struct timeval *  end_time,
struct timeval *  start_time 
)

Computes the difference between two time values.

Parameters:
[out]differenceProcessed difference
[in]end_timeEnd time
[in]start_timeStart time
Returns:
time difference in microseconds