aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-03-19 10:17:48 +0000
committerChristian Grothoff <christian@grothoff.org>2012-03-19 10:17:48 +0000
commitd79b3436609eb619247a8a682acf1fdd5e4d93fa (patch)
treeb53fe7e634183cb8673e2016c678c431c7504787 /src
parent595ba59c3158e65100f4f832cf3bdf15d7e2a72e (diff)
downloadgnunet-d79b3436609eb619247a8a682acf1fdd5e4d93fa.tar.gz
gnunet-d79b3436609eb619247a8a682acf1fdd5e4d93fa.zip
adding API for short (256-bit) hash codes
Diffstat (limited to 'src')
-rw-r--r--src/datastore/datastore.h4
-rw-r--r--src/exit/exit.h6
-rw-r--r--src/fs/gnunet-service-fs.h2
-rw-r--r--src/include/block_dns.h2
-rw-r--r--src/include/gnunet_common.h8
-rw-r--r--src/include/gnunet_crypto_lib.h99
-rw-r--r--src/include/gnunet_strings_lib.h4
-rw-r--r--src/util/crypto_hash.c202
-rw-r--r--src/util/gnunet-rsa.c18
-rw-r--r--src/util/strings.c16
-rw-r--r--src/vpn/vpn.h2
11 files changed, 254 insertions, 109 deletions
diff --git a/src/datastore/datastore.h b/src/datastore/datastore.h
index 1126027f8..c8b2c722b 100644
--- a/src/datastore/datastore.h
+++ b/src/datastore/datastore.h
@@ -130,7 +130,7 @@ struct GetMessage
130 * Desired key (optional). Check the "size" of the 130 * Desired key (optional). Check the "size" of the
131 * header to see if the key is actually present. 131 * header to see if the key is actually present.
132 */ 132 */
133 GNUNET_HashCode key GNUNET_PACKED; 133 GNUNET_HashCode key;
134 134
135}; 135};
136 136
@@ -253,7 +253,7 @@ struct DataMessage
253 /** 253 /**
254 * Key under which the item can be found. 254 * Key under which the item can be found.
255 */ 255 */
256 GNUNET_HashCode key GNUNET_PACKED; 256 GNUNET_HashCode key;
257 257
258}; 258};
259GNUNET_NETWORK_STRUCT_END 259GNUNET_NETWORK_STRUCT_END
diff --git a/src/exit/exit.h b/src/exit/exit.h
index dcc50f1b3..90df26d7d 100644
--- a/src/exit/exit.h
+++ b/src/exit/exit.h
@@ -49,7 +49,7 @@ struct GNUNET_EXIT_TcpServiceStartMessage
49 /** 49 /**
50 * Identification for the desired service. 50 * Identification for the desired service.
51 */ 51 */
52 GNUNET_HashCode service_descriptor GNUNET_PACKED; 52 GNUNET_HashCode service_descriptor;
53 53
54 /** 54 /**
55 * Skeleton of the TCP header to send. Port numbers are to 55 * Skeleton of the TCP header to send. Port numbers are to
@@ -142,7 +142,7 @@ struct GNUNET_EXIT_UdpServiceMessage
142 /** 142 /**
143 * Identification for the desired service. 143 * Identification for the desired service.
144 */ 144 */
145 GNUNET_HashCode service_descriptor GNUNET_PACKED; 145 GNUNET_HashCode service_descriptor;
146 146
147 /* followed by UDP payload */ 147 /* followed by UDP payload */
148}; 148};
@@ -230,7 +230,7 @@ struct GNUNET_EXIT_IcmpServiceMessage
230 /** 230 /**
231 * Identification for the desired service. 231 * Identification for the desired service.
232 */ 232 */
233 GNUNET_HashCode service_descriptor GNUNET_PACKED; 233 GNUNET_HashCode service_descriptor;
234 234
235 /** 235 /**
236 * ICMP header to use. 236 * ICMP header to use.
diff --git a/src/fs/gnunet-service-fs.h b/src/fs/gnunet-service-fs.h
index 5ea73ee28..0c796bf9c 100644
--- a/src/fs/gnunet-service-fs.h
+++ b/src/fs/gnunet-service-fs.h
@@ -129,7 +129,7 @@ struct GetMessage
129 * Hashcodes of the file(s) we're looking for. 129 * Hashcodes of the file(s) we're looking for.
130 * Details depend on the query type. 130 * Details depend on the query type.
131 */ 131 */
132 GNUNET_HashCode query GNUNET_PACKED; 132 GNUNET_HashCode query;
133 133
134 /* this is followed by hash codes as specified in the "hash_bitmap"; 134 /* this is followed by hash codes as specified in the "hash_bitmap";
135 * after that, an optional bloomfilter (with bits set for replies 135 * after that, an optional bloomfilter (with bits set for replies
diff --git a/src/include/block_dns.h b/src/include/block_dns.h
index 4c2f1a3f3..e047779c5 100644
--- a/src/include/block_dns.h
+++ b/src/include/block_dns.h
@@ -64,7 +64,7 @@ struct GNUNET_DNS_Record
64 * The descriptor for the service 64 * The descriptor for the service
65 * (a peer may provide more than one service) 65 * (a peer may provide more than one service)
66 */ 66 */
67 GNUNET_HashCode service_descriptor GNUNET_PACKED; 67 GNUNET_HashCode service_descriptor;
68 68
69 /** 69 /**
70 * When does this record expire? 70 * When does this record expire?
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h
index f26d6bc98..141df1a5d 100644
--- a/src/include/gnunet_common.h
+++ b/src/include/gnunet_common.h
@@ -208,27 +208,25 @@ struct GNUNET_MessageHeader
208 uint16_t type GNUNET_PACKED; 208 uint16_t type GNUNET_PACKED;
209 209
210}; 210};
211GNUNET_NETWORK_STRUCT_END 211
212 212
213/** 213/**
214 * @brief 512-bit hashcode 214 * @brief 512-bit hashcode
215 */ 215 */
216typedef struct 216typedef struct GNUNET_HashCode
217{ 217{
218 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */ 218 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
219} 219}
220GNUNET_HashCode; 220GNUNET_HashCode;
221 221
222 222
223GNUNET_NETWORK_STRUCT_BEGIN
224
225/** 223/**
226 * The identity of the host (basically the SHA-512 hashcode of 224 * The identity of the host (basically the SHA-512 hashcode of
227 * it's public key). 225 * it's public key).
228 */ 226 */
229struct GNUNET_PeerIdentity 227struct GNUNET_PeerIdentity
230{ 228{
231 GNUNET_HashCode hashPubKey GNUNET_PACKED; 229 GNUNET_HashCode hashPubKey;
232}; 230};
233GNUNET_NETWORK_STRUCT_END 231GNUNET_NETWORK_STRUCT_END
234 232
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 6224546b9..8270c89dd 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Christian Grothoff (and other contributing authors) 3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -138,6 +138,26 @@ struct GNUNET_CRYPTO_HashAsciiEncoded
138 138
139 139
140 140
141
142/**
143 * @brief 256-bit hashcode
144 */
145struct GNUNET_CRYPTO_ShortHashCode
146{
147 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
148};
149
150
151/**
152 * @brief 0-terminated ASCII encoding of a 'struct GNUNET_ShortHashCode'.
153 */
154struct GNUNET_CRYPTO_ShortHashAsciiEncoded
155{
156 unsigned char short_encoding[53];
157};
158
159
160
141/** 161/**
142 * @brief an RSA signature 162 * @brief an RSA signature
143 */ 163 */
@@ -433,7 +453,19 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
433 453
434 454
435/** 455/**
436 * Convert ASCII encoding back to GNUNET_CRYPTO_hash 456 * Convert short hash to ASCII encoding.
457 *
458 * @param block the hash code
459 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
460 * safely cast to char*, a '\\0' termination is set).
461 */
462void
463GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
464 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result);
465
466
467/**
468 * Convert ASCII encoding back to a 'GNUNET_HashCode'
437 * 469 *
438 * @param enc the encoding 470 * @param enc the encoding
439 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) 471 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
@@ -446,16 +478,41 @@ GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
446 478
447 479
448/** 480/**
449 * Convert ASCII encoding back to GNUNET_CRYPTO_hash 481 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
482 *
450 * @param enc the encoding 483 * @param enc the encoding
484 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
451 * @param result where to store the GNUNET_CRYPTO_hash code 485 * @param result where to store the GNUNET_CRYPTO_hash code
452 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding 486 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
453 */ 487 */
488int
489GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
490 struct GNUNET_CRYPTO_ShortHashCode * result);
491
492
493/**
494 * Convert ASCII encoding back to GNUNET_HashCode
495 *
496 * @param enc the encoding
497 * @param result where to store the hash code
498 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
499 */
454#define GNUNET_CRYPTO_hash_from_string(enc, result) \ 500#define GNUNET_CRYPTO_hash_from_string(enc, result) \
455 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result) 501 GNUNET_CRYPTO_hash_from_string2 (enc, strlen(enc), result)
456 502
457 503
458/** 504/**
505 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
506 *
507 * @param enc the encoding
508 * @param result where to store the GNUNET_CRYPTO_ShortHash
509 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
510 */
511#define GNUNET_CRYPTO_short_hash_from_string(enc, result) \
512 GNUNET_CRYPTO_short_hash_from_string2 (enc, strlen(enc), result)
513
514
515/**
459 * Compute the distance between 2 hashcodes. 516 * Compute the distance between 2 hashcodes.
460 * The computation must be fast, not involve 517 * The computation must be fast, not involve
461 * a.a or a.e (they're used elsewhere), and 518 * a.a or a.e (they're used elsewhere), and
@@ -483,6 +540,42 @@ GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret);
483 540
484 541
485/** 542/**
543 * Compute short (256-bit) hash of a given block.
544 *
545 * @param block the data to hash
546 * @param size size of the block
547 * @param ret pointer to where to write the hashcode
548 */
549void
550GNUNET_CRYPTO_short_hash (const void *block, size_t size,
551 struct GNUNET_CRYPTO_ShortHashCode * ret);
552
553
554/**
555 * Double short (256-bit) hash to create a long hash.
556 *
557 * @param sh short hash to double
558 * @param dh where to store the (doubled) long hash (not really a hash)
559 */
560void
561GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
562 struct GNUNET_HashCode *dh);
563
564
565/**
566 * Truncate doubled short hash back to a short hash.
567 *
568 * @param lh doubled short hash to reduce again
569 * @param sh where to store the short hash
570 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
571 * doubled short hash
572 */
573int
574GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
575 struct GNUNET_CRYPTO_ShortHashCode *sh);
576
577
578/**
486 * Calculate HMAC of a message (RFC 2104) 579 * Calculate HMAC of a message (RFC 2104)
487 * 580 *
488 * @param key secret key 581 * @param key secret key
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index 997ca7dd9..55b96fc2c 100644
--- a/src/include/gnunet_strings_lib.h
+++ b/src/include/gnunet_strings_lib.h
@@ -230,8 +230,8 @@ GNUNET_STRINGS_get_short_name (const char *filename);
230 * @return pointer to the next byte in 'out' or NULL on error. 230 * @return pointer to the next byte in 'out' or NULL on error.
231 */ 231 */
232char * 232char *
233GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, 233GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size,
234 char *out, size_t out_size); 234 char *out, size_t out_size);
235 235
236 236
237/** 237/**
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index f89639045..aaf5175d7 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors) 3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -34,6 +34,7 @@
34#include "gnunet_common.h" 34#include "gnunet_common.h"
35#include "gnunet_crypto_lib.h" 35#include "gnunet_crypto_lib.h"
36#include "gnunet_disk_lib.h" 36#include "gnunet_disk_lib.h"
37#include "gnunet_strings_lib.h"
37#include <gcrypt.h> 38#include <gcrypt.h>
38 39
39#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) 40#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
@@ -55,6 +56,21 @@ GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret)
55 56
56 57
57/** 58/**
59 * Compute short (256-bit) hash of a given block.
60 *
61 * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
62 * @param size the length of the data to GNUNET_CRYPTO_hash
63 * @param ret pointer to where to write the hashcode
64 */
65void
66GNUNET_CRYPTO_short_hash (const void *block, size_t size,
67 struct GNUNET_CRYPTO_ShortHashCode * ret)
68{
69 gcry_md_hash_buffer (GCRY_MD_SHA256, ret, block, size);
70}
71
72
73/**
58 * Context used when hashing a file. 74 * Context used when hashing a file.
59 */ 75 */
60struct GNUNET_CRYPTO_FileHashContext 76struct GNUNET_CRYPTO_FileHashContext
@@ -243,24 +259,6 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
243 259
244/* ***************** binary-ASCII encoding *************** */ 260/* ***************** binary-ASCII encoding *************** */
245 261
246/* FIXME: should use GNUNET_STRINGS_data_to_string and strings_to_data below!!! */
247
248/**
249 * Get the numeric value corresponding to a character.
250 *
251 * @param a a character
252 * @return corresponding numeric value
253 */
254static unsigned int
255getValue__ (unsigned char a)
256{
257 if ((a >= '0') && (a <= '9'))
258 return a - '0';
259 if ((a >= 'A') && (a <= 'V'))
260 return (a - 'A' + 10);
261 return -1;
262}
263
264 262
265/** 263/**
266 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather 264 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather
@@ -277,88 +275,32 @@ void
277GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block, 275GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
278 struct GNUNET_CRYPTO_HashAsciiEncoded *result) 276 struct GNUNET_CRYPTO_HashAsciiEncoded *result)
279{ 277{
280 /** 278 char *np;
281 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) 279
282 */ 280 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
283 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; 281 sizeof (struct GNUNET_HashCode),
284 unsigned int wpos; 282 (char*) result,
285 unsigned int rpos; 283 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
286 unsigned int bits; 284 GNUNET_assert (NULL != np);
287 unsigned int vbit; 285 *np = '\0';
288
289 GNUNET_assert (block != NULL);
290 GNUNET_assert (result != NULL);
291 vbit = 0;
292 wpos = 0;
293 rpos = 0;
294 bits = 0;
295 while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0))
296 {
297 if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5))
298 {
299 bits = (bits << 8) | ((unsigned char *) block)[rpos++]; /* eat 8 more bits */
300 vbit += 8;
301 }
302 if (vbit < 5)
303 {
304 bits <<= (5 - vbit); /* zero-padding */
305 GNUNET_assert (vbit == 2); /* padding by 3: 512+3 mod 5 == 0 */
306 vbit = 5;
307 }
308 GNUNET_assert (wpos < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
309 result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
310 vbit -= 5;
311 }
312 GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
313 GNUNET_assert (vbit == 0);
314 result->encoding[wpos] = '\0';
315} 286}
316 287
317 288
318/** 289/**
319 * Convert ASCII encoding back to GNUNET_CRYPTO_hash 290 * Convert ASCII encoding back to hash code.
320 * 291 *
321 * @param enc the encoding 292 * @param enc the encoding
322 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) 293 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
323 * @param result where to store the GNUNET_CRYPTO_hash code 294 * @param result where to store the hash code
324 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding 295 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
325 */ 296 */
326int 297int
327GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen, 298GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
328 GNUNET_HashCode * result) 299 GNUNET_HashCode * result)
329{ 300{
330 unsigned int rpos; 301 return GNUNET_STRINGS_string_to_data (enc, enclen,
331 unsigned int wpos; 302 (unsigned char*) result,
332 unsigned int bits; 303 sizeof (struct GNUNET_HashCode));
333 unsigned int vbit;
334 int ret;
335
336 if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
337 return GNUNET_SYSERR;
338
339 vbit = 2; /* padding! */
340 wpos = sizeof (GNUNET_HashCode);
341 rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
342 bits = (ret = getValue__ (enc[--rpos])) >> 3;
343 if (-1 == ret)
344 return GNUNET_SYSERR;
345 while (wpos > 0)
346 {
347 GNUNET_assert (rpos > 0);
348 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
349 if (-1 == ret)
350 return GNUNET_SYSERR;
351 vbit += 5;
352 if (vbit >= 8)
353 {
354 ((unsigned char *) result)[--wpos] = (unsigned char) bits;
355 bits >>= 8;
356 vbit -= 8;
357 }
358 }
359 GNUNET_assert (rpos == 0);
360 GNUNET_assert (vbit == 0);
361 return GNUNET_OK;
362} 304}
363 305
364 306
@@ -645,4 +587,88 @@ GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
645} 587}
646 588
647 589
590
591/**
592 * Double short (256-bit) hash to create a long hash.
593 *
594 * @param sh short hash to double
595 * @param dh where to store the (doubled) long hash (not really a hash)
596 */
597void
598GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
599 struct GNUNET_HashCode *dh)
600{
601 char *ptr;
602
603 ptr = (char*) dh;
604 memcpy (ptr, sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
605 memcpy (&ptr[sizeof (struct GNUNET_CRYPTO_ShortHashCode)], sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
606}
607
608
609/**
610 * Truncate doubled short hash back to a short hash.
611 *
612 * @param lh doubled short hash to reduce again
613 * @param sh where to store the short hash
614 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
615 * doubled short hash
616 */
617int
618GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
619 struct GNUNET_CRYPTO_ShortHashCode *sh)
620{
621 const struct GNUNET_CRYPTO_ShortHashCode *s;
622
623 s = (const struct GNUNET_CRYPTO_ShortHashCode *) dh;
624 if (0 != memcmp (&s[0],
625 &s[1],
626 sizeof (struct GNUNET_CRYPTO_ShortHashCode)))
627 return GNUNET_SYSERR;
628 *sh = *s;
629 return GNUNET_OK;
630}
631
632
633/**
634 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
635 *
636 * @param enc the encoding
637 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
638 * @param result where to store the GNUNET_CRYPTO_hash code
639 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
640 */
641int
642GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
643 struct GNUNET_CRYPTO_ShortHashCode * result)
644{
645 return GNUNET_STRINGS_string_to_data (enc, enclen,
646 (unsigned char*) result,
647 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
648}
649
650
651/**
652 * Convert short hash to ASCII encoding.
653 *
654 * @param block the hash code
655 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
656 * safely cast to char*, a '\\0' termination is set).
657 */
658void
659GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
660 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result)
661{
662 char *np;
663
664 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
665 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
666 (char*) result,
667 sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) - 1);
668 GNUNET_assert (NULL != np);
669 *np = '\0';
670}
671
672
673
648/* end of crypto_hash.c */ 674/* end of crypto_hash.c */
diff --git a/src/util/gnunet-rsa.c b/src/util/gnunet-rsa.c
index bfd854e65..f3cd83a8b 100644
--- a/src/util/gnunet-rsa.c
+++ b/src/util/gnunet-rsa.c
@@ -37,6 +37,11 @@ static int print_public_key;
37 */ 37 */
38static int print_peer_identity; 38static int print_peer_identity;
39 39
40/**
41 * Flag for printing short hash of public key.
42 */
43static int print_short_identity;
44
40 45
41/** 46/**
42 * Main function that will be run by the scheduler. 47 * Main function that will be run by the scheduler.
@@ -78,6 +83,16 @@ run (void *cls, char *const *args, const char *cfgfile,
78 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc); 83 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
79 fprintf (stdout, "%s\n", enc.encoding); 84 fprintf (stdout, "%s\n", enc.encoding);
80 } 85 }
86 if (print_short_identity)
87 {
88 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
89 struct GNUNET_CRYPTO_ShortHashCode sh;
90
91 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
92 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &sh);
93 GNUNET_CRYPTO_short_hash_to_enc (&sh, &enc);
94 fprintf (stdout, "%s\n", enc.short_encoding);
95 }
81 GNUNET_CRYPTO_rsa_key_free (pk); 96 GNUNET_CRYPTO_rsa_key_free (pk);
82} 97}
83 98
@@ -99,6 +114,9 @@ main (int argc, char *const *argv)
99 { 'P', "print-peer-identity", NULL, 114 { 'P', "print-peer-identity", NULL,
100 gettext_noop ("print the hash of the public key in ASCII format"), 115 gettext_noop ("print the hash of the public key in ASCII format"),
101 0, &GNUNET_GETOPT_set_one, &print_peer_identity }, 116 0, &GNUNET_GETOPT_set_one, &print_peer_identity },
117 { 's', "print-short-identity", NULL,
118 gettext_noop ("print the short hash of the public key in ASCII format"),
119 0, &GNUNET_GETOPT_set_one, &print_short_identity },
102 GNUNET_GETOPT_OPTION_END 120 GNUNET_GETOPT_OPTION_END
103 }; 121 };
104 return (GNUNET_OK == 122 return (GNUNET_OK ==
diff --git a/src/util/strings.c b/src/util/strings.c
index 2ed1a0da3..40968956e 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -643,10 +643,10 @@ getValue__ (unsigned char a)
643 * @return pointer to the next byte in 'out' or NULL on error. 643 * @return pointer to the next byte in 'out' or NULL on error.
644 */ 644 */
645char * 645char *
646GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size) 646GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out, size_t out_size)
647{ 647{
648 /** 648 /**
649 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) 649 * 32 characters for encoding
650 */ 650 */
651 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; 651 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
652 unsigned int wpos; 652 unsigned int wpos;
@@ -656,7 +656,11 @@ GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size
656 656
657 GNUNET_assert (data != NULL); 657 GNUNET_assert (data != NULL);
658 GNUNET_assert (out != NULL); 658 GNUNET_assert (out != NULL);
659 GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5)); 659 if (out_size < (((size*8) + ((size*8) % 5)) % 5))
660 {
661 GNUNET_break (0);
662 return NULL;
663 }
660 vbit = 0; 664 vbit = 0;
661 wpos = 0; 665 wpos = 0;
662 rpos = 0; 666 rpos = 0;
@@ -675,12 +679,18 @@ GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size
675 vbit = 5; 679 vbit = 5;
676 } 680 }
677 if (wpos >= out_size) 681 if (wpos >= out_size)
682 {
683 GNUNET_break (0);
678 return NULL; 684 return NULL;
685 }
679 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; 686 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
680 vbit -= 5; 687 vbit -= 5;
681 } 688 }
682 if (wpos != out_size) 689 if (wpos != out_size)
690 {
691 GNUNET_break (0);
683 return NULL; 692 return NULL;
693 }
684 GNUNET_assert (vbit == 0); 694 GNUNET_assert (vbit == 0);
685 return &out[wpos]; 695 return &out[wpos];
686} 696}
diff --git a/src/vpn/vpn.h b/src/vpn/vpn.h
index e937f5ef5..bec3a5b59 100644
--- a/src/vpn/vpn.h
+++ b/src/vpn/vpn.h
@@ -115,7 +115,7 @@ struct RedirectToServiceRequestMessage
115 /** 115 /**
116 * Service descriptor identifying the service. 116 * Service descriptor identifying the service.
117 */ 117 */
118 GNUNET_HashCode service_descriptor GNUNET_PACKED; 118 GNUNET_HashCode service_descriptor;
119 119
120 /** 120 /**
121 * Unique ID to match a future response to this request. 121 * Unique ID to match a future response to this request.