572586b34086d1d521dcb8e11b2765f0f59e01de
[oweals/gnunet.git] / src / util / crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19
20      SHA-512 code by Jean-Luc Cooke <jlcooke@certainkey.com>
21
22      Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
23      Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
24      Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
25 */
26
27 /**
28  * @file util/crypto_hash.c
29  * @brief SHA-512 GNUNET_CRYPTO_hash related functions
30  * @author Christian Grothoff
31  */
32
33 #include "platform.h"
34 #include "gnunet_common.h"
35 #include "gnunet_crypto_lib.h"
36 #include "gnunet_disk_lib.h"
37 #include <gcrypt.h>
38
39 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
41 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42
43 /**
44  * Hash block of given size.
45  *
46  * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
47  * @param size the length of the data to GNUNET_CRYPTO_hash
48  * @param ret pointer to where to write the hashcode
49  */
50 void
51 GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret)
52 {
53   gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
54 }
55
56
57 /**
58  * Context used when hashing a file.
59  */
60 struct GNUNET_CRYPTO_FileHashContext
61 {
62
63   /**
64    * Function to call upon completion.
65    */
66   GNUNET_CRYPTO_HashCompletedCallback callback;
67
68   /**
69    * Closure for callback.
70    */
71   void *callback_cls;
72
73   /**
74    * IO buffer.
75    */
76   unsigned char *buffer;
77
78   /**
79    * Name of the file we are hashing.
80    */
81   char *filename;
82
83   /**
84    * File descriptor.
85    */
86   struct GNUNET_DISK_FileHandle *fh;
87
88   /**
89    * Cummulated hash.
90    */
91   gcry_md_hd_t md;
92
93   /**
94    * Size of the file.
95    */
96   uint64_t fsize;
97
98   /**
99    * Current offset.
100    */
101   uint64_t offset;
102
103   /**
104    * Current task for hashing.
105    */
106   GNUNET_SCHEDULER_TaskIdentifier task;
107
108   /**
109    * Priority we use.
110    */
111   enum GNUNET_SCHEDULER_Priority priority;
112
113   /**
114    * Blocksize.
115    */
116   size_t bsize;
117
118 };
119
120
121 /**
122  * Report result of hash computation to callback
123  * and free associated resources.
124  */
125 static void
126 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
127                   const GNUNET_HashCode * res)
128 {
129   fhc->callback (fhc->callback_cls, res);
130   GNUNET_free (fhc->filename);
131   if (!GNUNET_DISK_handle_invalid (fhc->fh))
132     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
133   gcry_md_close (fhc->md);
134   GNUNET_free (fhc);            /* also frees fhc->buffer */
135 }
136
137
138 /**
139  * File hashing task.
140  *
141  * @param cls closure
142  * @param tc context
143  */
144 static void
145 file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
147   struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
148   GNUNET_HashCode *res;
149   size_t delta;
150
151   fhc->task = GNUNET_SCHEDULER_NO_TASK;
152   GNUNET_assert (fhc->offset <= fhc->fsize);
153   delta = fhc->bsize;
154   if (fhc->fsize - fhc->offset < delta)
155     delta = fhc->fsize - fhc->offset;
156   if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
157   {
158     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
159     file_hash_finish (fhc, NULL);
160     return;
161   }
162   gcry_md_write (fhc->md, fhc->buffer, delta);
163   fhc->offset += delta;
164   if (fhc->offset == fhc->fsize)
165   {
166     res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
167     file_hash_finish (fhc, res);
168     return;
169   }
170   fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
171                                                   &file_hash_task, fhc);
172 }
173
174
175 /**
176  * Compute the hash of an entire file.
177  *
178  * @param priority scheduling priority to use
179  * @param filename name of file to hash
180  * @param blocksize number of bytes to process in one task
181  * @param callback function to call upon completion
182  * @param callback_cls closure for callback
183  * @return NULL on (immediate) errror
184  */
185 struct GNUNET_CRYPTO_FileHashContext *
186 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
187                          const char *filename, size_t blocksize,
188                          GNUNET_CRYPTO_HashCompletedCallback callback,
189                          void *callback_cls)
190 {
191   struct GNUNET_CRYPTO_FileHashContext *fhc;
192
193   GNUNET_assert (blocksize > 0);
194   fhc =
195       GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
196   fhc->callback = callback;
197   fhc->callback_cls = callback_cls;
198   fhc->buffer = (unsigned char *) &fhc[1];
199   fhc->filename = GNUNET_strdup (filename);
200   if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
201   {
202     GNUNET_break (0);
203     GNUNET_free (fhc);
204     return NULL;
205   }
206   fhc->bsize = blocksize;
207   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO))
208   {
209     GNUNET_free (fhc->filename);
210     GNUNET_free (fhc);
211     return NULL;
212   }
213   fhc->fh =
214       GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
215                              GNUNET_DISK_PERM_NONE);
216   if (!fhc->fh)
217   {
218     GNUNET_free (fhc->filename);
219     GNUNET_free (fhc);
220     return NULL;
221   }
222   fhc->priority = priority;
223   fhc->task =
224       GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
225   return fhc;
226 }
227
228
229 /**
230  * Cancel a file hashing operation.
231  *
232  * @param fhc operation to cancel (callback must not yet have been invoked)
233  */
234 void
235 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
236 {
237   GNUNET_SCHEDULER_cancel (fhc->task);
238   GNUNET_free (fhc->filename);
239   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
240   GNUNET_free (fhc);
241 }
242
243
244 /* ***************** binary-ASCII encoding *************** */
245
246 /**
247  * Get the numeric value corresponding to a character.
248  *
249  * @param a a character
250  * @return corresponding numeric value
251  */
252 static unsigned int
253 getValue__ (unsigned char a)
254 {
255   if ((a >= '0') && (a <= '9'))
256     return a - '0';
257   if ((a >= 'A') && (a <= 'V'))
258     return (a - 'A' + 10);
259   return -1;
260 }
261
262
263 /**
264  * Convert binary data to ASCII encoding.  The ASCII encoding is rather
265  * GNUnet specific.  It was chosen such that it only uses characters
266  * in [0-9A-V], can be produced without complex arithmetics and uses a
267  * small number of characters.  
268  * Does not append 0-terminator, but returns a pointer to the place where
269  * it should be placed, if needed.
270  *
271  * @param data data to encode
272  * @param size size of data (in bytes)
273  * @param out buffer to fill
274  * @param out_size size of the buffer. Must be large enough to hold
275  * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
276  * @return pointer to the next byte in 'out' or NULL on error.
277  */
278 char *
279 GNUNET_CRYPTO_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size)
280 {
281   /**
282    * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
283    */
284   static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
285   unsigned int wpos;
286   unsigned int rpos;
287   unsigned int bits;
288   unsigned int vbit;
289
290   GNUNET_assert (data != NULL);
291   GNUNET_assert (out != NULL);
292   GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5));
293   vbit = 0;
294   wpos = 0;
295   rpos = 0;
296   bits = 0;
297   while ((rpos < size) || (vbit > 0))
298   {
299     if ((rpos < size) && (vbit < 5))
300     {
301       bits = (bits << 8) | data[rpos++];   /* eat 8 more bits */
302       vbit += 8;
303     }
304     if (vbit < 5)
305     {
306       bits <<= (5 - vbit);      /* zero-padding */
307       GNUNET_assert (vbit == ((size * 8) % 5));
308       vbit = 5;
309     }
310     if (wpos >= out_size)
311       return NULL;
312     out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
313     vbit -= 5;
314   }
315   if (wpos != out_size)
316     return NULL;
317   GNUNET_assert (vbit == 0);
318   return &out[wpos];
319 }
320
321
322 /**
323  * Convert ASCII encoding back to data
324  * out_size must match exactly the size of the data before it was encoded.
325  *
326  * @param enc the encoding
327  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
328  * @param out location where to store the decoded data
329  * @param out_size sizeof the output buffer
330  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
331  */
332 int
333 GNUNET_CRYPTO_string_to_data (const char *enc, size_t enclen,
334                               unsigned char *out, size_t out_size)
335 {
336   unsigned int rpos;
337   unsigned int wpos;
338   unsigned int bits;
339   unsigned int vbit;
340   int ret;
341   int shift;
342   int encoded_len = out_size * 8;
343   if (encoded_len % 5 > 0)
344   {
345     vbit = encoded_len % 5; /* padding! */
346     shift = 5 - vbit;
347   }
348   else
349   {
350     vbit = 0;
351     shift = 0;
352   }
353   if ((encoded_len + shift) / 5 != enclen)
354     return GNUNET_SYSERR;
355
356   wpos = out_size;
357   rpos = enclen;
358   bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
359   if (-1 == ret)
360     return GNUNET_SYSERR;
361   while (wpos > 0)
362   {
363     GNUNET_assert (rpos > 0);
364     bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
365     if (-1 == ret)
366       return GNUNET_SYSERR;
367     vbit += 5;
368     if (vbit >= 8)
369     {
370       out[--wpos] = (unsigned char) bits;
371       bits >>= 8;
372       vbit -= 8;
373     }
374   }
375   GNUNET_assert (rpos == 0);
376   GNUNET_assert (vbit == 0);
377   return GNUNET_OK;
378 }
379
380
381 /**
382  * Convert GNUNET_CRYPTO_hash to ASCII encoding.  The ASCII encoding is rather
383  * GNUnet specific.  It was chosen such that it only uses characters
384  * in [0-9A-V], can be produced without complex arithmetics and uses a
385  * small number of characters.  The GNUnet encoding uses 103
386  * characters plus a null terminator.
387  *
388  * @param block the hash code
389  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
390  *  safely cast to char*, a '\\0' termination is set).
391  */
392 void
393 GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
394                            struct GNUNET_CRYPTO_HashAsciiEncoded *result)
395 {
396   /**
397    * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
398    */
399   static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
400   unsigned int wpos;
401   unsigned int rpos;
402   unsigned int bits;
403   unsigned int vbit;
404
405   GNUNET_assert (block != NULL);
406   GNUNET_assert (result != NULL);
407   vbit = 0;
408   wpos = 0;
409   rpos = 0;
410   bits = 0;
411   while ((rpos < sizeof (GNUNET_HashCode)) || (vbit > 0))
412   {
413     if ((rpos < sizeof (GNUNET_HashCode)) && (vbit < 5))
414     {
415       bits = (bits << 8) | ((unsigned char *) block)[rpos++];   /* eat 8 more bits */
416       vbit += 8;
417     }
418     if (vbit < 5)
419     {
420       bits <<= (5 - vbit);      /* zero-padding */
421       GNUNET_assert (vbit == 2);        /* padding by 3: 512+3 mod 5 == 0 */
422       vbit = 5;
423     }
424     GNUNET_assert (wpos < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
425     result->encoding[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
426     vbit -= 5;
427   }
428   GNUNET_assert (wpos == sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
429   GNUNET_assert (vbit == 0);
430   result->encoding[wpos] = '\0';
431 }
432
433
434 /**
435  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
436  *
437  * @param enc the encoding
438  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
439  * @param result where to store the GNUNET_CRYPTO_hash code
440  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
441  */
442 int
443 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
444                                 GNUNET_HashCode * result)
445 {
446   unsigned int rpos;
447   unsigned int wpos;
448   unsigned int bits;
449   unsigned int vbit;
450   int ret;
451
452   if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
453     return GNUNET_SYSERR;
454
455   vbit = 2;                     /* padding! */
456   wpos = sizeof (GNUNET_HashCode);
457   rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
458   bits = (ret = getValue__ (enc[--rpos])) >> 3;
459   if (-1 == ret)
460     return GNUNET_SYSERR;
461   while (wpos > 0)
462   {
463     GNUNET_assert (rpos > 0);
464     bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
465     if (-1 == ret)
466       return GNUNET_SYSERR;
467     vbit += 5;
468     if (vbit >= 8)
469     {
470       ((unsigned char *) result)[--wpos] = (unsigned char) bits;
471       bits >>= 8;
472       vbit -= 8;
473     }
474   }
475   GNUNET_assert (rpos == 0);
476   GNUNET_assert (vbit == 0);
477   return GNUNET_OK;
478 }
479
480
481 /**
482  * Compute the distance between 2 hashcodes.  The computation must be
483  * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
484  * somewhat consistent. And of course, the result should be a positive
485  * number.
486  *
487  * @param a some hash code
488  * @param b some hash code
489  * @return a positive number which is a measure for
490  *  hashcode proximity.
491  */
492 unsigned int
493 GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
494                                  const GNUNET_HashCode * b)
495 {
496   unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
497   unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
498
499   return (x1 * x2);
500 }
501
502
503 /**
504  * Create a random hash code.
505  *
506  * @param mode desired quality level
507  * @param result hash code that is randomized
508  */
509 void
510 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
511                                   GNUNET_HashCode * result)
512 {
513   int i;
514
515   for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--)
516     result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
517 }
518
519
520 /**
521  * compute result(delta) = b - a
522  *
523  * @param a some hash code
524  * @param b some hash code
525  * @param result set to b - a
526  */
527 void
528 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
529                                const GNUNET_HashCode * b,
530                                GNUNET_HashCode * result)
531 {
532   int i;
533
534   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
535     result->bits[i] = b->bits[i] - a->bits[i];
536 }
537
538
539 /**
540  * compute result(b) = a + delta
541  *
542  * @param a some hash code
543  * @param delta some hash code
544  * @param result set to a + delta
545  */
546 void
547 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
548                         const GNUNET_HashCode * delta, GNUNET_HashCode * result)
549 {
550   int i;
551
552   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
553     result->bits[i] = delta->bits[i] + a->bits[i];
554 }
555
556
557 /**
558  * compute result = a ^ b
559  *
560  * @param a some hash code
561  * @param b some hash code
562  * @param result set to a ^ b
563  */
564 void
565 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
566                         GNUNET_HashCode * result)
567 {
568   int i;
569
570   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
571     result->bits[i] = a->bits[i] ^ b->bits[i];
572 }
573
574
575 /**
576  * Convert a hashcode into a key.
577  *
578  * @param hc hash code that serves to generate the key
579  * @param skey set to a valid session key
580  * @param iv set to a valid initialization vector
581  */
582 void
583 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
584                                struct GNUNET_CRYPTO_AesSessionKey *skey,
585                                struct GNUNET_CRYPTO_AesInitializationVector *iv)
586 {
587   GNUNET_assert (sizeof (GNUNET_HashCode) >=
588                  GNUNET_CRYPTO_AES_KEY_LENGTH +
589                  sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
590   memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH);
591   skey->crc32 =
592       htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH));
593   memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH],
594           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
595 }
596
597
598 /**
599  * Obtain a bit from a hashcode.
600  * @param code the GNUNET_CRYPTO_hash to index bit-wise
601  * @param bit index into the hashcode, [0...511]
602  * @return Bit \a bit from hashcode \a code, -1 for invalid index
603  */
604 int
605 GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
606 {
607   GNUNET_assert (bit < 8 * sizeof (GNUNET_HashCode));
608   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
609 }
610
611
612 /**
613  * Determine how many low order bits match in two
614  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
615  * the first two lowest order bits, and therefore the
616  * return value is two (NOT XOR distance, nor how many
617  * bits match absolutely!).
618  *
619  * @param first the first hashcode
620  * @param second the hashcode to compare first to
621  *
622  * @return the number of bits that match
623  */
624 unsigned int
625 GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
626                                   const GNUNET_HashCode * second)
627 {
628   unsigned int i;
629
630   for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
631     if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
632         GNUNET_CRYPTO_hash_get_bit (second, i))
633       return i;
634   return sizeof (GNUNET_HashCode) * 8;
635 }
636
637
638 /**
639  * Compare function for HashCodes, producing a total ordering
640  * of all hashcodes.
641  *
642  * @param h1 some hash code
643  * @param h2 some hash code
644  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
645  */
646 int
647 GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
648 {
649   unsigned int *i1;
650   unsigned int *i2;
651   int i;
652
653   i1 = (unsigned int *) h1;
654   i2 = (unsigned int *) h2;
655   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
656   {
657     if (i1[i] > i2[i])
658       return 1;
659     if (i1[i] < i2[i])
660       return -1;
661   }
662   return 0;
663 }
664
665
666 /**
667  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
668  * in the XOR metric (Kademlia).
669  *
670  * @param h1 some hash code
671  * @param h2 some hash code
672  * @param target some hash code
673  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
674  */
675 int
676 GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
677                            const GNUNET_HashCode * h2,
678                            const GNUNET_HashCode * target)
679 {
680   int i;
681   unsigned int d1;
682   unsigned int d2;
683
684   for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
685   {
686     d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
687     d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
688     if (d1 > d2)
689       return 1;
690     else if (d1 < d2)
691       return -1;
692   }
693   return 0;
694 }
695
696
697 /**
698  * @brief Derive an authentication key
699  * @param key authentication key
700  * @param rkey root key
701  * @param salt salt
702  * @param salt_len size of the salt
703  * @param ... pair of void * & size_t for context chunks, terminated by NULL
704  */
705 void
706 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
707                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
708                                const void *salt, size_t salt_len, ...)
709 {
710   va_list argp;
711
712   va_start (argp, salt_len);
713   GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
714   va_end (argp);
715 }
716
717
718 /**
719  * @brief Derive an authentication key
720  * @param key authentication key
721  * @param rkey root key
722  * @param salt salt
723  * @param salt_len size of the salt
724  * @param argp pair of void * & size_t for context chunks, terminated by NULL
725  */
726 void
727 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
728                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
729                                  const void *salt, size_t salt_len,
730                                  va_list argp)
731 {
732   GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
733                        sizeof (rkey->key), argp);
734 }
735
736
737 /**
738  * Calculate HMAC of a message (RFC 2104)
739  *
740  * @param key secret key
741  * @param plaintext input plaintext
742  * @param plaintext_len length of plaintext
743  * @param hmac where to store the hmac
744  */
745 void
746 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
747                     const void *plaintext, size_t plaintext_len,
748                     GNUNET_HashCode * hmac)
749 {
750   gcry_md_hd_t md;
751   const unsigned char *mc;
752
753   GNUNET_assert (GPG_ERR_NO_ERROR ==
754                  gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
755   gcry_md_setkey (md, key->key, sizeof (key->key));
756   gcry_md_write (md, plaintext, plaintext_len);
757   mc = gcry_md_read (md, GCRY_MD_SHA512);
758   if (mc != NULL)
759     memcpy (hmac->bits, mc, sizeof (hmac->bits));
760   gcry_md_close (md);
761 }
762
763
764 /* end of crypto_hash.c */