Skip to content

Commit a2b343b

Browse files
committed
streams-add-single-call-crypt-functions
fix mixed declarations and code add _memory chacha_ivctr32() test
1 parent 305a589 commit a2b343b

File tree

16 files changed

+454
-20
lines changed

16 files changed

+454
-20
lines changed

doc/crypt.tex

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

1304+
To encrypt plaintext (or decrypt ciphertext) using ChaCha for data already in
1305+
memory with a single function call, the following function may be used.
1306+
\begin{verbatim}
1307+
err = chacha_memory(key, keylen, iv, ivlen, datain, datalen, rounds, dataout);
1308+
\end{verbatim}
1309+
13041310
\mysection{Salsa20 and XSalsa20}
13051311

13061312
\textit{Salsa20} was Daniel Bernstein's submission to the EU eSTREAM
@@ -1361,6 +1367,18 @@ \chapter{Stream Ciphers}
13611367
err = salsa20_done(&st);
13621368
\end{verbatim}
13631369

1370+
To encrypt plaintext (or decrypt ciphertext) using Salsa20 for data already in
1371+
memory with a single function call, the following function may be used.
1372+
\begin{verbatim}
1373+
err = salsa20_memory(key, keylen, iv, ivlen, datain, datalen, rounds, dataout);
1374+
\end{verbatim}
1375+
1376+
To encrypt plaintext (or decrypt ciphertext) using XSalsa20 for data already in
1377+
memory with a single function call, the following function may be used.
1378+
\begin{verbatim}
1379+
err = xsalsa20_memory(key, keylen, nonce, nonce_len, datain, datalen, rounds, dataout);
1380+
\end{verbatim}
1381+
13641382
For both \textit{Salsa20} and \textit{XSalsa20} rounds must be an even number
13651383
and if set to 0 the default number of rounds, 20, will be used.
13661384
\vspace{1mm}
@@ -1427,6 +1445,12 @@ \chapter{Stream Ciphers}
14271445
you do not need to re-run \textit{sosemanuk\_setup()} again, unless of course, you called
14281446
\textit{sosemanuk\_done()}.
14291447

1448+
To encrypt plaintext (or decrypt ciphertext) using Sosemanuk for data already in
1449+
memory with a single function call, the following function may be used.
1450+
\begin{verbatim}
1451+
err = sosemanuk_memory(key, keylen, iv, ivlen, datain, datalen, dataout);
1452+
\end{verbatim}
1453+
14301454
\mysection{Rabbit}
14311455

14321456
\textit{Rabbit}, along with Salsa20, Sosemanuk, and HC-128, was named one of the winners
@@ -1482,6 +1506,12 @@ \chapter{Stream Ciphers}
14821506
You will want to use a different IV but you do not need to call \textit{rabbit\_setup()} a 2nd time,
14831507
unless of course, you skipped calling \textit{rabbit\_setiv()}.
14841508

1509+
To encrypt plaintext (or decrypt ciphertext) using Rabbit for data already in
1510+
memory with a single function call, the following function may be used.
1511+
\begin{verbatim}
1512+
err = rabbit_memory(key, keylen, iv, ivlen, datain, datalen, dataout);
1513+
\end{verbatim}
1514+
14851515
For more information, see: \newline
14861516
\hspace{4em}- \url{http://www.ecrypt.eu.org/stream/p3ciphers/rabbit/rabbit_p3.pdf} \newline
14871517
\hspace{4em}- \url{https://tools.ietf.org/html/rfc4503}
@@ -1515,6 +1545,12 @@ \chapter{Stream Ciphers}
15151545
err = rc4_stream_done(&st);
15161546
\end{verbatim}
15171547

1548+
To encrypt plaintext (or decrypt ciphertext) using RC6 for data already in
1549+
memory with a single function call, the following function may be used.
1550+
\begin{verbatim}
1551+
err = rc4_stream_memory(key, keylen, datain, datalen, dataout);
1552+
\end{verbatim}
1553+
15181554
\mysection{Sober128}
15191555

15201556
Supported key size: must be multiple of 4 bytes
@@ -1542,6 +1578,12 @@ \chapter{Stream Ciphers}
15421578
err = sober128_stream_done(&st);
15431579
\end{verbatim}
15441580

1581+
To encrypt plaintext (or decrypt ciphertext) using Sober128 for data already in
1582+
memory with a single function call, the following function may be used.
1583+
\begin{verbatim}
1584+
err = sober128_stream_memory(key, keylen, iv, ivlen, datain, datalen, dataout);
1585+
\end{verbatim}
1586+
15451587
\chapter{Authenticated Encryption}
15461588

15471589
Authenticated Encryption - sometimes also called Authenticated Encryption with Associated Data (AEAD) - is a variant of encryption

src/headers/tomcrypt_cipher.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,9 @@ int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen,
10091009
int chacha_keystream(chacha_state *st, unsigned char *out, unsigned long outlen);
10101010
int chacha_done(chacha_state *st);
10111011
int chacha_test(void);
1012+
int chacha_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds,
1013+
const unsigned char *iv, unsigned long ivlen, ulong64 counter,
1014+
const unsigned char *datain, unsigned long datalen, unsigned char *dataout);
10121015

10131016
#endif /* LTC_CHACHA */
10141017

@@ -1028,6 +1031,9 @@ int salsa20_crypt(salsa20_state *st, const unsigned char *in, unsigned long inle
10281031
int salsa20_keystream(salsa20_state *st, unsigned char *out, unsigned long outlen);
10291032
int salsa20_done(salsa20_state *st);
10301033
int salsa20_test(void);
1034+
int salsa20_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds,
1035+
const unsigned char *iv, unsigned long ivlen, ulong64 counter,
1036+
const unsigned char *datain, unsigned long datalen, unsigned char *dataout);
10311037

10321038
#endif /* LTC_SALSA20 */
10331039

@@ -1037,6 +1043,9 @@ int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long
10371043
const unsigned char *nonce, unsigned long noncelen,
10381044
int rounds);
10391045
int xsalsa20_test(void);
1046+
int xsalsa20_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds,
1047+
const unsigned char *nonce, unsigned long noncelen,
1048+
const unsigned char *datain, unsigned long datalen, unsigned char *dataout);
10401049

10411050
#endif /* LTC_XSALSA20 */
10421051

@@ -1061,6 +1070,10 @@ int sosemanuk_crypt(sosemanuk_state *st, const unsigned char *in, unsigned long
10611070
int sosemanuk_keystream(sosemanuk_state *st, unsigned char *out, unsigned long outlen);
10621071
int sosemanuk_done(sosemanuk_state *st);
10631072
int sosemanuk_test(void);
1073+
int sosemanuk_memory(const unsigned char *key, unsigned long keylen,
1074+
const unsigned char *iv, unsigned long ivlen,
1075+
const unsigned char *datain, unsigned long datalen,
1076+
unsigned char *dataout);
10641077

10651078
#endif /* LTC_SOSEMANUK */
10661079

@@ -1085,6 +1098,10 @@ int rabbit_crypt(rabbit_state* st, const unsigned char *in, unsigned long inlen,
10851098
int rabbit_keystream(rabbit_state* st, unsigned char *out, unsigned long outlen);
10861099
int rabbit_done(rabbit_state *st);
10871100
int rabbit_test(void);
1101+
int rabbit_memory(const unsigned char *key, unsigned long keylen,
1102+
const unsigned char *iv, unsigned long ivlen,
1103+
const unsigned char *datain, unsigned long datalen,
1104+
unsigned char *dataout);
10881105

10891106
#endif /* LTC_RABBIT */
10901107

@@ -1100,6 +1117,9 @@ int rc4_stream_crypt(rc4_state *st, const unsigned char *in, unsigned long inlen
11001117
int rc4_stream_keystream(rc4_state *st, unsigned char *out, unsigned long outlen);
11011118
int rc4_stream_done(rc4_state *st);
11021119
int rc4_stream_test(void);
1120+
int rc4_stream_memory(const unsigned char *key, unsigned long keylen,
1121+
const unsigned char *datain, unsigned long datalen,
1122+
unsigned char *dataout);
11031123

11041124
#endif /* LTC_RC4_STREAM */
11051125

@@ -1119,6 +1139,10 @@ int sober128_stream_crypt(sober128_state *st, const unsigned char *in, unsigned
11191139
int sober128_stream_keystream(sober128_state *st, unsigned char *out, unsigned long outlen);
11201140
int sober128_stream_done(sober128_state *st);
11211141
int sober128_stream_test(void);
1142+
int sober128_stream_memory(const unsigned char *key, unsigned long keylen,
1143+
const unsigned char *iv, unsigned long ivlen,
1144+
const unsigned char *datain, unsigned long datalen,
1145+
unsigned char *dataout);
11221146

11231147
#endif /* LTC_SOBER128_STREAM */
11241148

src/stream/chacha/chacha_memory.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_CHACHA
13+
14+
/**
15+
Encrypt (or decrypt) bytes of ciphertext (or plaintext) with ChaCha
16+
@param key The key
17+
@param keylen The key length
18+
@param iv The initial vector
19+
@param ivlen The initial vector length
20+
@param datain The plaintext (or ciphertext)
21+
@param datalen The length of the input and output (octets)
22+
@param rounds The number of rounds
23+
@param dataout [out] The ciphertext (or plaintext)
24+
@return CRYPT_OK if successful
25+
*/
26+
int chacha_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds,
27+
const unsigned char *iv, unsigned long ivlen, ulong64 counter,
28+
const unsigned char *datain, unsigned long datalen, unsigned char *dataout)
29+
{
30+
chacha_state st;
31+
int err;
32+
33+
LTC_ARGCHK(ivlen <= 8 || counter < 4294967296); /* 2**32 */
34+
35+
if ((err = chacha_setup(&st, key, keylen, rounds)) != CRYPT_OK) goto WIPE_KEY;
36+
if (ivlen > 8) {
37+
if ((err = chacha_ivctr32(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY;
38+
} else {
39+
if ((err = chacha_ivctr64(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY;
40+
}
41+
err = chacha_crypt(&st, datain, datalen, dataout);
42+
WIPE_KEY:
43+
chacha_done(&st);
44+
return err;
45+
}
46+
47+
#endif /* LTC_CHACHA */
48+
49+
/* ref: $Format:%D$ */
50+
/* git commit: $Format:%H$ */
51+
/* commit time: $Format:%ai$ */

src/stream/chacha/chacha_test.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,39 @@ int chacha_test(void)
4040
int err;
4141

4242
len = strlen(pt);
43-
/* crypt piece by piece */
43+
44+
/* crypt piece by piece - using chacha_ivctr32() */
4445
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
4546
if ((err = chacha_ivctr32(&st, n, sizeof(n), 1)) != CRYPT_OK) return err;
46-
if ((err = chacha_crypt(&st, (unsigned char*)pt, 35, out)) != CRYPT_OK) return err;
47+
if ((err = chacha_crypt(&st, (unsigned char*)pt, 35, out )) != CRYPT_OK) return err;
4748
if ((err = chacha_crypt(&st, (unsigned char*)pt + 35, 35, out + 35)) != CRYPT_OK) return err;
4849
if ((err = chacha_crypt(&st, (unsigned char*)pt + 70, 5, out + 70)) != CRYPT_OK) return err;
4950
if ((err = chacha_crypt(&st, (unsigned char*)pt + 75, 5, out + 75)) != CRYPT_OK) return err;
5051
if ((err = chacha_crypt(&st, (unsigned char*)pt + 80, len - 80, out + 80)) != CRYPT_OK) return err;
5152
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV1", 1)) return CRYPT_FAIL_TESTVECTOR;
52-
/* crypt in one go */
53+
54+
/* crypt in one go - using chacha_ivctr32() */
5355
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
5456
if ((err = chacha_ivctr32(&st, n, sizeof(n), 1)) != CRYPT_OK) return err;
5557
if ((err = chacha_crypt(&st, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
5658
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV2", 1)) return CRYPT_FAIL_TESTVECTOR;
59+
5760
/* crypt in one go - using chacha_ivctr64() */
5861
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
5962
if ((err = chacha_ivctr64(&st, n + 4, sizeof(n) - 4, 1)) != CRYPT_OK) return err;
6063
if ((err = chacha_crypt(&st, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
6164
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV3", 1)) return CRYPT_FAIL_TESTVECTOR;
6265

66+
/* crypt in a single call using 32-bit counter with a value of 1 */
67+
if ((err = chacha_memory(k, sizeof(k), 20,
68+
n, sizeof(n), 1, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
69+
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV4", 1)) return CRYPT_FAIL_TESTVECTOR;
70+
71+
/* crypt in a single call using 64-bit counter with a value of 1 */
72+
if ((err = chacha_memory(k, sizeof(k), 20,
73+
n + 4, sizeof(n) - 4, 1, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
74+
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV5", 1)) return CRYPT_FAIL_TESTVECTOR;
75+
6376
return CRYPT_OK;
6477
#endif
6578
}

src/stream/rabbit/rabbit.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,25 @@ int rabbit_test(void)
421421
if ((err = rabbit_crypt(&st, (unsigned char*)pt + 5, 29, out + 5)) != CRYPT_OK) return err;
422422
if ((err = rabbit_crypt(&st, (unsigned char*)pt + 34, 5, out + 34)) != CRYPT_OK) return err;
423423
if (compare_testvector(out, ptlen, ct, ptlen, "RABBIT-TV3", 1)) return CRYPT_FAIL_TESTVECTOR;
424+
425+
/* --- Test 4 (crypt in a single call) ------------------------------------ */
426+
427+
if ((err = rabbit_memory(k, sizeof(k), iv, sizeof(iv),
428+
(unsigned char*)pt, sizeof(pt), out)) != CRYPT_OK) return err;
429+
if (compare_testvector(out, ptlen, ct, ptlen, "RABBIT-TV4", 1)) return CRYPT_FAIL_TESTVECTOR;
424430
/* use 'out' (ciphertext) in the next decryption test */
425431

426-
/* --- Test 4 (decrypt ciphertext) ------------------------------------ */
432+
/* --- Test 5 (decrypt ciphertext) ------------------------------------ */
427433

428434
/* decrypt ct (out) and compare with pt (start with only setiv() to reset) */
429435
if ((err = rabbit_setiv(&st, iv, sizeof(iv))) != CRYPT_OK) return err;
430436
if ((err = rabbit_crypt(&st, out, ptlen, out2)) != CRYPT_OK) return err;
431-
if (compare_testvector(out2, ptlen, pt, ptlen, "RABBIT-TV4", 1)) return CRYPT_FAIL_TESTVECTOR;
437+
if (compare_testvector(out2, ptlen, pt, ptlen, "RABBIT-TV5", 1)) return CRYPT_FAIL_TESTVECTOR;
432438

433-
/* --- Test 5 (wipe state, incl key) ---------------------------------- */
439+
/* --- Test 6 (wipe state, incl key) ---------------------------------- */
434440

435441
if ((err = rabbit_done(&st)) != CRYPT_OK) return err;
436-
if (compare_testvector(&st, sizeof(st), nulls, sizeof(st), "RABBIT-TV5", 1)) return CRYPT_FAIL_TESTVECTOR;
442+
if (compare_testvector(&st, sizeof(st), nulls, sizeof(st), "RABBIT-TV6", 1)) return CRYPT_FAIL_TESTVECTOR;
437443

438444
}
439445

src/stream/rabbit/rabbit_memory.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
/* The implementation is based on:
11+
* chacha-ref.c version 20080118
12+
* Public domain from D. J. Bernstein
13+
*/
14+
15+
#include "tomcrypt_private.h"
16+
17+
#ifdef LTC_RABBIT
18+
19+
/**
20+
Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Rabbit
21+
@param key The key
22+
@param keylen The key length
23+
@param iv The initial vector
24+
@param ivlen The initial vector length
25+
@param datain The plaintext (or ciphertext)
26+
@param datalen The length of the input and output (octets)
27+
@param dataout [out] The ciphertext (or plaintext)
28+
@return CRYPT_OK if successful
29+
*/
30+
int rabbit_memory(const unsigned char *key, unsigned long keylen,
31+
const unsigned char *iv, unsigned long ivlen,
32+
const unsigned char *datain, unsigned long datalen,
33+
unsigned char *dataout)
34+
{
35+
rabbit_state st;
36+
int err;
37+
38+
if ((err = rabbit_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY;
39+
if ((err = rabbit_setiv(&st, iv, ivlen)) != CRYPT_OK) goto WIPE_KEY;
40+
err = rabbit_crypt(&st, datain, datalen, dataout);
41+
WIPE_KEY:
42+
rabbit_done(&st);
43+
return err;
44+
}
45+
46+
#endif /* LTC_RABBIT */
47+
48+
/* ref: $Format:%D$ */
49+
/* git commit: $Format:%H$ */
50+
/* commit time: $Format:%ai$ */

src/stream/rc4/rc4_stream_memory.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_RC4_STREAM
13+
14+
/**
15+
Encrypt (or decrypt) bytes of ciphertext (or plaintext) with RC4
16+
@param key The key
17+
@param keylen The key length
18+
@param datain The plaintext (or ciphertext)
19+
@param datalen The length of the input and output (octets)
20+
@param dataout [out] The ciphertext (or plaintext)
21+
@return CRYPT_OK if successful
22+
*/
23+
int rc4_stream_memory(const unsigned char *key, unsigned long keylen,
24+
const unsigned char *datain, unsigned long datalen,
25+
unsigned char *dataout)
26+
{
27+
rc4_state st;
28+
int err;
29+
30+
if ((err = rc4_stream_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY;
31+
err = rc4_stream_crypt(&st, datain, datalen, dataout);
32+
WIPE_KEY:
33+
rc4_stream_done(&st);
34+
return err;
35+
}
36+
37+
#endif /* LTC_RC4_STREAM */
38+
39+
/* ref: $Format:%D$ */
40+
/* git commit: $Format:%H$ */
41+
/* commit time: $Format:%ai$ */

src/stream/rc4/rc4_test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ int rc4_stream_test(void)
2525

2626
if ((err = rc4_stream_setup(&st, key, sizeof(key))) != CRYPT_OK) return err;
2727
if ((err = rc4_stream_crypt(&st, pt, sizeof(pt), buf)) != CRYPT_OK) return err;
28-
if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4", 0)) return CRYPT_FAIL_TESTVECTOR;
28+
if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV1", 0)) return CRYPT_FAIL_TESTVECTOR;
2929
if ((err = rc4_stream_done(&st)) != CRYPT_OK) return err;
3030

31+
/* crypt in a single call */
32+
if ((err = rc4_stream_memory(key, sizeof(key), pt, sizeof(pt), buf)) != CRYPT_OK) return err;
33+
if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV2", 0)) return CRYPT_FAIL_TESTVECTOR;
34+
3135
return CRYPT_OK;
3236
#endif
3337
}

0 commit comments

Comments
 (0)