1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 #include <stdarg.h>
6 
7 #ifdef LTC_BLAKE2SMAC
8 
9 /**
10    BLAKE2S MAC multiple blocks of memory to produce the authentication tag
11    @param key       The secret key
12    @param keylen    The length of the secret key (octets)
13    @param mac       [out] Destination of the authentication tag
14    @param maclen    [in/out] Max size and resulting size of authentication tag
15    @param in        The data to BLAKE2S MAC
16    @param inlen     The length of the data to BLAKE2S MAC (octets)
17    @param ...       tuples of (data,len) pairs to BLAKE2S MAC, terminated with a (NULL,x) (x=don't care)
18    @return CRYPT_OK if successful
19 */
blake2smac_memory_multi(const unsigned char * key,unsigned long keylen,unsigned char * mac,unsigned long * maclen,const unsigned char * in,unsigned long inlen,...)20 int blake2smac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in,  unsigned long inlen, ...)
21 {
22    blake2smac_state st;
23    int err;
24    va_list args;
25    const unsigned char *curptr;
26    unsigned long curlen;
27 
28    LTC_ARGCHK(key    != NULL);
29    LTC_ARGCHK(in     != NULL);
30    LTC_ARGCHK(mac    != NULL);
31    LTC_ARGCHK(maclen != NULL);
32 
33    va_start(args, inlen);
34    curptr = in;
35    curlen = inlen;
36    if ((err = blake2smac_init(&st, *maclen, key, keylen)) != CRYPT_OK)          { goto LBL_ERR; }
37    for (;;) {
38       if ((err = blake2smac_process(&st, curptr, curlen)) != CRYPT_OK) { goto LBL_ERR; }
39       curptr = va_arg(args, const unsigned char*);
40       if (curptr == NULL) break;
41       curlen = va_arg(args, unsigned long);
42    }
43    err = blake2smac_done(&st, mac, maclen);
44 LBL_ERR:
45 #ifdef LTC_CLEAN_STACK
46    zeromem(&st, sizeof(blake2smac_state));
47 #endif
48    va_end(args);
49    return err;
50 }
51 
52 #endif
53