Esempio n. 1
0
int main (int argc, char *argv[])
{
    aes256_context ctx; 
    uint8_t key[32];
    uint8_t buf[16], i;

    /* put a test vector */
    for (i = 0; i < sizeof(buf);i++) buf[i] = i * 16 + i;
    for (i = 0; i < sizeof(key);i++) key[i] = i;

    DUMP("txt: ", i, buf, sizeof(buf));
    DUMP("key: ", i, key, sizeof(key));
    printf("---\n");

    aes256_init(&ctx, key);
    aes256_encrypt_ecb(&ctx, buf);

    DUMP("enc: ", i, buf, sizeof(buf));
    printf("tst: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89\n");

    aes256_init(&ctx, key);
    aes256_decrypt_ecb(&ctx, buf);
    DUMP("dec: ", i, buf, sizeof(buf));

    aes256_done(&ctx);

    return 0;
} /* main */
int main (int argc, char *argv[])
{
    aes256_context ctx;
	FILE *in_file;
	long fileSize;
	FILE *out_file;
    uint8_t key[32];
	uint8_t i;
	uint8_t fileBuf[16];
	uint8_t *tmpBuf;
	time_t start, end;
	float gap;

	in_file = fopen(ENCODED_FILE_NAME, "rb");
	out_file = fopen(DECODED_FILE_NAME, "wb");
	if(in_file == NULL) exit(1);

	fseek(in_file, 0, SEEK_END);
	fileSize = ftell(in_file);
	rewind(in_file);
	
	//get a test key
	for (i = 0; i < sizeof(key);i++) key[i] = i;
	
	start = clock();

	while( fread(fileBuf, 1, sizeof(fileBuf), in_file) )
	{
		aes256_init(&ctx, key);
		aes256_decrypt_ecb(&ctx, fileBuf);
		
		fwrite(fileBuf, 1, sizeof(fileBuf), out_file);
		fileSize -= sizeof(fileBuf);

		if(0 < fileSize && fileSize < sizeof(fileBuf))
		{
			tmpBuf = (uint8_t *)malloc(sizeof(uint8_t)*fileSize);
			fread(tmpBuf, 1, fileSize, in_file);
			
			aes256_init(&ctx, key);
			aes256_decrypt_ecb(&ctx, tmpBuf);
			
			fwrite(tmpBuf, 1, fileSize, out_file);
			break;
		}
	}
	
	aes256_done(&ctx);

	end = clock();
	gap = (float) (end - start)/CLOCKS_PER_SEC;
	printf("걸린시간 : %f초", gap);

	fclose(in_file);
	fclose(out_file);
    return 0;
}
Esempio n. 3
0
// ------------------------------------------------------------------------------------------------
bool AES256::Init(CSStr key)
{
    // Clear current key, if any
    aes256_done(&m_Context);
    // Is the specified key empty?
    if (!key || *key == '\0')
    {
        return false; // Leave the context with an empty key
    }
    // Obtain the specified key size
    const Uint32 size = (std::strlen(key) * sizeof(SQChar));
    // See if the key size is accepted
    if (size > sizeof(m_Buffer))
    {
        STHROWF("The specified key is out of bounds: %u > %u", size, sizeof(m_Buffer));
    }
    // Initialize the key buffer to 0
    std::memset(m_Buffer, 0, sizeof(m_Buffer));
    // Copy the key into the key buffer
    std::memcpy(m_Buffer, key, size);
    // Initialize the context with the specified key
    aes256_init(&m_Context, m_Buffer);
    // This context was successfully initialized
    return true;
}
Esempio n. 4
0
File: id.c Progetto: SakaZulu/mifare
int id_init(int handle, const id_desc_t *desc, mifare_tag_t *tag,
		const void *id, int32_t counter)
{
	int i, block = desc->sector * 4;

	/* Generate per card key */
	uint8_t aes_key[32];
	memcpy(aes_key, desc->aes_key, 32);
	for (i = 0; i < 32; i++)
		aes_key[i] ^= tag->serial[i & 3];

	/* Prepare buffer */
	uint8_t buf[16];
	memcpy(buf, desc->magic, 4);
	memcpy(&buf[4], id, 12);

	/* Encrypt buffer */
	aes256_context aes;
	aes256_init(&aes, aes_key);
	aes256_encrypt_ecb(&aes, buf);
	aes256_done(&aes);

	if (mifare_select(handle, tag))
		return ID_ERROR_SELECT;
	if (mifare_login(handle, desc->sector, desc->key_type, desc->key))
		return ID_ERROR_LOGIN;
	if (mifare_write_block(handle, block + 1,  buf))
		return ID_ERROR_WRITE_BLOCK;
	if (mifare_write_value(handle, block + 2, 0))
		return ID_ERROR_WRITE_VALUE;

	return ID_OK;
}
Esempio n. 5
0
void aes256_decrypt(const aes_secret& key, aes_block& block)
{
    aes256_context context;
    aes256_init(&context, key.data());
    aes256_decrypt_ecb(&context, block.data());
    aes256_done(&context);
}
void setup()
{
    // Set up serial port for debugging
    Serial.begin(9600);

    while (!Serial)
    {
	; // wait for serial port to connect. Needed for Leonardo only
    }

    // Fullfil the data array
    createDataArray();

    // Read and set the key from the EEPROM
    readKey(KEYLENGTH);

    // Inicialization of the key
    time = micros(); // time start
    aes256_init(key, &key_init);
    emit = micros(); // time start

    Serial.print(F("Inicialisation total takes: "));
    Serial.print(emit - time);
    Serial.println(F(" [us]"));

    // initialize the digital pin as an output.
    // Pin 13 has an LED connected on most Arduino boards:
    pinMode(ledPin, OUTPUT);
}
Esempio n. 7
0
void FileAccessEncrypted::close() {

	if (!file)
		return;

	if (writing) {

		Vector<uint8_t> compressed;
		size_t len = data.size();
		if (len % 16) {
			len+=16-(len % 16);
		}

		MD5_CTX md5;
		MD5Init(&md5);
		MD5Update(&md5,data.ptr(),data.size());
		MD5Final(&md5);

		compressed.resize(len);
		zeromem( compressed.ptr(), len );
		for(int i=0;i<data.size();i++) {
			compressed[i]=data[i];
		}

		aes256_context ctx;
		aes256_init(&ctx,key.ptr());

		for(size_t i=0;i<len;i+=16) {

			aes256_encrypt_ecb(&ctx,&compressed[i]);
		}

		aes256_done(&ctx);

		file->store_32(COMP_MAGIC);
		file->store_32(mode);


		file->store_buffer(md5.digest,16);
		file->store_64(data.size());

		file->store_buffer(compressed.ptr(),compressed.size());
		file->close();
		memdelete(file);
		file=NULL;
		data.clear();

	} else {

		file->close();
		memdelete(file);
		data.clear();
		file=NULL;
	}



}
Esempio n. 8
0
uint8_t WaspAES::init(char* Password, uint16_t keySize){
  uint8_t* key;

  // Key Initialition 
  switch(keySize){
    case 128:
      key = (uint8_t*) calloc(16,sizeof(uint8_t));
      if (strlen(Password) < 17) {
      	for (uint16_t i=0; i < strlen(Password);i++){
      	  key[i] = Password[i];
      	}
      	if (strlen(Password) < 16){
      	  for(int aux = strlen(Password); aux < 16; aux++){
      	    key[aux] = 0;
      	  }      
      	}
      }
    	aes128_init(key, &ctx128);
      break;
      
    case 192:
      key = (uint8_t*) calloc(24,sizeof(uint8_t));
      if (strlen(Password) < 25) {
      	for (uint16_t i = 0; i < strlen(Password);i++){
      	  key[i] = Password[i];
      	}
      	if (strlen(Password) < 24){
      	  for(int aux = strlen(Password); aux < 24; aux++){
      	    key[aux] = 0;
      	  }      
      	}      	
      }
      aes192_init(key, &ctx192);
      break;
      
    case 256:

      key = (uint8_t*) calloc(32,sizeof(uint8_t));
      if (strlen(Password) < 33) {
      	for (uint16_t i = 0; i < strlen(Password);i++){
      	  key[i] = Password[i];
      	}
      	if (strlen(Password) < 32){
      	  for(int aux = strlen(Password); aux < 32; aux++){
      	    key[aux] = 0;
      	  }      
      	}
      }
      aes256_init(key, &ctx256);
      break;
      
    default:
      return 0;
  }
  return 1;
  
}
Esempio n. 9
0
PUBLIC ScriptData *script_data_create() {
	ScriptData *script = MALLOC(sizeof(ScriptData));
	if (NULL == script) {
		LOGE("script_data_create() ERROR: malloc fail!!");
		return NULL;
	}
	aes256_init(&script->ctx, AES_KEY);
	return script;
}
Esempio n. 10
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);
}
Esempio n. 11
0
void encrypt(uint8_t key[32], uint8_t *buf, unsigned long numbytes)
{
  printf("\nBeginning encryption\n");
  aes256_init(key);
  unsigned long offset;

  //printf("Creating %d threads over %d blocks\n", dimBlock.x*dimGrid.x, dimBlock.x);
  for (offset = 0; offset < numbytes; offset += AES_BLOCK_SIZE)
    aes256_encrypt_ecb(buf, offset);
}
Esempio n. 12
0
int main(int argc, char *argv[])
{
    aes256_context ctx;
    uint8_t k[32], v[16], x[24];
    char key[45], msg[33];
    int key_len, msg_len;
    unsigned int i;
    
    if (argc != 3)
    {
      printf("Uso: descifraping clave mensaje\n");
      return 1;
    }
    
    memset(k, 0, sizeof(k));
    memset(v, 0, sizeof(v));
    memset(x, 0, sizeof(x));
    memset(key, 'A', sizeof(key));
    memset(msg, 'A', sizeof(msg));
    key_len = strlen(argv[1]);
    msg_len = strlen(argv[2]);
    key_len = (key_len>44) ? 44 : key_len;
    msg_len = (msg_len>32) ? 32 : msg_len;
    
    strncpy(key+(44-key_len), argv[1], (key_len));
    strncpy(msg+(32-msg_len), argv[2], (msg_len));
    key[44]='\0';
    msg[32]='\0';
    
    printf("String clave (base64): %s\n", key);
    printf("String mensaje.......: %s\n", msg);
    
    base64_to_buf_r(k, (unsigned char *) key);
    base64_to_buf_r(x, (unsigned char *) msg);
    memcpy(k+24,x,8);
    memcpy(v,x+8,16);

    DUMP("CLAVE....: ", i, k, sizeof(k));
    DUMP("ENTRADA..: ", i, x, sizeof(x));
    DUMP("ENCRIPT..: ", i, v, sizeof(v));
    aes256_init(&ctx, k);
    aes256_decrypt_ecb(&ctx, v);
    aes256_done(&ctx);
    DUMP("DESCRIPT.: ", i, v, sizeof(v));
    printf("Resultado: %s\n\n", v);

    exit(EXIT_SUCCESS);
}
Esempio n. 13
0
void testrun_testkey_aes256(void){
	uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
	                    0x15, 0xca, 0x71, 0xbe,
	                    0x2b, 0x73, 0xae, 0xf0,
	                    0x85, 0x7d, 0x77, 0x81,
	                    0x1f, 0x35, 0x2c, 0x07,
	                    0x3b, 0x61, 0x08, 0xd7,
	                    0x2d, 0x98, 0x10, 0xa3,
	                    0x09, 0x14, 0xdf, 0xf4};
	aes256_ctx_t ctx;
	uint8_t i;
	memset(&ctx, 0, sizeof(aes256_ctx_t));
	aes256_init(key, &ctx);
	cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   ");
	cli_hexdump(key, 32);
	for(i=0; i<15; ++i){
		cli_putstr("\r\n index: ");
		cli_putc('0'+i/10);
		cli_putc('0'+i%10);
		cli_putstr(" roundkey ");
		cli_hexdump(ctx.key[i].ks, 16);
	}
}
Esempio n. 14
0
File: id.c Progetto: SakaZulu/mifare
int id_read(int handle, const id_desc_t *desc, mifare_tag_t *tag,
		void *id, int32_t *counter)
{
	int i, block = desc->sector * 4;

	/* Generate per card key */
	uint8_t aes_key[32];
	memcpy(aes_key, desc->aes_key, 32);
	for (i = 0; i < 32; i++)
		aes_key[i] ^= tag->serial[i & 3];

	uint8_t buf[16];

	if (mifare_select(handle, tag))
		return ID_ERROR_SELECT;
	if (mifare_login(handle, desc->sector, desc->key_type, desc->key))
		return ID_ERROR_LOGIN;
	if (mifare_read_block(handle, block + 1, buf))
		return ID_ERROR_READ_BLOCK;

	/* Decrypt block */
	aes256_context ctx;
	aes256_init(&ctx, aes_key);
	aes256_decrypt_ecb(&ctx, buf);
	aes256_done(&ctx);

	if (memcmp(buf, desc->magic, 4) != 0)
		return ID_ERROR_INVALID_MAGIC;
	if (mifare_dec_value(handle, block + 2, 1))
		return ID_ERROR_DEC_VALUE;
	if (mifare_read_value(handle, block + 2, counter))
		return ID_ERROR_READ_VALUE;

	memcpy(id, &buf[4], 12);

	return ID_OK;
}
Esempio n. 15
0
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
  static uint8_t success = 1;

  uint8_t bmRequestType = USB_ControlRequest.bmRequestType;
  uint8_t bRequest      = USB_ControlRequest.bRequest;
  //uint8_t wValue        = USB_ControlRequest.wValue;
  char data[51];

  HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);

  if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    char lock;
    uint16_t wLength = USB_ControlRequest.wLength;
    char pw[32];
    success = 1; /* default */

    eeprom_read(ADDR_LOCK, &lock, 1);
    if(lock == 0 || lock == 255)
      lock = 0;
    else
    {
      if(bRequest != OPENKUBUS_GET_NONCE && bRequest != OPENKUBUS_SET_TIMESTAMP && bRequest != OPENKUBUS_RESET)
      {
        success = 0;
        return;
      }
    }

    // read data
    if(wLength)
    {
      Endpoint_ClearSETUP();
      Endpoint_Read_Control_Stream_LE(data, MIN(sizeof(data), wLength));
      Endpoint_ClearIN();
    }

    switch(bRequest)
    {
      case OPENKUBUS_SET_LOCK:
        lock = 1;
        eeprom_write(ADDR_LOCK, &lock, 1);
        break;

      
      case OPENKUBUS_SET_OWNER:
        if(wLength)
          eeprom_write(ADDR_OWNER, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_COMPANY:
        if(wLength)
          eeprom_write(ADDR_COMPANY, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_DESCRIPTION:
        if(wLength)
          eeprom_write(ADDR_DESCRIPTION, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_ID:
        if(wLength == 4)
          eeprom_write(ADDR_ID, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_TIMESTAMP:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          eeprom_read(ADDR_SEED, pw, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            set_timestamp(array2int((uint8_t *)&data[12]));
            update_nonce();
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_RESET:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          memcpy_P(pw, MASTER_PASSWORD, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            clear_eeprom();
            wdt_enable(WDTO_15MS);
            while(1);
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_SET_SEED:
        if(wLength == LEN_SEED)
          eeprom_write(ADDR_SEED, data, LEN_SEED);
        else
          success = 0;

        break;


      case OPENKUBUS_SET_COUNTER:
        if(wLength == LEN_COUNTER)
          eeprom_write(ADDR_COUNTER, data, LEN_COUNTER);
        else
          success = 0;

        break;


      default:
        success = 0;
        break;
    }
  }
  else if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    uint8_t length = 0;
    uint8_t i;
    uint32_t temp32 = 0;
    char c;

    switch(bRequest)
    {
      case OPENKUBUS_GET_SUCCESS:
        data[length++] = success;
        break;


      case OPENKUBUS_GET_NONCE:
        for(i = 0; i < sizeof(nonce); i++)
          data[length++] = nonce[i];

        break;


      case OPENKUBUS_GET_TEMPERATURE:
        #ifdef RTC
          data[length++] = rtc_get_temperature();
        #else
          data[length++] = 0xFF;
        #endif
        break;


      case OPENKUBUS_GET_ID:
        for(i = 0; i < LEN_ID; i++)
        {
          eeprom_read(ADDR_ID+i, &c, 1); 
          data[length++] = c;
        }

        break;


      case OPENKUBUS_GET_TIME:
        temp32 = get_timestamp();

        data[length++] = temp32 >> 24;
        data[length++] = temp32 >> 16;
        data[length++] = temp32 >> 8;
        data[length++] = temp32;
        break;


      case OPENKUBUS_GET_SERIAL:
        for(i = 0x0e; i <= 0x18; i++)
          data[length++] = boot_signature_byte_get(i);
        break;


      case OPENKUBUS_GET_DESCRIPTION:
        for(i = 0; i < LEN_DESCRIPTION; i++)
        {
          eeprom_read(ADDR_DESCRIPTION+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_COMPANY:
        for(i = 0; i < LEN_COMPANY; i++)
        {
          eeprom_read(ADDR_COMPANY+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_OWNER:
        for(i = 0; i < LEN_OWNER; i++)
        {
          eeprom_read(ADDR_OWNER+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;

      default:
        data[length++] = 0;
    }

    // send data
    Endpoint_ClearSETUP();
    Endpoint_Write_Control_Stream_LE(data, length);
    Endpoint_ClearOUT();
  }
 void aes_transmitter (unsigned char bytes_to_send[], int bytes_size, HANDLE h1)
{
	int j;
	int temp1 = 16;
	
	int tempp1 = 16;
	unsigned char buff_aes1[16];
	unsigned char buff_aes2[16];
	unsigned char buff_aes3[16];
	unsigned char buff_aes4[16];
	unsigned char buff_aes5[16];
	unsigned char buff_aes6[16];
	unsigned char buff_aes7[16];
	unsigned char buff_aes8[16];
	unsigned char buff_aes9[16];
	unsigned char buff_aes10[16];
	unsigned char buff_aes11[16];
	/*unsigned char buff_aes12[16];
	unsigned char buff_aes13[16];
	unsigned char buff_aes14[16];
	unsigned char buff_aes15[16];
	unsigned char buff_aes16[16];
	unsigned char buff_aes17[16];
	unsigned char buff_aes18[16];
	unsigned char buff_aes19[16];
	unsigned char buff_aes20[16];
	unsigned char buff_aes21[16];
	unsigned char buff_aes22[16];
	unsigned char buff_aes23[16];
	unsigned char buff_aes24[16];
	unsigned char buff_aes25[16];
	unsigned char buff_aes26[16];
	unsigned char buff_aes27[16];*/

	unsigned char buff_enc[176];
	aes256_context ctx; 
    uint8_t key[32];
	uint8_t i;
	//HANDLE h1;
	//char c1[] = {"COM3"};
	int first_time;
	int temp, mytime, second_time;
	clock_t t1, t2, t;
	t1 = clock();
	first_time = time(NULL);
	for(j=0;j<sizeof(buff_aes1);j++) {
		buff_aes1[j] = bytes_to_send[j];
		buff_aes2[j] = bytes_to_send[j+16];
		buff_aes3[j] = bytes_to_send[j+32];
		buff_aes4[j] = bytes_to_send[j+48];
		buff_aes5[j] = bytes_to_send[j+64];
		buff_aes6[j] = bytes_to_send[j+80];
		buff_aes7[j] = bytes_to_send[j+96];
		buff_aes8[j] = bytes_to_send[j+112];
		buff_aes9[j] = bytes_to_send[j+128];
		buff_aes10[j] = bytes_to_send[j+144];
		buff_aes11[j] = bytes_to_send[j+160];
		/*buff_aes12[j] = bytes_to_send[j+176];
		buff_aes13[j] = bytes_to_send[j+192];
		buff_aes14[j] = bytes_to_send[j+208];
		buff_aes15[j] = bytes_to_send[j+224];
		buff_aes16[j] = bytes_to_send[j+240];
		buff_aes17[j] = bytes_to_send[j+256];
		buff_aes18[j] = bytes_to_send[j+272];
		buff_aes19[j] = bytes_to_send[j+288];
		buff_aes20[j] = bytes_to_send[j+304];
		buff_aes21[j] = bytes_to_send[j+320];
		buff_aes22[j] = bytes_to_send[j+336];
		buff_aes23[j] = bytes_to_send[j+352];
		buff_aes24[j] = bytes_to_send[j+368];
		buff_aes25[j] = bytes_to_send[j+384];
		buff_aes26[j] = bytes_to_send[j+400];
		buff_aes27[j] = bytes_to_send[j+416];
		temp1 = temp1 + 1;*/
	}
	//h1 = GetSerialPort(c1);	
	if (h1 == INVALID_HANDLE_VALUE)
	{
		fprintf(stderr, "Error opening serial port\n");
		exit(1);
	}   
	else {}
		//printf("serial port opened successfully\n");
	  /* put a test vector */
    for (i = 0; i < sizeof(key);i++) key[i] = i;	
	
//     DUMP("plain txt: ", i, bytes_to_send, bytes_size);
  //  printf("---\n");

   aes256_init(&ctx, key);
   aes256_encrypt_ecb(&ctx, buff_aes1);
   aes256_encrypt_ecb(&ctx, buff_aes2);
   aes256_encrypt_ecb(&ctx, buff_aes3);
   aes256_encrypt_ecb(&ctx, buff_aes4);
   aes256_encrypt_ecb(&ctx, buff_aes5);
   aes256_encrypt_ecb(&ctx, buff_aes6);
   aes256_encrypt_ecb(&ctx, buff_aes7);
   aes256_encrypt_ecb(&ctx, buff_aes8);
   aes256_encrypt_ecb(&ctx, buff_aes9);
   aes256_encrypt_ecb(&ctx, buff_aes10);
   aes256_encrypt_ecb(&ctx, buff_aes11);
   /*aes256_encrypt_ecb(&ctx, buff_aes12);
   aes256_encrypt_ecb(&ctx, buff_aes13);
   aes256_encrypt_ecb(&ctx, buff_aes14);
   aes256_encrypt_ecb(&ctx, buff_aes15);
   aes256_encrypt_ecb(&ctx, buff_aes16);
   aes256_encrypt_ecb(&ctx, buff_aes17);
   aes256_encrypt_ecb(&ctx, buff_aes18);
   aes256_encrypt_ecb(&ctx, buff_aes19);
   aes256_encrypt_ecb(&ctx, buff_aes20);
   aes256_encrypt_ecb(&ctx, buff_aes21);
   aes256_encrypt_ecb(&ctx, buff_aes22);
   aes256_encrypt_ecb(&ctx, buff_aes23);
   aes256_encrypt_ecb(&ctx, buff_aes24);
   aes256_encrypt_ecb(&ctx, buff_aes25);
   aes256_encrypt_ecb(&ctx, buff_aes26);
   aes256_encrypt_ecb(&ctx, buff_aes27);*/
   //DUMP("encrypted text: ", i, bytes_to_send, bytes_size);
    aes256_done(&ctx);
	
	//delay();
	for(j=0;j<sizeof(buff_aes1);j++) {
		buff_enc[j] = buff_aes1[j];
		buff_enc[j+16] = buff_aes2[j];
		buff_enc[j+32] = buff_aes3[j];
		buff_enc[j+48] = buff_aes4[j];
		buff_enc[j+64] = buff_aes5[j];
		buff_enc[j+80] = buff_aes6[j];
		buff_enc[j+96] = buff_aes7[j];
		buff_enc[j+112] = buff_aes8[j];
		buff_enc[j+128] = buff_aes9[j];
		buff_enc[j+144] = buff_aes10[j];
		buff_enc[j+160] = buff_aes11[j];
		/*buff_enc[j+176] = buff_aes12[j];
		buff_enc[j+192] = buff_aes13[j];
		buff_enc[j+208] = buff_aes14[j];
		buff_enc[j+224] = buff_aes15[j];
		buff_enc[j+240] = buff_aes16[j];
		buff_enc[j+256] = buff_aes17[j];
		buff_enc[j+272] = buff_aes18[j];
		buff_enc[j+288] = buff_aes19[j];
		buff_enc[j+304] = buff_aes20[j];
		buff_enc[j+320] = buff_aes21[j];
		buff_enc[j+336] = buff_aes22[j];
		buff_enc[j+352] = buff_aes23[j];
		buff_enc[j+368] = buff_aes24[j];
		buff_enc[j+384] = buff_aes25[j];
		buff_enc[j+400] = buff_aes26[j];
		buff_enc[j+416] = buff_aes27[j];
		tempp1 = tempp1 + 1;*/
	}
		ser_comm(buff_enc, h1);
		//ser_comm(bytes_to_send, h1);
	//printf("\n");	

	t2 = clock();
	second_time = time(NULL);
	t = t2 - t1;
	mytime = second_time - first_time;
//	printf ("Total execution time %f seconds.\n",((float)t)/CLOCKS_PER_SEC);
//	printf ("Program took me %d cycles to execute.\n",t);
//	printf("Actual time taken is %d seconds.\n", mytime);
} /* main */
Esempio n. 17
0
void aes_indep_key(uint8_t * key)
{
	aes256_init(key, &ctx);
}
Esempio n. 18
0
/*
 * 	init 
 * 
 * This function sets up the password filling with zeros until it reacehs the 
 * proper length depending on the 'keySize'. The possibilities are 128bits, 
 * 192bits and 256bits.
 * 
 * After padding with zeros, then an initialization function is called in order 
 * to create all the round keys which are stored in the proper context struct
 * 
 */ 
uint8_t WaspAES::init(char* password, uint16_t keySize)
{
	uint8_t key[32];
	memset( key, 0x00, sizeof(key) );
	
	// Key Initialition with ZEROS until the proper length
	switch(keySize)
	{
		case 128:
			// key size to be used: 16 Bytes
			if (strlen(password) < 17) 
			{
				for (uint16_t i=0; i < strlen(password);i++)
				{
					key[i] = password[i];
				}
				if (strlen(password) < 16)
				{
					for(int aux = strlen(password); aux < 16; aux++)
					{
						key[aux] = 0;
					}
				}
			}
			// Once the password is padded with zeros
			// generate the round keys from the 128 bit key 
			aes128_init(key, &ctx128);	
			break;

		case 192:
			// key size to be used: 24 Bytes
			if (strlen(password) < 25) 
			{
				for (uint16_t i = 0; i < strlen(password);i++)
				{
					key[i] = password[i];
				}
				if (strlen(password) < 24)
				{
					for(int aux = strlen(password); aux < 24; aux++)
					{
						key[aux] = 0;
					}
				}
			}
			// Once the password is padded with zeros
			// generate the round keys from the 192 bit key 
			aes192_init(key, &ctx192);
			break;

		case 256:
			// key size to be used: 32 Bytes
			if (strlen(password) < 33) 
			{
				for (uint16_t i = 0; i < strlen(password);i++)
				{
					key[i] = password[i];
				}
				if (strlen(password) < 32)
				{
					for(int aux = strlen(password); aux < 32; aux++)
					{
						key[aux] = 0;
					}
				}
			}
			// Once the password is padded with zeros
			// generate the round keys from the 256 bit key 
			aes256_init(key, &ctx256);
			break;

		default:
			return 0;
	}
	return 1;

}
Esempio n. 19
0
Error FileAccessEncrypted::open_and_parse(FileAccess *p_base,const Vector<uint8_t>& p_key,Mode p_mode) {

	print_line("open and parse!");
	ERR_FAIL_COND_V(file!=NULL,ERR_ALREADY_IN_USE);
	ERR_FAIL_COND_V(p_key.size()!=32,ERR_INVALID_PARAMETER);

	pos=0;
	eofed=false;

	if (p_mode==MODE_WRITE_AES256) {

		data.clear();
		writing=true;
		file=p_base;
		mode=p_mode;
		key=p_key;

	} else if (p_mode==MODE_READ) {

		writing=false;
		key=p_key;
		uint32_t magic = p_base->get_32();
		print_line("MAGIC: "+itos(magic));
		ERR_FAIL_COND_V(magic!=COMP_MAGIC,ERR_FILE_UNRECOGNIZED);
		mode=Mode(p_base->get_32());
		ERR_FAIL_INDEX_V(mode,MODE_MAX,ERR_FILE_CORRUPT);
		ERR_FAIL_COND_V(mode==0,ERR_FILE_CORRUPT);
		print_line("MODE: "+itos(mode));
		unsigned char md5d[16];
		p_base->get_buffer(md5d,16);
		length=p_base->get_64();
		base=p_base->get_pos();
		ERR_FAIL_COND_V(p_base->get_len() < base+length, ERR_FILE_CORRUPT );
		int ds = length;
		if (ds % 16) {
			ds+=16-(ds % 16);
		}

		data.resize(ds);

		int blen = p_base->get_buffer(data.ptr(),ds);
		ERR_FAIL_COND_V(blen!=ds,ERR_FILE_CORRUPT);

		aes256_context ctx;
		aes256_init(&ctx,key.ptr());

		for(size_t i=0;i<ds;i+=16) {

			aes256_decrypt_ecb(&ctx,&data[i]);
		}

		aes256_done(&ctx);

		data.resize(length);

		MD5_CTX md5;
		MD5Init(&md5);
		MD5Update(&md5,data.ptr(),data.size());
		MD5Final(&md5);


		ERR_FAIL_COND_V(String::md5(md5.digest)!=String::md5(md5d),ERR_FILE_CORRUPT)		;


		file=p_base;
	}

	return OK;
}
void *decryptor()
{
    unsigned char buff_aes1[16];
    unsigned char buff_aes2[16];
    unsigned char buff_aes3[16];
    unsigned char buff_aes4[16];
    unsigned char buff_aes5[16];
    unsigned char buff_aes6[16];
    unsigned char buff_aes7[16];
    unsigned char buff_aes8[16];
    unsigned char buff_aes9[16];
    unsigned char buff_aes10[16];
    unsigned char buff_aes11[16];

    unsigned char aes_dec[176], w_char1, w_char2, w_char3, w_char4;

    int temp, temp2;
    int index = 0, b;
    unsigned int filter = 32768,temp_r[9], temp_36extra[9]= {0};

    int s=0;
    int j =0;
    unsigned char key[32];
    int count_filewrite = 0;
    char out_name1[80] = "out1.bit";
    char out_name2[80] = "out2.bit";
    FILE *fp;

    unsigned char local_h2_buffer[176];

    int count = 0;
    DWORD byteswritten = 0, bytesread = 0;
    aes256_context ctx;
    unsigned char i;
    fp = fopen(out_name1, "wb");
    for (i = 0; i < sizeof(key); i++) key[i] = i;

    while(1)
    {
        sem_wait(&decryptor_start);
        memcpy(local_h2_buffer,h2_buffer,176);
        sem_post(&reciever_start);

        for(j=0; j<sizeof(buff_aes1); j++)
        {
            buff_aes1[j] = local_h2_buffer[j];
            buff_aes2[j] = local_h2_buffer[j+16];
            buff_aes3[j] = local_h2_buffer[j+32];
            buff_aes4[j] = local_h2_buffer[j+48];
            buff_aes5[j] = local_h2_buffer[j+64];
            buff_aes6[j] = local_h2_buffer[j+80];
            buff_aes7[j] = local_h2_buffer[j+96];
            buff_aes8[j] = local_h2_buffer[j+112];
            buff_aes9[j] = local_h2_buffer[j+128];
            buff_aes10[j] = local_h2_buffer[j+144];
            buff_aes11[j] = local_h2_buffer[j+160];
        }

        aes256_init(&ctx, key);
        aes256_decrypt_ecb(&ctx, buff_aes1);
        aes256_decrypt_ecb(&ctx, buff_aes2);
        aes256_decrypt_ecb(&ctx, buff_aes3);
        aes256_decrypt_ecb(&ctx, buff_aes4);
        aes256_decrypt_ecb(&ctx, buff_aes5);
        aes256_decrypt_ecb(&ctx, buff_aes6);
        aes256_decrypt_ecb(&ctx, buff_aes7);
        aes256_decrypt_ecb(&ctx, buff_aes8);
        aes256_decrypt_ecb(&ctx, buff_aes9);
        aes256_decrypt_ecb(&ctx, buff_aes10);
        aes256_decrypt_ecb(&ctx, buff_aes11);
        aes256_done(&ctx);

        for(j=0; j<sizeof(buff_aes1); j++)
        {
            local_h2_buffer[j] = buff_aes1[j];
            local_h2_buffer[j+16] = buff_aes2[j];
            local_h2_buffer[j+32] = buff_aes3[j];
            local_h2_buffer[j+48] = buff_aes4[j];
            local_h2_buffer[j+64] = buff_aes5[j];
            local_h2_buffer[j+80] = buff_aes6[j];
            local_h2_buffer[j+96] = buff_aes7[j];
            local_h2_buffer[j+112] = buff_aes8[j];
            local_h2_buffer[j+128] = buff_aes9[j];
            local_h2_buffer[j+144] = buff_aes10[j];
            local_h2_buffer[j+160] = buff_aes11[j];
        }

        fwrite((void *)local_h2_buffer,sizeof(char),176,fp);
        fwrite((void *)temp_36extra,sizeof(int),9,fp);


        count_filewrite = count_filewrite + 1;
        fclose(fp);
        sem_post(&melp_start);

        if (count_filewrite%2 == 1)
            fp = fopen(out_name2, "wb");
        else
            fp = fopen(out_name1, "wb");
    }
    pthread_exit(NULL);
}
Esempio n. 21
0
// 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);
}
Esempio n. 22
0
int main (UNUSED_ int argc, UNUSED_ char *argv[])
{
	uint8_t key[32] = {
		0xFF, 0x7A, 0x61, 0x7C, 0xE6, 0x91, 0x48, 0xE4,
		0xF1, 0x72, 0x6E, 0x2F, 0x43, 0x58, 0x1D, 0xE2,
		0xAA, 0x62, 0xD9, 0xF8, 0x05, 0x53, 0x2E, 0xDF,
		0xF1, 0xEE, 0xD6, 0x87, 0xFB, 0x54, 0x15, 0x3D
	};
	uint8_t buf[] = {
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
		0x20, 0x21, 0x22, 0x23
	};
	rfc3686_blk ctr = {
		{0x00, 0x1C, 0xC5, 0xB7},                         /* nonce   */
		{0x51, 0xA5, 0x1D, 0x70, 0xA1, 0xC1, 0x11, 0x48}, /* IV      */
		{0x00, 0x00, 0x00, 0x01}                          /* counter */
	};
	aes256_context ctx;
	int rc;
	uint8_t i;

	/*  **********************************************************
	*   First we test CTR
	*/

	printf("# CTR test\n");
	dump("Text:", buf, sizeof(buf));
	dump("Key:", key, sizeof(key));

	aes256_init(&ctx, key);
	aes256_setCtrBlk(&ctx, &ctr);

	aes256_encrypt_ctr(&ctx, buf, sizeof(buf));
	dump("Result:", buf, sizeof(buf));
	rc = mem_isequal(buf, RFC3686_TV9, sizeof(RFC3686_TV9));
	printf("\t^ %s\n", (rc == 0) ? "Ok" : "INVALID" );

	/* reset the counter to decrypt */
	ctr.ctr[0] = ctr.ctr[1] = ctr.ctr[2] = 0;
	ctr.ctr[3] = 1;
	aes256_setCtrBlk(&ctx, &ctr);
	aes256_decrypt_ctr(&ctx, buf, sizeof(buf));
	dump("Text:", buf, sizeof(buf));


	/* ************************************************************
	*   Now we test the core AES-256 ECB, just in case
	*/

	printf("\n# ECB test\n");
	for (i = 0; i < sizeof(AES256_TV); i++)
		buf[i] = ((i << 4) & 0xF0) + i;
	for (i = 0; i < sizeof(key); i++)
		key[i] = i;
	dump("Text:", buf, sizeof(AES256_TV));
	dump("Key:", key, sizeof(key));
	aes256_init(&ctx, key);
	aes256_encrypt_ecb(&ctx, buf);
	dump("Result:", buf, sizeof(AES256_TV));
	rc = mem_isequal(buf, AES256_TV, sizeof(AES256_TV));
	printf("\t^ %s\n", (rc == 0) ? "Ok" : "INVALID" );

	aes256_done(&ctx);

	return 0;
} /* main */
Esempio n. 23
0
int main
	(
	void
	)
	{
    platform_init();
	init_uart();	
	trigger_setup();
	
 	/* Uncomment this to get a HELLO message for debug */
	/*
	putch('h');
	putch('e');
	putch('l');
	putch('l');
	putch('o');
	putch('\n');
	*/
	    
	//Load Super-Secret key
    aes256_context ctx; 
    uint8_t tmp32[32] = SECRET_KEY;
    aes256_init(&ctx, tmp32);

    //Load super-secret IV
    uint8_t iv[16] = IV;
       
    //Do this forever (we don't actually have a jumper to bootloader)
    uint8_t i;
    uint16_t crc;
    uint8_t c;
    while(1){
        c = (uint8_t)getch();
        if (c == 0){
        
            //Initial Value
            crc = 0x0000;
    
            //Read 16 Bytes now            
            for(i = 0; i < 16; i++){
                c = (uint8_t)getch();
                crc = _crc_xmodem_update(crc, c);
                //Save two copies, as one used for IV
                tmp32[i] = c;                
                tmp32[i+16] = c;
            }
            
            //Validate CRC-16
            uint16_t inpcrc = (uint16_t)getch() << 8;
            inpcrc |= (uint8_t)getch();
            
            if (inpcrc == crc){                  
                
                //CRC is OK - continue with decryption
                trigger_high();                
				aes256_decrypt_ecb(&ctx, tmp32); /* encrypting the data block */
				trigger_low();
                             
                //Apply IV (first 16 bytes)
                for (i = 0; i < 16; i++){
                    tmp32[i] ^= iv[i];
                }

                /* Comment this block out to always use initial IV, instead
                   of performing actual CBC mode operation */
                //Save IV for next time from original ciphertext                
                for (i = 0; i < 16; i++){
                    iv[i] = tmp32[i+16];
                }
                

                //Always send OK, done before checking
                //signature to ensure there is no timing
                //attack. This does mean user needs to
                //add some delay before sending next packet
                putch(COMM_OK);
                putch(COMM_OK);

                //Check the signature
                if ((tmp32[0] == SIGNATURE1) &&
                   (tmp32[1] == SIGNATURE2) &&
                   (tmp32[2] == SIGNATURE3) &&
                   (tmp32[3] == SIGNATURE4)){
                   
                   //We now have 12 bytes of useful data to write to flash memory.
                   //We don't actually write anything here though in real life would
                   //probably do more than just delay a moment
                   _delay_ms(1);
                }   
            } else {
                putch(COMM_BADCHECKSUM);
                putch(COMM_BADCHECKSUM);
            }            
        }         
    }
	 
	return 1;
	}