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    Initialize an BLAKE2S MAC context.
10    @param st       The BLAKE2S MAC state
11    @param outlen   The size of the MAC output (octets)
12    @param key      The secret key
13    @param keylen   The length of the secret key (octets)
14    @return CRYPT_OK if successful
15 */
blake2smac_init(blake2smac_state * st,unsigned long outlen,const unsigned char * key,unsigned long keylen)16 int blake2smac_init(blake2smac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen)
17 {
18    LTC_ARGCHK(st  != NULL);
19    LTC_ARGCHK(key != NULL);
20    return blake2s_init(st, outlen, key, keylen);
21 }
22 
23 /**
24   Process data through BLAKE2S MAC
25   @param st      The BLAKE2S MAC state
26   @param in      The data to send through HMAC
27   @param inlen   The length of the data to HMAC (octets)
28   @return CRYPT_OK if successful
29 */
blake2smac_process(blake2smac_state * st,const unsigned char * in,unsigned long inlen)30 int blake2smac_process(blake2smac_state *st, const unsigned char *in, unsigned long inlen)
31 {
32    if (inlen == 0) return CRYPT_OK; /* nothing to do */
33    LTC_ARGCHK(st != NULL);
34    LTC_ARGCHK(in != NULL);
35    return blake2s_process(st, in, inlen);
36 }
37 
38 /**
39    Terminate a BLAKE2S MAC session
40    @param st      The BLAKE2S MAC state
41    @param mac     [out] The destination of the BLAKE2S MAC authentication tag
42    @param maclen  [in/out]  The max size and resulting size of the BLAKE2S MAC authentication tag
43    @return CRYPT_OK if successful
44 */
blake2smac_done(blake2smac_state * st,unsigned char * mac,unsigned long * maclen)45 int blake2smac_done(blake2smac_state *st, unsigned char *mac, unsigned long *maclen)
46 {
47    LTC_ARGCHK(st     != NULL);
48    LTC_ARGCHK(mac    != NULL);
49    LTC_ARGCHK(maclen != NULL);
50    LTC_ARGCHK(*maclen >= st->blake2s.outlen);
51 
52    *maclen = st->blake2s.outlen;
53    return blake2s_done(st, mac);
54 }
55 
56 #endif
57