xref: /freebsd/sys/contrib/libsodium/test/default/stream.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "stream"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer static unsigned char firstkey[32] = { 0x1b, 0x27, 0x55, 0x64, 0x73, 0xe9, 0x85,
6*0ac341f1SConrad Meyer                                       0xd4, 0x62, 0xcd, 0x51, 0x19, 0x7a, 0x9a,
7*0ac341f1SConrad Meyer                                       0x46, 0xc7, 0x60, 0x09, 0x54, 0x9e, 0xac,
8*0ac341f1SConrad Meyer                                       0x64, 0x74, 0xf2, 0x06, 0xc4, 0xee, 0x08,
9*0ac341f1SConrad Meyer                                       0x44, 0xf6, 0x83, 0x89 };
10*0ac341f1SConrad Meyer 
11*0ac341f1SConrad Meyer static unsigned char nonce[24] = { 0x69, 0x69, 0x6e, 0xe9, 0x55, 0xb6,
12*0ac341f1SConrad Meyer                                    0x2b, 0x73, 0xcd, 0x62, 0xbd, 0xa8,
13*0ac341f1SConrad Meyer                                    0x75, 0xfc, 0x73, 0xd6, 0x82, 0x19,
14*0ac341f1SConrad Meyer                                    0xe0, 0x03, 0x6b, 0x7a, 0x0b, 0x37 };
15*0ac341f1SConrad Meyer 
16*0ac341f1SConrad Meyer static unsigned char output[4194304];
17*0ac341f1SConrad Meyer 
18*0ac341f1SConrad Meyer static unsigned char h[32];
19*0ac341f1SConrad Meyer static char          hex[2 * 192 + 1];
20*0ac341f1SConrad Meyer 
21*0ac341f1SConrad Meyer int
main(void)22*0ac341f1SConrad Meyer main(void)
23*0ac341f1SConrad Meyer {
24*0ac341f1SConrad Meyer     int i;
25*0ac341f1SConrad Meyer 
26*0ac341f1SConrad Meyer     randombytes_buf(output, sizeof output);
27*0ac341f1SConrad Meyer     crypto_stream(output, sizeof output, nonce, firstkey);
28*0ac341f1SConrad Meyer     crypto_hash_sha256(h, output, sizeof output);
29*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, h, sizeof h);
30*0ac341f1SConrad Meyer     printf("%s\n", hex);
31*0ac341f1SConrad Meyer 
32*0ac341f1SConrad Meyer     assert(sizeof output > 4000);
33*0ac341f1SConrad Meyer 
34*0ac341f1SConrad Meyer     crypto_stream_xsalsa20_xor_ic(output, output, 4000, nonce, 0U, firstkey);
35*0ac341f1SConrad Meyer     for (i = 0; i < 4000; i++) {
36*0ac341f1SConrad Meyer         assert(output[i] == 0);
37*0ac341f1SConrad Meyer     }
38*0ac341f1SConrad Meyer     crypto_stream_xsalsa20_xor_ic(output, output, 4000, nonce, 1U, firstkey);
39*0ac341f1SConrad Meyer     crypto_hash_sha256(h, output, sizeof output);
40*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, h, sizeof h);
41*0ac341f1SConrad Meyer     printf("%s\n", hex);
42*0ac341f1SConrad Meyer 
43*0ac341f1SConrad Meyer     for (i = 0; i < 64; i++) {
44*0ac341f1SConrad Meyer         memset(output, i, 64);
45*0ac341f1SConrad Meyer         crypto_stream(output, (int) (i & 0xff), nonce, firstkey);
46*0ac341f1SConrad Meyer         sodium_bin2hex(hex, sizeof hex, output, 64);
47*0ac341f1SConrad Meyer         printf("%s\n", hex);
48*0ac341f1SConrad Meyer     }
49*0ac341f1SConrad Meyer 
50*0ac341f1SConrad Meyer     memset(output, 0, 192);
51*0ac341f1SConrad Meyer     crypto_stream_xsalsa20_xor_ic(output, output, 192, nonce,
52*0ac341f1SConrad Meyer                                   (1ULL << 32) - 1ULL, firstkey);
53*0ac341f1SConrad Meyer     sodium_bin2hex(hex, 192 * 2 + 1, output, 192);
54*0ac341f1SConrad Meyer     printf("%s\n", hex);
55*0ac341f1SConrad Meyer 
56*0ac341f1SConrad Meyer     assert(crypto_stream_keybytes() > 0U);
57*0ac341f1SConrad Meyer     assert(crypto_stream_noncebytes() > 0U);
58*0ac341f1SConrad Meyer     assert(crypto_stream_messagebytes_max() > 0U);
59*0ac341f1SConrad Meyer     assert(strcmp(crypto_stream_primitive(), "xsalsa20") == 0);
60*0ac341f1SConrad Meyer     assert(crypto_stream_keybytes() == crypto_stream_xsalsa20_keybytes());
61*0ac341f1SConrad Meyer     assert(crypto_stream_noncebytes() == crypto_stream_xsalsa20_noncebytes());
62*0ac341f1SConrad Meyer     assert(crypto_stream_messagebytes_max() == crypto_stream_xsalsa20_messagebytes_max());
63*0ac341f1SConrad Meyer 
64*0ac341f1SConrad Meyer     return 0;
65*0ac341f1SConrad Meyer }
66