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

test-ae-roundtrip.c (3055B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 #include <string.h>
      4 
      5 #include "lilliput-ae.h"
      6 
      7 #include "test-helpers.h"
      8 
      9 
     10 struct vector
     11 {
     12     char *name;
     13     uint8_t key[KEY_BYTES];
     14     uint8_t nonce[NONCE_BYTES];
     15     size_t auth_len;
     16     uint8_t *auth;
     17     size_t message_len;
     18     uint8_t *message;
     19 };
     20 
     21 typedef struct vector vector;
     22 
     23 
     24 /* Keys and nonces generated with /dev/urandom. */
     25 
     26 const vector VECTORS[] = {
     27     {
     28         .name = "short",
     29         .key = {
     30             0x06, 0xf4, 0x60, 0x05, 0x12, 0x4b, 0xfd, 0xda,
     31             0x8f, 0xfc, 0x55, 0x1c, 0xaa, 0xef, 0x81, 0x60,
     32             0x08, 0xb3, 0xe0, 0xa6, 0xad, 0xcb, 0x68, 0x05
     33         },
     34         .nonce = {
     35             0xcd, 0x6f, 0x24, 0xe1, 0xf8, 0xcd, 0x64, 0xde,
     36             0x18, 0x2f, 0x92, 0xab, 0xdb, 0xfa, 0xff
     37         },
     38         .auth_len = 8,
     39         .auth = (uint8_t*)"deadbeef",
     40         .message_len = 4,
     41         .message = (uint8_t[]){
     42             0xde, 0xad, 0xbe, 0xef
     43         }
     44     },
     45     {
     46         .name = "block-sized",
     47         .key = {
     48             0xa0, 0x3d, 0x36, 0xef, 0xef, 0xdd, 0x72, 0xad,
     49             0x35, 0xa1, 0x2b, 0xe2, 0x3d, 0x1e, 0x43, 0x94,
     50             0x38, 0x0f, 0x00, 0x4d, 0x8d, 0x78, 0xbc, 0x02
     51         },
     52         .nonce = {
     53             0xcd, 0x7d, 0xb0, 0xa0, 0x62, 0xdf, 0xda, 0x0a,
     54             0x23, 0x7a, 0x17, 0x32, 0x60, 0x42, 0xef
     55         },
     56         .auth_len = 13,
     57         .auth = (uint8_t*)"some metadata",
     58         .message_len = 2*BLOCK_BYTES,
     59         .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES"
     60     },
     61     {
     62         .name = "arbitrarily long",
     63         .key = {
     64             0x96, 0x6f, 0x23, 0x89, 0xb4, 0xa4, 0x00, 0x7c,
     65             0x9a, 0x9f, 0x34, 0x01, 0xb0, 0x73, 0x27, 0x56,
     66             0x09, 0xce, 0x17, 0xeb, 0xdc, 0xd6, 0xa7, 0x06
     67         },
     68         .nonce = {
     69             0x59, 0x41, 0xa7, 0x53, 0x0f, 0xde, 0xf1, 0xb1,
     70             0xca, 0xd5, 0x80, 0xc4, 0x1c, 0x16, 0x2b
     71         },
     72         .auth_len = 30,
     73         .auth = (uint8_t*)"a bunch of associated metadata",
     74         .message_len = 59,
     75         .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux"
     76     }
     77 };
     78 
     79 
     80 int main()
     81 {
     82     int diff = 0;
     83 
     84     for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++)
     85     {
     86         uint8_t ciphertext[v->message_len];
     87         uint8_t tag[TAG_BYTES];
     88 
     89         lilliput_ae_encrypt(
     90             v->message_len, v->message,
     91             v->auth_len, v->auth,
     92             v->key, v->nonce,
     93             ciphertext,
     94             tag
     95         );
     96 
     97         uint8_t deciphered[v->message_len];
     98         bool valid = lilliput_ae_decrypt(
     99             v->message_len, ciphertext,
    100             v->auth_len, v->auth,
    101             v->key, v->nonce, tag,
    102             deciphered
    103         );
    104 
    105         if (!valid)
    106         {
    107             REPORT_INVALID(v->name);
    108             diff++;
    109             continue;
    110         }
    111 
    112         if (memcmp(deciphered, v->message, v->message_len) != 0)
    113         {
    114             REPORT_DIFFERENCE(v->name, "deciphered plaintext");
    115             diff++;
    116             continue;
    117         }
    118     }
    119 
    120     return diff;
    121 }