Пример #1
0
void close_libfile(int fd, int count)
{
	_D(_D_WARN "fd = %d, count = %d", fd, count);
	lseek(fd, 32, SEEK_SET);
	write32_be(fd, count);
	close(fd);
}
Пример #2
0
static void F(const cf_hmac_ctx *startctx,
              uint32_t counter,
              const uint8_t *salt, size_t nsalt,
              uint32_t iterations,
              uint8_t *out)
{
  uint8_t U[CF_MAXHASH];
  size_t hashsz = startctx->hash->hashsz;
  
  uint8_t countbuf[4];
  write32_be(counter, countbuf);

  /* First iteration:
   *   U_1 = PRF(P, S || INT_32_BE(i))
   */
  cf_hmac_ctx ctx = *startctx;
  cf_hmac_update(&ctx, salt, nsalt);
  cf_hmac_update(&ctx, countbuf, sizeof countbuf);
  cf_hmac_finish(&ctx, U);
  memcpy(out, U, hashsz);

  /* Subsequent iterations:
   *   U_c = PRF(P, U_{c-1})
   */
  for (uint32_t i = 1; i < iterations; i++)
  {
    ctx = *startctx;
    cf_hmac_update(&ctx, U, hashsz);
    cf_hmac_finish(&ctx, U);
    xor_bb(out, out, U, hashsz);
  }
}
Пример #3
0
Файл: conv.c Проект: UIKit0/fsex
int convert_patches(struct fsex_libdata *lib, int dest, char *output, int force)
{
	uint8 *patch, *data;
	int i, fd;
	uint8 newpatch[2000];
	struct fsex_libdata newlib;

	data = lib->data;

	newlib.model = dest;
	fd = create_libfile(&newlib, output, force);
	if (fd < 0) {
		return fd;
	}

	printf("\nPatches from %s:\n\n", lib->filename);

	for (i = 0; i < lib->num_patch; i++) {
		if (lib->patch[i].skip)
			continue;

		memcpy(newpatch, lib->patch[i].patch, lib->patch[i].size);

		/* Patch Common */
		patch = newpatch + (lib->patch[i].common - lib->patch[i].patch);
		printf(" %04d  [%-12.12s]\r", i + 1, &patch[PATCH_NAME_1]);

		/* Patch Tone 1 */
		patch = newpatch + (lib->patch[i].tone1 - lib->patch[i].patch);
		do_conversion(1, lib->model, dest, patch);

		/* Patch Tone 2 */
		patch = newpatch + (lib->patch[i].tone2 - lib->patch[i].patch);
		do_conversion(2, lib->model, dest, patch);

		/* Patch Tone 3 */
		patch = newpatch + (lib->patch[i].tone3 - lib->patch[i].patch);
		do_conversion(3, lib->model, dest, patch);

		/* Patch Tone 4 */
		patch = newpatch + (lib->patch[i].tone4 - lib->patch[i].patch);
		do_conversion(4, lib->model, dest, patch);

		/* write raw data */
		write(fd, newpatch, lib->patch[i].size);
		write32_be(fd, 0);	/* FIXME: comment field */
	}

	printf("\nCreate new library: %s\n", output);

	close_libfile(fd, i);

	return 0;
}
Пример #4
0
static void check_ocb_long(size_t nkey, const void *expect_tag, size_t ntag)
{
  uint8_t C[22400];
  uint8_t K[32];
  uint8_t S[128] = { 0 };
  uint8_t N[12] = { 0 };
  size_t nC = 0;

  memset(K, 0, sizeof K);
  K[nkey - 1] = ntag * 8;

  cf_aes_context aes;
  cf_aes_init(&aes, K, nkey);

  for (size_t i = 0; i < 128; i++)
  {
    /* N = num2str(3i+1, 96) */
    memset(N, 0, sizeof N);
    write32_be(3 * i + 1, N + 8);

    /* C = C || OCB-ENCRYPT(K, N, S, S)
     * nb. OCB-ENCRYPT(Key, Nonce, AAD, Plain) */
    cf_ocb_encrypt(&cf_aes, &aes,
                   S, i,        /* plain */
                   S, i,        /* aad */
                   N, sizeof N, /* nonce */
                   C + nC,      /* cipher out */
                   C + nC + i,  /* tag out */
                   ntag);
    nC += i + ntag;

    /* N = num2str(3i+2,96) */
    write32_be(3 * i + 2, N + 8);

    /* C = C || OCB-ENCRYPT(K, N, <empty string>, S) */
    cf_ocb_encrypt(&cf_aes, &aes,
                   S, i,
                   NULL, 0,
                   N, sizeof N,
                   C + nC,
                   C + nC + i,
                   ntag);
    nC += i + ntag;

    /* N = num2str(3i+3,96) */
    write32_be(3 * i + 3, N + 8);

    /* C = C || OCB-ENCRYPT(K, N, S, <empty string>) */
    cf_ocb_encrypt(&cf_aes, &aes,
                   NULL, 0,
                   S, i,
                   N, sizeof N,
                   NULL,
                   C + nC,
                   ntag);
    nC += ntag;
  }

  /* N = num2str(385, 96) */
  write32_be(385, N + 8);

  /* Output : OCB-ENCRYPT(K, N, C, <empty string>) */
  uint8_t result[16];
  cf_ocb_encrypt(&cf_aes, &aes,
                 NULL, 0,
                 C, nC,
                 N, sizeof N,
                 NULL,
                 result, ntag);

  TEST_CHECK(memcmp(result, expect_tag, ntag) == 0);
}