1 #include "crypto_core_hsalsa20.h"
2 #include "crypto_stream_salsa20.h"
3 #include "crypto_stream_xsalsa20.h"
4 #include "randombytes.h"
5 #include "utils.h"
6
7 int
crypto_stream_xsalsa20(unsigned char * c,unsigned long long clen,const unsigned char * n,const unsigned char * k)8 crypto_stream_xsalsa20(unsigned char *c, unsigned long long clen,
9 const unsigned char *n, const unsigned char *k)
10 {
11 unsigned char subkey[32];
12 int ret;
13
14 crypto_core_hsalsa20(subkey, n, k, NULL);
15 ret = crypto_stream_salsa20(c, clen, n + 16, subkey);
16 sodium_memzero(subkey, sizeof subkey);
17
18 return ret;
19 }
20
21 int
crypto_stream_xsalsa20_xor_ic(unsigned char * c,const unsigned char * m,unsigned long long mlen,const unsigned char * n,uint64_t ic,const unsigned char * k)22 crypto_stream_xsalsa20_xor_ic(unsigned char *c, const unsigned char *m,
23 unsigned long long mlen, const unsigned char *n,
24 uint64_t ic, const unsigned char *k)
25 {
26 unsigned char subkey[32];
27 int ret;
28
29 crypto_core_hsalsa20(subkey, n, k, NULL);
30 ret = crypto_stream_salsa20_xor_ic(c, m, mlen, n + 16, ic, subkey);
31 sodium_memzero(subkey, sizeof subkey);
32
33 return ret;
34 }
35
36 int
crypto_stream_xsalsa20_xor(unsigned char * c,const unsigned char * m,unsigned long long mlen,const unsigned char * n,const unsigned char * k)37 crypto_stream_xsalsa20_xor(unsigned char *c, const unsigned char *m,
38 unsigned long long mlen, const unsigned char *n,
39 const unsigned char *k)
40 {
41 return crypto_stream_xsalsa20_xor_ic(c, m, mlen, n, 0ULL, k);
42 }
43
44 size_t
crypto_stream_xsalsa20_keybytes(void)45 crypto_stream_xsalsa20_keybytes(void)
46 {
47 return crypto_stream_xsalsa20_KEYBYTES;
48 }
49
50 size_t
crypto_stream_xsalsa20_noncebytes(void)51 crypto_stream_xsalsa20_noncebytes(void)
52 {
53 return crypto_stream_xsalsa20_NONCEBYTES;
54 }
55
56 size_t
crypto_stream_xsalsa20_messagebytes_max(void)57 crypto_stream_xsalsa20_messagebytes_max(void)
58 {
59 return crypto_stream_xsalsa20_MESSAGEBYTES_MAX;
60 }
61
62 void
crypto_stream_xsalsa20_keygen(unsigned char k[crypto_stream_xsalsa20_KEYBYTES])63 crypto_stream_xsalsa20_keygen(unsigned char k[crypto_stream_xsalsa20_KEYBYTES])
64 {
65 randombytes_buf(k, crypto_stream_xsalsa20_KEYBYTES);
66 }
67