xref: /freebsd/sys/contrib/libsodium/test/default/core3.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 
2 #define TEST_NAME "core3"
3 #include "cmptest.h"
4 
5 static unsigned char SECONDKEY[32] = { 0xdc, 0x90, 0x8d, 0xda, 0x0b, 0x93, 0x44,
6                                        0xa9, 0x53, 0x62, 0x9b, 0x73, 0x38, 0x20,
7                                        0x77, 0x88, 0x80, 0xf3, 0xce, 0xb4, 0x21,
8                                        0xbb, 0x61, 0xb9, 0x1c, 0xbd, 0x4c, 0x3e,
9                                        0x66, 0x25, 0x6c, 0xe4 };
10 
11 static unsigned char NONCESUFFIX[8] = { 0x82, 0x19, 0xe0, 0x03,
12                                         0x6b, 0x7a, 0x0b, 0x37 };
13 
14 static unsigned char C[16] = { 0x65, 0x78, 0x70, 0x61, 0x6e, 0x64, 0x20, 0x33,
15                                0x32, 0x2d, 0x62, 0x79, 0x74, 0x65, 0x20, 0x6b };
16 
17 int
18 main(void)
19 {
20     unsigned char *secondkey;
21     unsigned char *c;
22     unsigned char *noncesuffix;
23     unsigned char *in;
24     unsigned char *output;
25     unsigned char *h;
26     size_t         output_len = 64 * 256 * 256;
27     size_t         pos = 0;
28     int            i;
29 
30     pos = 0;
31     secondkey = (unsigned char *) sodium_malloc(32);
32     memcpy(secondkey, SECONDKEY, 32);
33     noncesuffix = (unsigned char *) sodium_malloc(8);
34     memcpy(noncesuffix, NONCESUFFIX, 8);
35     c = (unsigned char *) sodium_malloc(16);
36     memcpy(c, C, 16);
37     in = (unsigned char *) sodium_malloc(16);
38     output = (unsigned char *) sodium_malloc(output_len);
39     h = (unsigned char *) sodium_malloc(32);
40 
41     for (i = 0; i < 8; i++) {
42         in[i] = noncesuffix[i];
43     }
44     for (; i < 16; i++) {
45         in[i] = 0;
46     }
47     do {
48         do {
49             crypto_core_salsa20(output + pos, in, secondkey, c);
50             pos += 64;
51             in[8]++;
52         } while (in[8] != 0);
53         in[9]++;
54     } while (in[9] != 0);
55 
56     crypto_hash_sha256(h, output, output_len);
57 
58     for (i = 0; i < 32; ++i) {
59         printf("%02x", h[i]);
60     }
61     printf("\n");
62 
63 #ifndef SODIUM_LIBRARY_MINIMAL
64     pos = 0;
65     do {
66         do {
67             crypto_core_salsa2012(output + pos, in, secondkey, c);
68             pos += 64;
69             in[8]++;
70         } while (in[8] != 0);
71         in[9]++;
72     } while (in[9] != 0);
73 
74     crypto_hash_sha256(h, output, output_len);
75 
76     for (i = 0; i < 32; ++i) {
77         printf("%02x", h[i]);
78     }
79     printf("\n");
80 
81     pos = 0;
82     do {
83         do {
84             crypto_core_salsa208(output + pos, in, secondkey, c);
85             pos += 64;
86             in[8]++;
87         } while (in[8] != 0);
88         in[9]++;
89     } while (in[9] != 0);
90 
91     crypto_hash_sha256(h, output, output_len);
92 
93     for (i = 0; i < 32; ++i) {
94         printf("%02x", h[i]);
95     }
96     printf("\n");
97 #else
98     printf("a4e3147dddd2ba7775939b50208a22eb3277d4e4bad8a1cfbc999c6bd392b638\n"
99            "017421baa9959cbe894bd003ec87938254f47c1e757eb66cf89c353d0c2b68de\n");
100 #endif
101 
102     sodium_free(h);
103     sodium_free(output);
104     sodium_free(in);
105     sodium_free(c);
106     sodium_free(noncesuffix);
107     sodium_free(secondkey);
108 
109     assert(crypto_core_salsa20_outputbytes() == crypto_core_salsa20_OUTPUTBYTES);
110     assert(crypto_core_salsa20_inputbytes() == crypto_core_salsa20_INPUTBYTES);
111     assert(crypto_core_salsa20_keybytes() == crypto_core_salsa20_KEYBYTES);
112     assert(crypto_core_salsa20_constbytes() == crypto_core_salsa20_CONSTBYTES);
113 
114     return 0;
115 }
116