Commit 95f839af by Enrico Pozzobon

first scripts

parents
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
);
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#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;
}
}
}
#!/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))
#!/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("<BI", ord(action), len(data))
write(h)
write(data)
def obtain():
l = read(4)
(l, ) = struct.unpack("<I", l)
return read(l)
if read(14) != b"Hello, World!\n":
raise Exception("Unexpected output")
submit('c', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
submit('m', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
submit('a', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
submit('k', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
submit('p', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
submit('s', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
write(b'e')
write(b'C')
data = obtain()
print(data.hex())
if __name__ == "__main__":
sys.exit(main(sys.argv))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment