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 #include "tomcrypt_private.h"
12 
13 #ifdef LTC_SALSA20
14 
15 /**
16    Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Salsa20
17    @param key     The key
18    @param keylen  The key length
19    @param iv      The initial vector
20    @param ivlen   The initial vector length
21    @param datain  The plaintext (or ciphertext)
22    @param datalen The length of the input and output (octets)
23    @param rounds  The number of rounds
24    @param dataout [out] The ciphertext (or plaintext)
25    @return CRYPT_OK if successful
26 */
salsa20_memory(const unsigned char * key,unsigned long keylen,unsigned long rounds,const unsigned char * iv,unsigned long ivlen,ulong64 counter,const unsigned char * datain,unsigned long datalen,unsigned char * dataout)27 int salsa20_memory(const unsigned char *key,    unsigned long keylen,  unsigned long rounds,
28                    const unsigned char *iv,     unsigned long ivlen,   ulong64 counter,
29                    const unsigned char *datain, unsigned long datalen, unsigned char *dataout)
30 {
31    salsa20_state st;
32    int err;
33 
34    if ((err = salsa20_setup(&st, key, keylen, rounds))  != CRYPT_OK) goto WIPE_KEY;
35    if ((err = salsa20_ivctr64(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY;
36    err = salsa20_crypt(&st, datain, datalen, dataout);
37 WIPE_KEY:
38    salsa20_done(&st);
39    return err;
40 }
41 
42 #endif /* LTC_SALSA20 */
43 
44 /* ref:         $Format:%D$ */
45 /* git commit:  $Format:%H$ */
46 /* commit time: $Format:%ai$ */
47