void FastRijndael::encryptOneRound(unsigned char** block){ if (!_initd){ return; } _round = 0; #if DEBUG fprintf(STDOUT, "Round %i\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif addRoundKey(block); #if DEBUG fprintf(STDOUT, "Round %i after whitening ARK\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif _round++; subBytes(block); #if DEBUG fprintf(STDOUT, "Round %i after SB\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif shiftRows(block); #if DEBUG fprintf(STDOUT, "Round %i after SR\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif mixColumns(block); #if DEBUG fprintf(STDOUT, "Round %i after MC\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif addRoundKey(block); #if DEBUG fprintf(STDOUT, "Round %i after ARK\n", _round-1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ fprintf(STDOUT, "%x ", block[i][j]); } fprintf(STDOUT, "\n"); } #endif }
byte * aes(byte *in, byte *skey) { int i; for(i=0; i < 16; i++) { state[i] = in[i]; key[i] = skey[i]; } addRoundKey(); for(i = 0; i < 9; i++) { subBytes(); shiftRows(); mixColumns(); computeKey(rcon[i]); addRoundKey(); } subBytes(); shiftRows(); computeKey(rcon[i]); addRoundKey(); return state; }
void test_mixColumns_given_state2_and_expected_equal_exState(void){ printf("No4.0 - mixColumns\n"); uint8_t state2[4][4] = { {0xac,0xef,0x13,0x45}, \ {0xc1,0xb5,0x23,0x73}, \ {0xd6,0x5a,0xcf,0x11}, \ {0xb8,0x7b,0xdf,0xb5} }; uint8_t exState[4][4] = {{0x75,0x20,0x53,0xbb}, \ {0xec,0x0b,0xc0,0x25}, \ {0x09,0x63,0xcf,0xd0}, \ {0x93,0x33,0x7c,0xdc} }; mixColumns(state2); TEST_ASSERT_EQUAL_STATE(exState,state2); }
void test_mixColumns_given_state3_and_expected_equal_exState(void){ printf("No5.0 - mixColumns\n"); uint8_t state3[4][4] = { {0x87,0xf2,0x4d,0x97}, \ {0x6e,0x4c,0x90,0xec}, \ {0x46,0xe7,0x4a,0xc3}, \ {0xa6,0x8c,0xd8,0x95} }; uint8_t exState[4][4] = {{0x47,0x40,0xa3,0x4c}, \ {0x37,0xd4,0x70,0x9f}, \ {0x94,0xe4,0x3a,0x42}, \ {0xed,0xa5,0xa6,0xbc} }; mixColumns(state3); TEST_ASSERT_EQUAL_STATE(exState,state3); }
//Encrypts one 16-byte array of data void AES::encryptBlock(int keySize, uint8_t* state, uint8_t* key) { int rounds=6+keySize/32; addRoundKey(state,key); for(int i=1;i<=rounds;i++) { subBytes(state); shiftRows(state); if(i!=rounds) //don't mix columns on last round mixColumns(state); addRoundKey(state,key+16*i); } }
void FastRijndael::encrypt(unsigned char** block){ if (!_initd){ return; } _round = 0; addRoundKey(block); _round++; for (; _round < _nr; _round++){ subBytes(block); shiftRows(block); mixColumns(block); addRoundKey(block); } subBytes(block); shiftRows(block); addRoundKey(block); }
void crypt_AES128( const plainData_t plainText[PLAIN_BSIZE], cryptData_t cryptedText[CRYPT_BSIZE], expKey_t expKey[KSCH_AES128_SIZE]) { uint8_t state[4*NB]; uint32_t* ptr, *statePtr; uint8_t i, round; /* copy plain input into state to initialize */ ptr = (uint32_t*) &plainText[0]; statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = *ptr++; /* first iteration, round 0 */ addRoundkey((uint32_t*) &state[0], &expKey[0]); /* Nround - 1 iterations */ for(round=1;round<NR_AES128;round++) { statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = subWord(*statePtr); shiftRows(state); mixColumns((uint32_t*) &state[0]); addRoundkey((uint32_t*) &state[0], &expKey[round*NB]); } /* last iteration, round 11 */ statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = subWord(*statePtr); shiftRows(state); addRoundkey((uint32_t*) &state[0], &expKey[(NR_AES128)*NB]); /* now copy back the crypted text into the buffer */ ptr = (uint32_t*) &cryptedText[0]; statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *ptr++ = *statePtr++; }
void AES::verboseEncryptNoReset() { if (round == 0) { std::cout << "Round " << round << " plaintext:" << std::endl; printData(); keyAdd(); std::cout << std::endl << "Round " << round << " keyAdd:" << std::endl; printData(); round++; } for (; round < 10; round++) { std::cout << std::endl << "Round " << round << " plaintext:" << std::endl; printData(); subBytes(); std::cout << std::endl << "Round " << round << " subBytes:" << std::endl; printData(); shiftRows(); std::cout << std::endl << "Round " << round << " shiftRows:" << std::endl; printData(); mixColumns(); std::cout << std::endl << "Round " << round << " mixColumns:" << std::endl; printData(); keyAdd(); std::cout << std::endl << "Round " << round << " keyAdd:" << std::endl; printData(); } subBytes(); std::cout << std::endl << "Round " << round << " subBytes:" << std::endl; printData(); shiftRows(); std::cout << std::endl << "Round " << round << " shiftRows:" << std::endl; printData(); keyAdd(); std::cout << std::endl << "Round " << round << " keyAdd / ciphertext:" << std::endl; printData(); }
void cipher(uint16_t state[]){ uint16_t expanded_key[nk*(nb*(nr+1))]; int round; keyExpansion(expanded_key); addRoundKey(state, expanded_key, 0); for (round = 1; round < nr; round++) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, expanded_key, round); } subBytes(state); shiftRows(state); addRoundKey(state, expanded_key, round); }
void Rijndael::encryptNRounds(unsigned char** block, int rounds){ if (!_initd){ return; } _round = 0; addRoundKey(block); _round++; for (; _round <= rounds; _round++){ if (_round == _nr) break; subBytes(block); shiftRows(block); mixColumns(block); addRoundKey(block); } if (_round == _nr && rounds != _nr-1){ subBytes(block); shiftRows(block); addRoundKey(block); } }
void decipher_block(unsigned char * state,uint32 * word,uint32 nb,uint32 nr){ addRoundKey(state,word,nr,nb); int round; for(round = nr-1;round >= 1 ;round--){ shiftRows(state,nb,1); subBytes(state,nb,inv_s_box); addRoundKey(state,word,round,nb); mixColumns(state,nb,invGmix_columnTable); } shiftRows(state,nb,1); subBytes(state,nb,inv_s_box); addRoundKey(state,word,round,nb); printState(state,nb); }
byte * AES128::encrypt(byte *message) { int i; memcpy((void*)state, (const void*)message,16); initKey(); addRoundKey(); for(i = 0; i < 9; i++) { subBytes(); shiftRows(); mixColumns(); computeKey(pgm_read_byte(rcon + i)); addRoundKey(); } subBytes(); shiftRows(); computeKey(pgm_read_byte(rcon + i)); addRoundKey(); memcpy((void*)message,(const void*)state, 16); return message; }
void invMixColumns(unsigned char state[4][4], unsigned int INVP) { mixColumns(state, INVP); //invMixColumns is same, except we find circleX of a inverse and state }
__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; }