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

make-vector.c (2108B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <inttypes.h>
      5 
      6 #include "lilliput-ae.h"
      7 
      8 
      9 void parse_arguments(
     10     size_t argc,
     11     char const* const* argv,
     12     size_t* plaintext_len,
     13     size_t* auth_len,
     14     FILE** output
     15 )
     16 {
     17     sscanf(argv[1], "%zu", plaintext_len);
     18     sscanf(argv[2], "%zu", auth_len);
     19 
     20     if (argc > 3)
     21         *output = fopen(argv[3], "w");
     22     else
     23         *output = stdout;
     24 }
     25 
     26 void init_buffer(size_t len, uint8_t buf[len])
     27 {
     28     for (size_t i=0; i<len; i++)
     29         buf[i] = i;
     30 }
     31 
     32 void dump_buffer(FILE* output, const char* header, size_t len, const uint8_t buf[len])
     33 {
     34     fprintf(output, "%s = { ", header);
     35     for (const uint8_t* b=buf; b<buf+len-1; b++)
     36         fprintf(output, "0x%02x, ", *b);
     37     fprintf(output, "0x%02x };\n", buf[len-1]);
     38 }
     39 
     40 int main(int argc, char const* const* argv)
     41 {
     42     if (argc < 3)
     43     {
     44         fprintf(stderr, "usage: %s PLAINTEXT-LEN AUTHDATA-LEN [OUTPUT]\n", argv[0]);
     45         return 1;
     46     }
     47 
     48     size_t plaintext_len;
     49     size_t auth_len;
     50     FILE* output;
     51     parse_arguments(argc, argv, &plaintext_len, &auth_len, &output);
     52 
     53     uint8_t plaintext[plaintext_len];
     54     uint8_t auth[auth_len];
     55     uint8_t key[KEY_BYTES];
     56     uint8_t nonce[NONCE_BYTES];
     57 
     58     memset(nonce, 0, NONCE_BYTES);
     59     init_buffer(plaintext_len, plaintext);
     60     init_buffer(auth_len, auth);
     61     init_buffer(KEY_BYTES, key);
     62 
     63     uint8_t ciphertext[plaintext_len+TAG_BYTES];
     64 
     65     lilliput_ae_encrypt(
     66         plaintext_len, plaintext, auth_len, auth,
     67         key, nonce, ciphertext, ciphertext+plaintext_len
     68     );
     69 
     70     fprintf(output, "#include <stdint.h>\n");
     71     fprintf(output, "#include \"test_vectors.h\"\n");
     72 
     73     dump_buffer(output, "const uint8_t expectedPlaintext[MAXTEST_BYTES_M]", plaintext_len, plaintext);
     74     dump_buffer(output, "const uint8_t expectedAssociated[MAXTEST_BYTES_AD]", auth_len, auth);
     75     dump_buffer(output, "const uint8_t expectedKey[KEY_SIZE]", KEY_BYTES, key);
     76     dump_buffer(output, "const uint8_t expectedCiphertext[MAXTEST_BYTES_M+CRYPTO_ABYTES]", plaintext_len+TAG_BYTES, ciphertext);
     77 
     78     fclose(output);
     79 }