diff options
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r-- | src/util/crypto_hash.c | 127 |
1 files changed, 75 insertions, 52 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index d62ec8012..dcd46e5f9 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -135,18 +135,22 @@ GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a, | |||
135 | const struct GNUNET_HashCode *b, | 135 | const struct GNUNET_HashCode *b, |
136 | struct GNUNET_HashCode *result) | 136 | struct GNUNET_HashCode *result) |
137 | { | 137 | { |
138 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; | 138 | const unsigned long long *lla = (const unsigned long long *) a; |
139 | i >= 0; | 139 | const unsigned long long *llb = (const unsigned long long *) b; |
140 | i--) | 140 | unsigned long long *llr = (unsigned long long *) result; |
141 | result->bits[i] = a->bits[i] ^ b->bits[i]; | 141 | |
142 | GNUNET_static_assert (8 == sizeof (unsigned long long)); | ||
143 | GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long)); | ||
144 | for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--) | ||
145 | llr[i] = lla[i] ^ llb[i]; | ||
142 | } | 146 | } |
143 | 147 | ||
144 | 148 | ||
145 | void | 149 | void |
146 | GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, | 150 | GNUNET_CRYPTO_hash_to_aes_key ( |
147 | struct GNUNET_CRYPTO_SymmetricSessionKey *skey, | 151 | const struct GNUNET_HashCode *hc, |
148 | struct | 152 | struct GNUNET_CRYPTO_SymmetricSessionKey *skey, |
149 | GNUNET_CRYPTO_SymmetricInitializationVector *iv) | 153 | struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) |
150 | { | 154 | { |
151 | GNUNET_assert (GNUNET_YES == | 155 | GNUNET_assert (GNUNET_YES == |
152 | GNUNET_CRYPTO_kdf ( | 156 | GNUNET_CRYPTO_kdf ( |
@@ -167,33 +171,47 @@ GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, | |||
167 | } | 171 | } |
168 | 172 | ||
169 | 173 | ||
170 | int | 174 | unsigned int |
171 | GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, | 175 | GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h) |
172 | unsigned int bit) | ||
173 | { | 176 | { |
174 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | 177 | const unsigned long long *llp = (const unsigned long long *) h; |
175 | return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0; | 178 | unsigned int ret = 0; |
176 | } | 179 | unsigned int i; |
177 | |||
178 | 180 | ||
179 | int | 181 | GNUNET_static_assert (8 == sizeof (unsigned long long)); |
180 | GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code, | 182 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); |
181 | unsigned int bit) | 183 | for (i = 0; i<sizeof (*h) / sizeof (*llp); i++) |
182 | { | 184 | { |
183 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | 185 | if (0LLU != llp[i]) |
184 | return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; | 186 | break; |
187 | ret += sizeof (*llp) * 8; | ||
188 | } | ||
189 | if (ret == 8 * sizeof (*h)) | ||
190 | return ret; | ||
191 | ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
192 | return ret; | ||
185 | } | 193 | } |
186 | 194 | ||
187 | 195 | ||
188 | unsigned int | 196 | unsigned int |
189 | GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first, | 197 | GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h) |
190 | const struct GNUNET_HashCode *second) | ||
191 | { | 198 | { |
192 | for (unsigned int i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) | 199 | const unsigned long long *llp = (const unsigned long long *) h; |
193 | if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) != | 200 | unsigned int ret = 0; |
194 | GNUNET_CRYPTO_hash_get_bit_rtl (second, i)) | 201 | int i; |
195 | return i; | 202 | |
196 | return sizeof(struct GNUNET_HashCode) * 8; | 203 | GNUNET_static_assert (8 == sizeof (unsigned long long)); |
204 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); | ||
205 | for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--) | ||
206 | { | ||
207 | if (0LLU != llp[i]) | ||
208 | break; | ||
209 | ret += sizeof (*llp) * 8; | ||
210 | } | ||
211 | if (ret == 8 * sizeof (*h)) | ||
212 | return ret; | ||
213 | ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
214 | return ret; | ||
197 | } | 215 | } |
198 | 216 | ||
199 | 217 | ||
@@ -224,18 +242,19 @@ GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1, | |||
224 | const struct GNUNET_HashCode *h2, | 242 | const struct GNUNET_HashCode *h2, |
225 | const struct GNUNET_HashCode *target) | 243 | const struct GNUNET_HashCode *target) |
226 | { | 244 | { |
227 | unsigned int d1; | 245 | const unsigned long long *l1 = (const unsigned long long *) h1; |
228 | unsigned int d2; | 246 | const unsigned long long *l2 = (const unsigned long long *) h2; |
247 | const unsigned long long *t = (const unsigned long long *) target; | ||
229 | 248 | ||
230 | for (ssize_t i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1; | 249 | GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1)); |
231 | i >= 0; | 250 | for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++) |
232 | i--) | ||
233 | { | 251 | { |
234 | d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; | 252 | unsigned long long x1 = l1[i] ^ t[i]; |
235 | d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; | 253 | unsigned long long x2 = l2[i] ^ t[i]; |
236 | if (d1 > d2) | 254 | |
255 | if (x1 > x2) | ||
237 | return 1; | 256 | return 1; |
238 | else if (d1 < d2) | 257 | if (x1 < x2) |
239 | return -1; | 258 | return -1; |
240 | } | 259 | } |
241 | return 0; | 260 | return 0; |
@@ -243,25 +262,30 @@ GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1, | |||
243 | 262 | ||
244 | 263 | ||
245 | void | 264 | void |
246 | GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, | 265 | GNUNET_CRYPTO_hmac_derive_key ( |
247 | const struct | 266 | struct GNUNET_CRYPTO_AuthKey *key, |
248 | GNUNET_CRYPTO_SymmetricSessionKey *rkey, | 267 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, |
249 | const void *salt, size_t salt_len, ...) | 268 | const void *salt, size_t salt_len, |
269 | ...) | ||
250 | { | 270 | { |
251 | va_list argp; | 271 | va_list argp; |
252 | 272 | ||
253 | va_start (argp, salt_len); | 273 | va_start (argp, |
254 | GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp); | 274 | salt_len); |
275 | GNUNET_CRYPTO_hmac_derive_key_v (key, | ||
276 | rkey, | ||
277 | salt, salt_len, | ||
278 | argp); | ||
255 | va_end (argp); | 279 | va_end (argp); |
256 | } | 280 | } |
257 | 281 | ||
258 | 282 | ||
259 | void | 283 | void |
260 | GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, | 284 | GNUNET_CRYPTO_hmac_derive_key_v ( |
261 | const struct | 285 | struct GNUNET_CRYPTO_AuthKey *key, |
262 | GNUNET_CRYPTO_SymmetricSessionKey *rkey, | 286 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, |
263 | const void *salt, size_t salt_len, | 287 | const void *salt, size_t salt_len, |
264 | va_list argp) | 288 | va_list argp) |
265 | { | 289 | { |
266 | GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key), | 290 | GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key), |
267 | salt, salt_len, | 291 | salt, salt_len, |
@@ -283,7 +307,9 @@ GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len, | |||
283 | { | 307 | { |
284 | once = 1; | 308 | once = 1; |
285 | GNUNET_assert (GPG_ERR_NO_ERROR == | 309 | GNUNET_assert (GPG_ERR_NO_ERROR == |
286 | gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); | 310 | gcry_md_open (&md, |
311 | GCRY_MD_SHA512, | ||
312 | GCRY_MD_FLAG_HMAC)); | ||
287 | } | 313 | } |
288 | else | 314 | else |
289 | { | 315 | { |
@@ -323,15 +349,12 @@ GNUNET_CRYPTO_hash_context_start () | |||
323 | struct GNUNET_HashContext *hc; | 349 | struct GNUNET_HashContext *hc; |
324 | 350 | ||
325 | BENCHMARK_START (hash_context_start); | 351 | BENCHMARK_START (hash_context_start); |
326 | |||
327 | hc = GNUNET_new (struct GNUNET_HashContext); | 352 | hc = GNUNET_new (struct GNUNET_HashContext); |
328 | GNUNET_assert (0 == | 353 | GNUNET_assert (0 == |
329 | gcry_md_open (&hc->hd, | 354 | gcry_md_open (&hc->hd, |
330 | GCRY_MD_SHA512, | 355 | GCRY_MD_SHA512, |
331 | 0)); | 356 | 0)); |
332 | |||
333 | BENCHMARK_END (hash_context_start); | 357 | BENCHMARK_END (hash_context_start); |
334 | |||
335 | return hc; | 358 | return hc; |
336 | } | 359 | } |
337 | 360 | ||