1 /* Copyright 2000 Enhanced Software Technologies Inc.
2    All Rights Reserved.
3 
4    Released under a BSD-style license. See file LICENSE for details.
5 
6    dorandom.c -- read /dev/urandom.
7 
8 RCS CHANGE LOG:
9 $Log: dorandom.c,v $
10 Revision 1.1.1.1  2001/05/17 17:11:01  elgreen
11 Initial checkin
12 
13 Revision 1.5  2000/12/12 16:41:21  eric
14 aescrypt 0.6 (large patch by Peter Pentchev).
15 
16 Revision 1.4  2000/04/24 19:46:57  eric
17 More autoconf tweaks. Changed to using Counterpane's TwoFish rather than
18 Dr. Gladman's TwoFish in an attempt to fix the big endian/little endian
19 problems once and for all.
20 
21 Revision 1.2  2000/04/05 22:09:19  eric
22 Daily checkin...
23 
24 Revision 1.1  2000/03/28 23:54:28  eric
25 Initial checkin -- aescrypt/aesget
26 
27 $Date: 2001/05/17 17:11:01 $
28 
29 */
30 
31 
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include "rijndael.h"
40 #include "dorandom.h"
41 
42 static int infile;
43 
urand_seed(char * filename)44 int urand_seed(char *filename) {
45   const char *name;
46 
47   if (filename) {
48     name=filename;
49   } else {
50     name=URAND_NAME;
51   }
52 
53   infile=open(name,O_RDONLY);
54   if (infile <= 0) {
55     return 1; /* could not open! */
56   }
57   return 0; /* all okay! */
58 }
59 
urand_get(int numbytes)60 BYTE *urand_get(int numbytes) {
61   BYTE *retval;
62   int n, result;
63 
64   retval=(BYTE *)malloc(numbytes);
65   if (!retval) {
66     fprintf(stderr,"dorandom:urand_get:error:out of memory.");
67     exit(1); /* sorry :-(. */
68   }
69 
70   result=read(infile,retval,numbytes);
71   for (n = result = 0; result < numbytes; result += n) {
72     n = read(infile, retval + result, numbytes - result);
73     if (n <= 0) {
74       free(retval);
75       return NULL;
76     }
77   }
78 
79   return retval;
80 }
81 
82