using signed return value for read()
[strongswan.git] / src / libstrongswan / plugins / random / random_rng.c
1 /*
2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <debug.h>
26
27 #include "random_rng.h"
28
29 #ifndef DEV_RANDOM
30 # define DEV_RANDOM "/dev/random"
31 #endif
32
33 #ifndef DEV_URANDOM
34 # define DEV_URANDOM "/dev/urandom"
35 #endif
36
37 typedef struct private_random_rng_t private_random_rng_t;
38
39 /**
40 * Private data of an random_rng_t object.
41 */
42 struct private_random_rng_t {
43
44 /**
45 * Public random_rng_t interface.
46 */
47 random_rng_t public;
48
49 /**
50 * random device, depends on quality
51 */
52 int dev;
53
54 /**
55 * file we read random bytes from
56 */
57 char *file;
58 };
59
60 /**
61 * Implementation of random_rng_t.get_bytes.
62 */
63 static void get_bytes(private_random_rng_t *this, size_t bytes,
64 u_int8_t *buffer)
65 {
66 size_t done;
67 ssize_t got;
68
69 done = 0;
70
71 while (done < bytes)
72 {
73 got = read(this->dev, buffer + done, bytes - done);
74 if (got <= 0)
75 {
76 DBG1("reading from \"%s\" failed: %s, retrying...",
77 this->file, strerror(errno));
78 close(this->dev);
79 sleep(1);
80 this->dev = open(this->file, 0);
81 }
82 done += got;
83 }
84 }
85
86 /**
87 * Implementation of random_rng_t.allocate_bytes.
88 */
89 static void allocate_bytes(private_random_rng_t *this, size_t bytes,
90 chunk_t *chunk)
91 {
92 *chunk = chunk_alloc(bytes);
93 get_bytes(this, chunk->len, chunk->ptr);
94 }
95
96 /**
97 * Implementation of random_rng_t.destroy.
98 */
99 static void destroy(private_random_rng_t *this)
100 {
101 close(this->dev);
102 free(this);
103 }
104
105 /*
106 * Described in header.
107 */
108 random_rng_t *random_rng_create(rng_quality_t quality)
109 {
110 private_random_rng_t *this = malloc_thing(private_random_rng_t);
111
112 /* public functions */
113 this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes;
114 this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes;
115 this->public.rng.destroy = (void (*) (rng_t *))destroy;
116
117 if (quality == RNG_REAL)
118 {
119 this->file = DEV_RANDOM;
120 }
121 else
122 {
123 this->file = DEV_URANDOM;
124 }
125
126 this->dev = open(this->file, 0);
127 if (this->dev < 0)
128 {
129 DBG1("opening \"%s\" failed: %s", this->file, strerror(errno));
130 free(this);
131 return NULL;
132 }
133 return &this->public;
134 }
135