示例#1
0
void aes_indep_enc(uint8_t * pt)
{
	static uint8_t j[0];

	//Encryption with trigger enabled
	aes256_enc(j, pt, &ctx, 1);
}
示例#2
0
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++;
    }
  }   
}
示例#3
0
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++;
    }
  }
}
示例#4
0
/*
 * 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++;
		}
	}
}
示例#5
0
void crypto_test(void){
	uint8_t test_data[16], test_key[32];
	aes256_ctx_t ctx;
	memset(test_key, 0xA5, 32);
	memset(test_data, 0, 16);
	aes256_init(test_key, &ctx);
	aes256_enc(test_data, &ctx);
	cli_putstr("\r\ncrypto test data:\r\n");
	cli_hexdump_block(test_data, 16, 4, 8);
}
示例#6
0
int do_aes_internal(unsigned char bEncryptFlag,unsigned char * pIN, int nINLen, unsigned char *pOUT, int * pOUTLen)
{
	int nRet = -1;
	int nAESblkLen=16;
	aes256_ctx_t ctx;
	unsigned char blk_buf[64];
	int nLoadLen = nAESblkLen;
	
	//check
	if(!pIN || !nINLen || !pOUT || !pOUTLen)
		return nRet;

	//clear memory first?
	memset(pOUT,0,nINLen);
	
	memset(&ctx,0,sizeof(ctx));
	memcpy(ctx.key,g_AESkey,14*sizeof(aes_roundkey_t));	
		
	*pOUTLen = 0;
	do{
		memset(blk_buf,0,sizeof(blk_buf));
		memcpy(blk_buf,pIN+*pOUTLen,nLoadLen);
		if(bEncryptFlag)
			aes256_enc(blk_buf,&ctx);
		else
			aes256_dec(blk_buf,&ctx);
		memcpy(pOUT+*pOUTLen,blk_buf,nLoadLen);
		*pOUTLen += nLoadLen;
		if(*pOUTLen + nLoadLen > nINLen)
			nLoadLen = nINLen - *pOUTLen;
				
	}while(*pOUTLen != nINLen);
	
	nRet = 0;	
	
	return nRet;
}
示例#7
0
文件: AESLib.c 项目: 9zigen/AESLib
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
// key is assumed to be 256bit thus 32 uint8_t's
void aes256_enc_single(const uint8_t* key, void* data){
	aes256_ctx_t ctx;
	aes256_init(key, &ctx);
	aes256_enc(data, &ctx);
}
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
	aes256_enc(cipher, &key_init); // Encrypt message
	//	emit = micros(); // time end

	//	Serial.print(F("Encryption total takes: "));
	//	Serial.print(emit - time);
	//	Serial.println(F(" [ms]"));

	//	if (beVerbose)
	//	{
	//	    Serial.println();
	//	    Serial.println("CT:");
	//	    for (int i = 0; i < N_BLOCK; i++)
	//		Serial.print(cipher[i]);
	//	}

	//	delete [] cipher; // do not forget to free ram
    }
    // ------------------------------------------
    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);
}