Beispiel #1
0
/*
 * encrypt(keySize,password,original_message,encrypted_message, mode, padding,
 *          initialVector) - encrypts a message using AES Algorithm
 * 
 * Parameters: 
 *    keySize : Size of key to use in AES Algorithm
 *    password: Key to encrypt plaintext message.
 *    original_message: Plaintext message before calculating AES Algorithm
 *    encrypted_message: Ciphertext message after calculating AES Algorithm
 *    mode: cipher mode, CBC
 *    padding: padding mode to fill blocks, tree ways PKCS5, ZEROS, X923
 *    initialVector: 
 * 
 * Examples:
 *  AES.encrypt(128,"libelium","Libelium",encrypted_message,CBC,PKCS5,IV)
 * 
 */
uint8_t WaspAES::encrypt(uint16_t keySize,char* password,char original_message[],uint8_t* encrypted_message,uint8_t mode,uint8_t padding,uint8_t* initialVector){
  
  // Variables declaration 
  uint16_t size;
  size = sizeOfBlocks(original_message);
  uint8_t original_data[size];   
 
  if (init(password,keySize)){
    
    // Convert original message to uint8_t format
    for (uint16_t i = 0; i < strlen(original_message); i++){
      original_data[i] = original_message[i];
    }
    if (strlen(original_message) < size ) { // Se necesita rellenar el bloque
      paddingEncrypt(original_data,size,strlen(original_message),padding);
    }
   
    if (mode == CBC){
       CBCEncrypt(original_data,size,initialVector,keySize);
    }

    // Convert original_data to char format
    for (uint16_t i=0; i < size+1;i++){
      encrypted_message[i] = original_data[i];
    }
    return 1;
  
  }else{
    return 0;
  }
}
Beispiel #2
0
/*
 * encrypt(keySize,password,original_message,encrypted_message, mode, padding,
 *          initialVector) - encrypts a message using AES Algorithm
 *
 * Parameters:
 *    keySize : Size of key to use in AES Algorithm
 *    password: Key to encrypt plaintext message.
 *    original_message: Plaintext message before calculating AES Algorithm
 *    encrypted_message: Ciphertext message after calculating AES Algorithm
 *    mode: cipher mode, CBC
 *    padding: padding mode to fill blocks, tree ways PKCS5, ZEROS, X923
 *    initialVector:
 *
 */
uint8_t WaspAES::encrypt(	uint16_t keySize,
							char* password,
							uint8_t* original_message,
							uint16_t length,
							uint8_t* encrypted_message,
							uint8_t mode,
							uint8_t padding,
							uint8_t* initialVector)
{
	// Calculate length in Bytes of the encrypted message 
	// depending on the size of the original message
	uint16_t size;
	size = sizeOfBlocks(length);
	
	uint8_t original_data[size];

	if ( init(password,keySize) )
	{
		// Convert original message to uint8_t format
		for (uint16_t i = 0; i < length; i++)
		{
			original_data[i] = original_message[i];
		}
    
		if ( length < size ) 
		{ 
			// Se necesita rellenar el bloque
			paddingEncrypt( original_data, size, length, padding);
		}

		if (mode == CBC)
		{
			CBCEncrypt(original_data,size,initialVector,keySize);
		}

		// Copy result
		for (uint16_t i=0; i < size;i++)
		{
			encrypted_message[i] = original_data[i];
		}
		return 1;

	}
	else
	{
		return 0;
	}
}
int AES128CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
{
    return CBCEncrypt(enc, iv, data, size, pad, out);
}
Beispiel #4
0
double encrypt_decrypt_test(int blockCount, int repetitions)
{
  byte_ard Key[] = {
    0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c 
  };

  byte_ard IV[] = {
    0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,  0x30, 0x30 
  };

  // Allocate memory for key schedule and generate it.
  byte_ard Keys[KEY_BYTES*12];
  KeyExpansion(Key, Keys);

  // allocate some buffers to hold data
  int length = 16*blockCount;
  unsigned char *buf = (unsigned char *)malloc(length);
  unsigned char *ecbuf = (unsigned char *)malloc(length);
  unsigned char *dcbuf = (unsigned char *)malloc(length);

  // Fill the plaintext buffer with random data of multiples of block length.
  srand(time(NULL));
  for ( int i=0; i<(16*blockCount); i++ )
    buf[i] = rand() % 0xFF;
  
  // Start the timed test
  timeval tstart, tstop, tresult;  
  gettimeofday(&tstart,NULL);
  for( int i=0; i<repetitions; i++ )
  {
  	CBCEncrypt(	(void *)buf, (void *)ecbuf, length, AUTOPAD, 
				(const u_int32_ard*)Keys, (const u_int16_ard*)IV);
  	CBCDecrypt(	(void *)ecbuf, (void *)dcbuf, length, 
				(const u_int32_ard*)Keys, (const u_int16_ard*)IV);
  }
  gettimeofday(&tstop,NULL);

  // Calculate the results
  timersub(&tstop,&tstart,&tresult);
  double totaltime = tresult.tv_sec * 1000000.0 + tresult.tv_usec;
  double timePerOperation = totaltime / repetitions;
  double timePerBlock = timePerOperation / blockCount;

  // Check if decryption is consistent with plaintext
  if ( strncmp((char *)buf, (char *)dcbuf,length) != 0 )
	printf("Error in encrypt/decrypt!\n");

  // Output timing results.
  printf("blocks: %d\n", blockCount);
  printf("Duration: %f usec\n", totaltime);
  printf("Per operation: %f usec\n", timePerOperation);
  printf("Per block: %f usec\n", timePerBlock);
  printf("\n");

  /****
  // Dont print the blocks except when debugging
  printf("\nPlaintext : \n");
  printBytes2((byte_ard*)buf, length);

  printf("\nAfter CBC: \n");
  printBytes2((byte_ard*)ecbuf, length);
     
  printf("\nCBC Decipher: \n");
  printBytes2((byte_ard*)dcbuf, length);
  ****/

  return timePerBlock;
}