Bunny2.0
Headers/myFunctions.h
00001 #ifndef MY_FUNCTIONS_H
00002 #define MY_FUNCTIONS_H
00003 
00004 
00005 //#include "Bunny.h" //definition of the class Bunny
00006 
00007 using namespace std ;
00008 //using namespace Bunny<nb_msg,nb_key,nb_sbox,nround> ;
00009 
00010 #include <stdint.h>
00011 
00012 static uint8_t charToNum[128] = {
00013 //funziona sul mio compilatore (LINUX) ??
00014 // 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
00015 // 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16
00016 // 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32
00017 // 0,  1,  2,  3,  4,  5,  6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // 48
00018 // 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64
00019 // 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
00020 // 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96
00021 // 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00022 
00023 //Funziona sul compilatore di enrico (MAC) ??
00024  0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
00025  0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16
00026  0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32
00027  0,  1,  2,  3,  4,  5,  6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // 48
00028  0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64
00029  0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
00030  0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96
00031  0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00032 
00033 } ;
00034 
00035 static char numToChar[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
00036                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' } ;
00037 
00039 
00043 void
00044 stringToUcharB( string const & s, 
00045                uint8_t * c       
00046              ) {
00047   for ( unsigned i = 0 ; i < s.length() ; i += 2 ){
00048     *c++ = (charToNum[s[i]&0x3F]<<4) | charToNum[s[i+1]&0x3F]  ;
00049   }
00050 }
00051 
00052 void stringToUchar (string s, unsigned char c[])
00053 {
00054         unsigned d ;
00055 
00056         for (unsigned i = 0 ; i < s.length() ; i = i + 2){
00057                 switch( s[i] ){
00058                 case '0': d = 0 ;
00059                         break ;
00060                 case '1':  d = 1 * 16 ;
00061                         break ;
00062                 case '2':  d = 2 * 16 ;
00063                         break ;
00064                 case '3':  d = 3 * 16 ;
00065                         break ;
00066                 case '4':  d = 4 * 16 ;
00067                         break ;
00068                 case '5':  d = 5 * 16 ;
00069                         break ;
00070                 case '6':  d = 6 * 16 ;
00071                         break ;
00072                 case '7':  d = 7 * 16 ;
00073                         break ;
00074                 case '8':  d = 8 * 16 ;
00075                         break ;
00076                 case '9':  d = 9 * 16 ;
00077                         break ;
00078                 case 'a':  d = 10 * 16 ;
00079                         break ;
00080                 case 'b':  d = 11 * 16 ;
00081                         break ;
00082                 case 'c':  d = 12 * 16 ;
00083                         break ;
00084                 case 'd':  d = 13 * 16 ;
00085                         break ;
00086                 case 'e':  d = 14 * 16 ;
00087                         break ;
00088                 case 'f':  d = 15 * 16 ;
00089                         break ;
00090           }
00091           switch( s[i+1] ){
00092                 case '0': d = d + 0 ;
00093                         break ;
00094                 case '1':  d = d + 1 ;
00095                         break ;
00096                 case '2':  d = d + 2 ;
00097                         break ;
00098                 case '3':  d = d + 3 ;
00099                         break ;
00100                 case '4':  d = d + 4 ;
00101                         break ;
00102                 case '5':  d = d + 5 ;
00103                         break ;
00104                 case '6':  d = d + 6 ;
00105                         break ;
00106                 case '7':  d = d + 7 ;
00107                         break ;
00108                 case '8':  d = d + 8 ;
00109                         break ;
00110                 case '9':  d = d + 9 ;
00111                         break ;
00112                 case 'a':  d = d + 10 ;
00113                         break ;
00114                 case 'b':  d = d + 11 ;
00115                         break ;
00116                 case 'c':  d = d + 12 ;
00117                         break ;
00118                 case 'd':  d = d + 13 ;
00119                         break ;
00120                 case 'e':  d = d + 14 ;
00121                         break ;
00122                 case 'f':  d = d + 15 ;
00123                         break ;
00124           }
00125                 c[i / 2] = d ;
00126         }
00127 }
00128 
00129 
00131 
00136 template <std::size_t N>
00137 inline
00138 std::bitset<N>
00139 rotLeft(std::bitset<N>& b, unsigned m)
00140 {
00141         b = b << m | b >> (N-m);
00142         return b ;
00143 }
00144 
00145 
00147 
00156 template <std::size_t N>
00157 inline
00158 std::bitset<N>
00159 MoveBits(std::bitset<N>& b, unsigned pos1, unsigned pos2, unsigned nbits)
00160 {
00161         bool tmp ;
00162         for (unsigned i = 0 ; i < nbits ; ++i){
00163                 tmp = b[pos1+i] ;
00164                 b[pos1+i] = b[pos2+i] ;
00165                 b[pos2+i] = tmp ;
00166         }
00167         return b ;
00168 }
00169 
00170 
00171 
00173 
00178 inline
00179 bitset<128> hexToBitset128(string c){
00180         //unsigned l = c.length() ;
00181         bitset <128> b ;
00182         string tmp ;
00183         for (unsigned i = 0 ; i < c.length() ; ++i){
00184           switch( c[i] ){
00185                 case '0':  tmp.append("0000") ;
00186                         break ;
00187                 case '1':  tmp.append("0001") ;
00188                         break ;
00189                 case '2':  tmp.append("0010") ;
00190                         break ;
00191                 case '3':  tmp.append("0011") ;
00192                         break ;
00193                 case '4':  tmp.append("0100") ;
00194                         break ;
00195                 case '5':  tmp.append("0101") ;
00196                         break ;
00197                 case '6':  tmp.append("0110") ;
00198                         break ;
00199                 case '7':  tmp.append("0111") ;
00200                         break ;
00201                 case '8':  tmp.append("1000") ;
00202                         break ;
00203                 case '9':  tmp.append("1001") ;
00204                         break ;
00205                 case 'a':  tmp.append("1010") ;
00206                         break ;
00207                 case 'b':  tmp.append("1011") ;
00208                         break ;
00209                 case 'c':  tmp.append("1100") ;
00210                         break ;
00211                 case 'd':  tmp.append("1101") ;
00212                         break ;
00213                 case 'e':  tmp.append("1110") ;
00214                         break ;
00215                 case 'f':  tmp.append("1111") ;
00216                         break ;
00217           }
00218         }
00219         b = bitset<128> (tmp) ;
00220         return b ;
00221 }
00222 
00223 
00225 
00230 template <std::size_t N>
00231 inline
00232 string bitsetToHex(std::bitset<N> b){
00233   //string s = b.to_string();
00234   string hex ;
00235 
00236   int i = N%4 ;
00237     switch (i) {
00238         case 1: hex.append(1,numToChar[b[N-1]]) ; break ;
00239     case 2: hex.append(1,numToChar[b[N-2]+2*b[N-1]]) ; break ;
00240     case 3: hex.append(1,numToChar[b[N-3]+2*b[N-2]+4*b[N-1]]) ; break ;
00241   }
00242   for ( i = N-1; i >= 3 ; i -= 4 ){
00243     hex.append(1,numToChar[8*b[i] + 4*b[i-1] + 2*b[i-2] + b[i-3]]) ;
00244   }
00245   return hex ;
00246 }
00247 
00248 
00250 
00255 template <typename T>
00256 T
00257 hexTo( string const & c ) {
00258   T b ;
00259   unsigned k = 4*c.length() ;
00260   for (unsigned i = 0 ; i < c.length() ; ++i ){
00261         k -= 4 ;
00262     unsigned n = charToNum[(int)c[i]] ;
00263     b[k+0] = n & 0x01 ; n >>= 1 ;
00264     b[k+1] = n & 0x01 ; n >>= 1 ;
00265     b[k+2] = n & 0x01 ; n >>= 1 ;
00266     b[k+3] = n & 0x01 ;
00267   }
00268   return b ;
00269 }
00270 
00271 
00275 inline
00276 string bitset128ToHex(bitset<128> b){
00277         string s = b.to_string();
00278         string hex ;
00279         bitset<4> oct ;
00280         for (unsigned i = 0 ; i < s.length() ; i = i + 4){
00281                 oct[0] = b[s.length()-4-i] ;
00282                 oct[1] = b[s.length()-3-i] ;
00283                 oct[2] = b[s.length()-2-i] ;
00284                 oct[3] = b[s.length()-1-i] ;
00285                 if      (oct == bitset<4> (string("0000")) )  hex.append("0") ;
00286                 else if (oct == bitset<4> (string("0001")) )  hex.append("1") ;
00287                 else if (oct == bitset<4> (string("0010")) )  hex.append("2") ;
00288                 else if (oct == bitset<4> (string("0011")) )  hex.append("3") ;
00289                 else if (oct == bitset<4> (string("0100")) )  hex.append("4") ;
00290                 else if (oct == bitset<4> (string("0101")) )  hex.append("5") ;
00291                 else if (oct == bitset<4> (string("0110")) )  hex.append("6") ;
00292                 else if (oct == bitset<4> (string("0111")) )  hex.append("7") ;
00293                 else if (oct == bitset<4> (string("1000")) )  hex.append("8") ;
00294                 else if (oct == bitset<4> (string("1001")) )  hex.append("9") ;
00295                 else if (oct == bitset<4> (string("1010")) )  hex.append("a") ;
00296                 else if (oct == bitset<4> (string("1011")) )  hex.append("b") ;
00297                 else if (oct == bitset<4> (string("1100")) )  hex.append("c") ;
00298                 else if (oct == bitset<4> (string("1101")) )  hex.append("d") ;
00299                 else if (oct == bitset<4> (string("1110")) )  hex.append("e") ;
00300                 else if (oct == bitset<4> (string("1111")) )  hex.append("f") ;
00301         }
00302         return hex ;
00303 }
00304 
00310 template <class T>
00311 inline std::string ucharToString (const T& t)
00312 {
00313         std::stringstream ss ;
00314         std::string str ;
00315         ss << hex << (int)t ;
00316         if (t <= 0x0f && t>=0x00) str = "0" ;
00317         str.append(ss.str()) ;
00318 
00319         return str ;
00320 }
00321 
00329 unsigned char gmul(unsigned char a, unsigned char b) {
00330         unsigned char p = 0;
00331         unsigned char counter;
00332         unsigned char hi_bit_set;
00333         for(counter = 0; counter < 8; counter++) {
00334                 if((b & 1) == 1)
00335                         p ^= a;
00336                 hi_bit_set = (a & 0x80);
00337                 a <<= 1;
00338                 if(hi_bit_set == 0x80)
00339                         a ^= 0x1b;
00340                 b >>= 1;
00341         }
00342         return p;
00343 }
00344 
00352 void gmix_column(unsigned char *r) {
00353         unsigned char a[4] ;
00354         unsigned char c ;
00355         for(c = 0 ; c < 4 ; ++c) {
00356                 a[c] = r[c];
00357         }
00358         r[0] = gmul(a[0],2) ^ gmul(a[3],1) ^ gmul(a[2],1) ^ gmul(a[1],3) ;
00359         r[1] = gmul(a[1],2) ^ gmul(a[0],1) ^ gmul(a[3],1) ^ gmul(a[2],3) ;
00360         r[2] = gmul(a[2],2) ^ gmul(a[1],1) ^ gmul(a[0],1) ^ gmul(a[3],3) ;
00361         r[3] = gmul(a[3],2) ^ gmul(a[2],1) ^ gmul(a[1],1) ^ gmul(a[0],3) ;
00362 }
00363 
00371 inline
00372 void gmix_columnInv(unsigned char *r) {
00373         unsigned char a[4];
00374         unsigned char c;
00375         for(c=0;c<4;c++) {
00376                 a[c] = r[c];
00377                 }
00378         r[0] = gmul(a[0],14) ^ gmul(a[3],9) ^ gmul(a[2],13) ^ gmul(a[1],11);
00379         r[1] = gmul(a[1],14) ^ gmul(a[0],9) ^ gmul(a[3],13) ^ gmul(a[2],11);
00380         r[2] = gmul(a[2],14) ^ gmul(a[1],9) ^ gmul(a[0],13) ^ gmul(a[3],11);
00381         r[3] = gmul(a[3],14) ^ gmul(a[2],9) ^ gmul(a[1],13) ^ gmul(a[0],11);
00382 }
00383 
00384 #endif
 All Classes Functions Variables Typedefs