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 
11 /* The implementation is based on:
12  * Public Domain poly1305 from Andrew Moon
13  * https://github.com/floodyberry/poly1305-donna
14  */
15 
16 #include "tomcrypt_private.h"
17 #include <stdarg.h>
18 
19 #ifdef LTC_POLY1305
20 
21 /**
22    POLY1305 multiple blocks of memory to produce the authentication tag
23    @param key       The secret key
24    @param keylen    The length of the secret key (octets)
25    @param mac       [out] Destination of the authentication tag
26    @param maclen    [in/out] Max size and resulting size of authentication tag
27    @param in        The data to POLY1305
28    @param inlen     The length of the data to POLY1305 (octets)
29    @param ...       tuples of (data,len) pairs to POLY1305, terminated with a (NULL,x) (x=don't care)
30    @return CRYPT_OK if successful
31 */
poly1305_memory_multi(const unsigned char * key,unsigned long keylen,unsigned char * mac,unsigned long * maclen,const unsigned char * in,unsigned long inlen,...)32 int poly1305_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in,  unsigned long inlen, ...)
33 {
34    poly1305_state st;
35    int err;
36    va_list args;
37    const unsigned char *curptr;
38    unsigned long curlen;
39 
40    LTC_ARGCHK(key    != NULL);
41    LTC_ARGCHK(in     != NULL);
42    LTC_ARGCHK(mac    != NULL);
43    LTC_ARGCHK(maclen != NULL);
44 
45    va_start(args, inlen);
46    curptr = in;
47    curlen = inlen;
48    if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK)          { goto LBL_ERR; }
49    for (;;) {
50       if ((err = poly1305_process(&st, curptr, curlen)) != CRYPT_OK) { goto LBL_ERR; }
51       curptr = va_arg(args, const unsigned char*);
52       if (curptr == NULL) break;
53       curlen = va_arg(args, unsigned long);
54    }
55    err = poly1305_done(&st, mac, maclen);
56 LBL_ERR:
57 #ifdef LTC_CLEAN_STACK
58    zeromem(&st, sizeof(poly1305_state));
59 #endif
60    va_end(args);
61    return err;
62 }
63 
64 #endif
65 
66 /* ref:         $Format:%D$ */
67 /* git commit:  $Format:%H$ */
68 /* commit time: $Format:%ai$ */
69