1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6    @file pmac_memory.c
7    PMAC implementation, process a block of memory, by Tom St Denis
8 */
9 
10 #ifdef LTC_PMAC
11 
12 /**
13    PMAC a block of memory
14    @param cipher   The index of the cipher desired
15    @param key      The secret key
16    @param keylen   The length of the secret key (octets)
17    @param in       The data you wish to send through PMAC
18    @param inlen    The length of data you wish to send through PMAC (octets)
19    @param out      [out] Destination for the authentication tag
20    @param outlen   [in/out] The max size and resulting size of the authentication tag
21    @return CRYPT_OK if successful
22 */
pmac_memory(int cipher,const unsigned char * key,unsigned long keylen,const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)23 int pmac_memory(int cipher,
24                 const unsigned char *key, unsigned long keylen,
25                 const unsigned char *in, unsigned long inlen,
26                       unsigned char *out, unsigned long *outlen)
27 {
28    int err;
29    pmac_state *pmac;
30 
31    LTC_ARGCHK(key    != NULL);
32    LTC_ARGCHK(in    != NULL);
33    LTC_ARGCHK(out    != NULL);
34    LTC_ARGCHK(outlen != NULL);
35 
36    /* allocate ram for pmac state */
37    pmac = XMALLOC(sizeof(pmac_state));
38    if (pmac == NULL) {
39       return CRYPT_MEM;
40    }
41 
42    if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) {
43       goto LBL_ERR;
44    }
45    if ((err = pmac_process(pmac, in, inlen)) != CRYPT_OK) {
46       goto LBL_ERR;
47    }
48    if ((err = pmac_done(pmac, out, outlen)) != CRYPT_OK) {
49       goto LBL_ERR;
50    }
51 
52    err = CRYPT_OK;
53 LBL_ERR:
54 #ifdef LTC_CLEAN_STACK
55    zeromem(pmac, sizeof(pmac_state));
56 #endif
57 
58    XFREE(pmac);
59    return err;
60 }
61 
62 #endif
63