From 95f839afcc91490abd46ca8274112bb9f528acbf Mon Sep 17 00:00:00 2001 From: Enrico Pozzobon Date: Wed, 3 Jul 2019 00:32:03 +0200 Subject: [PATCH] first scripts --- .gitignore | 1 + assets/crypto_aead.h | 19 +++++++++++++++++++ assets/main.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ get_algos.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 286 insertions(+) create mode 100644 .gitignore create mode 100644 assets/crypto_aead.h create mode 100644 assets/main.c create mode 100755 get_algos.py create mode 100755 test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/assets/crypto_aead.h b/assets/crypto_aead.h new file mode 100644 index 0000000..eafabff --- /dev/null +++ b/assets/crypto_aead.h @@ -0,0 +1,19 @@ +int crypto_aead_encrypt( + unsigned char *c,unsigned long long *clen, + const unsigned char *m,unsigned long long mlen, + const unsigned char *ad,unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const unsigned char *k + ); + + +int crypto_aead_decrypt( + unsigned char *m,unsigned long long *outputmlen, + unsigned char *nsec, + const unsigned char *c,unsigned long long clen, + const unsigned char *ad,unsigned long long adlen, + const unsigned char *npub, + const unsigned char *k + ); + diff --git a/assets/main.c b/assets/main.c new file mode 100644 index 0000000..bb43415 --- /dev/null +++ b/assets/main.c @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include "crypto_aead.h" + +#ifndef KEY_LENGTH +#define KEY_LENGTH 16 +#endif + +#ifndef NSEC_LENGTH +#define NSEC_LENGTH 0 +#endif + +#ifndef NPUB_LENGTH +#define NPUB_LENGTH 0 +#endif + +unsigned char *c = NULL; +unsigned long long clen = 0; +unsigned char *m = NULL; +unsigned long long mlen = 0; +unsigned char *ad = NULL; +unsigned long long adlen = 0; +unsigned char *nsec = NULL; + +unsigned long long nslen = NSEC_LENGTH; +unsigned char *npub = NULL; +unsigned long long nplen = NPUB_LENGTH; +unsigned char *k = NULL; +unsigned long long klen = KEY_LENGTH; + +static void read_variable(unsigned char **target, unsigned long long *lenp) { + if (*target != NULL) { + free(*target); + } + + uint32_t len; + if (1 != fread(&len, sizeof(len), 1, stdin)) { + fprintf(stderr, "ERROR: couldn't read length\r\n"); + exit(1); + } + + *lenp = len; + if (*lenp == 0) { + *target = NULL; + return; + } + + *target = malloc(*lenp); + + if (NULL == *target) { + fprintf(stderr, "ERROR: couldn't malloc %llu bytes\r\n", *lenp); + exit(2); + } + + if (1 != fread(*target, *lenp, 1, stdin)) { + fprintf(stderr, "ERROR: didn't read %llu bytes of data\r\n", *lenp); + exit(1); + } +} + + +static void write_variable(unsigned char *target, unsigned long long len) { + uint32_t olen = len; + if (1 != fwrite(&olen, sizeof(olen), 1, stdout)) { + fprintf(stderr, "ERROR: didn't write length\r\n", len); + exit(1); + } + if (1 != fwrite(target, olen, 1, stdout)) { + fprintf(stderr, "ERROR: didn't write %llu bytes of data\r\n", len); + exit(1); + } +} + + +static bool is_ready() { + return !(k == NULL || m == NULL || c == NULL + || ad == NULL || nsec == NULL || npub == NULL); +} + + +int main() { + int res; + int8_t action; + + setvbuf(stdout, NULL, _IONBF, 0); + printf("Hello, World!\n"); + while (1) { + if (1 != fread(&action, sizeof(action), 1, stdin)) + return 1; + + switch (action) { + case 'c': read_variable(&c, &clen); break; + case 'm': read_variable(&m, &mlen); break; + case 'a': read_variable(&ad, &adlen); break; + case 'k': read_variable(&k, &klen); break; + + case 's': read_variable(&nsec, &nslen); break; + case 'p': read_variable(&npub, &nplen); break; + + case 'C': write_variable(c, clen); break; + case 'M': write_variable(m, mlen); break; + case 'A': write_variable(ad, adlen); break; + case 'K': write_variable(k, klen); break; + case 'S': write_variable(nsec, nslen); break; + case 'P': write_variable(npub, nplen); break; + + + case 'e': + if (!is_ready()) { + return 3; + } + fprintf(stderr, "Starting encryption\r\n"); + res = crypto_aead_encrypt(c, &clen, + m, mlen, ad, adlen, nsec, npub, k); + fprintf(stderr, "encryption finished\r\n"); + break; + + case 'd': + if (!is_ready()) { + return 3; + } + fprintf(stderr, "Starting decryption\r\n"); + res = crypto_aead_decrypt(m, &mlen, + nsec, c, clen, ad, adlen, npub, k); + fprintf(stderr, "decryption finished\r\n"); + break; + + default: + fprintf(stderr, "ERROR: unexpected action 0x%02x\r\n", action); + return 2; + } + + } + +} + diff --git a/get_algos.py b/get_algos.py new file mode 100755 index 0000000..3582b2c --- /dev/null +++ b/get_algos.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +import os +import sys +import shutil +import subprocess + +def bench(algo_dir): + print(algo_dir) + build_dir = "build" + assets_dir = "assets" + while os.path.isdir(build_dir): + shutil.rmtree(build_dir) + shutil.copytree(algo_dir, build_dir) + + hs = os.path.join(assets_dir, "crypto_aead.h") + h = os.path.join(build_dir, "crypto_aead.h") + shutil.copy(hs, h) + + ms = os.path.join(assets_dir, "main.c") + m = os.path.join(build_dir, "main.c") + shutil.copy(ms, m) + + c = os.path.join(build_dir, "genkat_aead.c") + if os.path.exists(c): + os.remove(c) + + wd = os.getcwd() + try: + os.chdir(build_dir) + + cfiles = [] + for r, d, f in os.walk("."): + for file in f: + if file.endswith(".c"): + cfiles.append(file) + + pargs = ['gcc'] + pargs.extend(cfiles) + pargs.extend(["-o", "test"]) + p = subprocess.Popen(pargs) + compout, comperr = p.communicate() + print(compout) + print("---") + print(comperr) + + finally: + os.chdir(wd) + # shutil.rmtree(build_dir) + + exit(0) # stop immediately for now + + + +def main(*argv): + submissions_dir = "all-lwc-submission-files" + subs = os.listdir(submissions_dir) + + files = set() + for submission in subs: + implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead") + + # r=root, d=directories, f = files + for r, d, f in os.walk(implementations_dir): + for file in f: + if file == "encrypt.c": + f = os.path.join(r, file) + d = os.path.split(f)[0] + bench(d) + assert os.path.isdir(d) + files.add(d) + print() + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/test.py b/test.py new file mode 100755 index 0000000..2b6f373 --- /dev/null +++ b/test.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import os +import sys +import struct +from subprocess import Popen, PIPE + + + + +def main(argv): + p = Popen(["build/test"], bufsize=0, stdin=PIPE, stdout=PIPE) + + def write(data): + l = p.stdin.write(data) + if len(data) != l: + raise Exception("could not write %d bytes of data (put %d)" % (len(data), l)) + + def read(l): + data = p.stdout.read(l) + if len(data) != l: + raise Exception("could not read %d bytes of data (got %d)" % (l, len(data))) + return data + + def submit(action, data): + h = struct.pack("