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  * chacha-ref.c version 20080118
13  * Public domain from D. J. Bernstein
14  */
15 
16 #include "tomcrypt_private.h"
17 
18 #ifdef LTC_CHACHA
19 
20 /**
21   Set IV + counter data to the ChaCha state
22   @param st      The ChaCha20 state
23   @param iv      The IV data to add
24   @param ivlen   The length of the IV (must be 8)
25   @param counter 64bit (unsigned) initial counter value
26   @return CRYPT_OK on success
27  */
chacha_ivctr64(chacha_state * st,const unsigned char * iv,unsigned long ivlen,ulong64 counter)28 int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter)
29 {
30    LTC_ARGCHK(st != NULL);
31    LTC_ARGCHK(iv != NULL);
32    /* 64bit IV + 64bit counter */
33    LTC_ARGCHK(ivlen == 8);
34 
35    st->input[12] = (ulong32)(counter & 0xFFFFFFFF);
36    st->input[13] = (ulong32)(counter >> 32);
37    LOAD32L(st->input[14], iv + 0);
38    LOAD32L(st->input[15], iv + 4);
39    st->ksleft = 0;
40    st->ivlen = ivlen;
41    return CRYPT_OK;
42 }
43 
44 #endif
45 
46 /* ref:         $Format:%D$ */
47 /* git commit:  $Format:%H$ */
48 /* commit time: $Format:%ai$ */
49