1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_CHACHA20POLY1305_MODE
7 
8 /**
9   Set IV + counter data (with RFC7905-magic) to the ChaCha20Poly1305 state and reset the context
10   @param st     The ChaCha20Poly1305 state
11   @param iv     The IV data to add
12   @param ivlen  The length of the IV (must be 12 or 8)
13   @param sequence_number   64bit sequence number which is incorporated into IV as described in RFC7905
14   @return CRYPT_OK on success
15  */
chacha20poly1305_setiv_rfc7905(chacha20poly1305_state * st,const unsigned char * iv,unsigned long ivlen,ulong64 sequence_number)16 int chacha20poly1305_setiv_rfc7905(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 sequence_number)
17 {
18    int i;
19    unsigned char combined_iv[12] = { 0 };
20 
21    LTC_ARGCHK(st != NULL);
22    LTC_ARGCHK(iv != NULL);
23    LTC_ARGCHK(ivlen == 12);
24 
25    STORE64L(sequence_number, combined_iv + 4);
26    for (i = 0; i < 12; i++) combined_iv[i] = iv[i] ^ combined_iv[i];
27    return chacha20poly1305_setiv(st, combined_iv, 12);
28 }
29 
30 #endif
31