]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/hmac/hmac_memory_multi.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / mac / hmac / hmac_memory_multi.c
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 #include "tomcrypt.h"
10 #include <stdarg.h>
11
12 /**
13   @file hmac_memory_multi.c
14   HMAC support, process multiple blocks of memory, Tom St Denis/Dobes Vandermeer
15 */
16
17 #ifdef LTC_HMAC
18
19 /**
20    HMAC multiple blocks of memory to produce the authentication tag
21    @param hash      The index of the hash to use
22    @param key       The secret key
23    @param keylen    The length of the secret key (octets)
24    @param out       [out] Destination of the authentication tag
25    @param outlen    [in/out] Max size and resulting size of authentication tag
26    @param in        The data to HMAC
27    @param inlen     The length of the data to HMAC (octets)
28    @param ...       tuples of (data,len) pairs to HMAC, terminated with a (NULL,x) (x=don't care)
29    @return CRYPT_OK if successful
30 */
31 int hmac_memory_multi(int hash,
32                 const unsigned char *key,  unsigned long keylen,
33                       unsigned char *out,  unsigned long *outlen,
34                 const unsigned char *in,   unsigned long inlen, ...)
35
36 {
37     hmac_state          *hmac;
38     int                  err;
39     va_list              args;
40     const unsigned char *curptr;
41     unsigned long        curlen;
42
43     LTC_ARGCHK(key    != NULL);
44     LTC_ARGCHK(in     != NULL);
45     LTC_ARGCHK(out    != NULL);
46     LTC_ARGCHK(outlen != NULL);
47
48     /* allocate ram for hmac state */
49     hmac = XMALLOC(sizeof(hmac_state));
50     if (hmac == NULL) {
51        return CRYPT_MEM;
52     }
53
54     if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
55        goto LBL_ERR;
56     }
57
58     va_start(args, inlen);
59     curptr = in;
60     curlen = inlen;
61     for (;;) {
62        /* process buf */
63        if ((err = hmac_process(hmac, curptr, curlen)) != CRYPT_OK) {
64           goto LBL_ERR;
65        }
66        /* step to next */
67        curptr = va_arg(args, const unsigned char*);
68        if (curptr == NULL) {
69           break;
70        }
71        curlen = va_arg(args, unsigned long);
72     }
73     if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {
74        goto LBL_ERR;
75     }
76 LBL_ERR:
77 #ifdef LTC_CLEAN_STACK
78    zeromem(hmac, sizeof(hmac_state));
79 #endif
80    XFREE(hmac);
81    va_end(args);
82    return err;
83 }
84
85 #endif
86
87
88 /* ref:         $Format:%D$ */
89 /* git commit:  $Format:%H$ */
90 /* commit time: $Format:%ai$ */