1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* The implementation is based on:
5  * chacha-ref.c version 20080118
6  * Public domain from D. J. Bernstein
7  */
8 
9 #include "tomcrypt_private.h"
10 
11 #ifdef LTC_CHACHA
12 
13 /**
14   Set IV + counter data to the ChaCha state
15   @param st      The ChaCha20 state
16   @param iv      The IV data to add
17   @param ivlen   The length of the IV (must be 8)
18   @param counter 64bit (unsigned) initial counter value
19   @return CRYPT_OK on success
20  */
chacha_ivctr64(chacha_state * st,const unsigned char * iv,unsigned long ivlen,ulong64 counter)21 int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter)
22 {
23    LTC_ARGCHK(st != NULL);
24    LTC_ARGCHK(iv != NULL);
25    /* 64bit IV + 64bit counter */
26    LTC_ARGCHK(ivlen == 8);
27 
28    st->input[12] = (ulong32)(counter & 0xFFFFFFFF);
29    st->input[13] = (ulong32)(counter >> 32);
30    LOAD32L(st->input[14], iv + 0);
31    LOAD32L(st->input[15], iv + 4);
32    st->ksleft = 0;
33    st->ivlen = ivlen;
34    return CRYPT_OK;
35 }
36 
37 #endif
38