quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

stream.c (2773B)


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