lilliput-ae-reference-implementation

Implementations of Lilliput-AE submitted to the NIST LWC standardization process
git clone https://git.kevinlegouguec.net/lilliput-ae-reference-implementation
Log | Files | Refs | README

traces-ae.c (4803B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 
      4 #include "lilliput-ae.h"
      5 
      6 #include "debug.h"
      7 #include "test-helpers.h"
      8 
      9 
     10 FILE *DUMP;
     11 
     12 
     13 struct vector
     14 {
     15     char *name;
     16     uint8_t key[KEY_BYTES];
     17     uint8_t nonce[NONCE_BYTES];
     18     size_t auth_len;
     19     uint8_t *auth;
     20     size_t message_len;
     21     uint8_t *message;
     22 };
     23 
     24 typedef struct vector vector;
     25 
     26 
     27 const vector VECTORS[] = {
     28     {
     29         .name = "order",
     30         .key = {
     31             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     32             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     33             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     34             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
     35         },
     36         .nonce = {
     37             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     38             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
     39         },
     40         .auth_len = 64,
     41         .auth = (uint8_t[]) {
     42             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     43             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     44             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     45             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
     46             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
     47             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
     48             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     49             0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
     50         },
     51         .message_len = 64,
     52         .message = (uint8_t[]) {
     53             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     54             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     55             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     56             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
     57             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
     58             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
     59             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     60             0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
     61         }
     62     },
     63     {
     64         .name = "order-padded",
     65         .key = {
     66             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     67             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     68             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     69             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
     70         },
     71         .nonce = {
     72             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     73             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
     74         },
     75         .auth_len = 66,
     76         .auth = (uint8_t[]) {
     77             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     78             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     79             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     80             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
     81             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
     82             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
     83             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     84             0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
     85             0x40, 0x41
     86         },
     87         .message_len = 66,
     88         .message = (uint8_t[]) {
     89             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     90             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     91             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     92             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
     93             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
     94             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
     95             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     96             0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
     97             0x40, 0x41
     98         }
     99     }
    100 };
    101 
    102 
    103 int main(int argc, char **argv)
    104 {
    105     if (argc < 3)
    106     {
    107         fprintf(stderr, "usage: %s OUTPUT-FOLDER PREFIX\n", argv[0]);
    108         return 1;
    109     }
    110 
    111     for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++)
    112     {
    113         debug_open_dump(argv[1], argv[2], v->name);
    114         debug_dump_buffer("message", v->message_len, v->message, 0);
    115         debug_dump_buffer("associated data", v->auth_len, v->auth, 0);
    116         debug_dump_buffer("key", KEY_BYTES, v->key, 0);
    117         debug_dump_buffer("nonce", NONCE_BYTES, v->nonce, 0);
    118 
    119         uint8_t ciphertext[v->message_len];
    120         uint8_t tag[TAG_BYTES];
    121 
    122         lilliput_ae_encrypt(
    123             v->message_len, v->message,
    124             v->auth_len, v->auth,
    125             v->key, v->nonce,
    126             ciphertext,
    127             tag
    128         );
    129 
    130         debug_dump_buffer("ciphertext", v->message_len, ciphertext, 0);
    131         debug_dump_buffer("tag", TAG_BYTES, tag, 0);
    132 
    133         fprintf(DUMP, "DECRYPTING\n");
    134 
    135         uint8_t cleartext[v->message_len];
    136         lilliput_ae_decrypt(
    137             sizeof(ciphertext), ciphertext,
    138             v->auth_len, v->auth,
    139             v->key, v->nonce,
    140             tag,
    141             cleartext
    142         );
    143 
    144         debug_dump_buffer("cleartext", sizeof(cleartext), cleartext, 0);
    145 
    146         fclose(DUMP);
    147     }
    148 }