summaryrefslogtreecommitdiff
path: root/crypto_aead/lilliputaei256v1/ref/test/test-ae-roundtrip.c
blob: cd2ea42f6ffc40fd221333a46004567155e89522 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "lilliput-ae.h"

#include "test-helpers.h"


struct vector
{
    char *name;
    uint8_t key[KEY_BYTES];
    uint8_t nonce[NONCE_BYTES];
    size_t auth_len;
    uint8_t *auth;
    size_t message_len;
    uint8_t *message;
};

typedef struct vector vector;


/* Keys and nonces generated with /dev/urandom. */

const vector VECTORS[] = {
    {
        .name = "short",
        .key = {
            0x69, 0x5a, 0x10, 0x7a, 0x34, 0xb6, 0x80, 0xc1,
            0x54, 0x85, 0xca, 0xc0, 0xfb, 0xf6, 0x09, 0x4c,
            0x9d, 0xd7, 0x04, 0xfa, 0x1a, 0xb7, 0xb9, 0x22,
            0x52, 0xea, 0x36, 0xd3, 0x12, 0x51, 0x3f, 0x86
        },
        .nonce = {
            0x85, 0xc6, 0x6b, 0xf4, 0x82, 0x99, 0x3a, 0x14,
            0x87, 0x7e, 0x45, 0x48, 0xe4, 0x51, 0x6c
        },
        .auth_len = 8,
        .auth = (uint8_t*)"deadbeef",
        .message_len = 4,
        .message = (uint8_t[]){
            0xde, 0xad, 0xbe, 0xef
        }
    },
    {
        .name = "block-sized",
        .key = {
            0x9c, 0x59, 0x6a, 0xfb, 0x07, 0x67, 0xd7, 0x52,
            0xae, 0xfb, 0xde, 0x3f, 0x9b, 0x68, 0x69, 0x60,
            0x22, 0x48, 0x77, 0x95, 0x6f, 0xba, 0x5e, 0x17,
            0x25, 0x2a, 0xa0, 0x7f, 0x0e, 0xd8, 0xc3, 0x16
        },
        .nonce = {
            0x2f, 0xae, 0xfb, 0xa5, 0xd0, 0xc8, 0x2c, 0xc2,
            0xb5, 0x16, 0x2e, 0xcc, 0xf8, 0x4f, 0xc8
        },
        .auth_len = 13,
        .auth = (uint8_t*)"some metadata",
        .message_len = 2*BLOCK_BYTES,
        .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES"
    },
    {
        .name = "arbitrarily long",
        .key = {
            0xe3, 0x95, 0x7d, 0xc3, 0xc3, 0x29, 0x81, 0x0a,
            0x06, 0x20, 0x33, 0xf9, 0x05, 0x6e, 0xb0, 0x45,
            0x81, 0x33, 0x64, 0x7d, 0x9f, 0x31, 0x8f, 0x98,
            0x1c, 0x97, 0x06, 0x95, 0x6a, 0xe9, 0x93, 0x54
        },
        .nonce = {
            0x34, 0x87, 0x0f, 0xf9, 0x75, 0x49, 0x4c, 0x02,
            0x6c, 0xac, 0x50, 0xfc, 0xc8, 0xe9, 0xed
        },
        .auth_len = 30,
        .auth = (uint8_t*)"a bunch of associated metadata",
        .message_len = 59,
        .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux"
    }
};


int main()
{
    int diff = 0;

    for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++)
    {
        uint8_t ciphertext[v->message_len];
        uint8_t tag[TAG_BYTES];

        lilliput_ae_encrypt(
            v->message_len, v->message,
            v->auth_len, v->auth,
            v->key, v->nonce,
            ciphertext,
            tag
        );

        uint8_t deciphered[v->message_len];
        bool valid = lilliput_ae_decrypt(
            v->message_len, ciphertext,
            v->auth_len, v->auth,
            v->key, v->nonce, tag,
            deciphered
        );

        if (!valid)
        {
            REPORT_INVALID(v->name);
            diff++;
            continue;
        }

        if (memcmp(deciphered, v->message, v->message_len) != 0)
        {
            REPORT_DIFFERENCE(v->name, "deciphered plaintext");
            diff++;
            continue;
        }
    }

    return diff;
}