1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 #include <stdarg.h>
5 
6 /**
7    @file pmac_memory_multi.c
8    PMAC implementation, process multiple blocks of memory, by Tom St Denis
9 */
10 
11 #ifdef LTC_PMAC
12 
13 /**
14    PMAC multiple blocks of memory
15    @param cipher   The index of the cipher desired
16    @param key      The secret key
17    @param keylen   The length of the secret key (octets)
18    @param out      [out] Destination for the authentication tag
19    @param outlen   [in/out] The max size and resulting size of the authentication tag
20    @param in       The data you wish to send through PMAC
21    @param inlen    The length of data you wish to send through PMAC (octets)
22    @param ...      tuples of (data,len) pairs to PMAC, terminated with a (NULL,x) (x=don't care)
23    @return CRYPT_OK if successful
24 */
pmac_memory_multi(int cipher,const unsigned char * key,unsigned long keylen,unsigned char * out,unsigned long * outlen,const unsigned char * in,unsigned long inlen,...)25 int pmac_memory_multi(int cipher,
26                 const unsigned char *key, unsigned long  keylen,
27                       unsigned char *out, unsigned long *outlen,
28                 const unsigned char *in,  unsigned long  inlen, ...)
29 {
30    int                  err;
31    pmac_state          *pmac;
32    va_list              args;
33    const unsigned char *curptr;
34    unsigned long        curlen;
35 
36    LTC_ARGCHK(key    != NULL);
37    LTC_ARGCHK(in     != NULL);
38    LTC_ARGCHK(out    != NULL);
39    LTC_ARGCHK(outlen != NULL);
40 
41    /* allocate ram for pmac state */
42    pmac = XMALLOC(sizeof(pmac_state));
43    if (pmac == NULL) {
44       return CRYPT_MEM;
45    }
46 
47    if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) {
48       goto LBL_ERR;
49    }
50    va_start(args, inlen);
51    curptr = in;
52    curlen = inlen;
53    for (;;) {
54       /* process buf */
55       if ((err = pmac_process(pmac, curptr, curlen)) != CRYPT_OK) {
56          goto LBL_ERR;
57       }
58       /* step to next */
59       curptr = va_arg(args, const unsigned char*);
60       if (curptr == NULL) {
61          break;
62       }
63       curlen = va_arg(args, unsigned long);
64    }
65    if ((err = pmac_done(pmac, out, outlen)) != CRYPT_OK) {
66       goto LBL_ERR;
67    }
68 LBL_ERR:
69 #ifdef LTC_CLEAN_STACK
70    zeromem(pmac, sizeof(pmac_state));
71 #endif
72    XFREE(pmac);
73    va_end(args);
74    return err;
75 }
76 
77 #endif
78