aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_random.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-10-08 17:43:44 +0000
committerChristian Grothoff <christian@grothoff.org>2010-10-08 17:43:44 +0000
commit7a7b8fdd051e89a62e4bc68e2cc6dd755d5fbce4 (patch)
tree1eda25b633c470430eb11e8ae003bdc181394976 /src/util/crypto_random.c
parentbf373f6c39138016ab84928d4e29780d1c02a424 (diff)
downloadgnunet-7a7b8fdd051e89a62e4bc68e2cc6dd755d5fbce4.tar.gz
gnunet-7a7b8fdd051e89a62e4bc68e2cc6dd755d5fbce4.zip
nonce
Diffstat (limited to 'src/util/crypto_random.c')
-rw-r--r--src/util/crypto_random.c29
1 files changed, 14 insertions, 15 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 14d87b1cd..4fcb157d9 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -60,8 +60,9 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
60 60
61 GNUNET_assert (i > 0); 61 GNUNET_assert (i > 0);
62 62
63 if (mode == GNUNET_CRYPTO_QUALITY_STRONG) 63 switch (mode)
64 { 64 {
65 case GNUNET_CRYPTO_QUALITY_STRONG:
65 /* 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 */
66#ifdef gcry_fast_random_poll 67#ifdef gcry_fast_random_poll
67 if ((invokeCount++ % 256) == 0) 68 if ((invokeCount++ % 256) == 0)
@@ -75,25 +76,23 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
75 } 76 }
76 while (ret >= ul); 77 while (ret >= ul);
77 return ret % i; 78 return ret % i;
78 } 79 case GNUNET_CRYPTO_QUALITY_NONCE:
79 else if (mode == GNUNET_CRYPTO_QUALITY_NONCE)
80 {
81 ul = UINT32_MAX - (UINT32_MAX % i); 80 ul = UINT32_MAX - (UINT32_MAX % i);
82 do 81 do
83 { 82 {
84 gcry_create_nonce(&ret, sizeof(ret)); 83 gcry_create_nonce(&ret, sizeof(ret));
85 } 84 }
86 while (ret >= ul); 85 while (ret >= ul);
87
88 return ret % i; 86 return ret % i;
89 } 87 case GNUNET_CRYPTO_QUALITY_WEAK:
90 else
91 {
92 ret = i * weak_random (); 88 ret = i * weak_random ();
93 if (ret >= i) 89 if (ret >= i)
94 ret = i - 1; 90 ret = i - 1;
95 return ret; 91 return ret;
92 default:
93 GNUNET_assert (0);
96 } 94 }
95 return 0;
97} 96}
98 97
99 98
@@ -142,8 +141,9 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
142 uint64_t ul; 141 uint64_t ul;
143 142
144 GNUNET_assert (max > 0); 143 GNUNET_assert (max > 0);
145 if (mode == GNUNET_CRYPTO_QUALITY_STRONG) 144 switch (mode)
146 { 145 {
146 case GNUNET_CRYPTO_QUALITY_STRONG:
147 ul = UINT64_MAX - (UINT64_MAX % max); 147 ul = UINT64_MAX - (UINT64_MAX % max);
148 do 148 do
149 { 149 {
@@ -152,9 +152,7 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
152 } 152 }
153 while (ret >= ul); 153 while (ret >= ul);
154 return ret % max; 154 return ret % max;
155 } 155 case GNUNET_CRYPTO_QUALITY_NONCE:
156 else if (mode == GNUNET_CRYPTO_QUALITY_NONCE)
157 {
158 ul = UINT64_MAX - (UINT64_MAX % max); 156 ul = UINT64_MAX - (UINT64_MAX % max);
159 do 157 do
160 { 158 {
@@ -163,14 +161,15 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
163 while (ret >= ul); 161 while (ret >= ul);
164 162
165 return ret % max; 163 return ret % max;
166 } 164 case GNUNET_CRYPTO_QUALITY_WEAK:
167 else
168 {
169 ret = max * weak_random (); 165 ret = max * weak_random ();
170 if (ret >= max) 166 if (ret >= max)
171 ret = max - 1; 167 ret = max - 1;
172 return ret; 168 return ret;
169 default:
170 GNUNET_assert (0);
173 } 171 }
172 return 0;
174} 173}
175 174
176/** 175/**