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 12)
18   @param counter 32bit (unsigned) initial counter value
19   @return CRYPT_OK on success
20  */
chacha_ivctr32(chacha_state * st,const unsigned char * iv,unsigned long ivlen,ulong32 counter)21 int chacha_ivctr32(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong32 counter)
22 {
23    LTC_ARGCHK(st != NULL);
24    LTC_ARGCHK(iv != NULL);
25    /* 96bit IV + 32bit counter */
26    LTC_ARGCHK(ivlen == 12);
27 
28    st->input[12] = counter;
29    LOAD32L(st->input[13], iv + 0);
30    LOAD32L(st->input[14], iv + 4);
31    LOAD32L(st->input[15], iv + 8);
32    st->ksleft = 0;
33    st->ivlen = ivlen;
34    return CRYPT_OK;
35 }
36 
37 #endif
38