aboutsummaryrefslogtreecommitdiffstats
path: root/rand.c
blob: 1542bc3d2524c5a5f61e28d85ab87f5ab282bc72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
  Licensed under the GNU Public License.
  Copyright (C) 1998-2009 by Thomas M. Vier, Jr. All Rights Reserved.

  wipe is free software.
  See LICENSE for more information.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "config.h"
#include "std.h"
#include "io.h"
#include "mt.h"
#include "main.h"
#include "rand.h"

extern char *argvzero;
extern int errno, exit_code;
extern struct opt_s options;

int entropyfd;
char entropy_name[13]; /* for do_read error reporting */

/*
  rand_init -- inits the entropy source file descriptor
*/

public int rand_init(void)
{
  /* try /dev/urandom first; if that fails, try /dev/random */
  if ((entropyfd = open("/dev/urandom", O_RDONLY)) < 0)
    {
      if ((entropyfd = open("/dev/random", O_RDONLY)) < 0)
	{
	  fprintf(stderr, "\r%s: cannot open entropy source: %s\n",
		  argvzero, strerror(errno));
	  exit(1);
	}
      else
	{
	  strncpy(entropy_name, "/dev/random", sizeof(entropy_name));
	  fprintf(stderr, "\r%s: warning: cannot open /dev/urandom, "
		  "using /dev/random instead\n", argvzero);
	}
    }
  else
    strncpy(entropy_name, "/dev/urandom", sizeof(entropy_name));

  /* we must seed once at least regardless of seclevel --tg */
  if (prng_seed())
    return FAILED;

  return 0;
}

/*** the following functions are PRNG dependent ***/

/*
  prng_seed -- init seed
*/

public int prng_seed(void)
{
  u_rand_t seed;

  if (do_read(entropy_name, entropyfd, &seed, sizeof(prng_seed)))
    return FAILED;

  seedMT(seed);
  return 0;
}

/*
  prng_get_rand -- return u_rand_t PRN
*/

public u_rand_t prng_get_rand(void)
{
  return randomMT();
}

/*
  prng_fillbuf -- fills a buffer with pseudo-random values
                  the buffer must be u_rand_t aligned
*/

public void prng_fillbuf(const int seclevel, u_rand_t *buf, const size_t size)
{
  int i, ii;
  u_rand_t rand;
  unsigned char *cbuf, *randp;

  i=0; ii = size / sizeof(u_rand_t);

  while (i < ii)
    buf[i++] = randomMT();

  ii = size % sizeof(u_rand_t);

  if (ii)
    {
      rand = randomMT();
      cbuf = (unsigned char *) ((void *) buf + (size - ii));
      randp = (unsigned char *) &rand;

      i=0;
      while (i < ii)
	{
	  cbuf[i] = randp[i];
	  i++;
	}
    }
}