summaryrefslogtreecommitdiff
path: root/test/i-128/test-ae-encrypt.c
blob: 06f3b8822eb21166de2f76653fa3212ac5218c6c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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;
    uint8_t *ciphertext;
    uint8_t tag[TAG_BYTES];
};

typedef struct vector vector;


const vector VECTORS[] = {
    {
        .name = "order",
        .key = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
        },
        .nonce = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
        },
        .auth_len = 64,
        .auth = (uint8_t[]) {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
            0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
            0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
            0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
        },
        .message_len = 64,
        .message = (uint8_t[]) {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
            0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
            0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
            0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
        },
        .ciphertext = (uint8_t[]) {
            0xca, 0x16, 0x03, 0x71, 0x0c, 0xca, 0x10, 0x2f,
            0xac, 0x46, 0x0c, 0xb4, 0x13, 0x2a, 0x55, 0x0d,
            0xc0, 0x59, 0x90, 0xb7, 0xca, 0xf2, 0x17, 0x9f,
            0x8f, 0xd6, 0x5a, 0x73, 0x49, 0x65, 0x14, 0xec,
            0x3e, 0xf4, 0xc5, 0xd0, 0x08, 0x3d, 0x85, 0x5c,
            0x11, 0x70, 0x42, 0x3b, 0x91, 0x61, 0xa7, 0xf2,
            0x7a, 0xbf, 0x51, 0x2c, 0xe3, 0x88, 0xc9, 0x97,
            0x34, 0x92, 0xed, 0xf9, 0x5b, 0xb4, 0x2a, 0x80
        },
        .tag = {
            0x47, 0x5b, 0x2c, 0x0e, 0x5f, 0xcf, 0x6f, 0xc0,
            0xab, 0x3c, 0x24, 0xc1, 0x66, 0x88, 0x83, 0x38
        }
    },
    {
        .name = "order-padded",
        .key = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
        },
        .nonce = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
        },
        .auth_len = 66,
        .auth = (uint8_t[]) {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
            0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
            0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
            0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
            0x40, 0x01
        },
        .message_len = 66,
        .message = (uint8_t[]) {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
            0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
            0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
            0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
            0x40, 0x01
        },
        .ciphertext = (uint8_t[]) {
            0xca, 0x16, 0x03, 0x71, 0x0c, 0xca, 0x10, 0x2f,
            0xac, 0x46, 0x0c, 0xb4, 0x13, 0x2a, 0x55, 0x0d,
            0xc0, 0x59, 0x90, 0xb7, 0xca, 0xf2, 0x17, 0x9f,
            0x8f, 0xd6, 0x5a, 0x73, 0x49, 0x65, 0x14, 0xec,
            0x3e, 0xf4, 0xc5, 0xd0, 0x08, 0x3d, 0x85, 0x5c,
            0x11, 0x70, 0x42, 0x3b, 0x91, 0x61, 0xa7, 0xf2,
            0x7a, 0xbf, 0x51, 0x2c, 0xe3, 0x88, 0xc9, 0x97,
            0x34, 0x92, 0xed, 0xf9, 0x5b, 0xb4, 0x2a, 0x80,
            0x27, 0x3f
        },
        .tag = {
            0xd4, 0xef, 0x1f, 0x11, 0x81, 0x90, 0xbe, 0xfd,
            0x1e, 0xa7, 0x32, 0xbe, 0x63, 0xe2, 0x18, 0x90
        }
    }
};


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
        );

        if (memcmp(ciphertext, v->ciphertext, v->message_len) != 0)
        {
            REPORT_DIFFERENCE(v->name, "ciphertext");
            dump_c_initializer(v->message_len, ciphertext);
            diff++;
        }

        if (memcmp(tag, v->tag, TAG_BYTES) != 0)
        {
            REPORT_DIFFERENCE(v->name, "tag");
            dump_c_initializer(TAG_BYTES, tag);
            diff++;
        }
    }

    return diff;
}