1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* The implementation is based on:
5  * Public Domain poly1305 from Andrew Moon
6  * https://github.com/floodyberry/poly1305-donna
7  */
8 
9 #include "tomcrypt_private.h"
10 #include <stdarg.h>
11 
12 #ifdef LTC_POLY1305
13 
14 /**
15    POLY1305 multiple blocks of memory to produce the authentication tag
16    @param key       The secret key
17    @param keylen    The length of the secret key (octets)
18    @param mac       [out] Destination of the authentication tag
19    @param maclen    [in/out] Max size and resulting size of authentication tag
20    @param in        The data to POLY1305
21    @param inlen     The length of the data to POLY1305 (octets)
22    @param ...       tuples of (data,len) pairs to POLY1305, terminated with a (NULL,x) (x=don't care)
23    @return CRYPT_OK if successful
24 */
poly1305_memory_multi(const unsigned char * key,unsigned long keylen,unsigned char * mac,unsigned long * maclen,const unsigned char * in,unsigned long inlen,...)25 int poly1305_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in,  unsigned long inlen, ...)
26 {
27    poly1305_state st;
28    int err;
29    va_list args;
30    const unsigned char *curptr;
31    unsigned long curlen;
32 
33    LTC_ARGCHK(key    != NULL);
34    LTC_ARGCHK(in     != NULL);
35    LTC_ARGCHK(mac    != NULL);
36    LTC_ARGCHK(maclen != NULL);
37 
38    va_start(args, inlen);
39    curptr = in;
40    curlen = inlen;
41    if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK)          { goto LBL_ERR; }
42    for (;;) {
43       if ((err = poly1305_process(&st, curptr, curlen)) != CRYPT_OK) { goto LBL_ERR; }
44       curptr = va_arg(args, const unsigned char*);
45       if (curptr == NULL) break;
46       curlen = va_arg(args, unsigned long);
47    }
48    err = poly1305_done(&st, mac, maclen);
49 LBL_ERR:
50 #ifdef LTC_CLEAN_STACK
51    zeromem(&st, sizeof(poly1305_state));
52 #endif
53    va_end(args);
54    return err;
55 }
56 
57 #endif
58