示例#1
0
文件: ent.c 项目: vasik041/openlab
void calc(uint8_t *buf, int len, int binary, double *csq)
{
long ccount[256];	      /* Bins to count occurrences of values */
long totalc = 0;	      /* Total character count */
double montepi=0, chip=0, scc=0, ent=0, mean=0, chisq=0;

    memset(ccount, 0, sizeof ccount);

    /* Initialise for calculations */
    rt_init(binary);

    /* Scan input file and count character occurrences */
    for(int i=0; i < len; i++) {
	unsigned char ocb = buf[i];
	totalc += (binary ? 8 : 1);

	if(binary) {
	    int b;
    	    unsigned char ob = ocb;
	    for (b = 0; b < 8; b++) {
		ccount[ob & 1]++;
		ob >>= 1;
	    }    
	} else {
	    ccount[ocb]++;
	}
	rt_add(&ocb, 1);
    }
示例#2
0
文件: ent_native.c 项目: wubugui/ent
VALUE rb_rt_read_string(VALUE self, VALUE rb_buf) {
    rt_ctx *ctx;

    Check_Type(rb_buf, T_STRING);

    Data_Get_Struct(self, rt_ctx, ctx);
    if(ctx->ended)
        rb_raise(rb_eIOError, "data cannot be added after finalizing");

    rt_add(ctx, RSTRING_PTR(rb_buf), RSTRING_LEN(rb_buf));

    return LONG2NUM(RSTRING_LEN(rb_buf));
}
示例#3
0
/**
 * Run the entire unit test.
 **/
int main(void)
{
  int i;
  int port;
  guint16 real_port;    /* port at its actual size (for randomtest) */
  double ent, chisq, mean, montepi, scc;        /* randomtest results */

  printf("Testing source port randomness; iterations=%d, minimal entropy=%f\n", ITERATIONS, MIN_ENTROPY);
  rt_init(FALSE);   /* initialize randomtest, not binary */

  /* run test iterations */
  for (i = 0; i < ITERATIONS; i++)
    {
      port = try_bind_once();
#if PRINT_PORTS
      printf("Port bound to: %d\n", port);
#endif
      if (port < PORT_MIN)
        {
          printf("Failed: port number below minimum.\n");
          exit(1);
        }
      if (port > PORT_MAX)
        {
          printf("Failed: port number above maximum.\n");
          exit(1);
        }
      real_port = (guint16)port;
      rt_add(&real_port, sizeof(real_port));
    }

  /* check randomness */
  rt_end(&ent, &chisq, &mean, &montepi, &scc);
  printf("Randomness: entropy=%f, chi-square=%f, mean=%f, monte-carlo-pi=%f, serial correlation=%f\n", ent, chisq, mean, montepi, scc);

  /* make decision */
  if (ent >= MIN_ENTROPY) {
      printf("Passed: entropy is high enough.\n");
      exit(0);
    }
  else
    {
      printf("Failed: entropy is not high enough.\n");
      exit(1);
    }
}
示例#4
0
文件: ipip.c 项目: dieterdeyke/WAMPES
static void ipip_receive(void *argp)
{

  int addrlen;
  int hdr_len;
  int l;
  int32 ipaddr;
  struct edv_t *edv;
  struct iface *ifp;
  struct ip *ipptr;
  struct mbuf *bp;
  struct sockaddr_in addr;
  uint8 buf[MAX_FRAME];
  uint8 *bufptr;

  ifp = (struct iface *) argp;
  edv = (struct edv_t *) ifp->edv;
  addrlen = sizeof(addr);
  l = recvfrom(edv->fd, (char *) (bufptr = buf), sizeof(buf), 0, (struct sockaddr *) &addr, &addrlen);
  if (edv->type == USE_IP) {
    if (l <= sizeof(struct ip)) goto Fail;
    ipptr = (struct ip *) bufptr;
    hdr_len = 4 * ipptr->ip_hl;
    bufptr += hdr_len;
    l -= hdr_len;
  }
  if (l <= 0) goto Fail;

  if ((ipaddr = get32(bufptr + 12)) && ismyaddr(ipaddr) == NULL)
    rt_add(ipaddr, 32, (int32) ntohl(addr.sin_addr.s_addr), ifp, 1L, 0x7fffffff / 1000, 0);

  bp = qdata(bufptr, l);
  net_route(ifp, &bp);
  return;

Fail:
  ifp->crcerrors++;
}
示例#5
0
文件: ent_native.c 项目: wubugui/ent
VALUE rb_rt_read_file(VALUE self, VALUE rb_filename) {
    rt_ctx *ctx;
    FILE *fp;
    int oc;
    size_t fsz = 0;

    Check_Type(rb_filename, T_STRING);
    Data_Get_Struct(self, rt_ctx, ctx);
    if(ctx->ended)
        rb_raise(rb_eIOError, "data cannot be added after finalizing");

    if ((fp = fopen(RSTRING_PTR(rb_filename), "rb")) == NULL)
        rb_sys_fail(0);

    while((oc=fgetc(fp)) != EOF) {
        unsigned char ocb = (unsigned char) oc;
        rt_add(ctx, &ocb, 1);
        fsz++;
    }
    fclose(fp);

    return LONG2NUM(fsz);
}
示例#6
0
文件: prng.c 项目: libbitc/libbitc
int main(int argc, char *argv[]) {

    if (!getenv("FORCE_PRNG_VERF")) {
        fprintf(stderr,
                "prng: Skipping PRNG verification test\n"
                "prng: Set FORCE_PRNG_VERF=1 to enable PRNG verification\n");
        return 77;
    }

    uint8_t ob[BUFFER_SIZE];
    unsigned long totalc = 0; /* Total character count */
    double montepi, chip, scc, ent, mean, chisq;

    /* Initialise for calculations */

    rt_init(0);

    /* Scan input and count character occurrences */

    for (totalc = 0; totalc < SAMPLE_SIZE_BYTES; totalc += BUFFER_SIZE) {
        assert(prng_get_random_bytes(ob, BUFFER_SIZE) >= 0);
        rt_add(ob, BUFFER_SIZE);
    }

    /* Complete calculation and return sequence metrics */

    rt_end(&ent, &chisq, &mean, &montepi, &scc);

    /* Calculate probability of observed distribution occurring from
    the results of the Chi-Square test */

    chip = pochisq(chisq, 255);

    /* Print calculated results */
    printf("Entropy:\n");
    printf("========\n");
    printf("Entropy = %f bits per byte.\n", ent);
    printf("\nOptimum compression would reduce the size\n");
    printf("of this %ld byte input by %d percent.\n\n", totalc,
           (int16_t)(100 * (8 - ent) / 8.0));

    // Optimum compression would reduction equal to 0%
    assert((int16_t)(100 * (8 - ent) / 8.0) == 0);

    printf("Chi-square Test:\n");
    printf("================\n");
    printf("Chi square distribution for %ld samples is %1.2f, and randomly\n",
           totalc, chisq);
    if (chip < 0.0001) {
        printf(
            "would exceed this value less than 0.01 percent of the times.\n\n");
    } else if (chip > 0.9999) {
        printf("would exceed this value more than than 99.99 percent of the "
               "times.\n\n");
    } else {
        printf("would exceed this value %1.2f percent of the times.\n\n",
               chip * 100);
    }

    // Chi-square test result between 10% and 90%
    assert(90 > (chip * 100));
    assert((chip * 100) > 10);

    printf("Arithmetic Mean:\n");
    printf("================\n");
    printf("Arithmetic mean value of data bytes is %1.4f (%.1f = random).\n\n",
           mean, 127.5);

    // Arithmetic Mean between 127 and 128
    assert(127.0 < mean);
    assert(mean < 128.0);

    printf("Monte Carlo Value for Pi:\n");
    printf("=========================\n");
    printf("Monte Carlo value for Pi is %1.9f (error %1.2f percent).\n\n",
           montepi, 100.0 * (fabs(PI - montepi) / PI));

    // Monte Carlo Value for Pi less than 0.5
    assert(0.5 > 100.0 * (fabs(PI - montepi) / PI));

    printf("Serial Correlation Coefficient:\n");
    printf("===============================\n");
    printf("Serial correlation coefficient is ");
    if (scc >= -99999) {
        printf("%1.6f (totally uncorrelated = 0.0).\n", scc);
    } else {
        printf("undefined (all values equal!).\n");
    }
    printf("\nSee https://www.fourmilab.ch/random/ for detailed description of "
           "output\n");

    // Serial Correlation Coefficient between -0.005 and 0.005
    assert(0.005 > scc);
    assert(scc > -0.005);

    return 0;
}
示例#7
0
int main(int argc, char *argv[])
{
	int i, oc, opt;
	long ccount[256];	      /* Bins to count occurrences of values */
	long totalc = 0;	      /* Total character count */
	char *samp;
	double a, montepi, chip,
	       scc, ent, mean, chisq;
	FILE *fp = stdin;
	int counts = FALSE,	      /* Print character counts */
	    fold = FALSE,	      /* Fold upper to lower */
	    binary = FALSE,	      /* Treat input as a bitstream */
	    terse = FALSE;	      /* Terse (CSV format) output */

        while ((opt = getopt(argc, argv, "bcftu?hBCFTUH")) != -1) {
	    switch (toISOlower(opt)) {
                 case 'b':
		    binary = TRUE;
		    break;

                 case 'c':
		    counts = TRUE;
		    break;

                 case 'f':
		    fold = TRUE;
		    break;

                 case 't':
		    terse = TRUE;
		    break;

                 case '?':
                 case 'u':
	         case 'h':
		    help();
		    return 0;
	    }
	}
	if (optind < argc) {
	   if (optind != (argc - 1)) {
              V printf("Duplicate file name.\n");
	      help();
	      return 2;
	   }
           if ((fp = fopen(argv[optind], "rb")) == NULL) {
              V printf("Cannot open file %s\n", argv[optind]);
	      return 2;
	   }
	}

        samp = binary ? "bit" : "byte";
	memset(ccount, 0, sizeof ccount);

	/* Initialise for calculations */

	rt_init(binary);

	/* Scan input file and count character occurrences */

	while ((oc = fgetc(fp)) != EOF) {
	   unsigned char ocb;

	   if (fold && isISOalpha(oc) && isISOupper(oc)) {
	      oc = toISOlower(oc);
	   }
	   ocb = (unsigned char) oc;
	   totalc += binary ? 8 : 1;
	   if (binary) {
	    int b;
	    unsigned char ob = ocb;

	    for (b = 0; b < 8; b++) {
		ccount[ob & 1]++;
		ob >>= 1;
	    }
	   } else {
	       ccount[ocb]++;	      /* Update counter for this bin */
	   }
	   rt_add(&ocb, 1);
	}