void aes_encrypt_dpi(svBitVecVal * ct, svBitVecVal * key, int Nk, svBitVecVal * pt) { int i; byte_t KEY[32]; byte_t RKEY[240]; byte_t PT[16], CT[16]; byte_t * bp; word_t * wp; // Number of 32-bit words comprising the Cipher Key. // For this standard, Nk = 4, 6, or 8. (Sec 6.3) bp = key; for (i = 0; i < 4*Nk; i++) KEY[i] = *bp++; bp = pt; for (i = 0; i < 16; i++) PT[i] = *bp++; KeyExpansion(KEY, RKEY, Nk); Cipher(PT, CT, RKEY, Nk); wp = (word_t *) CT; for (i = 0; i < 4; i++) ct[i] = *wp++; }
int main() { int i; int Nr = 0; int Nk = 0; unsigned char Key[32]= {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; unsigned char in[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; unsigned char out[16]; InitUART1(); while(Nr!=128 && Nr!=192 && Nr!=256) { SendString("Enter the length of Key(128, 192 or 256 only): "); Nr = GetKey(); } Nk = Nr / 32; Nr = Nk + 6; KeyExpansion(Key, Nk, Nr); Cipher(in, out, Nr); SendString("Text encrypted: "); for(i = 0; i < Nb*4; i++) { SendStringFromNum(out[i]); } SendEnter(); while(1); }
void CBm53AES::Cipher(char *input, char *output) { unsigned char uch_input[1024]; strToUChar(input, uch_input); Cipher(uch_input); ucharToHex(uch_input,output,16); }
//int main(int argc, char **argv) int main() { int i, Nr=0, Nk=0, NN=0; unsigned char in[16], out[16], Key[32], RoundKey[240]; // Receive the length of key here. while(NN!=128 && NN!=192 && NN!=256) { printf("Enter the length of Key(128, 192 or 256 only): "); scanf("%d",&NN); } // Calculate Nk and Nr from the received value. Nk = NN / 32; Nr = Nk + 6; // Part 1 is for demonstrative purpose. The key and plaintext are given in the program itself. // Part 1: ******************************************************** // The array temp stores the key. // The array temp2 stores the plaintext. unsigned char temp[16] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 , 0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; unsigned char temp2[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 , 0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; // Copy the Key and PlainText for(i=0;i<Nk*4;i++) { Key[i]=temp[i]; in[i]=temp2[i]; } // ********************************************************* // Uncomment Part 2 if you need to read Key and PlainText from the keyboard. // Part 2: ******************************************************** /* //Clear the input buffer flushall(); //Recieve the Key from the user printf("Enter the Key in hexadecimal: "); for(i=0;i<Nk*4;i++) { scanf("%x",&Key[i]); } printf("Enter the PlainText in hexadecimal: "); for(i=0;i<Nb*4;i++) { scanf("%x",&in[i]); } */ // ******************************************************** // The KeyExpansion routine must be called before encryption. AESKeyExpansion(RoundKey, Key, NN); // The next function call encrypts the PlainText with the Key using AES algorithm. Cipher(out, in, RoundKey, Nr); // Output the encrypted text. printf("\nText after encryption:\n"); for(i=0;i<Nk*4;i++) { printf("%02x ",out[i]); } printf("\n\n"); return 0; }
void SetOutReport (void) { /* FIO2CLR = LEDMSK; FIO2SET = OutReport[0]; lcd_clear(); lcd_print (&OutReport[1]); set_cursor (0, 1); lcd_print (&OutReport[17]); */ FIO2CLR = LEDMSK; if(OutReport[0] == AUTHENTICATION_CODE) { FIO2SET = 1; Decryp[0] = 'K'; Decryp[1] = 'e'; Decryp[2] = 'i'; Decryp[3] ='l'; Decryp[4] = ' '; Decryp[5] = 'M'; Decryp[6] = 'B';Decryp[7] ='C'; Decryp[8] = '2'; Decryp[9] = '3'; Decryp[10] = '0';Decryp[11] ='0'; Decryp[12] = ' '; Decryp[13] = 'H'; Decryp[14] = 'I';Decryp[15] = 'D'; InReport[0] = AUTHENTICATION_CODE; } else//if(OutReport[0] == CHECK_EXISTANCE_CODE) { FIO2SET = 2; Decryp[0] = 'C'; Decryp[1] = 'h'; Decryp[2] = 'e'; Decryp[3] ='c'; Decryp[4] = 'k'; Decryp[5] = ' '; Decryp[6] = 'e';Decryp[7] ='x'; Decryp[8] = 'i'; Decryp[9] = 's'; Decryp[10] = 't';Decryp[11] ='a'; Decryp[12] = 'n'; Decryp[13] = 'c'; Decryp[14] = 'e';Decryp[15] = '!'; InReport[0] = CHECK_EXISTANCE_CODE; } Cipher(Decryp, Encryp, Nr); }
void ZEncryptAES::AES_Encrypt(char *input, int in_size, char *output) { //set em for(int i=0;i<in_size;i += 16) { in = (unsigned char*)(input + i); out = (unsigned char*)(output + i); Cipher(); } }
int main() { int i; Nr = 128; Nk = Nr / 32; Nr = Nk + 6; unsigned char temp[16] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; unsigned char temp2[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; for(i=0; i<Nk*4; i++) { Key[i]=temp[i]; in[i]=temp2[i]; } printf("Advanced Encryption Standart (AES)\n\nText before encryption:\n"); for(i=0; i<Nk*4; i++) { printf("%02x ",in[i]); } printf("\n\n"); KeyExpansion(); Cipher(); printf("\nText after encryption:\n"); for(i=0; i<Nk*4; i++) { printf("%02x ",out[i]); } printf("\n\n"); for(i=0; i<Nk*4; i++) { in[i] = out[i]; } KeyExpansion(); InvCipher(); printf("\nText after decryption:\n"); for(i=0; i<Nk*4; i++) { printf("%02x ",out[i]); } printf("\n\n"); getch(); return 0; }
void AES128_CFB_decrypt(uint8x16_t* output, uint8x16_t input, uint32_t length, uint8x16_t RoundKey_v[], uint8x16_t iv_v) { if(length == 0) { Iv = iv_v; } *output = Iv; state = (state_t*)output; Cipher(RoundKey_v); XorWithIn(output,input); Iv = input; }
int main (int argc, char* argv[]) { char* input; unsigned offset; printf("Please input a string: \n"); scanf("%s", input); printf("Please input an even offset between 0 and 255: \n"); scanf("%d", &offset); Cipher(input, offset); printf("After Cipher: %s", input); }
void* AES::Cipher(void* input, int length) { unsigned char* in = (unsigned char*) input; int i; if(!length) { while(*(in+length++)); in = (unsigned char*) input; } for(i=0; i<length; i+=16) { Cipher(in+i); } return input; }
/** * @brief Encrypts or decrypts parts of a packet. * * This method is designed for parallel encryption and decryption of packets. Where * a packet is split up into chunks and the thread acts upon. * * @return some actions may require further activity by the thread which * cannot take place within the method call. This return value can be used * by the thread to determine what to do. */ void * ThreadMessageItemEncrypt::TakeAction() { for(size_t n = (ENCRYPTION_CHUNK_SIZE*threadID); n<packetSize; n+=(ENCRYPTION_CHUNK_SIZE*numThreads)) { if(encrypt == true) { Cipher(packet+n,packet+n,key.GetNumRounds(),key.GetRoundKeys()); } else { InverseCipher(packet+n,packet+n,key.GetNumRounds(),key.GetRoundKeys()); } } return NULL; }
/********************************************* * ∫Ø ˝√˚£∫ CBm53AES::CipherStr * √Ë ˆ£∫ ’˚∂ŒŒƒµµº”√‹ * @≤Œ ˝£∫ char *input * @≤Œ ˝£∫ char *output * @∑µªÿ÷µ£∫ void *********************************************/ void CBm53AES::CipherStr(const char *input, char *output) { int nLen = strlen(input); char * newAlock = (char*)malloc(strlen(input)+(16-nLen%16)+1); strcpy(newAlock,input); for (int n=0;n<(16-nLen%16);n++) { newAlock[nLen+n] = (16-nLen%16); } newAlock[nLen + (16-nLen%16)] = 0; int i=0,j=0; char block[16],e_block[32]; while (newAlock[i]!='\0') { strncpy(block,newAlock+i,16); Cipher(block,e_block); strncpy(output+j,e_block,32); i+=16; j+=32; } output[j]='\0'; free(newAlock); }
/* * === FUNCTION ====================================================================== * Name: main * Description: * ===================================================================================== */ int main(int argc, char **argv) { // Compteurs unsigned int i, j, k; // Clé de chiffrement unsigned char K[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; // Clé de déchiffrement unsigned char DK[16] = {0}; unsigned char Ksol[16] = {0}; // Ensemble des messages unsigned char **M = (unsigned char **) calloc (256, sizeof(unsigned char *)); unsigned char **C = (unsigned char **) calloc (256, sizeof(unsigned char *)); unsigned char **M2 = (unsigned char **) calloc (256, sizeof(unsigned char *)); unsigned char **C2 = (unsigned char **) calloc (256, sizeof(unsigned char *)); // Somme unsigned char SUM[16] = {0}; unsigned char *T = (unsigned char *) calloc (16, sizeof(unsigned char *)); unsigned char *T2 = (unsigned char *) calloc (16, sizeof(unsigned char *)); // Allocation de mémoire for (i=0; i<256; i++) { M[i] = (unsigned char *) calloc (16, sizeof(unsigned char)); C[i] = (unsigned char *) calloc (16, sizeof(unsigned char)); M2[i] = (unsigned char *) calloc (16, sizeof(unsigned char)); C2[i] = (unsigned char *) calloc (16, sizeof(unsigned char)); // Saturation d'un octet M[i][0] = i; M2[i][7] = i; // Octet fixe M[i][1] = 0x23; M[i][2] = 0x54; M[i][3] = 0x26; M[i][4] = 0xc7; M[i][5] = 0x29; M[i][6] = 0x2b; M[i][7] = 0x2c; M[i][8] = 0x2d; M[i][9] = 0x4f; M2[i][7] = 0xf2; M2[i][8] = 0xe5; M2[i][9] = 0xa2; M2[i][10] = 0x02; M2[i][11] = 0x63; M2[i][12] = 0xf8; M2[i][13] = 0x3d; M2[i][14] = 0xf4; M2[i][15] = 0x85; } // Crypter les messages (4 tours) for (i=0; i<256; i++) { Cipher(C[i], M[i], K, 4); Cipher(C2[i], M2[i], K, 4); //affichage_tableau4par4(C[i]); //affichage_tableau4par4(C2[i]); } // Attaque de la clé octet par octet // NOTE : Pour attaquer la clé il n'y a à prioris pas besoin de faire l'opération de ShiftRow vu // qu'elle ne fait que déplacer un octet, elle ne change pas la valeur de la somme. // Les 16 octets de la clé for (i=0; i<16; i++) { // Les 256 valeurs possibles de l'octet for (k=0; k<256; k++) { DK[i] = k; // Les 256 messages for (j=0; j<256; j++) { unsigned char t1, t2; t1 = DK[i]^C[j][i]; t2 = DK[i]^C2[j][i]; t1 = sboxinv[t1]; t2 = sboxinv[t2]; T[i] = T[i]^t1; T2[i] = T2[i]^t2; } if (T[i]==0 && T2[i]==0) break; if (k == 255) printf("Bordel de merde!\n"); } } printf("XOR de tous les messages après ajout de la clé (1e série):\n"); affichage_tableau4par4(T); printf("XOR de tous les messages après ajout de la clé (2e série):\n"); affichage_tableau4par4(T2); printf("Clé de la dernière ronde (fausse pour l'instant) :\n"); affichage_tableau4par4(DK); for (i=4; i>0; i--) { ExpandKeyInv(Ksol, DK, i); Affecte(DK, Ksol); } printf("Clé de chiffrement utilisée :\n"); affichage_tableau4par4(K); printf("Clé de chiffrement trouvée (fausse pour l'instant) :\n"); affichage_tableau4par4(Ksol); // Libération de mémoire for (i=0; i<256; i++) { free(M[i]); free(C[i]); free(M2[i]); free(C2[i]); } free(M); free(C); free(T); free(M2); free(C2); free(T2); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
// See aes128_key_wrap_unwrap.h for documentation on this function. void do_aes128_key_wrap(const uint8_t plaintext[], uint8_t wrapped_ciphertext[], uint32_t expanded_kek[]) { uint32_t i, j; // loop counters uint8_t in[16]; // 128-bit temporary plaintext input vector // step 1: initialize the byte-sized data variables // set A = IV // for i = 1 to n // R[i] = P[i] a[0] = iv[0]; a[1] = iv[1]; a[2] = iv[2]; a[3] = iv[3]; a[4] = iv[4]; a[5] = iv[5]; a[6] = iv[6]; a[7] = iv[7]; for (i = 1; i <= N; i++) { r[8 * i + 0] = plaintext[8 * (i - 1) + 0]; r[8 * i + 1] = plaintext[8 * (i - 1) + 1]; r[8 * i + 2] = plaintext[8 * (i - 1) + 2]; r[8 * i + 3] = plaintext[8 * (i - 1) + 3]; r[8 * i + 4] = plaintext[8 * (i - 1) + 4]; r[8 * i + 5] = plaintext[8 * (i - 1) + 5]; r[8 * i + 6] = plaintext[8 * (i - 1) + 6]; r[8 * i + 7] = plaintext[8 * (i - 1) + 7]; } // step 2: calculate intermediate values // for j = 0 to 5 // for i = 1 to n // B = AES(K, A | R[i]) // A = MSB(64, B) ^ (n*j)+i // R[i] = LSB(64, B) for (j = 0; j <= 5; j++) { for (i = 1; i <= N; i++) { in[0] = a[0]; in[1] = a[1]; in[2] = a[2]; in[3] = a[3]; in[4] = a[4]; in[5] = a[5]; in[6] = a[6]; in[7] = a[7]; in[8] = r[8 * i + 0]; in[9] = r[8 * i + 1]; in[10] = r[8 * i + 2]; in[11] = r[8 * i + 3]; in[12] = r[8 * i + 4]; in[13] = r[8 * i + 5]; in[14] = r[8 * i + 6]; in[15] = r[8 * i + 7]; #if defined BOOTLOADER_HOST Cipher(in, expanded_kek, 10, in); // perform aes128 encryption #else aes_encrypt((uint32_t *)in, expanded_kek, (uint32_t *)in); #endif // BOOTLOADER_HOST a[0] = in[0]; a[1] = in[1]; a[2] = in[2]; a[3] = in[3]; a[4] = in[4]; a[5] = in[5]; a[6] = in[6]; a[7] = in[7] ^ ((N * j) + i); r[8 * i + 0] = in[8]; r[8 * i + 1] = in[9]; r[8 * i + 2] = in[10]; r[8 * i + 3] = in[11]; r[8 * i + 4] = in[12]; r[8 * i + 5] = in[13]; r[8 * i + 6] = in[14]; r[8 * i + 7] = in[15]; } // end for (i) } // end for (j) // step 3: output the results // set C[0] = A // for i = 1 to n // C[i] = R[i] wrapped_ciphertext[0] = a[0]; wrapped_ciphertext[1] = a[1]; wrapped_ciphertext[2] = a[2]; wrapped_ciphertext[3] = a[3]; wrapped_ciphertext[4] = a[4]; wrapped_ciphertext[5] = a[5]; wrapped_ciphertext[6] = a[6]; wrapped_ciphertext[7] = a[7]; for (i = 1; i <= N; i++) { wrapped_ciphertext[8 * i + 0] = r[8 * i + 0]; wrapped_ciphertext[8 * i + 1] = r[8 * i + 1]; wrapped_ciphertext[8 * i + 2] = r[8 * i + 2]; wrapped_ciphertext[8 * i + 3] = r[8 * i + 3]; wrapped_ciphertext[8 * i + 4] = r[8 * i + 4]; wrapped_ciphertext[8 * i + 5] = r[8 * i + 5]; wrapped_ciphertext[8 * i + 6] = r[8 * i + 6]; wrapped_ciphertext[8 * i + 7] = r[8 * i + 7]; } }
int main() { int i; // Recieve the length of key here. while(Nr!=128 && Nr!=192 && Nr!=256) { printf("Enter the length of Key(128, 192 or 256 only): "); scanf("%d",&Nr); } // Calculate Nk and Nr from the recieved value. Nk = Nr / 32; Nr = Nk + 6; // Part 1 is for demonstrative purpose. The key and plaintext are given in the program itself. // Part 1: ******************************************************** // The array temp stores the key. // The array temp2 stores the plaintext. // unsigned char temp[32] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; // unsigned char temp2[32]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; unsigned char temp2[32] = {0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34}; //c2 assign plaintext = 128'h486C4EEE671D9D0D4DE3B138D65F58E7; // // // // // assign plaintext = 128'h58D268EEEB4A070ACDB0E0AC2B5425ED; // // // // //assign plaintext = 128'h52000000000000000000000000000000; // // //assign inikey = 128'h52000000000000000000000000000000; // // // unsigned char temp[32] = {0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c}; // // // Copy the Key and PlainText for(i=0;i<Nk*4;i++) { Key[i]=temp[i]; in[i]=temp2[i]; } // ********************************************************* // Uncomment Part 2 if you need to read key and plaintext from the keyboard. // Part 2: ******************************************************** /* //Clear the input buffer flushall(); //Recieve the key from the user printf("Enter the Key in hexadecimal: "); for(i=0;i<Nk*4;i++) { scanf("%x",&Key[i]); } printf("Enter the PlainText in hexadecimal: "); for(i=0;i<Nb*4;i++) { scanf("%x",&in[i]); } */ // ******************************************************** // The KeyExpansion routine must be called before encryption. KeyExpansion(); // The next function call encrypts the PlainText with the Key using AES algorithm. Cipher(); // Output the encrypted text. printf("\nText after encryption:\n"); for(i=0;i<Nb*4;i++) { printf("%02x ",out[i]); } printf("\n\n"); return 0; }
int aes::AES_do_crypt_from_file(char *infile, char *outfile, unsigned long *CifKey) { BYTE* in = new BYTE[4*Nb]; printf("Encoding...\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); unsigned long len; //WORD CifKey[Nk] = { 0x00010203, 0x04050607, // 0x08090a0b, 0x0c0d0e0f}; WORD ExpKey[Nb*(Nr+1)]; KeyExpansion(CifKey, ExpKey); while(rlen > 0 && !feof(stream_in)) { 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; #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); Cipher((WORD*)in, ExpKey); int nWritten = fwrite(in, sizeof(BYTE), 4*Nb, stream_out); } in[0] = len; int nWritten = fwrite(in, sizeof(BYTE), 1, stream_out); fclose(stream_out); }
uint32_t Decrypt(uint8_t* o, const uint8_t* i, uint32_t l, uint8_t m, const uint8_t* k, const uint8_t* IV) { return Cipher(o, i, l, m, k, IV, true); }