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