added API for random number generators, served through credential factory
[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, got;
67
68 done = 0;
69
70 while (done < bytes)
71 {
72 got = read(this->dev, buffer + done, bytes - done);
73 if (got <= 0)
74 {
75 DBG1("reading from \"%s\" failed: %s, retrying...",
76 this->file, strerror(errno));
77 close(this->dev);
78 sleep(1);
79 this->dev = open(this->file, 0);
80 }
81 done += got;
82 }
83 }
84
85 /**
86 * Implementation of random_rng_t.allocate_bytes.
87 */
88 static void allocate_bytes(private_random_rng_t *this, size_t bytes,
89 chunk_t *chunk)
90 {
91 *chunk = chunk_alloc(bytes);
92 get_bytes(this, chunk->len, chunk->ptr);
93 }
94
95 /**
96 * Implementation of random_rng_t.destroy.
97 */
98 static void destroy(private_random_rng_t *this)
99 {
100 close(this->dev);
101 free(this);
102 }
103
104 /*
105 * Described in header.
106 */
107 random_rng_t *random_rng_create(rng_quality_t quality)
108 {
109 private_random_rng_t *this = malloc_thing(private_random_rng_t);
110
111 /* public functions */
112 this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes;
113 this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes;
114 this->public.rng.destroy = (void (*) (rng_t *))destroy;
115
116 if (quality == RNG_REAL)
117 {
118 this->file = DEV_RANDOM;
119 }
120 else
121 {
122 this->file = DEV_URANDOM;
123 }
124
125 this->dev = open(this->file, 0);
126 if (this->dev < 0)
127 {
128 DBG1("opening \"%s\" failed: %s", this->file, strerror(errno));
129 free(this);
130 return NULL;
131 }
132 return &this->public;
133 }
134