1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 #include "tomcrypt_private.h"
5
6 #ifdef LTC_SALSA20
7
8 /**
9 Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Salsa20
10 @param key The key
11 @param keylen The key length
12 @param iv The initial vector
13 @param ivlen The initial vector length
14 @param datain The plaintext (or ciphertext)
15 @param datalen The length of the input and output (octets)
16 @param rounds The number of rounds
17 @param dataout [out] The ciphertext (or plaintext)
18 @return CRYPT_OK if successful
19 */
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)20 int salsa20_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds,
21 const unsigned char *iv, unsigned long ivlen, ulong64 counter,
22 const unsigned char *datain, unsigned long datalen, unsigned char *dataout)
23 {
24 salsa20_state st;
25 int err;
26
27 if ((err = salsa20_setup(&st, key, keylen, rounds)) != CRYPT_OK) goto WIPE_KEY;
28 if ((err = salsa20_ivctr64(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY;
29 err = salsa20_crypt(&st, datain, datalen, dataout);
30 WIPE_KEY:
31 salsa20_done(&st);
32 return err;
33 }
34
35 #endif /* LTC_SALSA20 */
36