A52HackTool 1.3.0
Turnkey & easy to use tool for cracking the GSM A5/2 cipher
utils.h
Go to the documentation of this file.
00001 /*============================================================================*
00002  *                                                                            *
00003  *                                   utils.h                                  *
00004  *                                                                            *
00005  *============================================================================*
00006  *                                                                            *
00007  * Part of A52HackTool                                                        *
00008  *                                                                            *
00009  * Copyright © 2011   -   Nicolas Paglieri   &   Olivier Benjamin             *
00010  * All rights reserved.                                                       *
00011  *                                                                            *
00012  * Contact Information:  nicolas.paglieri [at] ensimag.fr                     *
00013  *                       olivier.benjamin [at] ensimag.fr                     *
00014  *                                                                            *
00015  *============================================================================*
00016  *                                                                            *
00017  * This file may be used under the terms of the GNU General Public License    *
00018  * version 3 as published by the Free Software Foundation.                    *
00019  * See <http://www.gnu.org/licenses/> or GPL.txt included in the packaging of *
00020  * this file.                                                                 *
00021  *                                                                            *
00022  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE    *
00023  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  *
00024  *                                                                            *
00025  *============================================================================*/
00026 
00037 #ifndef _UTILS_H_
00038 #define _UTILS_H_
00039 
00040 
00042 typedef unsigned char byte;
00043 
00044 
00054 int stringToByteArray(char* s, byte a[], unsigned int len);
00055 
00056 
00064 int fileExists(const char * filename);
00065 
00066 
00067 
00068 #include <sys/time.h>
00079 long long timeval_diff(struct timeval *difference, struct timeval *end_time, struct timeval *start_time);
00080 
00081 
00082 
00084 #define DEBUG_INFO 0
00085 
00086 
00088 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
00089 
00090 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
00091 
00092 
00094 #define MSBIT(i) (((i) & 0x80000000) >> 31)
00095 
00096 
00097 // Operations on compact bit storage (32bit-integer array, every bit is meaningful)
00099 #define SET_INTARRAY_BIT(a,i,bit)                                                             \
00100         do {                                                                                  \
00101             unsigned int __i__ = (i);                                                         \
00102             byte __tmp__ = (bit)&1;                                                           \
00103             (a)[__i__/32] &= ~(1 << (31-(__i__%32)));                                         \
00104             (a)[__i__/32] |= ((__tmp__) << (31-(__i__%32)));                                  \
00105         } while (0)
00106 
00107 #define GET_INTARRAY_BIT(a,i)                                                                 \
00108         (((int)(i)<0) ? 0 : ((a)[(i)/32] >> (31-((i)%32)))&1)
00109 
00110 
00111 // Operations on compact bit storage (8bit-char array, every bit is meaningful)
00113 #define SET_CHARARRAY_BIT(a,i,bit)                                                            \
00114         do {                                                                                  \
00115             unsigned int __i__ = (i);                                                         \
00116             byte __tmp__ = (bit)&1;                                                           \
00117             (a)[__i__/8] &= ~(1 << (7-(__i__%8)));                                            \
00118             (a)[__i__/8] |= ((__tmp__) << (7-(__i__%8)));                                     \
00119         } while (0)
00120 
00121 #define GET_CHARARRAY_BIT(a,i)                                                                \
00122         (((int)(i)<0) ? 0 : ((a)[(i)/8] >> (7-((i)%8)))&1)
00123 
00124 
00126 #define XOR_CHARARRAYS(V1, V2, len)                                                           \
00127         do {                                                                                  \
00128             unsigned int __len__ = (len);                                                     \
00129             for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
00130                 (V1)[__i__] ^= (V2)[__i__];                                                   \
00131             }                                                                                 \
00132         } while (0)
00133 
00134 
00136 #define MAJORITY(bit1, bit2, bit3)                                                            \
00137         ( ( ((bit1)!=0) + ((bit2)!=0) + ((bit3)!=0) >= 2 ) & 1 )
00138 
00139 
00141 #define DUMP_CHAR_MATRIX(M, lines, cols, name)                                                \
00142         do {                                                                                  \
00143             unsigned int __lines__ = (lines);                                                 \
00144             unsigned int __cols__  = (cols);                                                  \
00145             printf("[%s: %s, l.%u] Matrix " name "\n", __FILE__,  __func__, __LINE__);        \
00146             for (unsigned int __i__=0 ; __i__<__lines__ ; ++__i__) {                          \
00147                 for (unsigned int __j__=0 ; __j__<__cols__ ; ++__j__) {                       \
00148                     printf("%d", (M)[__i__][__j__]);                                          \
00149                 }                                                                             \
00150                 printf("\n");                                                                 \
00151             }                                                                                 \
00152         } while (0)
00153 
00154 
00156 #define DUMP_CHAR_VECTOR(V, len, name)                                                        \
00157         do {                                                                                  \
00158             unsigned int __len__ = (len);                                                     \
00159             printf("[%s: %s, l.%u] Vector " name "\n", __FILE__,  __func__, __LINE__);        \
00160             for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
00161                 printf("%d", (V)[__i__]);                                                     \
00162             }                                                                                 \
00163             printf("\n");                                                                     \
00164         } while (0)
00165 
00166 
00168 #define DUMP_INT_VECTOR(V, len, name)                                                         \
00169         do {                                                                                  \
00170             unsigned int __len__ = (len);                                                     \
00171             printf("[%s: %s, l.%u] Vector " name "\n", __FILE__,  __func__, __LINE__);        \
00172             for (unsigned int __i__=0 ; __i__<__len__ ; ++__i__) {                            \
00173                 printf("%08X\n", (V)[__i__]);                                                 \
00174             }                                                                                 \
00175         } while (0)
00176 
00177 
00179 #define DEBUG(msg, ...)                                                                       \
00180     do                                                                                        \
00181         printf("[%s: %s, l.%u] " msg "\n", __FILE__,                                          \
00182                                            __func__,                                          \
00183                                            __LINE__,                                          \
00184                                            ##__VA_ARGS__);                                    \
00185     while (0)
00186 
00187 
00189 #define DEBUG_LF                                                                              \
00190     do                                                                                        \
00191         printf("\n");                                                                         \
00192     while (0)
00193 
00194 
00196 #define CHAR_IDENTITY(I, size)                                                                \
00197     do {                                                                                      \
00198         unsigned int __size__ = (size);                                                       \
00199         memset((I), 0, __size__*__size__*sizeof(char));                                       \
00200         for(unsigned int __i__=0 ; __i__<__size__ ; ++__i__){                                 \
00201             (I)[__i__][__i__]=1;                                                              \
00202         }                                                                                     \
00203      } while(0)
00204 
00205 
00209 #define BINPRODUCT_MATRIX_MATRIX(M1, M2, PROD, PROD_lines, PROD_cols, depth)                 \
00210         do {                                                                                 \
00211             unsigned int __PROD_lines__ = (PROD_lines);                                      \
00212             unsigned int __PROD_cols__ = (PROD_cols);                                        \
00213             unsigned int __depth__ = (depth);                                                \
00214             memset((PROD), 0, __PROD_lines__*__PROD_cols__*sizeof(byte));                    \
00215             for (unsigned int __line__=0 ; __line__<__PROD_lines__ ; ++__line__) {           \
00216                 for (unsigned int __col__=0 ; __col__<__PROD_cols__ ; ++__col__) {           \
00217                     for (unsigned int __i__=0 ; __i__<__depth__ ; ++__i__) {                 \
00218                         (PROD)[__line__][__col__] ^= ((M1)[__line__][__i__])                 \
00219                                                  & ((M2)[__i__][__col__])  & 1;              \
00220                     }                                                                        \
00221                 }                                                                            \
00222             }                                                                                \
00223         } while (0)
00224 
00225 
00228 #define BINPRODUCT_MATRIX_VECTOR(M, V, PROD, M_lines, M_cols)                                \
00229         do {                                                                                 \
00230             unsigned int __M_lines__ = (M_lines);                                            \
00231             unsigned int __M_cols__ = (M_cols);                                              \
00232             memset((PROD), 0, __M_lines__*sizeof(byte));                                     \
00233             for (unsigned int __line__=0 ; __line__<__M_lines__ ; ++__line__) {              \
00234                 for (unsigned int __i__=0 ; __i__<__M_cols__ ; ++__i__) {                    \
00235                     (PROD)[__line__] ^= ((M)[__line__][__i__]) & ((V)[__i__]) & 1;           \
00236                 }                                                                            \
00237             }                                                                                \
00238         } while (0)
00239 
00240 
00243 #define BINPRODUCT_VECTOR_MATRIX(V, M, PROD, M_lines, M_cols)                                \
00244         do {                                                                                 \
00245             unsigned int __M_lines__ = (M_lines);                                            \
00246             unsigned int __M_cols__ = (M_cols);                                              \
00247             memset((PROD), 0, __M_cols__*sizeof(byte));                                      \
00248             for (unsigned int __col__=0 ; __col__<__M_cols__ ; ++__col__) {                  \
00249                 for (unsigned int __i__=0 ; __i__<__M_lines__ ; ++__i__) {                   \
00250                     (PROD)[__col__] ^= ((M)[__i__][__col__]) & ((V)[__i__]) & 1;             \
00251                 }                                                                            \
00252             }                                                                                \
00253         } while (0)
00254 
00255 
00256 //(CHAR_V_size+31)/32 ensures that the size is right even if CHAR_V_size is not a multiple of 32
00258 #define CHAR_VECTOR_TO_INT_VECTOR(CHAR_V, INT_V, CHAR_V_size)                                \
00259         do {                                                                                 \
00260             unsigned int __CHAR_V_size__ = (CHAR_V_size);                                    \
00261             memset((INT_V), 0, ((__CHAR_V_size__+31)/32)*sizeof(unsigned int));              \
00262             for (unsigned int __k__=0 ; __k__<__CHAR_V_size__ ; ++__k__) {                   \
00263                 SET_INTARRAY_BIT((INT_V), __k__, ((CHAR_V)[__k__]));                         \
00264             }                                                                                \
00265         } while (0)
00266 
00267 
00269 #define INT_VECTOR_TO_CHAR_VECTOR(INT_V, CHAR_V, CHAR_V_size)                                \
00270         do {                                                                                 \
00271             unsigned int __CHAR_V_size__ = (CHAR_V_size);                                    \
00272             memset((CHAR_V), 0, __CHAR_V_size__*sizeof(byte));                               \
00273             for (unsigned int __k__=0 ; __k__<__CHAR_V_size__ ; ++__k__) {                   \
00274                 (CHAR_V)[__k__] = GET_INTARRAY_BIT((INT_V), __k__);                          \
00275             }                                                                                \
00276         } while (0)
00277 
00278 
00280 #define BIT_VECTOR_TO_BYTE_VECTOR(BIT_V, BYTE_V, BIT_V_size)                                 \
00281         do {                                                                                 \
00282             unsigned int __BIT_V_size__ = (BIT_V_size);                                      \
00283             memset((BYTE_V), 0, ((__BIT_V_size__+7)/8)*sizeof(byte));                        \
00284             for (unsigned int __k__=0 ; __k__<__BIT_V_size__ ; ++__k__) {                    \
00285                 SET_CHARARRAY_BIT((BYTE_V), __k__, ((BIT_V)[__k__]));                        \
00286             }                                                                                \
00287         } while (0)
00288 
00289 
00291 #define BYTE_VECTOR_TO_BIT_VECTOR(BYTE_V, BIT_V, BIT_V_size)                                 \
00292         do {                                                                                 \
00293             unsigned int __BIT_V_size__ = (BIT_V_size);                                      \
00294             memset((BIT_V), 0, __BIT_V_size__*sizeof(byte));                                 \
00295             for (unsigned int __k__=0 ; __k__<__BIT_V_size__ ; ++__k__) {                    \
00296                 (BIT_V)[__k__] = GET_CHARARRAY_BIT((BYTE_V), __k__);                         \
00297             }                                                                                \
00298         } while (0)
00299 
00300 
00302 #define PROGRESSBAR(percent)                                                                \
00303         do {                                                                                \
00304             printf("[");                                                                    \
00305             unsigned int __percent__ = (percent);                                           \
00306             unsigned int __steps__   = __percent__*20/100;                                  \
00307             for (unsigned int __i__=0 ; __i__<__steps__ ; ++__i__)                          \
00308                 printf("=");                                                                \
00309             for (unsigned int __i__=0 ; __i__<20-__steps__ ; ++__i__)                       \
00310                 printf("-");                                                                \
00311             printf("] %d%%\n", __percent__);                                                \
00312         } while (0)
00313 
00314 
00316 #define PROGRESSBAR_NOLF(percent)                                                           \
00317         do {                                                                                \
00318             printf("[");                                                                    \
00319             unsigned int __percent__ = (percent);                                           \
00320             unsigned int __steps__   = __percent__*20/100;                                  \
00321             for (unsigned int __i__=0 ; __i__<__steps__ ; ++__i__)                          \
00322                 printf("=");                                                                \
00323             for (unsigned int __i__=0 ; __i__<20-__steps__ ; ++__i__)                       \
00324                 printf("-");                                                                \
00325             printf("] %d%%", __percent__);                                                  \
00326         } while (0)
00327 
00328 
00330 #define CLEARLINE                                                                           \
00331         do {                                                                                \
00332             for (int __c__=0 ; __c__<80 ; ++__c__) printf("\b");                            \
00333         } while (0)
00334 
00335 
00336 
00337 
00338 #endif