used rsa coeff field in OpenPGP secret key payload
[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
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <debug.h>
24
25 #include "random_rng.h"
26
27 #ifndef DEV_RANDOM
28 # define DEV_RANDOM "/dev/random"
29 #endif
30
31 #ifndef DEV_URANDOM
32 # define DEV_URANDOM "/dev/urandom"
33 #endif
34
35 typedef struct private_random_rng_t private_random_rng_t;
36
37 /**
38 * Private data of an random_rng_t object.
39 */
40 struct private_random_rng_t {
41
42 /**
43 * Public random_rng_t interface.
44 */
45 random_rng_t public;
46
47 /**
48 * random device, depends on quality
49 */
50 int dev;
51
52 /**
53 * file we read random bytes from
54 */
55 char *file;
56 };
57
58 /**
59 * Implementation of random_rng_t.get_bytes.
60 */
61 static void get_bytes(private_random_rng_t *this, size_t bytes,
62 u_int8_t *buffer)
63 {
64 size_t done;
65 ssize_t got;
66
67 done = 0;
68
69 while (done < bytes)
70 {
71 got = read(this->dev, buffer + done, bytes - done);
72 if (got <= 0)
73 {
74 DBG1("reading from \"%s\" failed: %s, retrying...",
75 this->file, strerror(errno));
76 close(this->dev);
77 sleep(1);
78 this->dev = open(this->file, 0);
79 }
80 done += got;
81 }
82 }
83
84 /**
85 * Implementation of random_rng_t.allocate_bytes.
86 */
87 static void allocate_bytes(private_random_rng_t *this, size_t bytes,
88 chunk_t *chunk)
89 {
90 *chunk = chunk_alloc(bytes);
91 get_bytes(this, chunk->len, chunk->ptr);
92 }
93
94 /**
95 * Implementation of random_rng_t.destroy.
96 */
97 static void destroy(private_random_rng_t *this)
98 {
99 close(this->dev);
100 free(this);
101 }
102
103 /*
104 * Described in header.
105 */
106 random_rng_t *random_rng_create(rng_quality_t quality)
107 {
108 private_random_rng_t *this = malloc_thing(private_random_rng_t);
109
110 /* public functions */
111 this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes;
112 this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes;
113 this->public.rng.destroy = (void (*) (rng_t *))destroy;
114
115 if (quality == RNG_TRUE)
116 {
117 this->file = DEV_RANDOM;
118 }
119 else
120 {
121 this->file = DEV_URANDOM;
122 }
123
124 this->dev = open(this->file, 0);
125 if (this->dev < 0)
126 {
127 DBG1("opening \"%s\" failed: %s", this->file, strerror(errno));
128 free(this);
129 return NULL;
130 }
131 return &this->public;
132 }
133