Advertisement
Guest User

QES Key Recovery

a guest
Jun 20th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1.  
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef u_int8_t uchar;
  7.  
  8. // inp could point the same memory as outp
  9. template <typename T>
  10. void qes_encrypt_block(T* inp, T *outp, T *matrix)
  11. {
  12.     T buf[3];
  13.        
  14.     buf[0] = matrix[0] * inp[0] + matrix[1] * inp[1] + matrix[2] * inp[2];
  15.     buf[1] = matrix[3] * inp[0] + matrix[4] * inp[1] + matrix[5] * inp[2];
  16.     buf[2] = matrix[6] * inp[0] + matrix[7] * inp[1] + matrix[8] * inp[2];
  17.      
  18.     outp[0] = buf[0];
  19.     outp[1] = buf[1];
  20.     outp[2] = buf[2];
  21. }
  22.  
  23. template <typename T>
  24. class vec3
  25. {
  26.     T vec[3];
  27.  
  28. public:
  29.     T& operator[](int i)
  30.     {
  31.         return vec[i];
  32.     }
  33. };
  34.  
  35. template <typename T, int N, int M>
  36. class matrix
  37. {
  38.     T matrix[N*M];
  39.  
  40. public:
  41.     T& at(int i, int j)
  42.     {
  43.         return matrix[i+j*N];
  44.     }
  45.  
  46.     T* raw()
  47.     {
  48.         return &matrix[0];
  49.     }
  50. };
  51.  
  52. template <typename T>
  53. class matrix3x3
  54.     : public matrix<T,3,3>
  55. {
  56. public:
  57.     uchar det()
  58.     {
  59.         return    this->at(0,0) * ( this->at(1,1)*this->at(2,2) - this->at(1,2)*this->at(2,1) )
  60.             - this->at(0,1) * ( this->at(1,0)*this->at(2,2) - this->at(2,0)*this->at(1,2) )
  61.             + this->at(0,2) * ( this->at(1,0)*this->at(2,1) - this->at(1,1)*this->at(2,0) );
  62.     }
  63.  
  64.     void setColumn(int id, vec3<T>& vec)
  65.     {
  66.         for (int i=0; i<3; i++)
  67.             this->at(i,id) = vec[i];
  68.     }
  69.  
  70.  
  71.     void setRow(int id, vec3<T>& vec)
  72.     {
  73.         for (int i=0; i<3; i++)
  74.             this->at(id,i) = vec[i];
  75.     }
  76. };
  77.  
  78.  
  79. template <typename T, unsigned TMAX>
  80. class analyzer
  81. {
  82. public:
  83.     vec3<T> m[3]; // three plaintext messages
  84.     vec3<T> c[3]; // three encrypted messages
  85.  
  86.     matrix3x3<T> E; // resulting master matrix, we are looking for
  87.  
  88.     bool evaluate()
  89.     {
  90.         matrix3x3<T> EQM;
  91.  
  92.         for (int i=0; i<3; i++)
  93.             for (int j=0; j<3; j++)
  94.                 EQM.at(i,j) = m[i][j];
  95.        
  96.         T det = EQM.det();
  97.  
  98.         if (det == 0)
  99.         {
  100.             return false;
  101.         }
  102.  
  103.  
  104.         for (int jidx = 0; jidx < 3; jidx ++)
  105.         {
  106.             for (int idx = 0; idx < 3; idx ++ )
  107.             {
  108.                 matrix3x3<T> e0EQM = EQM; // default C-CTOR is ok
  109.                 e0EQM.at(0, idx) = c[0][jidx];
  110.                 e0EQM.at(1, idx) = c[1][jidx];
  111.                 e0EQM.at(2, idx) = c[2][jidx];
  112.  
  113.                 T det0 = e0EQM.det();
  114.                
  115.                 int first = 1;
  116.                 for (int i=0; i<10; i++)
  117.                 {
  118.                     if ( ((det0+TMAX*i) % det) == 0)
  119.                     {
  120.                         T result = (det0+TMAX*i) / det;
  121.  
  122.                     //  if (!first)
  123.                     //      printf(" or ");
  124.  
  125.                     //  printf("%d", result);
  126.                        
  127.                         if (first)
  128.                             E.at(idx,jidx) = result;
  129.  
  130.                         first = 0;
  131.                     }
  132.                 }
  133.                 //printf("\n");
  134.             }
  135.         }
  136.     }
  137. };
  138.  
  139.  
  140.  
  141. int main()
  142. {
  143.     uchar input[9] = {0, 9, 2, 3, 4, 5, 6, 7, 8 };
  144.     uchar output[9];
  145.  
  146.     uchar key[9] = {10,5,7,1,4,2,9,7,8};
  147.  
  148.     qes_encrypt_block(&input[0], &output[0], &key[0]);
  149.     qes_encrypt_block(&input[3], &output[3], &key[0]);
  150.     qes_encrypt_block(&input[6], &output[6], &key[0]);
  151.  
  152.     printf("Encrypted: ");
  153.  
  154.     for (int i=0; i<9; i++)
  155.         printf("%d ", output[i]);
  156.     printf("\n");
  157.  
  158.     printf("key: ");
  159.     for (int i=0; i<9; i++)
  160.         printf("%d ", key[i]);
  161.     printf("\n");
  162.  
  163.  
  164.     analyzer<uchar,256> a;
  165.  
  166.     // preparing analyzer - feeding in the data
  167.     for (int i=0; i<3; i++)
  168.     {
  169.         for (int j=0; j<3; j++)
  170.         {
  171.             a.m[i][j] = input[j+3*i];
  172.             a.c[i][j] = output[j+3*i];
  173.         }
  174.     }
  175.  
  176.     if (a.evaluate())
  177.     {
  178.         printf("recovered key: ");
  179.         for (int i=0; i<9; i++)
  180.             printf("%d ", a.E.raw()[i]);
  181.         printf("\n");
  182.     }
  183.     else
  184.     {
  185.         printf("Select another dataset, determenant is zero\n");
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement