1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 #include <stdarg.h>
12 
13 /**
14   @file omac_memory_multi.c
15   OMAC1 support, process multiple blocks of memory, Tom St Denis
16 */
17 
18 #ifdef LTC_OMAC
19 
20 /**
21    OMAC multiple blocks of memory
22    @param cipher    The index of the desired cipher
23    @param key       The secret key
24    @param keylen    The length of the secret key (octets)
25    @param out       [out] The destination of the authentication tag
26    @param outlen    [in/out]  The max size and resulting size of the authentication tag (octets)
27    @param in        The data to send through OMAC
28    @param inlen     The length of the data to send through OMAC (octets)
29    @param ...       tuples of (data,len) pairs to OMAC, terminated with a (NULL,x) (x=don't care)
30    @return CRYPT_OK if successful
31 */
omac_memory_multi(int cipher,const unsigned char * key,unsigned long keylen,unsigned char * out,unsigned long * outlen,const unsigned char * in,unsigned long inlen,...)32 int omac_memory_multi(int cipher,
33                 const unsigned char *key, unsigned long keylen,
34                       unsigned char *out, unsigned long *outlen,
35                 const unsigned char *in,  unsigned long inlen, ...)
36 {
37    int                  err;
38    omac_state          *omac;
39    va_list              args;
40    const unsigned char *curptr;
41    unsigned long        curlen;
42 
43    LTC_ARGCHK(key    != NULL);
44    LTC_ARGCHK(in     != NULL);
45    LTC_ARGCHK(out    != NULL);
46    LTC_ARGCHK(outlen != NULL);
47 
48    /* allocate ram for omac state */
49    omac = XMALLOC(sizeof(omac_state));
50    if (omac == NULL) {
51       return CRYPT_MEM;
52    }
53 
54    /* omac process the message */
55    if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
56       goto LBL_ERR;
57    }
58    va_start(args, inlen);
59    curptr = in;
60    curlen = inlen;
61    for (;;) {
62       /* process buf */
63       if ((err = omac_process(omac, curptr, curlen)) != CRYPT_OK) {
64          goto LBL_ERR;
65       }
66       /* step to next */
67       curptr = va_arg(args, const unsigned char*);
68       if (curptr == NULL) {
69          break;
70       }
71       curlen = va_arg(args, unsigned long);
72    }
73    if ((err = omac_done(omac, out, outlen)) != CRYPT_OK) {
74       goto LBL_ERR;
75    }
76 LBL_ERR:
77 #ifdef LTC_CLEAN_STACK
78    zeromem(omac, sizeof(omac_state));
79 #endif
80    XFREE(omac);
81    va_end(args);
82    return err;
83 }
84 
85 #endif
86 
87 /* ref:         $Format:%D$ */
88 /* git commit:  $Format:%H$ */
89 /* commit time: $Format:%ai$ */
90