summaryrefslogtreecommitdiff
path: root/crypto_aead/lilliputaei128v1/ref/lilliput-ae-i.c
blob: f67be075b66ad42dc0362482f12d915304ed8417 (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
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include "cipher.h"
#include "lilliput-ae.h"


static void _lilliput_tbc(const uint8_t key[KEY_BYTES],
                          const uint8_t tweak[TWEAK_BYTES],
                          const uint8_t message[BLOCK_BYTES],
                          uint8_t ciphertext[BLOCK_BYTES])
{
    lilliput_tbc_encrypt(key, tweak, message, ciphertext, NULL);
}

static void _xor_into(size_t len, uint8_t dest[len], uint8_t src[len])
{
    for (size_t i=0; i<len; i++)
        dest[i] ^= src[i];
}

static void _process_associated_data(
    const uint8_t key[KEY_BYTES],
    size_t auth_data_len, const uint8_t auth_data[auth_data_len],
    uint8_t auth[BLOCK_BYTES]
)
{
    size_t l_a = auth_data_len / BLOCK_BYTES;

    memset(auth, 0, BLOCK_BYTES);

    for (size_t i=0; i<l_a; i++)
    {
        uint8_t tweak[TWEAK_BYTES];
        /* TODO: generate tweak */
        uint8_t Ek_Ai[BLOCK_BYTES];
        _lilliput_tbc(key, tweak, auth_data+i*BLOCK_BYTES, Ek_Ai);
        _xor_into(BLOCK_BYTES, auth, Ek_Ai);
    }
}

static void _encrypt_message(
    const uint8_t key[KEY_BYTES],
    size_t message_len, const uint8_t message[message_len],
    const uint8_t nonce[NONCE_BYTES],

    size_t *ciphertext_len, uint8_t ciphertext[message_len+BLOCK_BYTES],
    uint8_t final[BLOCK_BYTES]
)
{
}

static void _decrypt_message(
    const uint8_t key[KEY_BYTES],
    size_t ciphertext_len, const uint8_t ciphertext[ciphertext_len],
    const uint8_t nonce[NONCE_BYTES],

    size_t *message_len, uint8_t message[ciphertext_len],
    uint8_t final[BLOCK_BYTES]
)
{
}

static void _generate_tag(
    const uint8_t final[BLOCK_BYTES],
    const uint8_t auth[BLOCK_BYTES],
    uint8_t tag[TAG_BYTES]
)
{
}


void lilliput_ae_encrypt(
    size_t message_len,    const uint8_t message[message_len],
    size_t  auth_data_len, const uint8_t auth_data[auth_data_len],
    const uint8_t key[KEY_BYTES],
    const uint8_t nonce[NONCE_BYTES],

    size_t *ciphertext_len, uint8_t ciphertext[message_len+BLOCK_BYTES],
    uint8_t tag[TAG_BYTES]
)
{
    uint8_t auth[BLOCK_BYTES];
    _process_associated_data(key, auth_data_len, auth_data, auth);

    uint8_t final[BLOCK_BYTES];
    _encrypt_message(key, message_len, message, nonce,
                     ciphertext_len, ciphertext, final);

    _generate_tag(final, auth, tag);
}

bool lilliput_ae_decrypt(
    size_t ciphertext_len, const uint8_t ciphertext[ciphertext_len],
    size_t auth_data_len,  const uint8_t auth_data[auth_data_len],
    const uint8_t key[KEY_BYTES],
    const uint8_t nonce[NONCE_BYTES],
    const uint8_t tag[TAG_BYTES],

    size_t *message_len, uint8_t message[ciphertext_len]
)
{
    uint8_t auth[BLOCK_BYTES];
    _process_associated_data(key, auth_data_len, auth_data, auth);

    uint8_t final[BLOCK_BYTES];
    _decrypt_message(key, ciphertext_len, ciphertext, nonce,
                     message_len, message, final);

    uint8_t effective_tag[TAG_BYTES];
    _generate_tag(final, auth, effective_tag);

    return memcmp(tag, effective_tag, TAG_BYTES) == 0;
}