418eeae287a6f37424b91703264ddd12d64888dc
[strongswan.git] / src / libstrongswan / plugins / random / random_plugin.c
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.
14 */
15
16 #include "random_plugin.h"
17
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23
24 #include <library.h>
25 #include <debug.h>
26 #include "random_rng.h"
27
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
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
49 /** /dev/random file descriptor */
50 static int dev_random = -1;
51 /** /dev/urandom file descriptor */
52 static int dev_urandom = -1;
53
54 /**
55 * See header.
56 */
57 int random_plugin_get_dev_random()
58 {
59 return dev_random;
60 }
61
62 /**
63 * See header.
64 */
65 int random_plugin_get_dev_urandom()
66 {
67 return dev_urandom;
68 }
69
70 /**
71 * Open a random device file
72 */
73 static bool open_dev(char *file, int *fd)
74 {
75 *fd = open(file, O_RDONLY);
76 if (*fd == -1)
77 {
78 DBG1(DBG_LIB, "opening \"%s\" failed: %s", file, strerror(errno));
79 return FALSE;
80 }
81 return TRUE;
82 }
83
84 METHOD(plugin_t, get_name, char*,
85 private_random_plugin_t *this)
86 {
87 return "random";
88 }
89
90 METHOD(plugin_t, get_features, int,
91 private_random_plugin_t *this, plugin_feature_t *features[])
92 {
93 static plugin_feature_t f[] = {
94 PLUGIN_REGISTER(RNG, random_rng_create),
95 PLUGIN_PROVIDE(RNG, RNG_STRONG),
96 PLUGIN_PROVIDE(RNG, RNG_TRUE),
97 };
98 *features = f;
99 return countof(f);
100 }
101
102 METHOD(plugin_t, destroy, void,
103 private_random_plugin_t *this)
104 {
105 if (dev_random != -1)
106 {
107 close(dev_random);
108 }
109 if (dev_urandom != -1)
110 {
111 close(dev_urandom);
112 }
113 free(this);
114 }
115
116 /*
117 * see header file
118 */
119 plugin_t *random_plugin_create()
120 {
121 private_random_plugin_t *this;
122
123 INIT(this,
124 .public = {
125 .plugin = {
126 .get_name = _get_name,
127 .get_features = _get_features,
128 .destroy = _destroy,
129 },
130 },
131 );
132
133 if (!open_dev(DEV_URANDOM, &dev_urandom) ||
134 !open_dev(DEV_RANDOM, &dev_random))
135 {
136 destroy(this);
137 return NULL;
138 }
139
140 return &this->public.plugin;
141 }
142