File: crypto_stream_chacha20test.c

package info (click to toggle)
tinyssh 20190101-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,208 kB
  • sloc: ansic: 11,914; sh: 1,005; python: 385; makefile: 18
file content (59 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
20140709
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include "misc.h"
#include "crypto_stream_chacha20.h"

static unsigned char space[5232];
static unsigned char k[crypto_stream_chacha20_KEYBYTES + 16];
static unsigned char n[crypto_stream_chacha20_NONCEBYTES + 16];

static unsigned char o[32] = {
    0x98, 0x43, 0x28, 0xe2, 0x76, 0xc2, 0xc3, 0xa1, 
    0xe8, 0xf3, 0x50, 0x98, 0x15, 0xc4, 0xea, 0xa6, 
    0x2c, 0x54, 0x87, 0x1b, 0x02, 0x4c, 0x61, 0xd1, 
    0x01, 0xd9, 0xf0, 0xb1, 0xd5, 0x21, 0xc4, 0x7a
};

static void test_alignment(void) {

    long long i;

    for (i = 0; i < 16; ++i) {
        crypto_stream_chacha20_xor(space + i, space + i, sizeof space - i, n + i, k + i);
    }
}

static void test_rand(void) {

    long long i, j;
    unsigned int u;

    pseudorandombytes(space, sizeof space);
    pseudorandombytes(k, sizeof k);
    pseudorandombytes(n, sizeof n);

    for (i = 0; i < sizeof space; i += 1 + i / 16) {
        u = 1;
        for (j = 0; j < 8; ++j) {
            u += (unsigned int) n[j];
            n[j] = u;
            u >>= 8;
        }
        crypto_stream_chacha20_xor(space, space, i, n, k);
    }
    checksum(space, sizeof space);
    fail_whenbadchecksum(o);
}


int main(void) {

    test_alignment();
    test_rand();
    _exit(0);
}