summaryrefslogtreecommitdiff
path: root/test/felics/make-vector.c
blob: b222f9036714c90c4398bdce6895c3ddc3fa6e36 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "lilliput-ae.h"


void parse_arguments(
    size_t argc,
    char const* const* argv,
    size_t* plaintext_len,
    size_t* auth_len,
    FILE** output
)
{
    sscanf(argv[1], "%zu", plaintext_len);
    sscanf(argv[2], "%zu", auth_len);

    if (argc > 3)
        *output = fopen(argv[3], "w");
    else
        *output = stdout;
}

void init_buffer(size_t len, uint8_t buf[len])
{
    for (size_t i=0; i<len; i++)
        buf[i] = i;
}

void dump_buffer(FILE* output, const char* header, size_t len, const uint8_t buf[len])
{
    fprintf(output, "%s = { ", header);
    for (const uint8_t* b=buf; b<buf+len-1; b++)
        fprintf(output, "0x%02x, ", *b);
    fprintf(output, "0x%02x };\n", buf[len-1]);
}

int main(int argc, char const* const* argv)
{
    if (argc < 3)
    {
        fprintf(stderr, "usage: %s PLAINTEXT-LEN AUTHDATA-LEN [OUTPUT]\n", argv[0]);
        return 1;
    }

    size_t plaintext_len;
    size_t auth_len;
    FILE* output;
    parse_arguments(argc, argv, &plaintext_len, &auth_len, &output);

    uint8_t plaintext[plaintext_len];
    uint8_t auth[auth_len];
    uint8_t key[KEY_BYTES];
    uint8_t nonce[NONCE_BYTES];

    memset(nonce, 0, NONCE_BYTES);
    init_buffer(plaintext_len, plaintext);
    init_buffer(auth_len, auth);
    init_buffer(KEY_BYTES, key);

    uint8_t ciphertext[plaintext_len+TAG_BYTES];

    lilliput_ae_encrypt(
        plaintext_len, plaintext, auth_len, auth,
        key, nonce, ciphertext, ciphertext+plaintext_len
    );

    fprintf(output, "#include <stdint.h>\n");
    fprintf(output, "#include \"test_vectors.h\"\n");

    dump_buffer(output, "const uint8_t expectedPlaintext[MAXTEST_BYTES_M]", plaintext_len, plaintext);
    dump_buffer(output, "const uint8_t expectedAssociated[MAXTEST_BYTES_AD]", auth_len, auth);
    dump_buffer(output, "const uint8_t expectedKey[KEY_SIZE]", KEY_BYTES, key);
    dump_buffer(output, "const uint8_t expectedCiphertext[MAXTEST_BYTES_M+CRYPTO_ABYTES]", plaintext_len+TAG_BYTES, ciphertext);

    fclose(output);
}