1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 #include <stdarg.h>
5 
6 /**
7   @file hmac_memory_multi.c
8   HMAC support, process multiple blocks of memory, Tom St Denis/Dobes Vandermeer
9 */
10 
11 #ifdef LTC_HMAC
12 
13 /**
14    HMAC multiple blocks of memory to produce the authentication tag
15    @param hash      The index of the hash to use
16    @param key       The secret key
17    @param keylen    The length of the secret key (octets)
18    @param out       [out] Destination of the authentication tag
19    @param outlen    [in/out] Max size and resulting size of authentication tag
20    @param in        The data to HMAC
21    @param inlen     The length of the data to HMAC (octets)
22    @param ...       tuples of (data,len) pairs to HMAC, terminated with a (NULL,x) (x=don't care)
23    @return CRYPT_OK if successful
24 */
hmac_memory_multi(int hash,const unsigned char * key,unsigned long keylen,unsigned char * out,unsigned long * outlen,const unsigned char * in,unsigned long inlen,...)25 int hmac_memory_multi(int hash,
26                 const unsigned char *key,  unsigned long keylen,
27                       unsigned char *out,  unsigned long *outlen,
28                 const unsigned char *in,   unsigned long inlen, ...)
29 
30 {
31     hmac_state          *hmac;
32     int                  err;
33     va_list              args;
34     const unsigned char *curptr;
35     unsigned long        curlen;
36 
37     LTC_ARGCHK(key    != NULL);
38     LTC_ARGCHK(in     != NULL);
39     LTC_ARGCHK(out    != NULL);
40     LTC_ARGCHK(outlen != NULL);
41 
42     /* allocate ram for hmac state */
43     hmac = XMALLOC(sizeof(hmac_state));
44     if (hmac == NULL) {
45        return CRYPT_MEM;
46     }
47 
48     if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
49        goto LBL_ERR;
50     }
51 
52     va_start(args, inlen);
53     curptr = in;
54     curlen = inlen;
55     for (;;) {
56        /* process buf */
57        if ((err = hmac_process(hmac, curptr, curlen)) != CRYPT_OK) {
58           goto LBL_ERR;
59        }
60        /* step to next */
61        curptr = va_arg(args, const unsigned char*);
62        if (curptr == NULL) {
63           break;
64        }
65        curlen = va_arg(args, unsigned long);
66     }
67     if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {
68        goto LBL_ERR;
69     }
70 LBL_ERR:
71 #ifdef LTC_CLEAN_STACK
72    zeromem(hmac, sizeof(hmac_state));
73 #endif
74    XFREE(hmac);
75    va_end(args);
76    return err;
77 }
78 
79 #endif
80 
81