aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_random.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/util/crypto_random.c
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/util/crypto_random.c')
-rw-r--r--src/util/crypto_random.c186
1 files changed, 93 insertions, 93 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 5928b96ea..03f6c0e98 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -61,37 +61,37 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
61 GNUNET_assert (i > 0); 61 GNUNET_assert (i > 0);
62 62
63 switch (mode) 63 switch (mode)
64 { 64 {
65 case GNUNET_CRYPTO_QUALITY_STRONG: 65 case GNUNET_CRYPTO_QUALITY_STRONG:
66 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ 66 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
67#ifdef gcry_fast_random_poll 67#ifdef gcry_fast_random_poll
68 if ((invokeCount++ % 256) == 0) 68 if ((invokeCount++ % 256) == 0)
69 gcry_fast_random_poll (); 69 gcry_fast_random_poll ();
70#endif 70#endif
71 ul = UINT32_MAX - (UINT32_MAX % i); 71 ul = UINT32_MAX - (UINT32_MAX % i);
72 do 72 do
73 { 73 {
74 gcry_randomize ((unsigned char *) &ret, 74 gcry_randomize ((unsigned char *) &ret,
75 sizeof (uint32_t), GCRY_STRONG_RANDOM); 75 sizeof (uint32_t), GCRY_STRONG_RANDOM);
76 } 76 }
77 while (ret >= ul); 77 while (ret >= ul);
78 return ret % i; 78 return ret % i;
79 case GNUNET_CRYPTO_QUALITY_NONCE: 79 case GNUNET_CRYPTO_QUALITY_NONCE:
80 ul = UINT32_MAX - (UINT32_MAX % i); 80 ul = UINT32_MAX - (UINT32_MAX % i);
81 do 81 do
82 { 82 {
83 gcry_create_nonce(&ret, sizeof(ret)); 83 gcry_create_nonce (&ret, sizeof (ret));
84 }
85 while (ret >= ul);
86 return ret % i;
87 case GNUNET_CRYPTO_QUALITY_WEAK:
88 ret = i * weak_random ();
89 if (ret >= i)
90 ret = i - 1;
91 return ret;
92 default:
93 GNUNET_assert (0);
94 } 84 }
85 while (ret >= ul);
86 return ret % i;
87 case GNUNET_CRYPTO_QUALITY_WEAK:
88 ret = i * weak_random ();
89 if (ret >= i)
90 ret = i - 1;
91 return ret;
92 default:
93 GNUNET_assert (0);
94 }
95 return 0; 95 return 0;
96} 96}
97 97
@@ -117,12 +117,12 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
117 for (i = 0; i < n; i++) 117 for (i = 0; i < n; i++)
118 ret[i] = i; 118 ret[i] = i;
119 for (i = n - 1; i > 0; i--) 119 for (i = n - 1; i > 0; i--)
120 { 120 {
121 x = GNUNET_CRYPTO_random_u32 (mode, i+1); 121 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
122 tmp = ret[x]; 122 tmp = ret[x];
123 ret[x] = ret[i]; 123 ret[x] = ret[i];
124 ret[i] = tmp; 124 ret[i] = tmp;
125 } 125 }
126 return ret; 126 return ret;
127} 127}
128 128
@@ -142,33 +142,33 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
142 142
143 GNUNET_assert (max > 0); 143 GNUNET_assert (max > 0);
144 switch (mode) 144 switch (mode)
145 {
146 case GNUNET_CRYPTO_QUALITY_STRONG:
147 ul = UINT64_MAX - (UINT64_MAX % max);
148 do
145 { 149 {
146 case GNUNET_CRYPTO_QUALITY_STRONG: 150 gcry_randomize ((unsigned char *) &ret,
147 ul = UINT64_MAX - (UINT64_MAX % max); 151 sizeof (uint64_t), GCRY_STRONG_RANDOM);
148 do
149 {
150 gcry_randomize ((unsigned char *) &ret,
151 sizeof (uint64_t), GCRY_STRONG_RANDOM);
152 }
153 while (ret >= ul);
154 return ret % max;
155 case GNUNET_CRYPTO_QUALITY_NONCE:
156 ul = UINT64_MAX - (UINT64_MAX % max);
157 do
158 {
159 gcry_create_nonce(&ret, sizeof(ret));
160 }
161 while (ret >= ul);
162
163 return ret % max;
164 case GNUNET_CRYPTO_QUALITY_WEAK:
165 ret = max * weak_random ();
166 if (ret >= max)
167 ret = max - 1;
168 return ret;
169 default:
170 GNUNET_assert (0);
171 } 152 }
153 while (ret >= ul);
154 return ret % max;
155 case GNUNET_CRYPTO_QUALITY_NONCE:
156 ul = UINT64_MAX - (UINT64_MAX % max);
157 do
158 {
159 gcry_create_nonce (&ret, sizeof (ret));
160 }
161 while (ret >= ul);
162
163 return ret % max;
164 case GNUNET_CRYPTO_QUALITY_WEAK:
165 ret = max * weak_random ();
166 if (ret >= max)
167 ret = max - 1;
168 return ret;
169 default:
170 GNUNET_assert (0);
171 }
172 return 0; 172 return 0;
173} 173}
174 174
@@ -205,33 +205,33 @@ entropy_generator (void *cls,
205 if (0 != strcmp (what, "need_entropy")) 205 if (0 != strcmp (what, "need_entropy"))
206 return; 206 return;
207 if (current == total) 207 if (current == total)
208 {
209 if (genproc != NULL)
208 { 210 {
209 if (genproc != NULL)
210 {
211 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
213 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
214 GNUNET_OS_process_close (genproc);
215 genproc = NULL;
216 }
217 return;
218 }
219 if (genproc != NULL)
220 {
221 ret = GNUNET_OS_process_status (genproc, &type, &code);
222 if (ret == GNUNET_NO)
223 return; /* still running */
224 if (ret == GNUNET_SYSERR)
225 {
226 GNUNET_break (0);
227 return;
228 }
229 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) 211 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
230 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); 212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
231 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); 213 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
232 GNUNET_OS_process_close (genproc); 214 GNUNET_OS_process_close (genproc);
233 genproc = NULL; 215 genproc = NULL;
234 } 216 }
217 return;
218 }
219 if (genproc != NULL)
220 {
221 ret = GNUNET_OS_process_status (genproc, &type, &code);
222 if (ret == GNUNET_NO)
223 return; /* still running */
224 if (ret == GNUNET_SYSERR)
225 {
226 GNUNET_break (0);
227 return;
228 }
229 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
230 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
231 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
232 GNUNET_OS_process_close (genproc);
233 genproc = NULL;
234 }
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 _("Starting `%s' process to generate entropy\n"), "find"); 236 _("Starting `%s' process to generate entropy\n"), "find");
237 genproc = GNUNET_OS_start_process (NULL, NULL, "sh", 237 genproc = GNUNET_OS_start_process (NULL, NULL, "sh",
@@ -246,11 +246,11 @@ static void
246killfind () 246killfind ()
247{ 247{
248 if (genproc != NULL) 248 if (genproc != NULL)
249 { 249 {
250 GNUNET_OS_process_kill (genproc, SIGKILL); 250 GNUNET_OS_process_kill (genproc, SIGKILL);
251 GNUNET_OS_process_close (genproc); 251 GNUNET_OS_process_close (genproc);
252 genproc = NULL; 252 genproc = NULL;
253 } 253 }
254} 254}
255 255
256 256
@@ -258,19 +258,20 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
258{ 258{
259 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 259 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
260 if (!gcry_check_version (GCRYPT_VERSION)) 260 if (!gcry_check_version (GCRYPT_VERSION))
261 { 261 {
262 fprintf (stderr, 262 fprintf (stderr,
263 _ 263 _
264 ("libgcrypt has not the expected version (version %s is required).\n"), 264 ("libgcrypt has not the expected version (version %s is required).\n"),
265 GCRYPT_VERSION); 265 GCRYPT_VERSION);
266 abort (); 266 abort ();
267 } 267 }
268#ifdef gcry_fast_random_poll 268#ifdef gcry_fast_random_poll
269 gcry_fast_random_poll (); 269 gcry_fast_random_poll ();
270#endif 270#endif
271 gcry_set_progress_handler (&entropy_generator, NULL); 271 gcry_set_progress_handler (&entropy_generator, NULL);
272 atexit (&killfind); 272 atexit (&killfind);
273 SRANDOM (time (NULL) ^ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX)); 273 SRANDOM (time (NULL) ^
274 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
274} 275}
275 276
276 277
@@ -282,4 +283,3 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini ()
282 283
283 284
284/* end of crypto_random.c */ 285/* end of crypto_random.c */
285