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