コード例 #1
0
ファイル: vd80spd.c プロジェクト: bradomyn/coht
int main(int argc, char **argv)
{
	int rc = 0;

	if (vd80_map())
		exit(1);

	if (vd80_init())
		exit(1);

	vd80_cmd_setbig(0);

	/* Run a 15 s recording to be sure to get 1M samples */
	if ((rc = test_sampling(15000000)))
		goto out;

	/* Check we've got at least 1M samples */
	if (shot_samples < (1024*1024)) {
		printf("No samples recorded - exiting\n");
		rc = 1;
		goto out;
	}

	/* Make sure we won't be swapped */
	if (mlockall(MCL_CURRENT|MCL_FUTURE)) {
		printf("Failed to lock process address space: %s\n",
		       strerror(errno));
		printf("Results may not be accurate\n");
	}

	/* Read 1M samples (512K frames) from 1 channel */
	if ((rc = time_read_samples(0, 512*1024, 16)))
		goto out;

out:
	rc = vd80_exit();

	munlockall();

	return rc;
}
コード例 #2
0
ファイル: lwekextest.c プロジェクト: google/jalic
static int test_lwekex(BIO *out, int single) {
  LWE_PAIR *alice = NULL, *bob = NULL;
  LWE_REC *rec = NULL;

  LWE_PUB *bob_reconstructed = NULL;
  LWE_REC *rec_reconstructed = NULL;

  LWE_CTX *ctx = NULL;

  unsigned char *apubbuf = NULL, *bpubbuf = NULL;
  size_t apublen, bpublen;

  unsigned char *recbuf = NULL;
  size_t reclen;

  unsigned char *assbuf = NULL, *bssbuf = NULL;
  size_t asslen, bsslen;

  int i, ret = 0;
  uint32_t *v =
      (uint32_t *)OPENSSL_malloc(LWE_N_BAR * LWE_N_BAR * sizeof(uint32_t));
  uint32_t *w =
      (uint32_t *)OPENSSL_malloc(LWE_N_BAR * LWE_N_BAR * sizeof(uint32_t));

  alice = LWE_PAIR_new();
  bob = LWE_PAIR_new();
  bob_reconstructed = LWE_PUB_new();
  rec = LWE_REC_new();
  rec_reconstructed = LWE_REC_new();
  ctx = LWE_CTX_new();
  if ((alice == NULL) || (bob == NULL) || (bob_reconstructed == NULL) ||
      (rec == NULL) || (rec_reconstructed == NULL) || (ctx == NULL)) {
    goto err;
  }

  if (single) {
    BIO_puts(out, "Testing packing/unpacking\n");
    if (!test_packing_unpacking(out)) goto err;
  }

  if (single) {
    BIO_puts(out, "Testing sampling routines\n");
    if (!test_sampling(out)) goto err;
  }

  if (single) BIO_puts(out, "Testing key generation  \n");

  if (single) BIO_puts(out, "Generating key for Alice (Server)\n");
  if (!LWE_PAIR_generate_key(alice, ctx, 1)) goto err;
  apublen = i2o_LWE_PUB(LWE_PAIR_get_publickey(alice), &apubbuf);
  if (single) BIO_printf(out, "  public B (%d bytes) = ", (int)apublen);
  if (apublen <= 0) {
    fprintf(stderr, "Error in LWEKEX routines\n");
    ret = 0;
    goto err;
  }
  if (single) {
    BIO_printf(out, "0x%02X 0x%02X 0x%02X 0x%02X ... 0x%02X\n", apubbuf[0],
               apubbuf[1], apubbuf[3], apubbuf[4], apubbuf[apublen - 1]);
  }

  if (single) BIO_puts(out, "Generating key for Bob (Client)\n");
  if (!LWE_PAIR_generate_key(bob, ctx, 0)) goto err;
  bpublen = i2o_LWE_PUB(LWE_PAIR_get_publickey(bob), &bpubbuf);
  if (single) {
    BIO_printf(out, "  public B' (%i bytes) = ", (int)bpublen);
    BIO_printf(out, "0x%02X 0x%02X 0x%02X 0x%02X ... 0x%02X\n", bpubbuf[0],
               bpubbuf[1], bpubbuf[3], bpubbuf[4], bpubbuf[apublen - 1]);
  }

  if (single) BIO_puts(out, "Testing Bob shared secret generation \n");

  bsslen = KDF1_SHA1_len;
  bssbuf = (unsigned char *)OPENSSL_malloc(bsslen);
  bsslen =
      LWEKEX_compute_key_bob(bssbuf, bsslen, rec, LWE_PAIR_get_publickey(alice),
                             bob, KDF1_SHA1, ctx, v);
  if (single) {
    BIO_printf(out, "  key_B (%i bytes) = ", (int)bsslen);
    for (i = 0; i < bsslen; i++) {
      BIO_printf(out, "%02X", bssbuf[i]);
    }
    BIO_puts(out, "\n");
  }
  reclen = i2o_LWE_REC(rec, &recbuf);
  if (single) {
    BIO_printf(out, "  rec (%i bytes) = ", (int)reclen);
    for (i = 0; i < reclen; i++) {
      BIO_printf(out, "0x%02X ", ((unsigned char *)recbuf)[i]);
    }
    BIO_puts(out, "\n");
  }

  if (single) BIO_puts(out, "Reconstructing Bob's values \n");

  // if (single) BIO_puts(out, "  Bob's key reconstruction from string \n");
  if (o2i_LWE_PUB(&bob_reconstructed, bpubbuf, bpublen) == NULL) {
    fprintf(stderr,
            "Error in LWEKEX routines (Bob public key reconstruction)\n");
    ret = 0;
    goto err;
  }
  // if (single) BIO_puts(out, "  Bob's reconciliation value reconstruction from
  // string \n");
  if (o2i_LWE_REC(&rec_reconstructed, recbuf, reclen) == NULL) {
    fprintf(stderr,
            "Error in LWEKEX routines (Bob reconciliation reconstruction)\n");
    ret = 0;
    goto err;
  }

  if (single) BIO_puts(out, "Testing Alice shared secret generation \n");

  asslen = KDF1_SHA1_len;
  assbuf = (unsigned char *)OPENSSL_malloc(asslen);
  asslen =
      LWEKEX_compute_key_alice(assbuf, asslen, bob_reconstructed,
                               rec_reconstructed, alice, KDF1_SHA1, ctx, w);
  if (single) {
    BIO_printf(out, "  key_A (%i bytes) = ", (int)asslen);
    for (i = 0; i < asslen; i++) {
      BIO_printf(out, "%02X", assbuf[i]);
    }
    BIO_puts(out, "\n");
  }

  if ((bsslen != asslen) || (memcmp(assbuf, bssbuf, asslen) != 0)) {
    if (single) {
      BIO_printf(out, " failed\n\n");
      fprintf(stderr, "Error in LWEKEX routines (mismatched shared secrets)\n");
    }
    ret = 0;
  } else {
    if (single) BIO_printf(out, "ok!\n");
    ret = 1;
  }

  // computing the Hamming distance vector between v and w
  if (single) {
    BIO_printf(out, "Hamming distance between the keys: [");
    for (i = 0; i < LWE_N_BAR * LWE_N_BAR; i++) {
      BIO_printf(out, "%08X", v[i] ^ w[i]);
      if (i + 1 < LWE_N_BAR * LWE_N_BAR) BIO_printf(out, ", ");
    }
    BIO_printf(out, "]\n");

    // computing the number of the lsb bits corrupted by noise

    BIO_printf(out,
               "The number of corrupted least significant bits (out of 32): [");
    int count_bits = 0;
    int max = 0;
    for (i = 0; i < LWE_N_BAR * LWE_N_BAR; i++) {
      int64_t diff = (int64_t)v[i] - w[i];
      if (diff < 0) diff = -diff;
      count_bits = 0;
      while (diff != 0) {
        count_bits++;
        diff >>= 1;
      }
      if (count_bits > max) max = count_bits;
      BIO_printf(out, "%i", count_bits);
      if (i + 1 < LWE_N_BAR * LWE_N_BAR) BIO_printf(out, ", ");
    }
    BIO_printf(out, "], MAX = %i\n", max);
  }

err:
  ERR_print_errors_fp(stderr);

  OPENSSL_free(w);
  OPENSSL_free(v);
  OPENSSL_free(bssbuf);
  OPENSSL_free(assbuf);
  OPENSSL_free(apubbuf);
  OPENSSL_free(bpubbuf);
  OPENSSL_free(recbuf);
  LWE_REC_free(rec_reconstructed);
  LWE_REC_free(rec);
  LWE_PUB_free(bob_reconstructed);
  LWE_PAIR_free(bob);
  LWE_PAIR_free(alice);
  LWE_CTX_free(ctx);
  return (ret);
}