void reseedStrongPool(quint8* buffer1,int l1,quint8* buffer2,int l2){
	if(l1>l2*4){
		yarrow256_update(&StrongCtx,0,100,100,buffer1);
		buffer1=buffer1+100;
		l1=l1-100;
	}
	else{
		yarrow256_update(&StrongCtx,1,100,25,buffer2);
		buffer2=buffer2+25;
		l2=l2-25;
	}
	
	if(l1>l2*4){
		yarrow256_update(&StrongCtx,0,160,160,buffer1);
		l1-=160;
		buffer1+=160;
		yarrow256_update(&StrongCtx,1,l1,l1,buffer1);
		yarrow256_update(&StrongCtx,1,4*l2,l2,buffer2);
	}
	else{
		yarrow256_update(&StrongCtx,0,160,40,buffer2);
		l2-=40;
		buffer2+=40;
		yarrow256_update(&StrongCtx,1,l2*4,l2,buffer2);
		yarrow256_update(&StrongCtx,1,l1,l1,buffer1);
	}
}
Exemple #2
0
static int do_trivia_source(struct rnd_ctx_st *ctx, int init, struct event_st *event)
{
	unsigned entropy = 0;

	if (init) {
		ctx->trivia_time_count = 0;
	} else {
		ctx->trivia_time_count++;

		if (event->now.tv_sec != ctx->trivia_previous_time) {
			/* Count one bit of entropy if we either have more than two
			 * invocations in one second, or more than two seconds
			 * between invocations. */
			if ((ctx->trivia_time_count > 2)
			    || ((event->now.tv_sec - ctx->trivia_previous_time) > 2))
				entropy++;

			ctx->trivia_time_count = 0;
		}
	}
	ctx->trivia_previous_time = event->now.tv_sec;

	return yarrow256_update(&ctx->yctx, RANDOM_SOURCE_TRIVIA, entropy,
				sizeof(*event), (void *) event);
}
Exemple #3
0
static int do_device_source(struct rnd_ctx_st *ctx, int init, struct event_st *event)
{
	unsigned int read_size = DEVICE_READ_SIZE;
	int ret;

	if (init) {
		memcpy(&ctx->device_last_read, &event->now,
		       sizeof(ctx->device_last_read));

		read_size = DEVICE_READ_SIZE_MAX;	/* initially read more data */
	}

	if ((init
	     || (timespec_sub_sec(&event->now, &ctx->device_last_read) >
		 DEVICE_READ_INTERVAL))) {
		/* More than 20 minutes since we last read the device */
		uint8_t buf[DEVICE_READ_SIZE_MAX];

		ret = _rnd_get_system_entropy(buf, read_size);
		if (ret < 0)
			return gnutls_assert_val(ret);

		memcpy(&ctx->device_last_read, &event->now,
		       sizeof(ctx->device_last_read));

		return yarrow256_update(&ctx->yctx, RANDOM_SOURCE_DEVICE,
					read_size * 8 /
					2 /* we trust the RNG */ ,
					read_size, buf);
	}
	return 0;
}
static int
do_trivia_source (int init)
{
  struct
  {
    FILETIME now;
    unsigned count;
  } event;

  unsigned entropy = 0;

  GetSystemTimeAsFileTime (&event.now);
  event.count = 0;

  if (init)
    {
      trivia_time_count = 0;
    }
  else
    {
      event.count = trivia_time_count++;
      entropy = 1;
    }

  return yarrow256_update (&yctx, RANDOM_SOURCE_TRIVIA, entropy,
                           sizeof (event), (const uint8_t *) &event);
}
static int
do_trivia_source (int init)
{
  struct
  {
    struct timeval now;
#ifdef HAVE_GETRUSAGE
    struct rusage rusage;
#endif
    unsigned count;
    pid_t pid;
  } event;

  unsigned entropy = 0;

  if (gettimeofday (&event.now, NULL) < 0)
    {
      _gnutls_debug_log ("gettimeofday failed: %s\n", strerror (errno));
      abort ();
    }
#ifdef HAVE_GETRUSAGE
  if (getrusage (RUSAGE_SELF, &event.rusage) < 0)
    {
      _gnutls_debug_log ("getrusage failed: %s\n", strerror (errno));
      abort ();
    }
#endif

  event.count = 0;
  if (init)
    {
      trivia_time_count = 0;
    }
  else
    {
      event.count = trivia_time_count++;

      if (event.now.tv_sec != trivia_previous_time)
        {
          /* Count one bit of entropy if we either have more than two
           * invocations in one second, or more than two seconds
           * between invocations. */
          if ((trivia_time_count > 2)
              || ((event.now.tv_sec - trivia_previous_time) > 2))
            entropy++;

          trivia_time_count = 0;
        }
    }
  trivia_previous_time = event.now.tv_sec;
  event.pid = getpid ();

  return yarrow256_update (&yctx, RANDOM_SOURCE_TRIVIA, entropy,
                           sizeof (event), (const uint8_t *) &event);
}
static int
do_device_source_egd (int init)
{
  time_t now = gnutls_time (NULL);
  unsigned int read_size = DEVICE_READ_SIZE;

  if (init)
    {
      device_fd = _rndegd_connect_socket ();
      if (device_fd < 0)
        {
          _gnutls_debug_log ("Cannot open egd socket!\n");
          return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
        }

      device_last_read = now;

      read_size = DEVICE_READ_SIZE_MAX; /* initially read more data */
    }

  if ((device_fd > 0)
      && (init || ((now - device_last_read) > DEVICE_READ_INTERVAL)))
    {

      /* More than 20 minutes since we last read the device */
      uint8_t buf[DEVICE_READ_SIZE_MAX];
      uint32_t done;

      for (done = 0; done < read_size;)
        {
          int res;
          res = _rndegd_read (&device_fd, buf + done, sizeof (buf) - done);
          if (res <= 0)
            {
              if (res < 0)
                {
                  _gnutls_debug_log ("Failed to read egd.\n");
                }
              else
                {
                  _gnutls_debug_log ("Failed to read egd: end of file\n");
                }

              return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
            }
          done += res;
        }

      device_last_read = now;
      return yarrow256_update (&yctx, RANDOM_SOURCE_DEVICE, read_size * 8 / 2,
                               read_size, buf);
    }
  return 0;
}
static int
do_device_source (int init)
{
  time_t now = gnutls_time (NULL);
  int read_size = DEVICE_READ_SIZE;

  if (init)
    {
      int old;

      if (!CryptAcquireContext
          (&device_fd, NULL, NULL, PROV_RSA_FULL,
           CRYPT_SILENT | CRYPT_VERIFYCONTEXT))
        {
          _gnutls_debug_log ("error in CryptAcquireContext!\n");
          return GNUTLS_E_INTERNAL_ERROR;
        }
      device_last_read = now;
      read_size = DEVICE_READ_SIZE_MAX; /* initially read more data */
    }

  if ((device_fd != 0)
      && (init || ((now - device_last_read) > DEVICE_READ_INTERVAL)))
    {

      /* More than 20 minutes since we last read the device */
      uint8_t buf[DEVICE_READ_SIZE_MAX];

      if (!CryptGenRandom (device_fd, (DWORD) read_size, buf))
        {
          _gnutls_debug_log ("Error in CryptGenRandom: %s\n",
                             GetLastError ());
          return GNUTLS_E_INTERNAL_ERROR;
        }

      device_last_read = now;
      return yarrow256_update (&yctx, RANDOM_SOURCE_DEVICE,
                               read_size * 8 /
                               2 /* we trust the system RNG */ ,
                               read_size, buf);
    }
  return 0;
}
static int
do_device_source_urandom (int init)
{
  time_t now = gnutls_time (NULL);
  unsigned int read_size = DEVICE_READ_SIZE;

  if (init)
    {
      int old;

      device_fd = open ("/dev/urandom", O_RDONLY);
      if (device_fd < 0)
        {
          _gnutls_debug_log ("Cannot open urandom!\n");
          return GNUTLS_E_FILE_ERROR;
        }

      old = fcntl (device_fd, F_GETFD);
      fcntl (device_fd, F_SETFD, old | 1);
      device_last_read = now;

      read_size = DEVICE_READ_SIZE_MAX; /* initially read more data */
    }

  if ((device_fd > 0)
      && (init || ((now - device_last_read) > DEVICE_READ_INTERVAL)))
    {
      /* More than 20 minutes since we last read the device */
      uint8_t buf[DEVICE_READ_SIZE_MAX];
      uint32_t done;

      for (done = 0; done < read_size;)
        {
          int res;
          do
            res = read (device_fd, buf + done, sizeof (buf) - done);
          while (res < 0 && errno == EINTR);

          if (res <= 0)
            {
              if (res < 0)
                {
                  _gnutls_debug_log ("Failed to read /dev/urandom: %s\n",
                                     strerror (errno));
                }
              else
                {
                  _gnutls_debug_log
                    ("Failed to read /dev/urandom: end of file\n");
                }

              return GNUTLS_E_INTERNAL_ERROR;
            }

          done += res;
        }

      device_last_read = now;
      return yarrow256_update (&yctx, RANDOM_SOURCE_DEVICE,
                               read_size * 8 / 2 /* we trust the RNG */ ,
                               read_size, buf);
    }
  return 0;
}
void yarrowUpdateStrong(unsigned source, unsigned entropy, unsigned length, const quint8 *data){
	yarrow256_update(&StrongCtx,source,entropy,length,data);
}