Skip to content

Commit 82ac9a2

Browse files
authored
Merge pull request #387 from libtom/add_xsalsa20
Add XSalsa20
2 parents 5ab8dcf + 8144209 commit 82ac9a2

File tree

13 files changed

+324
-25
lines changed

13 files changed

+324
-25
lines changed

doc/crypt.tex

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,49 +1301,85 @@ \chapter{Stream Ciphers}
13011301
err = chacha_done(&st);
13021302
\end{verbatim}
13031303

1304-
\mysection{Salsa20}
1304+
\mysection{Salsa20 and XSalsa20}
1305+
1306+
\textit{Salsa20} was Daniel Bernstein's submission to the EU eSTREAM
1307+
competition where a reduced-round version, \textit{Salsa20/12}, was named
1308+
one of the winners. A third version, \textit{Salsa20/8}, was also evaluated.
1309+
\vspace{1mm}
1310+
1311+
While 20 rounds is the conservative default number of rounds, eSTREAM deemed
1312+
12 rounds to be a decent balance between strength and better performance.
1313+
The 8-round version, while still secure as of this writing, is faster but
1314+
does not enjoy the same margin of safety. Regardless of the number of rounds,
1315+
\textit{Salsa20} accepts either a 128- or a 256-bit key, a 64-bit IV, and a
1316+
64-bit counter.
1317+
\vspace{1mm}
1318+
1319+
\textit{XSalsa20} is yet another variant of \textit{Salsa20} designed to accept
1320+
only a 256-bit key and a longer 192-bit nonce, initialization being the only
1321+
difference between \textit{XSalsa20} and \textit{Salsa20}. Even the
1322+
\textit{salsa20\_state} is the same. Thereafter, salsa20\_crypt(),
1323+
salsa20\_keystream(), and salsa20\_done() are used unaltered.
1324+
salsa20\_ivctr64() is NOT used with xsalsa20\_setup().
1325+
\vspace{1mm}
1326+
1327+
To initialize \textit{Salsa20} for 8, 12, or 20 rounds with a 128- or a
1328+
256-bit key (16 or 32 bytes), a 64-bit IV (8 bytes), and counter (typically
1329+
zero), use:
13051330

1306-
\textit{Salsa20} is the forerunner of the ChaCha stream cipher. The ChaCha cipher is
1307-
Salsa20 with a few minor tweaks to further improve its strength, and in so doing, increase its
1308-
speed performance by about 5 percent. Unless you need Salsa20 for some reason, you should
1309-
probably choose ChaCha instead.
1310-
1311-
In April 2008 \textit{Salsa20/12} was named one of the winners in the EU eSTREAM competition.
1312-
Salsa20 was originally submitted by Daniel Bernstein with 20 rounds of strength but the
1313-
12-round reduced-round version was deemed to have sufficient strength and declared a winner.
1314-
Even the 8-round reduced-round version, Salsa20/8, has withstood attack.
1315-
1316-
For more information about Salsa20 see \url{https://en.wikipedia.org/wiki/Salsa20}.
1317-
1318-
Supported key size: 16 or 32 bytes (128 or 256 bits).
1319-
1320-
You can initialize Salsa20 with 64bit \textit{nonce} + 64bit \textit{counter}:
13211331
\begin{verbatim}
13221332
salsa20_state st;
1333+
ulong64 counter = 0;
13231334
err = salsa20_setup(&st, key, key_len, rounds);
1324-
err = salsa20_ivctr64(&st, nonce, 8, initial_64bit_ctr);
1335+
err = salsa20_ivctr64(&st, nonce, 8, counter);
13251336
\end{verbatim}
13261337

1327-
The \textit{salsa20\_setup} takes the number of rounds as a parameter -- choose 20 (the default)
1328-
if you are not sure. As always never ever use the same key + nonce pair more than once.
1338+
To initialize \textit{XSalsa20} for the recommended 20 rounds with a 256-bit
1339+
key (32 bytes) and a 192-bit nonce (24 bytes), use:
13291340

1330-
For the actual encryption or decryption you have to call:
1341+
\begin{verbatim}
1342+
salsa20_state st;
1343+
err = xsalsa20_setup(&st, key, key_len, nonce, nonce_len, rounds);
1344+
\end{verbatim}
1345+
1346+
Both \textit{Salsa20} and \textit{XSalsa20} use the following functions. To
1347+
encrypt or decrypt call:
13311348
\begin{verbatim}
13321349
err = salsa20_crypt(&st, in_buffer, in_len, out_buffer);
13331350
\end{verbatim}
13341351

1335-
If you just want a random stream of bytes initialize the cipher with a truly random \textit{key}
1336-
(32 bytes), a truly random \textit{nonce} (8 bytes) and zero initial counter. After that you can
1337-
get a stream of pseudo--random bytes via:
1352+
For a random keystream initialize the cipher with a truly random \textit{key}
1353+
and random \textit{nonce} after which you can get a stream of
1354+
pseudo--random bytes via:
13381355
\begin{verbatim}
13391356
err = salsa20_keystream(&st, out_buffer, out_len);
13401357
\end{verbatim}
13411358

1342-
When finished you should wipe the state:
1359+
Finally, when finished you should wipe the state.
13431360
\begin{verbatim}
13441361
err = salsa20_done(&st);
13451362
\end{verbatim}
13461363

1364+
For both \textit{Salsa20} and \textit{XSalsa20} rounds must be an even number
1365+
and if set to 0 the default number of rounds, 20, will be used.
1366+
\vspace{1mm}
1367+
1368+
If you define \textit{LTC_XSALSA20} to include \textit{XSalsa20} in a minimal
1369+
\textit{libtomcrypt} library build, you must also define \textit{LTC_SALSA20}.
1370+
\vspace{1mm}
1371+
1372+
As always, never ever use the same key + nonce/IV pair more than once.
1373+
\vspace{1mm}
1374+
1375+
For more information about Salsa20 see
1376+
\url{https://en.wikipedia.org/wiki/Salsa20}.
1377+
\vspace{1mm}
1378+
1379+
For more information about XSalsa20 see
1380+
\url{https://cr.yp.to/snuffle/xsalsa-20081128.pdf}.
1381+
\vspace{1mm}
1382+
13471383
\mysection{Sosemanuk}
13481384

13491385
\textit{Sosemanuk}, along with Salsa20, HC-128, and Rabbit, was named one of the winners in

libtomcrypt_VS2008.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,14 @@
26342634
RelativePath="src\stream\salsa20\salsa20_test.c"
26352635
>
26362636
</File>
2637+
<File
2638+
RelativePath="src\stream\salsa20\xsalsa20_setup.c"
2639+
>
2640+
</File>
2641+
<File
2642+
RelativePath="src\stream\salsa20\xsalsa20_test.c"
2643+
>
2644+
</File>
26372645
</Filter>
26382646
<Filter
26392647
Name="sober128"

makefile.mingw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ src/stream/rabbit/rabbit.o src/stream/rc4/rc4_stream.o src/stream/rc4/rc4_test.o
204204
src/stream/salsa20/salsa20_crypt.o src/stream/salsa20/salsa20_done.o \
205205
src/stream/salsa20/salsa20_ivctr64.o src/stream/salsa20/salsa20_keystream.o \
206206
src/stream/salsa20/salsa20_setup.o src/stream/salsa20/salsa20_test.o \
207+
src/stream/salsa20/xsalsa20_setup.o src/stream/salsa20/xsalsa20_test.o \
207208
src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_test.o \
208209
src/stream/sosemanuk/sosemanuk.o src/stream/sosemanuk/sosemanuk_test.o
209210

makefile.msvc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ src/stream/rabbit/rabbit.obj src/stream/rc4/rc4_stream.obj src/stream/rc4/rc4_te
197197
src/stream/salsa20/salsa20_crypt.obj src/stream/salsa20/salsa20_done.obj \
198198
src/stream/salsa20/salsa20_ivctr64.obj src/stream/salsa20/salsa20_keystream.obj \
199199
src/stream/salsa20/salsa20_setup.obj src/stream/salsa20/salsa20_test.obj \
200+
src/stream/salsa20/xsalsa20_setup.obj src/stream/salsa20/xsalsa20_test.obj \
200201
src/stream/sober128/sober128_stream.obj src/stream/sober128/sober128_test.obj \
201202
src/stream/sosemanuk/sosemanuk.obj src/stream/sosemanuk/sosemanuk_test.obj
202203

makefile.unix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ src/stream/rabbit/rabbit.o src/stream/rc4/rc4_stream.o src/stream/rc4/rc4_test.o
214214
src/stream/salsa20/salsa20_crypt.o src/stream/salsa20/salsa20_done.o \
215215
src/stream/salsa20/salsa20_ivctr64.o src/stream/salsa20/salsa20_keystream.o \
216216
src/stream/salsa20/salsa20_setup.o src/stream/salsa20/salsa20_test.o \
217+
src/stream/salsa20/xsalsa20_setup.o src/stream/salsa20/xsalsa20_test.o \
217218
src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_test.o \
218219
src/stream/sosemanuk/sosemanuk.o src/stream/sosemanuk/sosemanuk_test.o
219220

makefile_include.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ src/stream/rabbit/rabbit.o src/stream/rc4/rc4_stream.o src/stream/rc4/rc4_test.o
374374
src/stream/salsa20/salsa20_crypt.o src/stream/salsa20/salsa20_done.o \
375375
src/stream/salsa20/salsa20_ivctr64.o src/stream/salsa20/salsa20_keystream.o \
376376
src/stream/salsa20/salsa20_setup.o src/stream/salsa20/salsa20_test.o \
377+
src/stream/salsa20/xsalsa20_setup.o src/stream/salsa20/xsalsa20_test.o \
377378
src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_test.o \
378379
src/stream/sosemanuk/sosemanuk.o src/stream/sosemanuk/sosemanuk_test.o
379380

src/headers/tomcrypt_cipher.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,15 @@ int salsa20_test(void);
10311031

10321032
#endif /* LTC_SALSA20 */
10331033

1034+
#ifdef LTC_XSALSA20
1035+
1036+
int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long keylen,
1037+
const unsigned char *nonce, unsigned long noncelen,
1038+
int rounds);
1039+
int xsalsa20_test(void);
1040+
1041+
#endif /* LTC_XSALSA20 */
1042+
10341043
#ifdef LTC_SOSEMANUK
10351044

10361045
typedef struct {

src/headers/tomcrypt_custom.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
/* stream ciphers */
209209
#define LTC_CHACHA
210210
#define LTC_SALSA20
211+
#define LTC_XSALSA20
211212
#define LTC_SOSEMANUK
212213
#define LTC_RABBIT
213214
#define LTC_RC4_STREAM
@@ -589,6 +590,10 @@
589590
#error LTC_CHACHA20_PRNG requires LTC_CHACHA
590591
#endif
591592

593+
#if defined(LTC_XSALSA20) && !defined(LTC_SALSA20)
594+
#error LTC_XSALSA20 requires LTC_SALSA20
595+
#endif
596+
592597
#if defined(LTC_RC4) && !defined(LTC_RC4_STREAM)
593598
#error LTC_RC4 requires LTC_RC4_STREAM
594599
#endif

src/misc/crypt/crypt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ const char *crypt_build_settings =
135135
#if defined(LTC_SALSA20)
136136
" Salsa20\n"
137137
#endif
138+
#if defined(LTC_XSALSA20)
139+
" XSalsa20\n"
140+
#endif
138141
#if defined(LTC_SOSEMANUK)
139142
" Sosemanuk\n"
140143
#endif

src/stream/salsa20/salsa20_crypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int salsa20_crypt(salsa20_state *st, const unsigned char *in, unsigned long inle
6262
LTC_ARGCHK(st != NULL);
6363
LTC_ARGCHK(in != NULL);
6464
LTC_ARGCHK(out != NULL);
65-
LTC_ARGCHK(st->ivlen == 8);
65+
LTC_ARGCHK(st->ivlen == 8 || st->ivlen == 24);
6666

6767
if (st->ksleft > 0) {
6868
j = MIN(st->ksleft, inlen);

0 commit comments

Comments
 (0)