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 
11 #ifdef LTC_POLY1305
12 
13 /**
14    POLY1305 a block of memory to produce the authentication tag
15    @param key       The secret key
16    @param keylen    The length of the secret key (octets)
17    @param in        The data to POLY1305
18    @param inlen     The length of the data to POLY1305 (octets)
19    @param mac       [out] Destination of the authentication tag
20    @param maclen    [in/out] Max size and resulting size of authentication tag
21    @return CRYPT_OK if successful
22 */
poly1305_memory(const unsigned char * key,unsigned long keylen,const unsigned char * in,unsigned long inlen,unsigned char * mac,unsigned long * maclen)23 int poly1305_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen)
24 {
25    poly1305_state st;
26    int err;
27 
28    LTC_ARGCHK(key    != NULL);
29    LTC_ARGCHK(in     != NULL);
30    LTC_ARGCHK(mac    != NULL);
31    LTC_ARGCHK(maclen != NULL);
32 
33    if ((err = poly1305_init(&st, key, keylen))  != CRYPT_OK) { goto LBL_ERR; }
34    if ((err = poly1305_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
35    err = poly1305_done(&st, mac, maclen);
36 LBL_ERR:
37 #ifdef LTC_CLEAN_STACK
38    zeromem(&st, sizeof(poly1305_state));
39 #endif
40    return err;
41 }
42 
43 #endif
44