1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_BLAKE2SMAC
7 
8 /**
9    BLAKE2S MAC a block of memory to produce the authentication tag
10    @param key       The secret key
11    @param keylen    The length of the secret key (octets)
12    @param in        The data to BLAKE2S MAC
13    @param inlen     The length of the data to BLAKE2S MAC (octets)
14    @param mac       [out] Destination of the authentication tag
15    @param maclen    [in/out] Max size and resulting size of authentication tag
16    @return CRYPT_OK if successful
17 */
blake2smac_memory(const unsigned char * key,unsigned long keylen,const unsigned char * in,unsigned long inlen,unsigned char * mac,unsigned long * maclen)18 int blake2smac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen)
19 {
20    blake2smac_state st;
21    int err;
22 
23    LTC_ARGCHK(key    != NULL);
24    LTC_ARGCHK(in     != NULL);
25    LTC_ARGCHK(mac    != NULL);
26    LTC_ARGCHK(maclen != NULL);
27 
28    if ((err = blake2smac_init(&st, *maclen, key, keylen))  != CRYPT_OK) { goto LBL_ERR; }
29    if ((err = blake2smac_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
30    err = blake2smac_done(&st, mac, maclen);
31 LBL_ERR:
32 #ifdef LTC_CLEAN_STACK
33    zeromem(&st, sizeof(blake2smac_state));
34 #endif
35    return err;
36 }
37 
38 #endif
39