1/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10 */
11#include "tomcrypt.h"
12#include <stdarg.h>
13
14/**
15  @file hmac_memory_multi.c
16  HMAC support, process multiple blocks of memory, Tom St Denis/Dobes Vandermeer
17*/
18
19#ifdef LTC_HMAC
20
21/**
22   HMAC multiple blocks of memory to produce the authentication tag
23   @param hash      The index of the hash to use
24   @param key       The secret key
25   @param keylen    The length of the secret key (octets)
26   @param out       [out] Destination of the authentication tag
27   @param outlen    [in/out] Max size and resulting size of authentication tag
28   @param in        The data to HMAC
29   @param inlen     The length of the data to HMAC (octets)
30   @param ...       tuples of (data,len) pairs to HMAC, terminated with a (NULL,x) (x=don't care)
31   @return CRYPT_OK if successful
32*/
33int hmac_memory_multi(int hash,
34                const unsigned char *key,  unsigned long keylen,
35                      unsigned char *out,  unsigned long *outlen,
36                const unsigned char *in,   unsigned long inlen, ...)
37
38{
39    hmac_state          *hmac;
40    int                  err;
41    va_list              args;
42    const unsigned char *curptr;
43    unsigned long        curlen;
44
45    LTC_ARGCHK(key    != NULL);
46    LTC_ARGCHK(in     != NULL);
47    LTC_ARGCHK(out    != NULL);
48    LTC_ARGCHK(outlen != NULL);
49
50    /* allocate ram for hmac state */
51    hmac = XMALLOC(sizeof(hmac_state));
52    if (hmac == NULL) {
53       return CRYPT_MEM;
54    }
55
56    if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
57       goto LBL_ERR;
58    }
59
60    va_start(args, inlen);
61    curptr = in;
62    curlen = inlen;
63    for (;;) {
64       /* process buf */
65       if ((err = hmac_process(hmac, curptr, curlen)) != CRYPT_OK) {
66          goto LBL_ERR;
67       }
68       /* step to next */
69       curptr = va_arg(args, const unsigned char*);
70       if (curptr == NULL) {
71          break;
72       }
73       curlen = va_arg(args, unsigned long);
74    }
75    if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {
76       goto LBL_ERR;
77    }
78LBL_ERR:
79#ifdef LTC_CLEAN_STACK
80   zeromem(hmac, sizeof(hmac_state));
81#endif
82   XFREE(hmac);
83   va_end(args);
84   return err;
85}
86
87#endif
88
89
90/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_memory_multi.c,v $ */
91/* $Revision: 1.5 $ */
92/* $Date: 2006/11/03 00:39:49 $ */
93