Example #1
0
void test_Sbox()
{
        std::cout << "\n---- Testing Sbox ----" << std::endl;
        
        cnf::Sbox sbox(4, 4, {  0x5, 0xb, 0x6, 0xe, 0x8, 0x2, 0x7, 0xa,
                                0x3, 0x4, 0x0, 0xc, 0x1, 0x9, 0xf, 0xd});
        
        for (unsigned int x=0; x<16; x++)
        {
                
                cnf::VariableSet v;
                v.add_subset("in",  {4});
                v.add_subset("out", {4});
                
                cnf::Formula f(&v);
                std::vector<long int> input, output;
                for (unsigned int i=0; i<4; i++)
                {
                        input.push_back(v.var("in", {i}));
                        output.push_back(v.var("out", {i}));
                }
                sbox.add_clauses_image(&f, input, output);
                f.assign_to_integer(input, x);
                if (cnf::Solver("minisat", {}).solve(f, &v))
                {
                        std::cout << std::hex << x << " "
                                  << v.little_endian(output)
                                  << std::endl;
                }
                else
                        std::cout << "Problem with Sbox" << std::endl;
        }

}
Example #2
0
void byte_sub(unsigned char *in, unsigned char *out)
{
    int i;
    for (i=0; i< 16; i++)
    {
        out[i] = sbox(in[i]);
    }
}
Example #3
0
int main(void)
{
   unsigned x, y;
   
   for (x = 0; x < 16; x++) Ei[E[x]] = x;
   
//   for (x = 0; x < 16; x++) printf("%2x ", sbox(x));
   for (y = 1; y < 8; y++) {
      for (x = 0; x < 8; x++) {
          cir[y][x] = cir[y-1][(x-1)&7];
      }
   }

/*   
   printf("\n");
   for (y = 0; y < 8; y++) {
       for (x = 0; x < 8; x++) printf("%2d ", cir[y][x]);
       printf("\n");
   }
*/

   for (y = 0; y < 8; y++) {
       printf("static const ulong64 sbox%d[] = {\n", y);
       for (x = 0; x < 256; ) {
           printf("CONST64(0x%02x%02x%02x%02x%02x%02x%02x%02x)",
              gf_mul(sbox(x), cir[y][0]),
              gf_mul(sbox(x), cir[y][1]),
              gf_mul(sbox(x), cir[y][2]),
              gf_mul(sbox(x), cir[y][3]),
              gf_mul(sbox(x), cir[y][4]),
              gf_mul(sbox(x), cir[y][5]),
              gf_mul(sbox(x), cir[y][6]),
              gf_mul(sbox(x), cir[y][7]));
           if (x < 255) printf(", ");
           if (!(++x & 3)) printf("\n");
       }
       printf("};\n\n");
  }
  
  printf("static const ulong64 cont[] = {\n");
  for (y = 0; y <= 10; y++) {
      printf("CONST64(0x");
      for (x = 0; x < 8; x++) {
         printf("%02x", sbox((8*y + x)&255));
      }
      printf("),\n");
  }
  printf("};\n\n");
  return 0;
   
}
Example #4
0
uint16_t four_sboxes(uint16_t input) {
        uint16_t
        result = sbox(input & 0x000f);
        result |= sbox((input & 0x00f0) >> 4) << 4;
        result |= sbox((input & 0x0f00) >> 8) << 8;
        result |= sbox((input & 0xf000) >> 12) << 12;

        return result;
}
Example #5
0
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++) // E/P Permutation
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)//Performing XOR with Key
if(i<4)
slip[i] = epd[i]^keys[round][i]; // Using Key _ 1=>0
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);//Calling SBox 1, 0->SBOX 1
sbox(srip,p,1,3);//Calling SBox 1, 1->SBOX 2
for(i=0;i<4;i++) //P4 permutation
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
Example #6
0
/*
 * 1. copy old config file to backup
 * 2. save boards to new config file
 * 3. then copy new to old config file
 */
void Hardware::saveBoards()
{
    QString back = +".backup";
    QMessageBox mbox(QMessageBox::Critical,tr("xBasic.cfg File Error"),"", QMessageBox::Ok);
    QFile file;
    if(!file.exists(xBasicCfgFile)) {
        mbox.setInformativeText(tr("Can't find file: ")+xBasicCfgFile);
        mbox.exec();
        return;
    }
    QFile cfg(xBasicCfgFile);
/*
    QFile bup(this->xBasicCfgFile+back);
    if(bup.exists()) bup.remove();
    if(!cfg.copy(this->xBasicCfgFile+back)) {
        mbox.setInformativeText(tr("Can't backup file: ")+xBasicCfgFile);
        mbox.exec();
        return;
    }
*/
    QByteArray barry = "";
    QString currentName = ui->comboBoxBoard->currentText().toUpper();
    XBasicBoard *board = xBasicConfig->getBoardByName(currentName);
    if(board == NULL)
        ui->comboBoxBoard->addItem(currentName);
    for(int n = 0; n < ui->comboBoxBoard->count(); n++)
    {
        QString name = ui->comboBoxBoard->itemText(n);
        XBasicBoard *board = xBasicConfig->getBoardByName(name);
        if(board == NULL && name == currentName)
            board = xBasicConfig->newBoard(name);
        if(name == currentName)
            setBoardInfo(board);
        QString ba = board->getFormattedConfig();
        qDebug() << ba;
        barry.append(ba);
    }


    QMessageBox sbox(QMessageBox::Question,tr("Saving xBasic.cfg File"),"",
                     QMessageBox::Save | QMessageBox::Cancel);

    sbox.setInformativeText(tr("Save new ")+xBasicCfgFile+tr("?"));

    if(sbox.exec() == QMessageBox::Save) {
        if(cfg.open(QIODevice::WriteOnly | QFile::Text)) {
            cfg.write(barry);
            cfg.flush();
            cfg.close();
        }
    }
}
void sboxLayer(bit block[block_length]){
	
	int i;
	
	for(i = 0; i < 4; i++)
	{
		bit chunk[chunk_length];	
		getChunk(block , chunk , i);
		
		sbox(chunk , i);
		
		setChunk(block , chunk , i);		
	}	
}
Example #8
0
int main(int argc, char** argv) {

    Box<double> sbox(2);
    sbox.mA[0] = 0;
    sbox.mA[1] = 0;
    sbox.mB[0] = 4;
    sbox.mB[1] = 3;

    std::vector< Box<double> > bv;
    Box<double> cbox1(2);

    /** Test 1 **/
    cbox1.mA[0] = -1;
    cbox1.mA[1] = 1;
    cbox1.mB[0] = 5;
    cbox1.mB[1] = 2;

    BoxUtils::complement(sbox, cbox1, bv);
    printBoxList(bv);
    BNB_ASSERT(bv.size() == 2);
    BNB_ASSERT((bv[0].mA[0] == 0) && (bv[0].mA[1] == 0) && (bv[0].mB[0] == 4) && (bv[0].mB[1] == 1));
    BNB_ASSERT((bv[1].mA[0] == 0) && (bv[1].mA[1] == 2) && (bv[1].mB[0] == 4) && (bv[1].mB[1] == 3));

    /** Test 2 **/
    cbox1.mA[0] = 1;
    cbox1.mA[1] = 1;
    cbox1.mB[0] = 2;
    cbox1.mB[1] = 2;
    bv.clear();
    BoxUtils::complement(sbox, cbox1, bv);
    printBoxList(bv);
    BNB_ASSERT(bv.size() == 4);
    BNB_ASSERT((bv[0].mA[0] == 0) && (bv[0].mA[1] == 0) && (bv[0].mB[0] == 1) && (bv[0].mB[1] == 3));
    BNB_ASSERT((bv[1].mA[0] == 2) && (bv[1].mA[1] == 0) && (bv[1].mB[0] == 4) && (bv[1].mB[1] == 3));
    BNB_ASSERT((bv[2].mA[0] == 1) && (bv[2].mA[1] == 0) && (bv[2].mB[0] == 2) && (bv[2].mB[1] == 1));
    BNB_ASSERT((bv[3].mA[0] == 1) && (bv[3].mA[1] == 2) && (bv[3].mB[0] == 2) && (bv[3].mB[1] == 3));

    /** Test 3 **/
    cbox1.mA[0] = -2;
    cbox1.mA[1] = -2;
    cbox1.mB[0] = -1;
    cbox1.mB[1] = -1;
    bv.clear();
    BoxUtils::complement(sbox, cbox1, bv);
    printBoxList(bv);
    BNB_ASSERT(bv.size() == 1);
    BNB_ASSERT((bv[0].mA[0] == 0) && (bv[0].mA[1] == 0) && (bv[0].mB[0] == 4) && (bv[0].mB[1] == 3));

    return 0;
}
Example #9
0
void next_key(unsigned char *key, int round)
{
    unsigned char rcon;
    unsigned char sbox_key[4];
    unsigned char rcon_table[12] =
    {
        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
        0x1b, 0x36, 0x36, 0x36
    };

    sbox_key[0] = sbox(key[13]);
    sbox_key[1] = sbox(key[14]);
    sbox_key[2] = sbox(key[15]);
    sbox_key[3] = sbox(key[12]);

    rcon = rcon_table[round];

    xor_32(&key[0], sbox_key, &key[0]);
    key[0] = key[0] ^ rcon;

    xor_32(&key[4], &key[0], &key[4]);
    xor_32(&key[8], &key[4], &key[8]);
    xor_32(&key[12], &key[8], &key[12]);
}
Example #10
0
void *subRound(void *hnd, c4snet_data_t *sd, c4snet_data_t*sk, int c) {
  char *D = (char*) C4SNetGetData(sd);
  char *K = (char *) C4SNetGetData(sk);
  int i;
  for (i = 0; i < 8; i++) {
    int j;
    char *L = D;           /* The left 4 bytes (half) of the data block.   */
    char *R = &(D[4]);     /* The right half of the ciphertext block.      */
    char  Rexp[6];         /* Expanded right half.                         */
    char  Rn[4];           /* New value of R, as we manipulate it.         */
    char  SubK[6];         /* The 48-bit subkey.                           */

    /* Generate the subkey for this round.
    */
    KeyShift( K, KeyRotation[i + c * 8] );
    Permute( SubK, K, KeyCompression, 6 );

    /* Expand the right half (R) of the data block to 48 bytes,
     * then XOR the result with the Subkey for this round.
     */
    Permute( Rexp, R, DataExpansion, 6 );
    xor( Rexp, Rexp, SubK, 6 );

    /* S-Box substitutions, P-Box permutation, and final XOR.
     * The S-Box substitutions return a 32-bit value, which is then
     * run through the 32-bit to 32-bit P-Box permutation.  The P-Box
     * result is then XOR'd with the left-hand half of the key.
     * (Rexp is used as a temporary variable between the P-Box & XOR).
     */
    sbox( Rn, Rexp );
    Permute( Rexp, Rn, PBox, 4 );
    xor( Rn, L, Rexp, 4 );

    /* The previous R becomes the new L,
     * and Rn is moved into R ready for the next round.
     */
    for( j = 0; j < 4; j++ )
    {
      L[j] = R[j];
      R[j] = Rn[j];
    }
  }
  c++;
  C4SNetOut(hnd, 1, sd, sk, c);
  return hnd;
}
Example #11
0
/*  This function is used by key expansion method
 *	It performs following transformations on a word
 *		1. One Byte Left circular rotation
 *		2. Byte Substitution using sbox
 *		3. XOR first byte with RCON Value
 *  Input:  word : 4 byte array of bytes
 *			iteration: function uses rcon value which depends on iteration value
 *			NOTE: iteration value starts with zero
 *	Output: void , operations are carried out on word itself
*/
void g(unsigned char *word, int iteration) {
    int i;
    //-------------- One byte left circular rotation-----------
    unsigned char tmp = word[0];
    word[0]=word[1];
    word[1]=word[2];
    word[2]=word[3];
    word[3]=tmp;
    //-------------- Byte Substitution by the same lookup table as used in Sub Bytes step-----------
    for(i=0; i<4; i++) {
        word[i]= sbox(word[i]);
    }

    //-------------- XOR with round constant-----------
    // we don't need more than 10 round constants
    // 128 bit key requires 10 roundConstants, 192 bit key variant requires 8 and 256 bit variant requires 7
    unsigned char rcon[] = {0x01 ,0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
    word[0]= rcon[iteration] ^ word[0];

}
Example #12
0
unsigned char *CRYPT_DESunhash( unsigned char *dst, const unsigned char *key, const unsigned char *src )
{
    int i;
    unsigned char K[7];
    unsigned char D[8];

    Permute( K, key, KeyPermuteMap, 7 );
    Permute( D, src, InitialPermuteMap, 8 );

    for (i = 0; i < 16; i++)
    {
        int j;
        unsigned char *L = D;
        unsigned char *R = &(D[4]);
        unsigned char  Rexp[6];
        unsigned char  Rn[4];
        unsigned char  SubK[6];

        Permute( SubK, K, KeyCompression, 6 );

        Permute( Rexp, R, DataExpansion, 6 );
        xor( Rexp, Rexp, SubK, 6 );

        sbox( Rn, Rexp );
        Permute( Rexp, Rn, PBox, 4 );
        xor( Rn, L, Rexp, 4 );

        for (j = 0; j < 4; j++)
        {
            L[j] = R[j];
            R[j] = Rn[j];
        }

        KeyShiftRight( K, KeyRotation[15 - i] );
    }

    Permute( dst, D, FinalPermuteMap, 8 );

    return dst;
}
Example #13
0
/*
 *	Input:
 *	key: user specified key
 *	expandedKey: array of unsigned chars for expanded key
 *				NOTE: function does not check the size of expandedKey
 *						It should be according to n
 *						if n = 16 size of expandedKey >= 176
 *						if n = 24 size of expandedKey >= 208
 *						if n = 32 size of expandedKey >= 240
 *	n = size of key (in bytes)
 *	Output:
 *	void, function works on expandedKey
*/
void keyExpansion(unsigned char key[], unsigned char expandedKey[], const int n) {
    int rounds = n/4 +6;// rounds = number of rounds
    int sizeExpandedKey = 16*(rounds+1);// expected size of expandedKey
    int iteration=0;// for round constants // should start with zero
    int i;
    int sizeCurrent=0;// current size of expanded key
    //-------------- First n bytes of expanded key are same as those of original key, so copy--------------
    for(i=0; i<n; i++) {
        expandedKey[i]= key[i];
    }
    sizeCurrent = n;// we already have n bytes in the expanded key

    unsigned char tmp[4];
    while(sizeCurrent < sizeExpandedKey) {
        // copy previous 4 bytes in tmp .. If key = (w1 w2 w3 w4), tmp will contain w1
        for(i=0; i<4; i++) {
            tmp[i]= expandedKey[( sizeCurrent - 4 ) + i ];
        }
        // apply g every n bytes
        if(sizeCurrent % n ==0) {
            g(tmp, iteration);
            iteration++;

        }
        // Special case for 256 bit key
        // Why is this added?? TODO
        if( n==32 &&   ((sizeCurrent % 32) == 16)) {
            for(i=0; i<4; i++) {
                tmp[i]= sbox(tmp[i]);
            }
        }
        // XOR with "word" n bytes before this one
        for(i=0; i<4; i++) {
            expandedKey[sizeCurrent] = expandedKey[sizeCurrent - n] ^ tmp[i];
            sizeCurrent++;
        }
    }
}
Example #14
0
void p4(YMM(*state)[2]) {
	for (int round = 0; round < p4_rounds; round++) {

		//Sub Bytes
		sbox(state);

		//Shift Rows
		for (int reg = 0; reg < 5; reg++) {
			state[reg][0] = _mm256_shuffle_epi8(state[reg][0], shuffleControlMaskFirstReg);
			state[reg][1] = _mm256_shuffle_epi8(state[reg][1], shuffleControlMaskSecondReg);
		}

		//Mix Columns
		mixcolumns_80bit(state);

		//Constant Addition
		state[0][0] = XOR(state[0][0], p4_constants_bit0[round]);
		state[1][0] = XOR(state[1][0], p4_constants_bit1[round]);
		state[2][0] = XOR(state[2][0], p4_constants_bit2[round]);
		state[3][0] = XOR(state[3][0], p4_constants_bit3[round]);
		state[4][0] = XOR(state[4][0], p4_constants_bit4[round]);
	}
}
Example #15
0
File: des.c Project: cadizm/crypto
/*
 * DES symmetric-key block cipher encryption of `m' (which is an array of
 * integers representing a 64-bit block of data) using IP, E, P, S{1-8}, V,
 * PC1, PC2 tables specified in arg `table' respectively. Keys for each
 * round of the DES algorithm are specified using arg `keys' and the result
 * of encryption is stored in arg `res'.
 */
void des(int m[64], int** table, int keys[16][48], int res[64])
{
    int L[32];
    int R[32];
    int Li[32];
    int Ri[32];
    int T[48];
    int B[6];

    memset(L, 0, 32 * sizeof(int));
    memset(R, 0, 32 * sizeof(int));
    memset(Li, 0, 32 * sizeof(int));
    memset(Ri, 0, 32 * sizeof(int));
    memset(T, 0, 48 * sizeof(int));
    memset(B, 0, 6 * sizeof(int));

    int* IP = table[0];
    int* E  = table[1];
    int* P  = table[2];
    int* S1 = table[3];
    int* S2 = table[4];
    int* S3 = table[5];
    int* S4 = table[6];
    int* S5 = table[7];
    int* S6 = table[8];
    int* S7 = table[9];
    int* S8 = table[10];

    /* setup L and R using IP table */
    for (int i = 0; i < 32; ++i) {
        int pos = IP[i] - 1;  /* decrement by 1, DES tables are 1-based */
        L[i] = m[pos];
    }
    for (int i = 32; i < 64; ++i) {
        int pos = IP[i] - 1;  /* decrement by 1, DES tables are 1-based */
        R[i-32] = m[pos];
    }

    int LR[64];
    memset(LR, 0, 64 * sizeof(int));

    /* concatenate L and R */
    memcpy(&LR[0], L, 32 * sizeof(int));
    memcpy(&LR[32], R, 32 * sizeof(int));

    uint8_t lrstr[64/4+1];
    memset(lrstr, '\0', sizeof(lrstr));

    if (m_flag) {
        hexstring(LR, 64, lrstr);
        fprintf(stderr, "(L0,R0)=%s\n", lrstr);
    }

    /* perform 16 des rounds */
    for (int i = 0; i < 16; ++i) {

        /* compute Li and Ri from prev values */
        memcpy(Li, R, 32 * sizeof(int));
        memcpy(Ri, L, 32 * sizeof(int));

        /* expand R from 32 to 48 bits */
        for (int j = 0; j < 48; ++j) {
            int pos = E[j] - 1;  /* decrement by 1, DES tables are 1-based */
            T[j] = R[pos];
        }

        /* xor T with this round's key */
        for (int j = 0; j < 48; ++j) {
            T[j] = T[j] ^ keys[i][j];
        }

        int TT[32];
        memset(TT, 0, 32 * sizeof(int));

        /* map every 8 bits to 6 using S-boxes */
        for (int j = 0, u = 0; j < 48; j += 6, u += 4) {
            memcpy(B, &T[j], 6 * sizeof(int));

            int t[4];
            memset(t, 0, 4 * sizeof(int));

            int k = j / 6 + 1;

            switch (k) {
                case 1: sbox(B, S1, t); break;
                case 2: sbox(B, S2, t); break;
                case 3: sbox(B, S3, t); break;
                case 4: sbox(B, S4, t); break;
                case 5: sbox(B, S5, t); break;
                case 6: sbox(B, S6, t); break;
                case 7: sbox(B, S7, t); break;
                case 8: sbox(B, S8, t); break;
                default:
                    fprintf(stderr, "Error: invalid S-box index: %d\n", k);
                    exit(-1);
            }

            memcpy(&TT[u], &t[0], 4 * sizeof(int));
        }

        int TTT[32];
        memset(TTT, 0, 32 * sizeof(int));

        /* transform TT to TTT (L) using P table */
        for (int j = 0; j < 32; ++j) {
            int pos = P[j] - 1;  /* decrement by 1, DES tables are 1-based */
            TTT[j] = TT[pos];
        }

        /* Ri has L from previous round, so xor with self and TTT */
        for (int j = 0; j < 32; ++j) {
            Ri[j] = Ri[j] ^ TTT[j];
        }

        memcpy(&LR[0], Li, 32 * sizeof(int));
        memcpy(&LR[32], Ri, 32 * sizeof(int));

        memset(lrstr, '\0', sizeof(lrstr));

        if (m_flag) {
            hexstring(LR, 64, lrstr);
            fprintf(stderr, "(L%d,R%d)=%s\n", i+1, i+1, lrstr);
        }

        /* copy to get ready for next round */
        memcpy(L, R, 32 * sizeof(int));
        memcpy(R, Ri, 32 * sizeof(int));
    }

    /* exchange final blocks */
    memcpy(&LR[0], Ri, 32 * sizeof(int));
    memcpy(&LR[32], Li, 32 * sizeof(int));

    int IPINV[64];
    memset(IPINV, 0, 64 * sizeof(int));

    /* transpose IP table to get inverse */
    transpose(IP, IPINV);

    /* copy inverse values to final result */
    for (int i = 0; i < 64; ++i) {
        int pos = IPINV[i] - 1;  /* decrement by 1, DES tables are 1-based */
        res[i] = LR[pos];
    }

    m_flag = 0;
}
void keySchedule(bit key[key_length] , bit schedule[16][key_length]){
	
	//Step 1

	bit word[88][6];
	int i , j , r;
	
	for(i = 0; i < 4; i++){
		getChunk(key, word[i], i);
		
		for(j = 0; j < chunk_length; j++)
			word[i + 4][j] = word[i][j];
	}
	
	for(i = 4; i < 8; i++)
	{
		sbox(word[i], i - 4);
		
		if(i < 7)
			arraySum(word[i - 3] , word[i]  , word[i] , chunk_length);
		else
			arraySum(word[0] , word[i]  , word[i] , chunk_length);
		
	}
	
	//step 2

	for(j = 9; j <= 88; j++)
	{
		i = j-1;
		if((j % 8) == 1)
			{
			bit buffer[chunk_length];
			arrayCopy(word[i - 1] , buffer , chunk_length);
			rotate(buffer, -1 , chunk_length);	
			sbox(buffer , 1);
			arraySum(buffer , word[i - 8], word[i], chunk_length);
			arraySum(word[i], keyScheduleArray , word[i], chunk_length);
			}
		else if((j % 8) == 5)
			{
			bit buffer[chunk_length];
			arrayCopy(word[i - 1] , buffer , chunk_length);	
			sbox(buffer , 2);	
			arraySum(word[i - 8] , buffer , word[i] , chunk_length);			
			}
		else if((j % 4) != 1)
			arraySum(word[i - 8] , word[i - 1] , word[i] , chunk_length);
	}
	
	//phase 3

	for(r = 0; r < 3; r++)	//for rect 1,2,3
	{		
		for(j = 0; j < 5; j++) //for key 1...5
		{
			int z = (8 + 20*r) + j;
			int index = (r*5)+j;
			for(i = 0; i < 4; i++)	//for each word of the key
			{
				setChunk(schedule[index] , word[z] , i);
				z += 5;
			}
		}
	}
	
	int z = 68;
	for(i = 0; i < 4; i++)	//last key
	{
		setChunk(schedule[15] , word[z] , i);
		z += 5;
	}
}
Example #17
0
void encrypt( unsigned char *data, unsigned char *output )
{
    unsigned char block[16], work[4], V ;
    register int loop, i ;
    int sbox( unsigned char data ) ;

    for ( i = 0 ; i < 16 ; i++ )
     X1[i] = data[i] ;

    printf( "\nInput X1 of IP function\n" ) ;
    for ( i = 0 ; i < 16 ; i++ )
     printf( " %02X", X1[i] ) ;
    printf( "\nInput X2 of IP function\n" ) ;
    for ( i = 0 ; i < 16 ; i++ )
     printf( " %02X", X2[i] ) ;
    for ( i = 0 ; i < 8 ; i++ )
     block[i] = X2[i] ^ X1[i+8] ^ 0xaa ;
    for ( i = 0 ; i < 8 ; i++ )
     block[i+8] = X2[i+8] ^ X1[i] ^ 0xaa ;
    V = 0 ;
    for ( loop = 0 ; loop < 8 ; loop++ )
    {
     for ( i = 0 ; i < 4 ; i++ )
         work[i] = block[i] ^ X1[i] ;

     /* P1 */

     ++V ;
     work[3] ^= V ;
     work[1] ^= work[0] ;
     work[2] ^= work[3] ;
     work[1] = sbox( work[1] + work[2] + 1 ) ;
     work[2] = sbox( work[2] + work[1] ) ;
     work[0] = sbox( work[0] + work[1] ) ;
     work[3] = sbox( work[3] + work[2] + 1 ) ;

     for ( i = 0 ; i < 4 ; i++ )
         work[i] ^= block[i+4] ;
     for ( i = 0 ; i < 4 ; i++ )
         block[i+12] ^= work[i] ;
     for ( i = 0 ; i < 4 ; i++ )
         work[i] ^= X1[i+4] ;

     /* P2 */

     ++V ;
     work[3] ^= V ;
     work[1] ^= work[0] ;
     work[2] ^= work[3] ;
     work[1] = sbox( work[1] + work[2] + 1 ) ;
     work[2] = sbox( work[2] + work[1] ) ;
     work[0] = sbox( work[0] + work[1] ) ;
     work[3] = sbox( work[3] + work[2] + 1 ) ;

     for ( i = 0 ; i < 4 ; i++ )
         block[i+8] ^= work[i] ^ block[i] ;

     /******************************/

     for ( i = 0 ; i < 4 ; i++ )
         work[i] = block[i+8] ^ X1[i+8] ;

     /* P3 */

     ++V ;
     work[3] ^= V ;
     work[1] ^= work[0] ;
     work[2] ^= work[3] ;
     work[1] = sbox( work[1] + work[2] + 1 ) ;
     work[2] = sbox( work[2] + work[1] ) ;
     work[0] = sbox( work[0] + work[1] ) ;
     work[3] = sbox( work[3] + work[2] + 1 ) ;

     for ( i = 0 ; i < 4 ; i++ )
         work[i] ^= block[i+12] ;
     for ( i = 0 ; i < 4 ; i++ )
         block[i+4] ^= work[i] ;
     for ( i = 0 ; i < 4 ; i++ )
         work[i] ^= X1[i+12] ;

     /* P4 */

     ++V ;
     work[3] ^= V ;
     work[1] ^= work[0] ;
     work[2] ^= work[3] ;
     work[1] = sbox( work[1] + work[2] + 1 ) ;
     work[2] = sbox( work[2] + work[1] ) ;
     work[0] = sbox( work[0] + work[1] ) ;
     work[3] = sbox( work[3] + work[2] + 1 ) ;

     for ( i = 0 ; i < 4 ; i++ )
         block[i] ^= work[i] ^ block[i+8] ;
    }

    for ( i = 0 ; i < 16 ; i++ )
     output[i] = block[i] ^ X1[i] ^ X2[i] ;

    printf( "\nOutput Y of IP function\n" ) ;
    for ( i = 0 ; i < 16 ; i++ )
     printf( " %02X", output[i] ) ;
}
Example #18
0
	/*! \brief Cell list
	 *
	 * \param box Domain where this cell list is living
	 * \param origin of the Cell list
	 * \param div grid size on each dimension
	 *
	 */
	CellList(Box<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
	{
		SpaceBox<dim,T> sbox(box);
		Initialize(sbox,div,orig,pad);
	}
Example #19
0
main()
{
    float ship[XY];
    long org[XY];
    long size[XY];
    Device dev;
    short val;
    Device mdev[XY];
    short mval[XY];
    long nhits;
    short buffer[BUFSIZE];
    Boolean run;

    prefsize(400, 400);
    winopen("select1");
    getorigin(&org[X], &org[Y]);
    getsize(&size[X], &size[Y]);
    mmode(MVIEWING);
    ortho2(-0.5, size[X] - 0.5, -0.5, size[Y] - 0.5);
    qdevice(LEFTMOUSE);
    qdevice(ESCKEY);
    color(BLACK);
    clear();
    mdev[X] = MOUSEX;
    mdev[Y] = MOUSEY;

    drawplanet();
    run = TRUE;
    while (run) {
	dev = qread(&val);
	if (val == 0) {				/* on upstroke */
	    switch (dev) {
	    case LEFTMOUSE:
		getdev(XY, mdev, mval);
		ship[X] = mval[X] - org[X];
		ship[Y] = mval[Y] - org[Y];
		color(BLUE);
		sbox(ship[X], ship[Y], 
		     ship[X] + SHIPWIDTH, ship[Y] + SHIPHEIGHT);

		/*
		 * specify the selecting region to be a box surrounding the
		 * rocket ship 
		 */
		ortho2(ship[X], ship[X] + SHIPWIDTH, 
		       ship[Y], ship[Y] + SHIPHEIGHT);

		initnames();
		gselect(buffer, BUFSIZE);
		    loadname(PLANET);
		    /* no actual drawing takes place */
		    drawplanet();
		nhits = endselect(buffer);

		/*
		 * restore the Projection matrix; NB. can't use push/popmatrix 
		 * since they only work for the ModelView matrix stack 
		 * when in MVIEWING mode
		 */
		ortho2(-0.5, size[X] - 0.5, -0.5, size[Y] - 0.5);

		/* 
		 * check to see if PLANET was selected; NB. nhits is NOT the
		 * number of buffer elements written
		 */
		if (nhits < 0) {
		    fprintf(stderr, "gselect buffer overflow\n");
		    run = FALSE;
		} 
		else if (nhits >= 1 && buffer[0] == 1 && buffer[1] == PLANET)
		    ringbell();
		break;
	    case ESCKEY:
		run = FALSE;
		break;
	    }
	}
    }
    gexit();
    return 0;
}
Example #20
0
__kernel void aes_enc(__global  uchar4* output, __global  uchar4* IVs, __global  uchar4* roundKeys){
	
	__private unsigned int index=get_global_id(0);
	__private unsigned int output_index=index*4;
	__private unsigned int page_index=index/Blocks_per_Page;
	__private unsigned int offset_in_page=index%Blocks_per_Page;
	__private unsigned int iv_index=page_index*4;
	__private unsigned int rk_index=page_index*4*(rounds+1);
	
	__private uchar4 input[4];
	
	input[3]=(uchar4)(0,0,0,offset_in_page+2);
	if(offset_in_page>=254)
		input[3]=(uchar4)(0,0,1,offset_in_page-254);
	input[2]=IVs[iv_index+2];
	input[1]=IVs[iv_index+1];
	input[0]=IVs[iv_index];
	
	//for(int i=0;i<4;i++)
	//	output[output_index+i]=input[i];
	//return;
	
	__private uchar4 block1[4];
	
	__private uchar4 galiosCoeff[4];
	
	/*
	galiosCoeff[0] = (uchar4)(2, 1, 1, 3);
	galiosCoeff[1] = (uchar4)(3, 2, 1, 1);
	galiosCoeff[2] = (uchar4)(1, 3, 2, 1);
	galiosCoeff[3] = (uchar4)(1, 1, 3, 2);
	*/
	
	galiosCoeff[0] = (uchar4)(2, 3, 1, 1);
	galiosCoeff[1] = (uchar4)(1, 2, 3, 1);
	galiosCoeff[2] = (uchar4)(1, 1, 2, 3);
	galiosCoeff[3] = (uchar4)(3, 1, 1, 2);
	
	for(int i=0; i<4; i++)
		input[i] ^= roundKeys[rk_index+i];
	
	for(unsigned int r=1; r < rounds; ++r){
		
		for (int i=0; i<4; i++){
			input[i] = sbox(input[i]);
			//input[i] = shiftRows(input[i], i); 
		}
		
		block1[0].xyzw=(uchar4)(input[0].x,input[1].y,input[2].z,input[3].w);
		block1[1].xyzw=(uchar4)(input[1].x,input[2].y,input[3].z,input[0].w);
		block1[2].xyzw=(uchar4)(input[2].x,input[3].y,input[0].z,input[1].w);
		block1[3].xyzw=(uchar4)(input[3].x,input[0].y,input[1].z,input[2].w);
		
		mixColumns(input,block1, galiosCoeff); 
		
		for(int i=0; i<4; i++)
			input[i] = input[i] ^ roundKeys[rk_index+r*4 + i];
		
	}  
	
	for(int i=0; i<4; i++)
		input[i] = sbox(input[i]);
		
	block1[0].xyzw=(uchar4)(input[0].x,input[1].y,input[2].z,input[3].w);
	block1[1].xyzw=(uchar4)(input[1].x,input[2].y,input[3].z,input[0].w);
	block1[2].xyzw=(uchar4)(input[2].x,input[3].y,input[0].z,input[1].w);
	block1[3].xyzw=(uchar4)(input[3].x,input[0].y,input[1].z,input[2].w);

	for(int i=0;i<4;i++)
		output[output_index+i] =  block1[i] ^ roundKeys[rk_index+(rounds)*4 + i];
	
	return;
}