1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_BLAKE2BMAC
7 
8 /**
9    BLAKE2B 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 BLAKE2B MAC
13    @param inlen     The length of the data to BLAKE2B 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 */
blake2bmac_memory(const unsigned char * key,unsigned long keylen,const unsigned char * in,unsigned long inlen,unsigned char * mac,unsigned long * maclen)18 int blake2bmac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen)
19 {
20    blake2bmac_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 = blake2bmac_init(&st, *maclen, key, keylen))  != CRYPT_OK) { goto LBL_ERR; }
29    if ((err = blake2bmac_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
30    err = blake2bmac_done(&st, mac, maclen);
31 LBL_ERR:
32 #ifdef LTC_CLEAN_STACK
33    zeromem(&st, sizeof(blake2bmac_state));
34 #endif
35    return err;
36 }
37 
38 #endif
39