]>
Commit | Line | Data |
---|---|---|
6a365f07 MW |
1 | /* |
2 | * Copyright (C) 2008 Martin Willi | |
3 | * Hochschule fuer Technik Rapperswil | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
6a365f07 MW |
14 | */ |
15 | ||
16 | #include "random_plugin.h" | |
17 | ||
35852af7 MW |
18 | #include <unistd.h> |
19 | #include <sys/types.h> | |
20 | #include <sys/stat.h> | |
21 | #include <fcntl.h> | |
22 | #include <errno.h> | |
23 | ||
6a365f07 | 24 | #include <library.h> |
f05b4272 | 25 | #include <utils/debug.h> |
6a365f07 MW |
26 | #include "random_rng.h" |
27 | ||
35852af7 MW |
28 | #ifndef DEV_RANDOM |
29 | # define DEV_RANDOM "/dev/random" | |
30 | #endif | |
31 | ||
32 | #ifndef DEV_URANDOM | |
33 | # define DEV_URANDOM "/dev/urandom" | |
34 | #endif | |
35 | ||
6a365f07 MW |
36 | typedef struct private_random_plugin_t private_random_plugin_t; |
37 | ||
38 | /** | |
39 | * private data of random_plugin | |
40 | */ | |
41 | struct private_random_plugin_t { | |
42 | ||
43 | /** | |
44 | * public functions | |
45 | */ | |
46 | random_plugin_t public; | |
47 | }; | |
48 | ||
35852af7 MW |
49 | /** /dev/random file descriptor */ |
50 | static int dev_random = -1; | |
51 | /** /dev/urandom file descriptor */ | |
52 | static int dev_urandom = -1; | |
53 | ||
b63246c5 AS |
54 | /** Is strong randomness equivalent to true randomness? */ |
55 | static bool strong_equals_true = FALSE; | |
56 | ||
35852af7 MW |
57 | /** |
58 | * See header. | |
59 | */ | |
60 | int random_plugin_get_dev_random() | |
61 | { | |
62 | return dev_random; | |
63 | } | |
64 | ||
65 | /** | |
66 | * See header. | |
67 | */ | |
68 | int random_plugin_get_dev_urandom() | |
69 | { | |
70 | return dev_urandom; | |
71 | } | |
72 | ||
b63246c5 AS |
73 | /** |
74 | * See header. | |
75 | */ | |
76 | bool random_plugin_get_strong_equals_true() | |
77 | { | |
78 | return strong_equals_true; | |
79 | } | |
80 | ||
35852af7 MW |
81 | /** |
82 | * Open a random device file | |
83 | */ | |
84 | static bool open_dev(char *file, int *fd) | |
85 | { | |
86 | *fd = open(file, O_RDONLY); | |
87 | if (*fd == -1) | |
88 | { | |
89 | DBG1(DBG_LIB, "opening \"%s\" failed: %s", file, strerror(errno)); | |
90 | return FALSE; | |
91 | } | |
92 | return TRUE; | |
93 | } | |
94 | ||
787b5884 MW |
95 | METHOD(plugin_t, get_name, char*, |
96 | private_random_plugin_t *this) | |
97 | { | |
98 | return "random"; | |
99 | } | |
100 | ||
b2c44ace MW |
101 | METHOD(plugin_t, get_features, int, |
102 | private_random_plugin_t *this, plugin_feature_t *features[]) | |
103 | { | |
104 | static plugin_feature_t f[] = { | |
105 | PLUGIN_REGISTER(RNG, random_rng_create), | |
106 | PLUGIN_PROVIDE(RNG, RNG_STRONG), | |
107 | PLUGIN_PROVIDE(RNG, RNG_TRUE), | |
108 | }; | |
109 | *features = f; | |
110 | return countof(f); | |
111 | } | |
112 | ||
1bb67ff8 AS |
113 | METHOD(plugin_t, destroy, void, |
114 | private_random_plugin_t *this) | |
6a365f07 | 115 | { |
35852af7 MW |
116 | if (dev_random != -1) |
117 | { | |
118 | close(dev_random); | |
119 | } | |
120 | if (dev_urandom != -1) | |
121 | { | |
122 | close(dev_urandom); | |
123 | } | |
6a365f07 MW |
124 | free(this); |
125 | } | |
126 | ||
127 | /* | |
128 | * see header file | |
129 | */ | |
9ce567f8 | 130 | plugin_t *random_plugin_create() |
6a365f07 | 131 | { |
1bb67ff8 | 132 | private_random_plugin_t *this; |
7b68cd92 | 133 | char *urandom_file, *random_file; |
7daf5226 | 134 | |
1bb67ff8 AS |
135 | INIT(this, |
136 | .public = { | |
137 | .plugin = { | |
787b5884 | 138 | .get_name = _get_name, |
b2c44ace | 139 | .get_features = _get_features, |
1bb67ff8 AS |
140 | .destroy = _destroy, |
141 | }, | |
142 | }, | |
143 | ); | |
7daf5226 | 144 | |
b63246c5 | 145 | strong_equals_true = lib->settings->get_bool(lib->settings, |
8dc6e716 | 146 | "%s.plugins.random.strong_equals_true", FALSE, lib->ns); |
7b68cd92 | 147 | urandom_file = lib->settings->get_str(lib->settings, |
8dc6e716 | 148 | "%s.plugins.random.urandom", DEV_URANDOM, lib->ns); |
7b68cd92 | 149 | random_file = lib->settings->get_str(lib->settings, |
8dc6e716 | 150 | "%s.plugins.random.random", DEV_RANDOM, lib->ns); |
7b68cd92 MW |
151 | if (!open_dev(urandom_file, &dev_urandom) || |
152 | !open_dev(random_file, &dev_random)) | |
35852af7 MW |
153 | { |
154 | destroy(this); | |
155 | return NULL; | |
156 | } | |
157 | ||
6a365f07 MW |
158 | return &this->public.plugin; |
159 | } | |
160 |