void WaspAES::CBCEncrypt(uint8_t *original_data,uint16_t size, uint8_t *InitialVector,uint16_t keySize){ /////////////////////////// // In CBC mode the message is divided into blocks of 16 bytes, to 1 block // Applied to the XOR and calculated its block cipher with block // Encryption XOR obtained is applied to the second data block in clear // And the result is encrypted with AES given, the result will be the 2nd block // Encryption, so on. // // This function is: // 1 - separated data block of 16 bytes, // 2 - XOR operation is performed with the IV / block precesor // 3 - was called to the encryption function // 4 - Once all blocks are encrypted will join // 5 - forms the encrypted message and returns // /////////////////////////// uint8_t IV[16]; uint8_t Plain_text[16]; uint8_t Previous_block[16]; uint16_t index,index2; index = 0; index2 = 0; //Assign Initial Vector to IV variable assignBlock(IV,InitialVector); while(index<size){ // Encrypt for (int i =0; i<16;i++){ Plain_text[i]= original_data[index]; index++; } if (index == 16){ XOR(Plain_text,IV,block_data); }else { XOR(Plain_text,Previous_block,block_data); } switch(keySize){ case 128: aes128_enc(block_data, &ctx128); break; case 192: aes192_enc(block_data, &ctx192); break; case 256: aes256_enc(block_data, &ctx256); break; } assignBlock(Previous_block,block_data); for (int i = 0; i<16;i++){ original_data[index2] = block_data[i]; index2++; } } }
void WaspAES::ECBEncrypt(uint8_t *original_data,uint16_t size,uint16_t keySize){ // In ECB mode is separated from message in blocks of 16 bytes and cipher each one individually uint16_t index,index2; index = 0; index2 = 0; while( index < size ){ for (int i =0; i<16;i++){ block_data[i]=original_data[index]; index++; } // Encrypt switch(keySize){ case 128: aes128_enc(block_data, &ctx128); break; case 192: aes192_enc(block_data, &ctx192); break; case 256: aes256_enc(block_data, &ctx256); break; } for (int i = 0; i<16;i++){ original_data[index2] = block_data[i]; index2++; } } }
/* * CBCEncrypt - Encrypt message using CBC mode * * In CBC mode the message is divided into blocks of 16 bytes, to 1 block * Applied to the XOR and calculated its block cipher with block * Encryption XOR obtained is applied to the second data block in clear * And the result is encrypted with AES given, the result will be the 2nd block * Encryption, so on. * * This function is: * 1 - separated data block of 16 bytes, * 2 - XOR operation is performed with the IV / block precesor * 3 - was called to the encryption function * 4 - Once all blocks are encrypted will join * 5 - forms the encrypted message and returns * */ void WaspAES::CBCEncrypt( uint8_t *original_data, uint16_t size, uint8_t *InitialVector, uint16_t keySize) { uint8_t IV[16]; uint8_t Plain_text[16]; uint8_t Previous_block[16]; uint16_t index,index2; index = 0; index2 = 0; //Assign Initial Vector to IV variable assignBlock(IV,InitialVector); while( index < size ) { // Copy 16B block for (int i =0; i<16 ; i++) { Plain_text[i] = original_data[index]; index++; } // Perform XOR with corresponding IV block if ( index == 16) { // the first time the XOR is done with the Original IV XOR( Plain_text, IV, block_data); } else { // the rest of the times, the XOR is done with // the previous encrypted block XOR( Plain_text, Previous_block, block_data); } switch(keySize) { case 128: aes128_enc(block_data, &ctx128); break; case 192: aes192_enc(block_data, &ctx192); break; case 256: aes256_enc(block_data, &ctx256); break; } assignBlock( Previous_block, block_data); for (int i = 0; i<16;i++) { original_data[index2] = block_data[i]; index2++; } } }
void loop() { Serial.print(F("Before encryption of message freeMemory()= ")); Serial.print(freeRam()); Serial.println(F(" [byte]")); time = micros(); // time start // ------------------------------------------ // for (int piece = 0; piece < N_BLOCK; piece += N_BLOCK, pPlain += N_BLOCK) for (int piece = 0; piece < DATALENGTH; piece += N_BLOCK, pPlain += N_BLOCK) { // Divide message into a block of 128 bit = 16 byte // byte cipher[N_BLOCK]; // getBlockOfMsg(pPlain, cipher); // if (beVerbose) // { // Serial.println(); // Serial.println("OT:"); // for (unsigned int i = 0; i < N_BLOCK; i++) // Serial.print(char(cipher[i])); // } // time = micros(); // time start // aes192_enc(cipher, &key_init); // Encrypt message aes192_enc(pPlain, &key_init); // Encrypt message // emit = micros(); // time end // Serial.print(F("Encryption total takes: ")); // Serial.print(emit - time); // Serial.println(F(" [us]")); // if (beVerbose) // { // Serial.println(); // Serial.println("CT:"); // for (int i = 0; i < N_BLOCK; i++) // Serial.print(cipher[i]); // // Serial.println(); // Serial.println("HASH:"); // for (int i = 0; i < N_BLOCK; i++) // Serial.print(hash[i]); // } } // void hmac_md5(void* dest, void* key, uint16_t keylength_b, void* msg, uint32_t msglength_b); byte hash[16]; hmac_md5(hash, key, 192, plain, 8192); // ------------------------------------------ emit = micros(); // time start Serial.print(F("After encryption of message freeMemory()= ")); Serial.print(freeRam()); Serial.println(F(" [byte]")); Serial.print(F("Encryption total takes: ")); Serial.print(emit - time); Serial.println(F(" [us]")); Serial.println(); Serial.println(F("Light")); digitalWrite(ledPin, HIGH); // set the LED on delay(1000); Serial.println(F("Dark")); digitalWrite(ledPin, LOW); // set the LED off delay(1000); }