//加密运算,inputSize必须为128,192,256位 void Encrypt(RijndaelContextPtr context,void* input) { unsigned int round; //明文的最大长度 unsigned long state[8] = {0}; unsigned char* inputStringPtr = (unsigned char*)input; unsigned int nb = context->nb; //经过这个过程,由a0 a1 a2 a3 a4 a5组成的字节序列就组成了 // a0 a4 a8 a12 a16 ... // a1 a5 a9 a13 a17 ... // a2 a6 a10 a14 a18 ... // a3 a7 a11 a15 a19 ... //这样的字节矩阵 state[0] = GETINT32(inputStringPtr,0,nb); state[1] = GETINT32(inputStringPtr,1,nb); state[2] = GETINT32(inputStringPtr,2,nb); state[3] = GETINT32(inputStringPtr,3,nb); if (nb >= (KeySize_192/4)) { state[4] = GETINT32(inputStringPtr,4,nb); state[5] = GETINT32(inputStringPtr,5,nb); } if (nb >= (KeySize_256/4)) { state[6] = GETINT32(inputStringPtr,6,nb); state[7] = GETINT32(inputStringPtr,7,nb); } AddRoundKey(context,0,state); for (round = 1; round<context->nr; round++) { SubByte(context,state); ShiftRow(context,state); MixColumn(context,state); AddRoundKey(context,round,state); } SubByte(context,state); ShiftRow(context,state); AddRoundKey(context,context->nr,state); //就这个格式输出的密文,其实也没有关系的 //将格式纠正 StateToChars((unsigned char*)input,state,context->nb); }
/* encrypt one 128 bit block */ void HAM_CALLCONV aes_encrypt (uchar *in, uchar *expkey, uchar *out) { uchar state[Nb * 4]; unsigned round; memcpy (state, in, Nb * 4); AddRoundKey ((unsigned *)state, (unsigned *)expkey); for( round = 1; round < Nr + 1; round++ ) { if( round < Nr ) MixSubColumns (state); else ShiftRows (state); AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb); } memcpy (out, state, sizeof(state)); }
void Decrypt (uchar *in, uchar *expkey, uchar *out) { uchar state[Nb * 4]; unsigned round; memcpy (state, in, sizeof(state)); AddRoundKey ((unsigned *)state, (unsigned *)expkey + Nr * Nb); InvShiftRows(state); for( round = Nr; round--; ) { AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb); if( round ) InvMixSubColumns (state); } memcpy (out, state, sizeof(state)); }
// Cipher is the main function that encrypts the PlainText. void Cipher() { int i,j,round=0; //Copy the input PlainText to state array. for(i=0;i<4;i++) { for(j=0;j<4;j++) { state[j][i] = in[i*4 + j]; } } // Add the First round key to the state before starting the rounds. AddRoundKey(0); // There will be Nr rounds. // The first Nr-1 rounds are identical. // These Nr-1 rounds are executed in the loop below. for(round=1;round<Nr;round++) { SubBytes(); ShiftRows(); MixColumns(); AddRoundKey(round); } // The last round is given below. // The MixColumns function is not here in the last round. SubBytes(); ShiftRows(); AddRoundKey(Nr); // The encryption process is over. // Copy the state array to output array. for(i=0;i<4;i++) { for(j=0;j<4;j++) { out[i*4+j]=state[j][i]; } } }
void Cipher(WORD block[Nb], WORD key[Nb*(Nr+1)]) { for(int round=1; round<Nr; round++) { log_it("Round %d started with:\n", block); // ByteSub for(int i=0; i<Nb; i++) { BYTE* temp = (BYTE*)&block[i]; for (int j = 0; j < 4; j++) temp[j] = SubBytesTab[temp[j]]; block[i] = pack(temp); } log_it("After ByteSub\n", block); // ShiftRows ShiftRows(block); log_it("After ShiftRows\n", block); // MixColumn MixColumn(block); log_it("After MixColumn\n", block); // AddRoundKey AddRoundKey(block, &key[4*round]); log_it("After AddRoundKey\n", block); } for(int i=0; i<Nb; i++) { BYTE* temp = (BYTE*)&block[i]; for (int j = 0; j < 4; j++) temp[j] = SubBytesTab[temp[j]]; block[i] = pack(temp); } ShiftRows(block); AddRoundKey(block, &key[4*Nr]); log_it("After AddRoundKey\n", block); }
// decrypt is the main function that decrypts the CipherText. void decrypt(uint8_t *out, uint8_t *in, uint8_t *expanded_key) { int i,j,round=0; uint8_t state[4][4]; int Nr = 128/32+6;// replace 128 by 192, 256 for larger keys //Copy the input CipherText to state array. for(i=0; i<4; i++) { for(j=0; j<4; j++) { state[j][i] = in[i*4 + j]; } } // Add the First round key to the state before starting the rounds. AddRoundKey(Nr,state,expanded_key); // There will be Nr rounds. // The first Nr-1 rounds are identical. // These Nr-1 rounds are executed in the loop below. for(round=Nr-1; round>0; round--) { InvShiftRows(state); InvSubBytes(state); AddRoundKey(round,state,expanded_key); InvMixColumns(state); } // The last round is given below. // The MixColumns function is not here in the last round. InvShiftRows(state); InvSubBytes(state); AddRoundKey(0,state,expanded_key); // The decryption process is over. // Copy the state array to output array. for(i=0; i<4; i++) { for(j=0; j<4; j++) { out[i*4+j]=state[j][i]; } } }
/** * This method performs decrypt operation given cipher. * It read tables from tf and ciphertext from fp. * Decrypts using key. Using fiestel structure, decrypts is nothigs but encryption * with reverse round keys input. */ void ProcessDecrypt(char *key, FILE *tf, FILE *fp) { table_check = 0; ProcessTableCheck(tf); char buf[16]; int ret = fread(buf, 1, 16, fp); if (ret < 16) { fprintf(stderr, "Input size for decryption can not be less than 16 bytes\n"); exit(1); } int i, Nr = 10, Nb = 4, round; unsigned char **state = (unsigned char **) malloc( sizeof(unsigned char *) * 4); for (i = 0; i < 4; i++) state[i] = (unsigned char *) malloc(sizeof(unsigned char) * 4); copyInStateArray(state, buf); unsigned char **word = doProcessKeyExpand(key); printOutD(state, "iinput", 0); AddRoundKey(state, word, Nr * Nb); printWordD(word, "ik_sch", Nr * Nb, 0); for (round = Nr - 1; round > 0; round--) { printOutD(state, "istart", Nr - round); InvShiftRows(state); printOutD(state, "is_row", Nr - round); InvSubBytes(state); printOutD(state, "is_box", Nr - round); AddRoundKey(state, word, round * Nb); printWordD(word, "ik_sch", round * Nb, Nr - round); printOutD(state, "ik_add", Nr - round); MixColumns(state, INVP); } printOutD(state, "istart", Nr - round); InvShiftRows(state); printOutD(state, "is_row", Nr - round); InvSubBytes(state); printOutD(state, "is_box", Nr - round); AddRoundKey(state, word, 0); printWordD(word, "ik_sch", round * Nb, Nr - round); printOutD(state, "ioutput", Nr - round); }
unsigned char* AES::Cipher(unsigned char* input) { unsigned char state[4][4]; int i = 0; int r = 0; int c = 0; for(r=0; r<4; r++) { for(c=0; c<4 ; c++) { state[r][c] = input[c*4+r]; } } AddRoundKey(state,w[0]); for(i=1; i<=10; i++) { SubBytes(state); ShiftRows(state); if(i!=10) { MixColumns(state); } AddRoundKey(state,w[i]); } for(r=0; r<4; r++) { for(c=0; c<4 ; c++) { input[c*4+r] = state[r][c]; } } return input; }
void AES_Cipher(void *_ctx, u1 *data) { char r; AES_CTX *ctx = (AES_CTX *)_ctx; u4 *w = ctx->W; AddRoundKey((u4 *)data, w); w += 4; for(r = 1;r < ctx->Nr;r++) { SubBytes(data); ShiftRows(data); MixColumns(data); AddRoundKey((u4 *)data, w); w += 4; } SubBytes(data); ShiftRows(data); AddRoundKey((u4 *)data, w); }
unsigned char* AES::InvCipher(unsigned char* input) { unsigned char state[4][4]; int i = 0; int r = 0; int c = 0; for(r=0; r<4; r++) { for(c=0; c<4 ; c++) { state[r][c] = input[c*4+r]; } } AddRoundKey(state, w[10]); for(i=9; i>=0; i--) { InvShiftRows(state); InvSubBytes(state); AddRoundKey(state, w[i]); if(i) { InvMixColumns(state); } } for(r=0; r<4; r++) { for(c=0; c<4 ; c++) { input[c*4+r] = state[r][c]; } } return input; }
void AES_InvCipher(void *_ctx, u1 *data) { char r; AES_CTX *ctx = (AES_CTX *)_ctx; u4 *w = ctx->W; w = &w[ctx->Nr * Nb]; AddRoundKey((u4 *)data, w); w -= 4; for(r = 1;r < ctx->Nr;r++) { InvShiftRows(data); InvSubBytes(data); AddRoundKey((u4 *)data, w); InvMixColumns(data); w -= 4; } InvShiftRows(data); InvSubBytes(data); AddRoundKey((u4 *)data, w); }
static void buildAESCircuit(GarbledCircuit *gc, block *inputLabels) { GarblingContext ctxt; int q = 50000; //Just an upper bound int r = 50000; int *addKeyInputs = calloc(2 * m, sizeof(int)); int *addKeyOutputs = calloc(m, sizeof(int)); int *subBytesOutputs = calloc(m, sizeof(int)); int *shiftRowsOutputs = calloc(m, sizeof(int)); int *mixColumnOutputs = calloc(m, sizeof(int)); block *outputMap = allocate_blocks(2 * m); createEmptyGarbledCircuit(gc, n, m, q, r, inputLabels); startBuilding(gc, &ctxt); countToN(addKeyInputs, 256); for (int round = 0; round < roundLimit; round++) { AddRoundKey(gc, &ctxt, addKeyInputs, addKeyOutputs); for (int i = 0; i < 16; i++) { SubBytes(gc, &ctxt, addKeyOutputs + 8 * i, subBytesOutputs + 8 * i); } ShiftRows(gc, &ctxt, subBytesOutputs, shiftRowsOutputs); for (int i = 0; i < 4; i++) { if (round != roundLimit - 1) MixColumns(gc, &ctxt, shiftRowsOutputs + i * 32, mixColumnOutputs + 32 * i); } for (int i = 0; i < 128; i++) { addKeyInputs[i] = mixColumnOutputs[i]; addKeyInputs[i + 128] = (round + 2) * 128 + i; } } finishBuilding(gc, &ctxt, outputMap, mixColumnOutputs); /* writeCircuitToFile(gc, AES_CIRCUIT_FILE_NAME); */ free(addKeyInputs); free(addKeyOutputs); free(subBytesOutputs); free(shiftRowsOutputs); free(mixColumnOutputs); free(outputMap); }
/////////////////////////////////////////////////////////////////////////////// // 函数名: BlockDecrypt // 描述: 对单块数据解密。 // 输入参数: pState -- 状态数据。 // 输出参数: pState -- 解密后的状态数据。 // 返回值: 无。 /////////////////////////////////////////////////////////////////////////////// static void BlockDecrypt(unsigned char *pState) { unsigned char i; AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]); for (i = Nr; i > 0; i--) // i = [Nr, 1] { ShiftRows(pState, 1); SubBytes(pState, 4*Nb, 1); AddRoundKey(pState, &g_roundKeyTable[4*Nb*(i-1)]); if (i != 1) { MixColumns(pState, 1); } } // 为了节省代码,合并到循化执行 // ShiftRows(pState, 1); // SubBytes(pState, 4*Nb, 1); // AddRoundKey(pState, g_roundKeyTable); }
void DecryptBlock(void* pEncrypted, const u_int32_ard *pKeys) { // XOR the first key to the first state. AddRoundKey(pEncrypted, pKeys, ROUNDS); #if defined(unroll_decrypt_loop) dtransform(pEncrypted, pKeys, 9); dtransform(pEncrypted, pKeys, 8); dtransform(pEncrypted, pKeys, 7); dtransform(pEncrypted, pKeys, 6); dtransform(pEncrypted, pKeys, 5); dtransform(pEncrypted, pKeys, 4); dtransform(pEncrypted, pKeys, 3); dtransform(pEncrypted, pKeys, 2); dtransform(pEncrypted, pKeys, 1); #else int round; for(round=ROUNDS-1; round>0; round--) { // FIXME: use non-arduino specific debug #ifdef verbose_debug Serial.print("Encryption round " ); Serial.println(round); #endif InvSubAndShift(pEncrypted); AddRoundKey(pEncrypted, pKeys, round); InvMixColumns(pEncrypted); } #endif // The last round is different (Round 0) -- there is no MixColumns. InvSubAndShift(pEncrypted); AddRoundKey(pEncrypted, pKeys, 0); } // DecryptBlock()
/////////////////////////////////////////////////////////////////////////////// // 函数名: BlockEncrypt // 描述: 对单块数据加密。 // 输入参数: pState -- 状态数据。 // 输出参数: pState -- 加密后的状态数据。 // 返回值: 无。 /////////////////////////////////////////////////////////////////////////////// static void BlockEncrypt(unsigned char *pState) { unsigned char i; AddRoundKey(pState, g_roundKeyTable); for (i = 1; i <= Nr; i++) // i = [1, Nr] { SubBytes(pState, 4*Nb, 0); ShiftRows(pState, 0); if (i != Nr) { MixColumns(pState, 0); } AddRoundKey(pState, &g_roundKeyTable[4*Nb*i]); } // 为了节省代码,合并到循化执行 // SubBytes(pState, 4*Nb); // ShiftRows(pState, 0); // AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]); }
int main(int argc, char** argv){ print(state); KeySchedule(); AddRoundKey(); for(iteracja = 1; iteracja < ROUND_cnt; ++iteracja){ SubBytes(); ShiftRows(); MixColumns(); print(state); AddRoundKey(); } SubBytes(); ShiftRows() AddRoundKey(); print(state); /*encrypted*/ AddRoundKey(); InvShiftRows(); InvSubBytes(); for(iteracja = ROUND_cnt-1; iteracja > 0; --iteracja){ AddRoundKey(); InvMixColumns(); InvShiftRows(); InvSubBytes(); } AddRoundKey(); print(state); return 0; }
/* * Copyright (C) 2005 * Akira Iwata & Masayuki Sato * Akira Iwata Laboratory, * Nagoya Institute of Technology in Japan. * * All rights reserved. * * This software is written by Masayuki Sato. * And if you want to contact us, send an email to Kimitake Wakayama * ([email protected]) * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. All advertising materials mentioning features or use of this software must * display the following acknowledgment: * "This product includes software developed by Akira Iwata Laboratory, * Nagoya Institute of Technology in Japan (http://mars.elcom.nitech.ac.jp/)." * * 4. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by Akira Iwata Laboratory, * Nagoya Institute of Technology in Japan (http://mars.elcom.nitech.ac.jp/)." * * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. * AKIRA IWATA LABORATORY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, * IN NO EVENT SHALL AKIRA IWATA LABORATORY BE LIABLE FOR ANY SPECIAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * */ int decrypt (int statemt[32], int key[32], int type) { int i; /* +--------------------------------------------------------------------------+ | * Test Vector (added for CHStone) | | out_enc_statemt : expected output data for "decrypt" | +--------------------------------------------------------------------------+ */ const int out_dec_statemt[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x7, 0x34 }; KeySchedule (type, key); switch (type) { case 128128: round = 10; nb = 4; break; case 128192: case 192192: round = 12; nb = 6; break; case 192128: round = 12; nb = 4; break; case 128256: case 192256: round = 14; nb = 8; break; case 256128: round = 14; nb = 4; break; case 256192: round = 14; nb = 6; break; case 256256: round = 14; nb = 8; break; } AddRoundKey (statemt, type, round); InversShiftRow_ByteSub (statemt, nb); for (i = round - 1; i >= 1; --i) { AddRoundKey_InversMixColumn (statemt, nb, i); InversShiftRow_ByteSub (statemt, nb); } AddRoundKey (statemt, type, 0); printf ("\ndecrypto message\t"); for (i = 0; i < ((type % 1000) / 8); ++i) { if (statemt[i] < 16) printf ("0"); printf ("%x", statemt[i]); } for (i = 0; i < 16; i++) main_result += (statemt[i] != out_dec_statemt[i]); return 0; }
// La fonction cipher sert a chiffrer le bloc de message void Cipher(unsigned char *C, unsigned char M[], unsigned char Key[], int Nr) { int i,j,round=0; unsigned char T[16],U[16], V[16],W[16]; unsigned char RoundKey[240]; //On fabrique les clefs de ronde KeyExpansion( RoundKey, Key, 10); //On copide le messge dans le bloc d'etat for(i=0;i<4;i++) { for(j=0;j<4;j++) { W[4*j+i] = M[4*j + i]; } } // for (i=0; i<4;i++) { // for (j=16; j<24; j++) { // if (!(j%4)) // printf(" "); // printf("%02x ", RoundKey[j*4 + i]); // } // printf("\n"); // } //printf("\n"); // AddKey de la premiere clef AddRoundKey(T,W,RoundKey,0); // Il y a Nr rondes. // Les Nr-1 premieres rondes sont identiques // Ces Nr-1 rondes sont effectuées dans la boucle for ci-dessous for(round=1;round<Nr;round++) { SubBytes(U,T); ShiftRows(V,U); MixColumns(W,V); AddRoundKey(T,W,RoundKey,round); } // Derniere ronde // La fonction MixColumns n'est pas dans la derniere ronde SubBytes(U,T); ShiftRows(V,U); AddRoundKey(W,V,RoundKey,Nr); // Le processus de chiffremen est fini // On copie le bloc d'etat du message dans le bloc de message chiffre for(i=0;i<4;i++) { for(j=0;j<4;j++) { C[j*4+i]=W[4*j+i]; } } }
void dechiffrerBloc(unsigned char mat[TAILLEMATRICE][TAILLEMATRICE], unsigned char cleEtendue[TAILLEMATRICE*(NBROUND+1)][TAILLEMATRICE]){ int ligne, colonne, round; // Traitement du bloc //BLOC CHIFFRE------------------------------------------ printf("\n---------------------------------\n"); printf("\nBLOC CHIFFRE \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n"); //FIN BLOC CHIFFRE----------------------------------- AddRoundKey(cleEtendue,NBROUND,mat); /* //ADD ROUND KEY------------------------------------------ printf("ADD ROUND KEY [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //FIN ADD ROUND KEY-----------------------------------*/ for(round=NBROUND-1; round>=0; round--){ InvShiftRow(mat); /* //INV SHIFT ROW------------------------------------------ printf("SHIFT ROW [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //INV SHIFT ROW-----------------------------------*/ InvSubBytes(mat); /*//INV SUB BYTES------------------------------------------ printf("INV SUBBYTES[%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //INV SUBBYTES------------------------------------------*/ AddRoundKey(cleEtendue,round,mat); /* //ADD ROUND KEY------------------------------------------ printf("ADD ROUND KEY [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //FIN ADD ROUND KEY-----------------------------------*/ if(round!=(0)) { InvMixColumns(mat); /*//DEBUG INV MIXCOLUMNS------------------------------------------ printf("DEBUG INV MIXCOLUMNS [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG INV MIXCOLUMNS---------------------------------------*/ } } //DEBUG MESSAGE CRYPTE EN HEXA------------------------------------------ printf("BLOC DECHIFRRE EN HEXA \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } }
void chiffrerBloc(unsigned char mat[TAILLEMATRICE][TAILLEMATRICE], unsigned char cleEtendue[TAILLEMATRICE*(NBROUND+1)][TAILLEMATRICE]){ int ligne, colonne, round; // Traitement du bloc for(round=0; round<NBROUND; round++){ /*//DEBUG BLOC EN CLAIR ------------------------------------------ printf("\nBLOC EN CLAIR \n"); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG BLOC EN CLAIR---------------------------------------*/ AddRoundKey(cleEtendue,round,mat); /*//DEBUG ADDROUNDKEY---------------------------------------------- printf("\nADDROUNDKEY [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG BLOC EN CLAIR---------------------------------------*/ SubBytes(mat); /*//DEBUG SUBBYTES------------------------------------------ printf("DEBUG SUBBYTES[%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG SUBBYTES---------------------------------------*/ ShiftRow(mat); /*//DEBUG SHIFTROW------------------------------------------ printf("DEBUG SHIFTROW[%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG SHIFTROW---------------------------------------*/ if(round!=(NBROUND-1)) { MixColumns(mat); /* //DEBUG MIXCOLUMNS------------------------------------------ printf("DEBUG MIXCOLUMNS [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG MIXCOLUMNS---------------------------------------*/ } } AddRoundKey(cleEtendue,round,mat); //DEBUG MESSAGE CRYPTE EN HEXA------------------------------------------ printf("BLOC CHIFFRE EN HEXA \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } // FIN DEBUG MESSAGE CRYPTE EN HEXA--------------------------------------- }
static void aes_add_round_keys(const TAES &key, TAES &state) { /* unroll AddRoundKey */ AddRoundKey(0); AddRoundKey(1); AddRoundKey(2); AddRoundKey(3); AddRoundKey(4); AddRoundKey(5); AddRoundKey(6); AddRoundKey(7); AddRoundKey(8); AddRoundKey(9); AddRoundKey(10); AddRoundKey(11); AddRoundKey(12); AddRoundKey(13); AddRoundKey(14); AddRoundKey(15); }
void encrypt() { bzero(StateArray, 4*4*sizeof(unsigned char)); bzero(ExpandedKey, 11*4*sizeof(unsigned char)); #if (AES_PRINT & AES_PRINT_MAIN) xil_printf("-- Test Encyption Key \r\n\n"); AES_printf(Key); xil_printf("-------------------------\r\n\n"); xil_printf("-- Test Plaintext \r\n\n"); AES_printf(PlainText); xil_printf("-------------------------\r\n\n"); #endif #if (AES_PRINT & AES_PRINT_MAIN) xil_printf("-- Starting Key Expension \r\n\n"); #endif ExpandKey(Key, ExpandedKey); // #if (AES_PRINT & AES_PRINT_MAIN) xil_printf("-- Starting Encryption \r\n\n"); #endif long int x; for(x=0; x<100; x++) { memcpy(StateArray, PlainText, 4*4*sizeof(unsigned char)); #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Start of Round 0 \r\n\n"); AES_printf(StateArray); xil_printf("-------------------------\r\n\n"); #endif AddRoundKey(ExpandedKey[0], StateArray); // #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Start of Round 0 \r\n\n"); AES_printf(StateArray); xil_printf("-------------------------\r\n\n"); #endif int i; //Rounds for(i=1; i<=10; i++) { SubBytes(StateArray); #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Round %d after SubBytes \r\n\n", i); AES_printf(StateArray); xil_printf("-------------------\r\n\n"); #endif ShiftRows(StateArray); #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Round %d after shiftRows \r\n\n", i); AES_printf(StateArray); xil_printf("-----------------------------\r\n\n"); #endif if(i != 10) { MixColumns(StateArray); #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Round %d after MixColumns \r\n\n", i); AES_printf(StateArray); xil_printf("-----------------------------\r\n\n"); #endif } AddRoundKey(ExpandedKey[i], StateArray); #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- Test State - Round %d after RoundKeyValue \r\n\n", i); AES_printf(StateArray); xil_printf("-----------------------------\r\n\n"); #endif } } #if (AES_PRINT & AES_PRINT_DETAILS) xil_printf("-- AES key expansion and encryption test completed. \r\n\n"); xil_printf("-- Test State - End \r\n\n"); AES_printf(StateArray); xil_printf("-----------------------------\r\n\n"); #endif xil_printf("****************Encryption*********************\r\n"); }
int aes::AES_do_decrypt_from_file(char *infile, char *outfile, unsigned long *CifKey) { BYTE* in = new BYTE[4*Nb]; printf("Decoding...\n"); GenPowerTab(); GenSubBytesTab(); FILE* stream_in; FILE* stream_out; if ( !(stream_in = fopen(infile, "rb"))) { printf("File in: %s cannot be read", infile); return -1; } if ( !(stream_out = fopen(outfile, "wb")) ) { printf("File out: %s cannot be read", outfile); return -1; } fpos_t flen; // fseek(stream_in, 0, SEEK_END); fgetpos(stream_in, &flen); unsigned long rlen = file_len(flen); // fseek(stream_in, 0, SEEK_SET); WORD ExpKey[Nb*(Nr+1)]; //WORD CifKey[Nk] = { 0x00010203, 0x04050607, // 0x08090a0b, 0x0c0d0e0f}; KeyExpansion(CifKey, ExpKey); while(rlen > 0 && !feof(stream_in)) { unsigned long len = (unsigned long)fread(in, 1, 4*Nb, stream_in); if (rlen < 4*Nb) for (int i = rlen; i < 4*Nb; i++) in[i] = 0; rlen -= len; //if (len != 4*Nb) #ifdef LOGit printf("\nNew block\n"); for(int i=0; i<4; i++) { printf("%02x %02x %02x %02x\n", in[i], in[4+i], in[8+i], in[12+i]); } #endif AddRoundKey((WORD*)in, &ExpKey[4*Nr]); InvCipher((WORD*)in, ExpKey); if (rlen == 1) { BYTE* out = new BYTE[1]; fread(out, sizeof(BYTE), 1, stream_in); len = out[0]; rlen = 0; } int nWritten = fwrite(in, sizeof(BYTE), len, stream_out); } fclose(stream_out); }
/* ******* encrypto ************ */ int encrypt (int statemt[32], int key[32], int type) { int i; /* +--------------------------------------------------------------------------+ | * Test Vector (added for CHStone) | | out_enc_statemt : expected output data for "encrypt" | +--------------------------------------------------------------------------+ */ const int out_enc_statemt[16] = { 0x39, 0x25, 0x84, 0x1d, 0x2, 0xdc, 0x9, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0xb, 0x32 }; KeySchedule (type, key); switch (type) { case 128128: round = 0; nb = 4; break; case 192128: round = 2; nb = 4; break; case 256128: round = 4; nb = 4; break; case 128192: case 192192: round = 2; nb = 6; break; case 256192: round = 4; nb = 6; break; case 128256: case 192256: case 256256: round = 4; nb = 8; break; } AddRoundKey (statemt, type, 0); for (i = 1; i <= round + 9; ++i) { ByteSub_ShiftRow (statemt, nb); MixColumn_AddRoundKey (statemt, nb, i); } ByteSub_ShiftRow (statemt, nb); AddRoundKey (statemt, type, i); printf ("encrypted message \t"); for (i = 0; i < nb * 4; ++i) { if (statemt[i] < 16) printf ("0"); printf ("%x", statemt[i]); } for (i = 0; i < 16; i++) main_result += (statemt[i] != out_enc_statemt[i]); return 0; }