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 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 }
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); }
//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); } }
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 AES::encrypt() { round = 0; keyAdd(); round++; for (; round < 10; round++) runRound(); subBytes(); shiftRows(); keyAdd(); printData(); }
int main(int argc, char *argv[]) { byte shortkey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; byte expandedkey[176]; keyExpansion(shortkey, expandedkey); byte state[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; shiftRows(state); int i; for(i = 0;i < 16;i++){ printf(" %02x ", state[i]); if((i + 1) % 4 == 0){ printf("\n"); } } return 0; }
bool shiftRows_test() { std::cout << "\nshiftRows_test:\n"; BYTE a[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, }; DWORD d[4] = { setBytes(a[0]), setBytes(a[1]), setBytes(a[2]), setBytes(a[3]), }; testUtils::printArray(a); BYTE arrState[4][c_bBlockSize] = {0}; setToState(d, arrState); std::cout << "\nshiftRows\n"; shiftRows(arrState); getFromState(d, arrState); a[0][0] = getByte(d[0], 0); a[0][1] = getByte(d[0], 1); a[0][2] = getByte(d[0], 2); a[0][3] = getByte(d[0], 3); a[1][0] = getByte(d[1], 0); a[1][1] = getByte(d[1], 1); a[1][2] = getByte(d[1], 2); a[1][3] = getByte(d[1], 3); a[2][0] = getByte(d[2], 0); a[2][1] = getByte(d[2], 1); a[2][2] = getByte(d[2], 2); a[2][3] = getByte(d[2], 3); a[3][0] = getByte(d[3], 0); a[3][1] = getByte(d[3], 1); a[3][2] = getByte(d[3], 2); a[3][3] = getByte(d[3], 3); testUtils::printArray(a); std::cout << "\ninvShiftRows\n"; invShiftRows(arrState); getFromState(d, arrState); a[0][0] = getByte(d[0], 0); a[0][1] = getByte(d[0], 1); a[0][2] = getByte(d[0], 2); a[0][3] = getByte(d[0], 3); a[1][0] = getByte(d[1], 0); a[1][1] = getByte(d[1], 1); a[1][2] = getByte(d[1], 2); a[1][3] = getByte(d[1], 3); a[2][0] = getByte(d[2], 0); a[2][1] = getByte(d[2], 1); a[2][2] = getByte(d[2], 2); a[2][3] = getByte(d[2], 3); a[3][0] = getByte(d[3], 0); a[3][1] = getByte(d[3], 1); a[3][2] = getByte(d[3], 2); a[3][3] = getByte(d[3], 3); testUtils::printArray(a); return true; }