Ejemplo n.º 1
0
static int setup_openiboot() {
	arm_setup();
	mmu_setup();
	tasks_setup();
	setup_devices();

	LeaveCriticalSection();

#ifndef CONFIG_IPHONE_4
	clock_set_sdiv(0);

	aes_setup();

	nor_setup();
	syscfg_setup();
	images_setup();
	nvram_setup();

	lcd_setup();
	framebuffer_setup();

	audiohw_init();
#endif
    isMultitouchLoaded = 0;
	return 0;
}
Ejemplo n.º 2
0
/**
   Initialize a Pelican state
   @param pelmac    The Pelican state to initialize
   @param cipher    The index of the desired cipher, must be AES
   @param key       The secret key
   @param keylen    The length of the secret key (octets)
   @return CRYPT_OK if successful
*/
int pelican_init(pelican_state *pelmac, int cipher, const unsigned char *key, unsigned long keylen)
{
    int index;
    int err;

    LTC_ARGCHK(pelmac != NULL);
    LTC_ARGCHK(key    != NULL);

   index = find_cipher("aes");
   if (cipher != index || index < 0) {
      return CRYPT_INVALID_CIPHER;
   }

#ifdef LTC_FAST
    if (16 % sizeof(LTC_FAST_TYPE)) {
        return CRYPT_INVALID_ARG;
    }
#endif

    if ((err = aes_setup(key, keylen, 0, &pelmac->K)) != CRYPT_OK) {
       return err;
    }

    zeromem(pelmac->state, 16);
    aes_ecb_encrypt(pelmac->state, pelmac->state, &pelmac->K);
    pelmac->buflen = 0;

    return CRYPT_OK;
}
Ejemplo n.º 3
0
static int setup_openiboot() {
	arm_setup();
	mmu_setup();
	tasks_setup();
	setup_devices();

	LeaveCriticalSection();

	clock_set_sdiv(0);

	aes_setup();

	nor_setup();
	images_setup();
	nvram_setup();

	lcd_setup();
	framebuffer_setup();

	audiohw_init();

	camera_setup();

	return 0;
}
Ejemplo n.º 4
0
void platform_init()
{
	arm_setup();
	mmu_setup();
	tasks_setup();

	// Basic prerequisites for everything else
	miu_setup();
	power_setup();

	clock_setup();

	// Need interrupts for everything afterwards
	interrupt_setup();
	
	gpio_setup();

	// For scheduling/sleeping niceties
	timer_setup();
	event_setup();
	wdt_setup();

	// Other devices
//	uart_setup();
	i2c_setup();

//	dma_setup();

	spi_setup();

	LeaveCriticalSection();

	aes_setup();

	nor_setup();
	syscfg_setup();
	images_setup();
	nvram_setup();

//	lcd_setup();
	framebuffer_hook();	// TODO: Remove once LCD implemented
	framebuffer_setup();

//	audiohw_init();

	framebuffer_setdisplaytext(TRUE);


	gpio_register_interrupt(BUTTONS_HOLD_IRQ, BUTTONS_HOLD_IRQTYPE, BUTTONS_HOLD_IRQLEVEL, BUTTONS_HOLD_IRQAUTOFLIP, gpio_test_handler, 0);
	gpio_interrupt_enable(BUTTONS_HOLD_IRQ);
	gpio_register_interrupt(BUTTONS_HOME_IRQ, BUTTONS_HOME_IRQTYPE, BUTTONS_HOME_IRQLEVEL, BUTTONS_HOME_IRQAUTOFLIP, gpio_test_handler, 1);
	gpio_interrupt_enable(BUTTONS_HOME_IRQ);
	gpio_register_interrupt(BUTTONS_VOLUP_IRQ, BUTTONS_VOLUP_IRQTYPE, BUTTONS_VOLUP_IRQLEVEL, BUTTONS_VOLUP_IRQAUTOFLIP, gpio_test_handler, 2);
	gpio_interrupt_enable(BUTTONS_VOLUP_IRQ);
	gpio_register_interrupt(BUTTONS_VOLDOWN_IRQ, BUTTONS_VOLDOWN_IRQTYPE, BUTTONS_VOLDOWN_IRQLEVEL, BUTTONS_VOLDOWN_IRQAUTOFLIP, gpio_test_handler, 3);
	gpio_interrupt_enable(BUTTONS_VOLDOWN_IRQ);
}
Ejemplo n.º 5
0
static void run_tomcrypt_aes128(uint8_t *output,
                                const uint8_t *input, unsigned size)
{
    symmetric_key aes;
    unsigned i;

    aes_setup(hardcoded_key, 16, 0, &aes);
    size -= 15;
    for (i = 0; i < size; i += 16)
        aes_ecb_encrypt(input + i, output + i, &aes);
}
Ejemplo n.º 6
0
/*****************************************************************
 * This function calculates the hash for the application. 
 * When this function returns successfully, the hash
 * is written to hashOutput.
 *
 * Returns false if the operation failed. In this case
 * the value of hashOutput should be ignored. 
 *****************************************************************/
bool calculateHash(uint8_t *startAddr, int length, uint8_t *hashOutput)
{
  // Helper variables
  int cryptError, i, j;

  // This variable will always point to the current block
  // to be decrypted
  uint8_t *curBlock;

  // Calculate the number of AES blocks
  int aesBlocks = length / AES_BLOCKSIZE; 

  // Input buffer used to hold the input block to the 
  // encryption routine
  uint8_t inputBlock[AES_BLOCKSIZE];

  // Initialize key
  symmetric_key skey;
  cryptError = aes_setup(hashKey, HASH_KEY_SIZE, 0, &skey);

  if ( cryptError != CRYPT_OK ) {
    fprintf(stderr, "Error initializing crypto library\n");
    return false;
  }

  // hashOutput will always contain the last encrypted block
  // Initialize with the init vector for the first iteration
  memcpy(hashOutput, hashInitVector, AES_BLOCKSIZE);

  // Loop over all blocks 
  for ( i=0; i<aesBlocks; i++ ) {
    
    // Get address of the current AES block
    curBlock = startAddr + i * AES_BLOCKSIZE;

    // XOR current block with previous cipher
    for ( j=0; j<AES_BLOCKSIZE; j++ ) {
      inputBlock[j] = curBlock[j] ^ hashOutput[j];
    }

    // Encrypt a block. Result is stored in hashOutput
    cryptError = aes_ecb_encrypt(inputBlock, hashOutput, &skey);

    if ( cryptError != CRYPT_OK ) {
      fprintf(stderr, "Error during hash calculation\n");
      return false;
    }
  }

  // Success
  return true;
}
Ejemplo n.º 7
0
/*****************************************************************
 * Encrypt the entire firmware image (including header) with AES
 * in CBC mode. The image is encrypted in place, 
 * when this function returns the buffer array will contain the 
 * encrypted image. 
 *****************************************************************/
bool encrypt(void)
{
  // Helper variables
  int cryptError, i, j;

  // Compute number of AES blocks to encrypt
  int aesBlocks = encryptedSize / AES_BLOCKSIZE;

  // The pointer to the current block to be encrypted
  uint8_t *curBlock;

  // Pointer to the last encrypted block
  // Initialize with the init vector
  uint8_t *prevBlock = initVector;

  // Initialize key
  symmetric_key skey;
  cryptError = aes_setup(encryptionKey, AES_KEY_SIZE, 0, &skey);

  if ( cryptError != CRYPT_OK ) {
    fprintf(stderr, "Error initializing crypto library\n");
    return false;
  }

  // Loop over the entire image
  for ( i=0; i<aesBlocks; i++ ) {
    
    // Get address of the current AES block
    curBlock = buffer + i * AES_BLOCKSIZE;

    // XOR current block with the last encrypted block
    for ( j=0; j<AES_BLOCKSIZE; j++ ) {
      curBlock[j] = curBlock[j] ^ prevBlock[j];
    }

    // Encrypt block in place
    cryptError = aes_ecb_encrypt(curBlock, curBlock, &skey);

    // Store address of current block for next iteration
    prevBlock = curBlock;

    if ( cryptError != CRYPT_OK ) {
      fprintf(stderr, "Error during encryption\n");
      return false;
    }
  }

  return true;
}
Ejemplo n.º 8
0
void platform_init()
{
	arm_setup();
	mmu_setup();
	tasks_setup();

	// Basic prerequisites for everything else
	miu_setup();
	power_setup();

	clock_setup();

	// Need interrupts for everything afterwards
	interrupt_setup();
	
	gpio_setup();

	// For scheduling/sleeping niceties
	timer_setup();
	event_setup();
	wdt_setup();

	// Other devices
	uart_setup();
	i2c_setup();

//	dma_setup();

	spi_setup();

	LeaveCriticalSection();

	aes_setup();

	displaypipe_init();
	framebuffer_setup();
	framebuffer_setdisplaytext(TRUE);
	lcd_set_backlight_level(186);

//	audiohw_init();
	
	//TODO: remove
	task_init(&iboot_loader_task, "iboot loader", TASK_DEFAULT_STACK_SIZE);
	task_start(&iboot_loader_task, &iboot_loader_run, NULL);
}
Ejemplo n.º 9
0
Archivo: disk_read.c Proyecto: CPFL/xen
static int load_root_pre(struct disk_root_sector *root, struct mem_tpm_mgr *dst)
{
	int rc;

	aes_setup(&dst->tm_key_e, &dst->tm_key);

	rc = disk_read_crypt_sector(root, sizeof(*root), root_loc(dst), dst);

	if (rc) {
		vtpmloginfo(VTPM_LOG_VTPM, "root cmac verify failed in slot %d\n", dst->active_root);
		return 2;
	}

	dst->root_seals_valid = 1 + dst->active_root;
	dst->sequence = be64_native(root->v.sequence);

	return 0;
}
Ejemplo n.º 10
0
void platform_init()
{
	arm_setup();
	mmu_setup();
	tasks_setup();

	// Basic prerequisites for everything else
	miu_setup();
	power_setup();

	clock_setup();

	// Need interrupts for everything afterwards
	interrupt_setup();

	gpio_setup();

	// For scheduling/sleeping niceties
	timer_setup();
	event_setup();
	wdt_setup();

	// Other devices
	uart_setup();
	i2c_setup();

	dma_setup();

	spi_setup();

	LeaveCriticalSection();

	clock_set_sdiv(0);

	aes_setup();

	lcd_setup();
	framebuffer_setup();

	audiohw_init();

	framebuffer_setdisplaytext(TRUE);
}
Ejemplo n.º 11
0
int main(void) 
{

    uint8_t command;

    serial_init();
    trigger_setup();
    aes_setup();
#ifdef DELAYS
    random_setup();
#endif

    while(1) {
        command = usart_recv_byte();

        switch(command) {
            case 'e':
                fill_buf();
                random_setup();
                encrypt();
                send_buf();
                break;

            case 'd':
                fill_buf();
                random_setup();
                decrypt();
                send_buf();
                break;

            default:
                continue;
        }
    }
    return 0;
}
Ejemplo n.º 12
0
void platform_init()
{
	arm_setup();
	mmu_setup();

	tasks_setup();

	// Basic prerequisites for everything else
	miu_setup();
	power_setup();

	//framebuffer_hook(); // TODO: Remove once LCD implemented -- Ricky26
	//framebuffer_setdisplaytext(TRUE);

	clock_setup();

	// Need interrupts for everything afterwards
	interrupt_setup();

	gpio_setup();

	// For scheduling/sleeping niceties
	timer_setup();
	event_setup();

	// Other devices
	dma_setup();
	//usb_shutdown();
	uart_setup();
	i2c_setup();
	spi_setup();

	LeaveCriticalSection();

	aes_setup();
}
Ejemplo n.º 13
0
void aes_key_wrap(UWORD8 *text, UWORD8 *key, UWORD32 n)
{
    UWORD32       t        = 0;
    UWORD32       i        = 0;
    UWORD32       j        = 0;
    UWORD32       k        = 0;
    UWORD32       c        = 0;
    UWORD32       idx      = 0;
    UWORD16       curr_idx = 0;
    UWORD8        *A       = 0; //[8];
    UWORD8        *B       = 0; //[16];
    UWORD8        *R[8]    = {0}; //[8][100];
    UWORD8        *Rbuff   = NULL;
    aes_context_t ctx      = {{0},};

    /* Save the current scratch memory index */
    curr_idx = get_scratch_mem_idx();

    /* Allocate scratch memory for the temporary variables */
    A = (UWORD8 *)scratch_mem_alloc(8);
    if(A == NULL)
    {
        /* Restore the saved scratch memory index */
        restore_scratch_mem_idx(curr_idx);

        return;
    }

    B = (UWORD8 *)scratch_mem_alloc(16);
    if(B == NULL)
    {
        /* Restore the saved scratch memory index */
        restore_scratch_mem_idx(curr_idx);

        return;
    }

    Rbuff = (UWORD8 *)scratch_mem_alloc(8*100);
    if(Rbuff == NULL)
        {
            /* Restore the saved scratch memory index */
            restore_scratch_mem_idx(curr_idx);

            return;
        }

    for(i = 0; i < 8; i ++)
    {
        R[i] = Rbuff + i*100;
    }

    /* AES initialization - Key scheduling  */
    ctx.nrounds = NUM_ROUNDS;
    aes_setup(&ctx, 16, key);


    /* 1) Initialize variables */
    /* Set A = IV              */
    /* For i = 1 to n          */
    /* R[i] = P[i]             */
    for(k = 0; k < 8; k++)
        A[k] = g_iv[k];

    for(i = 0, idx = 0; i < n; i++)
        for(k = 0; k < 8; k++)
            R[k][i] = text[idx++];

    /* 2) Compute intermediate values                */
    /* For j = 5 to 0                                */
    /*     For i = 1 to n                            */
    /*         B = AES(K, A | R[i])                  */
    /*         A = MSB(64, B) ^ t where t = (n*j)+i  */
    /*         R[i] = LSB(64, B)                     */
    for(j = 0; j <= 5; j++)
    {
        for(i = 0; i < n; i++)
        {
            UWORD8 temp128[16];

            /* A | R[i] */
            for(k = 0; k < 8; k ++)
                temp128[k] = A[k];
            for(k = 8, c = 0; k < 16 && c < 8; k++, c++)
                temp128[k] = R[c][i];

            /* B = AES(K, A | R[i] */
            aes_encrypt(&ctx, temp128, B);

            /* MSB(64, B) */
            for(k = 0; k < 8; k++)
                A[k] = B[k];

            /* t does not need to be greater than 32 bits for allowed data   */
            /* lengths. Verify this.                                         */
            t = (n * j) + (i + 1);

            /* A = MSB(64, B) ^ t where t = (n*j)+i  */
            for(k = 0; k < 4; k ++)
            {
                UWORD8 ptr = (UWORD8)(t >> (k << 3));
                A[7 - k] = A[7 - k] ^ ptr;
            }

            /* R[i] = LSB(64, B) */
            for(k = 0; k < 8; k++)
                R[k][i] = B[k + 8];
        }
    }

    /* 3) Output Results */
    /* C[0] = A          */
    /* For i = 1 to n    */
    /*      C[i] = R[i]  */
    for(k = 0; k < 8; k ++)
        text[k] = A[k];

    idx = 8;

    for(i = 0; i < n; i++)
        for(k = 0; k < 8; k ++)
            text[idx++] = R[k][i];

    /* Restore the saved scratch memory index */
    restore_scratch_mem_idx(curr_idx);
}
Ejemplo n.º 14
0
Archivo: disk_read.c Proyecto: CPFL/xen
static int load_root_post(struct mem_tpm_mgr *dst, const struct disk_root_sector *root)
{
	int rc, i, j;
	uint32_t nr_disk_rbs = be32_native(root->nr_rb_macs);

	rc = TPM_disk_check_counter(dst->counter_index, dst->counter_auth,
			root->v.tpm_counter_value);
	if (rc)
		return 2;
	dst->counter_value = root->v.tpm_counter_value;

	dst->nr_groups = be32_native(root->v.nr_groups);
	dst->groups = calloc(sizeof(dst->groups[0]), dst->nr_groups);

	if (!dst->groups) {
		vtpmlogerror(VTPM_LOG_VTPM, "load_root_post alloc %x\n", dst->nr_groups);
		return 2;
	}

	rc = load_verify_group_itree(dst, 0, dst->nr_groups,
			root->v.group_hash, root->group_loc, NR_ENTRIES_PER_ROOT);
	if (rc)
		return rc;

	/* Sanity check: group0 must be open */
	if (!dst->groups[0].v) {
		printk("Error opening group 0\n");
		return 2;
	}

	/* TODO support for spilling rollback list */
	if (nr_disk_rbs > NR_RB_MACS_PER_ROOT)
		return 3;

	i = 0;
	j = 0;
	while (i < dst->nr_groups) {
		aes_context key_e;
		struct mem_group_hdr *group = &dst->groups[i];
		struct mem_group *groupv = group->v;
		const struct disk_rb_mac_entry *ent = &root->rb_macs[j];

		if (!groupv) {
			i++;
			// this group is not open - no need to verify now
			continue;
		}

		if (be32_native(ent->id) < i) {
			// this entry is for a group that is not open
			j++;
			continue;
		}

		if (j >= nr_disk_rbs || be32_native(ent->id) != i) {
			// TODO allow delegation
			if (!(groupv->details.flags.value & FLAG_ROLLBACK_DETECTED)) {
				groupv->details.flags.value |= FLAG_ROLLBACK_DETECTED;
				group->disk_loc.value = 0;
			}
			i++;
			continue;
		}

		aes_setup(&key_e, &groupv->rollback_mac_key);
		if (aes_cmac_verify(&ent->mac, &root->v, sizeof(root->v), &key_e)) {
			if (!(groupv->details.flags.value & FLAG_ROLLBACK_DETECTED)) {
				groupv->details.flags.value |= FLAG_ROLLBACK_DETECTED;
				group->disk_loc.value = 0;
			}
		}
		i++; j++;
	}

	return 0;
}
Ejemplo n.º 15
0
Archivo: disk_read.c Proyecto: CPFL/xen
/* Load and verify one group's data structure, including its vTPMs.
 */
static int load_verify_group(struct mem_group_hdr *dst, const struct mem_tpm_mgr *mgr)
{
	struct mem_group *group;
	struct disk_group_sector disk;
	int rc;
	aes_context key_e;
	aes_context *opened_key = NULL;

	disk_set_used(dst->disk_loc, mgr);

	rc = disk_read_crypt_sector(&disk, sizeof(disk), dst->disk_loc, mgr);
	if (rc) {
		printk("Malformed sector %d\n", be32_native(dst->disk_loc));
		return rc;
	}
	
	rc = sha256_verify(&dst->disk_hash, &disk.v, sizeof(disk.v) + sizeof(disk.group_mac));
	if (rc) {
		printk("Hash mismatch in sector %d\n", be32_native(dst->disk_loc));
		return rc;
	}
	
	dst->v = group = calloc(1, sizeof(*group));

	rc = find_group_key(group, &disk, mgr);
	if (rc == 0) {
		opened_key = &key_e;
		/* Verify the group with the group's own key */
		aes_setup(opened_key, &group->group_key);
		if (aes_cmac_verify(&disk.group_mac, &disk.v, sizeof(disk.v), opened_key)) {
			printk("Group CMAC failed\n");
			return 2;
		}

		memcpy(&group->id_data, &disk.v.id_data, sizeof(group->id_data));
		memcpy(&group->details, &disk.v.details, sizeof(group->details));
	} else if (rc == 1) {
		// still need to walk the vtpm list
		rc = 0;
	} else {
		printk("Group key unsealing failed\n");
		return rc;
	}

	group->nr_vtpms = be32_native(disk.v.nr_vtpms);
	group->nr_pages = (group->nr_vtpms + VTPMS_PER_SECTOR - 1) / VTPMS_PER_SECTOR;

	group->data = calloc(group->nr_pages, sizeof(group->data[0]));

	rc = load_verify_vtpm_itree(dst, 0, group->nr_pages, disk.v.vtpm_hash,
			disk.vtpm_location, NR_ENTRIES_PER_GROUP_BASE, mgr, opened_key);

	if (!opened_key) {
		/* remove the struct */
		free(group->data);
		free(group->seals);
		free(group);
		dst->v = NULL;
	}

	return rc;
}
Ejemplo n.º 16
0
int
main(int argc, char **argv)
{
    symmetric_key skey;
    int err;

    /* Build the nonse */
    uint8_t nonse[13];
    uint8_t *ptr = nonse;
    memcpy(ptr, addr, 8);
    ptr += 8;
    memcpy(ptr, (uint8_t *)&frame_ctr, 4);
    ptr += 4;
    memcpy(ptr, &sec_level, 1);

    /* build the message */
    memset(msg, 0, sizeof(msg));
    msg[0] = 0xaa;

    printf("VARIABLES\n");
    printf("key: ");
    print_buffer(key, sizeof(key));
    printf("\n");

    printf("nonse: ");
    print_buffer(nonse, sizeof(nonse));
    printf("\n");

    printf("a: ");
    print_buffer(hdr, sizeof(hdr));
    printf("\n");
    printf("\n");

    register_cipher(&aes_desc);

    err = aes_setup(key, 16, 0, &skey);
    if (err != CRYPT_OK)
    {
        printf("setup failed with error %s\n", error_to_string(err));
        return -1;
    }

    err = ccm_memory(find_cipher("aes"),
                     NULL, 0,
                     &skey,
                     nonse, 13,
                     hdr, sizeof(hdr),
                     msg, 2,
                     ct,
                     tag, &tag_len,
                     CCM_ENCRYPT);
    if (err != CRYPT_OK)
    {
        printf("encryption failed with error %s\n", error_to_string(err));
        return -1;
    }

    printf("ENCRYPTED\n");
    printf("input: ");
    print_buffer(msg, sizeof(msg));
    printf("\n");

    printf("output: ");
    print_buffer(ct, sizeof(ct));
    printf("\n");

    printf("tag: ");
    print_buffer(tag, tag_len);
    printf("\n");
    printf("\n");

    memset(msg, 0xff, sizeof(msg));
    memset(tag, 0xff, sizeof(tag));
    memset(ct+2, 0x00, sizeof(ct)-2);

    err = ccm_memory(find_cipher("aes"),
                     NULL, 0,
                     &skey,
                     nonse, 13,
                     hdr, sizeof(hdr),
                     msg, 2,
                     ct,
                     tag, &tag_len,
                     CCM_DECRYPT);
    if (err != CRYPT_OK)
    {
        printf("encryption failed with error %s\n", error_to_string(err));
        return -1;
    }

    printf("DECRYPTED\n");
    printf("input: ");
    print_buffer(ct, sizeof(ct));
    printf("\n");

    printf("output: ");
    print_buffer(msg, sizeof(msg));
    printf("\n");

    printf("tag: ");
    print_buffer(tag, tag_len);
    printf("\n");

    aes_done(&skey);

    return 0;
}
Ejemplo n.º 17
0
BOOL_T aes_key_unwrap(UWORD8 *text, UWORD8 *key, UWORD32 n)
{
    BOOL_T        ret_val  = BFALSE;
    UWORD32       t        = 0;
    UWORD32       i        = 0;
    UWORD32       j        = 0;
    UWORD32       k        = 0;
    UWORD32       c        = 0;
    UWORD32       idx      = 0;
    UWORD16       curr_idx = 0;
    UWORD8        *A       = 0; //[8];
    UWORD8        *B       = 0; //[16];
    UWORD8        *R[8]    = {0}; //[8][100];
    UWORD8        *Rbuff   = NULL;
    aes_context_t ctx      = {{0},};

    /* Save the current scratch memory index */
    curr_idx = get_scratch_mem_idx();

    /* Allocate scratch memory for the temporary variables */
    A = (UWORD8 *)scratch_mem_alloc(8);
    if(A == NULL)
    {
        /* Restore the saved scratch memory index */
        restore_scratch_mem_idx(curr_idx);

        return ret_val;
    }

    B = (UWORD8 *)scratch_mem_alloc(16);
    if(B == NULL)
    {
        /* Restore the saved scratch memory index */
        restore_scratch_mem_idx(curr_idx);

        return ret_val;
    }

    Rbuff = (UWORD8 *)scratch_mem_alloc(8*100);
    if(Rbuff == NULL)
    {
        /* Restore the saved scratch memory index */
        restore_scratch_mem_idx(curr_idx);

        return ret_val;
    }

    for(i = 0; i < 8; i++)
    {
        R[i] = Rbuff + i*100;
    }

    /* AES initialization - Key scheduling */
    ctx.nrounds = NUM_ROUNDS;
    aes_setup(&ctx, 16, key);

    /* 1) Initialize variables  */
    /* Set A = C[0]             */
    /* For i = 1 to n           */
    /* R[i] = C[i]              */

    idx = 0;

    for(k = 0; k < 8; k++)
        A[k] = text[idx++];

    for(i = 0; i < n; i++)
        for(k = 0; k < 8; k++)
            R[k][i] = text[idx++];

    /* 2) Compute intermediate values                       */
    /* For j = 5 to 0                                       */
    /*     For i = n to 1                                   */
    /*         B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i */
    /*         A = MSB(64, B)                               */
    /*         R[i] = LSB(64, B)                            */
    for(j = 6; j > 0; j--)
    {
        for(i = n; i > 0; i--)
        {
            UWORD8 temp128[16];

            /* t does not need to be greater than 32 bits for allowed data */
            /* lengths. Verify this.                                       */
            t = (n * (j - 1)) + i;

            /* (A ^ t) */
            for(k = 0; k < 4; k ++)
            {
                UWORD8 ptr = (UWORD8)(t >> (k << 3));
                A[7 - k] = A[7 - k] ^ ptr;
            }

            /* (A ^ t) | R[i] */
            for(k = 0; k < 8; k ++)
                temp128[k] = A[k];
            for(k = 8, c = 0; k < 16 && c < 8; k++, c++)
                temp128[k] = R[c][i - 1];

            /* B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i */
            aes_decrypt(&ctx, temp128, B);

            /* A = MSB(64, B) */
            for(k = 0; k < 8; k++)
                A[k] = B[k];

            /* R[i] = LSB(64, B) */
            for(k = 0; k < 8; k++)
                R[k][i - 1] = B[k + 8];
        }

    }

    /* 3) Output Results                       */
    /* If A is an appropriate initial value,   */
    /* Then                                    */
    /*      For i = 1 to n                     */
    /*          P[i] = R[i]                    */
    /* Else                                    */
    /*     Return an error                     */
    if(memcmp(g_iv, A, 8) == 0)
    {
        idx = 0;

        for(i = 1; i <= n; i++)
            for(k = 0; k < 8; k ++)
                text[idx++] = R[k][i - 1];

        ret_val = BTRUE;
    }
    else
    {
        ret_val = BFALSE;
    }

    /* Restore the saved scratch memory index */
    restore_scratch_mem_idx(curr_idx);

    return ret_val;
}
Ejemplo n.º 18
0
Archivo: tom_aes.c Proyecto: ufwt/bacs
int main(){
	unsigned char 	key128[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
	unsigned char 	key192[24] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
	unsigned char 	key256[32] = {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};

	unsigned char 	ct[16];
	unsigned char 	vt[16];
	unsigned char 	pt[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
	symmetric_key 	skey128;
	symmetric_key 	skey192;
	symmetric_key 	skey256;


	/* AES 128 */
	if (aes_setup(key128, 16, 10, &skey128) != CRYPT_OK){
		printf("ERROR: in %s, unable to setup AES 128 key\n", __func__);
		return 0;
	}

	if (aes_ecb_encrypt(pt, ct, &skey128) != CRYPT_OK){
		printf("ERROR: in %s, unable to encrypt AES 128\n", __func__);
	}

	printf("Plaintext:      ");
	fprintBuffer_raw(stdout, (char*)pt, 16);
	printf("\nKey 128:        ");
	fprintBuffer_raw(stdout, (char*)key128, 16);
	printf("\nCiphertext 128: ");
	fprintBuffer_raw(stdout, (char*)ct, 16);

	if (aes_ecb_decrypt(ct, vt, &skey128) != CRYPT_OK){
		printf("ERROR: in %s, unable to decrypt AES 128\n", __func__);
	}

	if (!memcmp(vt, pt, 16)){
		printf("\nRecovery 128:   OK\n");
	}
	else{
		printf("\nRecovery 128:   FAIL\n");
	}

	aes_done(&skey128);


	/* AES 192 */
	if (aes_setup(key192, 24, 12, &skey192) != CRYPT_OK){
		printf("ERROR: in %s, unable to setup AES 192 key\n", __func__);
		return 0;
	}

	if (aes_ecb_encrypt(pt, ct, &skey192) != CRYPT_OK){
		printf("ERROR: in %s, unable to encrypt AES 192\n", __func__);
	}

	printf("Key 192:        ");
	fprintBuffer_raw(stdout, (char*)key192, 24);
	printf("\nCiphertext 192: ");
	fprintBuffer_raw(stdout, (char*)ct, 16);

	if (aes_ecb_decrypt(ct, vt, &skey192) != CRYPT_OK){
		printf("ERROR: in %s, unable to decrypt AES 192\n", __func__);
	}

	if (!memcmp(vt, pt, 16)){
		printf("\nRecovery 192:   OK\n");
	}
	else{
		printf("\nRecovery 192:   FAIL\n");
	}

	aes_done(&skey192);


	/* AES 256 */
	if (aes_setup(key256, 32, 14, &skey256) != CRYPT_OK){
		printf("ERROR: in %s, unable to setup AES 256 key\n", __func__);
		return 0;
	}

	if (aes_ecb_encrypt(pt, ct, &skey256) != CRYPT_OK){
		printf("ERROR: in %s, unable to encrypt AES 256\n", __func__);
	}

	printf("Key 256:        ");
	fprintBuffer_raw(stdout, (char*)key256, 32);
	printf("\nCiphertext 256: ");
	fprintBuffer_raw(stdout, (char*)ct, 16);

	if (aes_ecb_decrypt(ct, vt, &skey256) != CRYPT_OK){
		printf("ERROR: in %s, unable to decrypt AES 256\n", __func__);
	}

	if (!memcmp(vt, pt, 16)){
		printf("\nRecovery 256:   OK\n");
	}
	else{
		printf("\nRecovery 256:   FAIL\n");
	}

	aes_done(&skey256);

	return 0;
}