diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/common.mk | 66 | ||||
| -rw-r--r-- | test/i-128/Makefile | 21 | ||||
| -rw-r--r-- | test/i-128/test-ae-decrypt.c | 156 | ||||
| -rw-r--r-- | test/i-128/test-ae-encrypt.c | 158 | ||||
| -rw-r--r-- | test/i-128/test-ae-roundtrip.c | 118 | ||||
| -rw-r--r-- | test/i-128/test-tbc-decrypt.c | 84 | ||||
| -rw-r--r-- | test/i-128/test-tbc-encrypt.c | 84 | ||||
| -rw-r--r-- | test/i-128/test-tweakey.c | 113 | ||||
| -rw-r--r-- | test/i-192/Makefile | 12 | ||||
| -rw-r--r-- | test/i-192/test-ae-roundtrip.c | 121 | ||||
| -rw-r--r-- | test/i-256/Makefile | 12 | ||||
| -rw-r--r-- | test/i-256/test-ae-roundtrip.c | 124 | ||||
| -rw-r--r-- | test/ii-128/Makefile | 12 | ||||
| -rw-r--r-- | test/ii-128/test-ae-roundtrip.c | 118 | ||||
| -rw-r--r-- | test/ii-192/Makefile | 12 | ||||
| -rw-r--r-- | test/ii-192/test-ae-roundtrip.c | 121 | ||||
| -rw-r--r-- | test/ii-256/Makefile | 12 | ||||
| -rw-r--r-- | test/ii-256/test-ae-roundtrip.c | 124 |
18 files changed, 1468 insertions, 0 deletions
diff --git a/test/common.mk b/test/common.mk new file mode 100644 index 0000000..6d4cb28 --- /dev/null +++ b/test/common.mk @@ -0,0 +1,66 @@ +# use "make VERBOSE=1" to have full commands printed out. +VERBOSE = 0 +ifeq ($(VERBOSE),1) +Q = +else +Q = @ +endif + + +test_dir = $(dir $(lastword $(MAKEFILE_LIST))) +root_dir = $(test_dir)/.. +results_dir = $(root_dir)/results +src_dir = $(root_dir)/src + + +nist_flags = -std=c99 -Wall -Wextra -Wshadow -fsanitize=address,undefined -O2 +CFLAGS += -I$(src_dir) -I$(variant_dir) $(nist_flags) -Werror +LDFLAGS += $(nist_flags) + + +.PHONY: test + + +$(results_dir): + @ mkdir -p $@ + +$(results_dir)/%.o: $(src_dir)/%.c + @ mkdir -p $(dir $@) + @ echo "CC $@" + $(Q) gcc -c $< $(CFLAGS) -o $@ + +$(results_dir)/test-%: $(results_dir)/test/test-%.o + @ echo "LD $@" + $(Q) gcc $^ $(LDFLAGS) -o $@ + +$(results_dir)/traces-%: $(results_dir)/test/traces-%.o + @ echo "LD $@" + $(Q) gcc $^ $(LDFLAGS) -o $@ + + +$(results_dir)/$(src_dir)/cipher.o: $(src_dir)/cipher.h \ +$(src_dir)/tweakey.h $(src_dir)/constants.h $(src_dir)/parameters.h \ +$(variant_dir)/_parameters.h + +$(results_dir)/$(src_dir)/constants.o: $(src_dir)/constants.h + +$(results_dir)/$(src_dir)/lilliput-ae-i.o: $(src_dir)/lilliput-ae.h \ +$(src_dir)/cipher.h $(src_dir)/parameters.h \ +$(variant_dir)/_parameters.h + +$(results_dir)/$(src_dir)/lilliput-ae-ii.o: $(src_dir)/lilliput-ae.h \ +$(src_dir)/cipher.h $(src_dir)/parameters.h \ +$(variant_dir)/_parameters.h + +$(results_dir)/$(src_dir)/tweakey.o: $(src_dir)/tweakey.h \ +$(src_dir)/constants.h $(src_dir)/parameters.h \ +$(variant_dir)/_parameters.h + +$(results_dir)/test-*.o: $(src_dir)/test-helpers.h \ +$(src_dir)/parameters.h $(variant_dir)/_parameters.h + + +# TODO: should add order-only prerequisites to remove mkdirs inside recipes +# TODO: add valgrind, although it does not seem to play well with ASAN +# TODO: should use gcc -M... to generate .o -> .h dependencies +# TODO: move debug.h and test-helpers.h here diff --git a/test/i-128/Makefile b/test/i-128/Makefile new file mode 100644 index 0000000..3eb0a50 --- /dev/null +++ b/test/i-128/Makefile @@ -0,0 +1,21 @@ +mode = i +keylen = 128 + +include ../common.mk + +results/test-ae-decrypt: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results +results/test-ae-encrypt: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results +results/test-ae-roundtrip: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results +results/test-tbc-decrypt: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results +results/test-tbc-encrypt: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results +results/test-tweakey: results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-128-i: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-128-i: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-decrypt.o: src/lilliput-ae.h +results/test-ae-encrypt.o: src/lilliput-ae.h +results/test-ae-roundtrip.o: src/lilliput-ae.h +results/test-tbc-decrypt.o: src/cipher.h +results/test-tbc-encrypt.o: src/cipher.h +results/test-tweakey.o: src/tweakey.h diff --git a/test/i-128/test-ae-decrypt.c b/test/i-128/test-ae-decrypt.c new file mode 100644 index 0000000..8ae6308 --- /dev/null +++ b/test/i-128/test-ae-decrypt.c @@ -0,0 +1,156 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + uint8_t *ciphertext; + size_t ciphertext_len; + uint8_t tag[TAG_BYTES]; + uint8_t *message; +}; + +typedef struct vector vector; + + +const vector VECTORS[] = { + { + .name = "order", + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .nonce = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e + }, + .auth_len = 64, + .auth = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + .ciphertext_len = 64, + .ciphertext = (uint8_t[]) { + 0x92, 0xf0, 0xd5, 0x7c, 0x31, 0x0f, 0x73, 0x38, + 0xbb, 0xc6, 0x11, 0xfb, 0xe7, 0x49, 0xd2, 0xcd, + 0xae, 0x29, 0x67, 0xeb, 0xcd, 0xca, 0xd1, 0x07, + 0xf0, 0x2d, 0x2a, 0x14, 0x8e, 0xec, 0x4d, 0xae, + 0x92, 0xe3, 0x96, 0x65, 0x96, 0x84, 0xe3, 0x8d, + 0x48, 0x36, 0x0e, 0x11, 0xec, 0xe2, 0x0a, 0x4e, + 0xe4, 0x3c, 0xc0, 0xb5, 0xf8, 0xe7, 0xb9, 0x7a, + 0xc1, 0xf4, 0x3b, 0xa7, 0x8b, 0xaa, 0x89, 0xe6 + }, + .tag = { + 0xf3, 0x78, 0x87, 0xc3, 0xb0, 0x4a, 0xe7, 0x10, + 0x8c, 0x14, 0x67, 0x0b, 0x38, 0x9c, 0xc0, 0x2c + }, + .message = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + } + }, + { + .name = "order-padded", + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .nonce = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e + }, + .auth_len = 66, + .auth = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x01 + }, + .ciphertext_len = 66, + .ciphertext = (uint8_t[]) { + 0x92, 0xf0, 0xd5, 0x7c, 0x31, 0x0f, 0x73, 0x38, + 0xbb, 0xc6, 0x11, 0xfb, 0xe7, 0x49, 0xd2, 0xcd, + 0xae, 0x29, 0x67, 0xeb, 0xcd, 0xca, 0xd1, 0x07, + 0xf0, 0x2d, 0x2a, 0x14, 0x8e, 0xec, 0x4d, 0xae, + 0x92, 0xe3, 0x96, 0x65, 0x96, 0x84, 0xe3, 0x8d, + 0x48, 0x36, 0x0e, 0x11, 0xec, 0xe2, 0x0a, 0x4e, + 0xe4, 0x3c, 0xc0, 0xb5, 0xf8, 0xe7, 0xb9, 0x7a, + 0xc1, 0xf4, 0x3b, 0xa7, 0x8b, 0xaa, 0x89, 0xe6, + 0x2d, 0x48 + }, + .tag = { + 0x12, 0x99, 0x0c, 0x33, 0x41, 0x59, 0x34, 0xa7, + 0xd9, 0xa6, 0xcc, 0xb2, 0x90, 0xfe, 0x6d, 0x3d + }, + .message = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x01 + } + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t message[v->ciphertext_len]; + + if (! lilliput_ae_decrypt( + v->ciphertext_len, v->ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, + v->tag, + message + )) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(message, v->message, v->ciphertext_len) != 0) + { + REPORT_DIFFERENCE(v->name, "ciphertext"); + diff++; + } + } + + return diff; +} diff --git a/test/i-128/test-ae-encrypt.c b/test/i-128/test-ae-encrypt.c new file mode 100644 index 0000000..9a7dce0 --- /dev/null +++ b/test/i-128/test-ae-encrypt.c @@ -0,0 +1,158 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; + uint8_t *ciphertext; + uint8_t tag[TAG_BYTES]; +}; + +typedef struct vector vector; + + +const vector VECTORS[] = { + { + .name = "order", + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .nonce = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e + }, + .auth_len = 64, + .auth = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + .message_len = 64, + .message = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + .ciphertext = (uint8_t[]) { + 0x92, 0xf0, 0xd5, 0x7c, 0x31, 0x0f, 0x73, 0x38, + 0xbb, 0xc6, 0x11, 0xfb, 0xe7, 0x49, 0xd2, 0xcd, + 0xae, 0x29, 0x67, 0xeb, 0xcd, 0xca, 0xd1, 0x07, + 0xf0, 0x2d, 0x2a, 0x14, 0x8e, 0xec, 0x4d, 0xae, + 0x92, 0xe3, 0x96, 0x65, 0x96, 0x84, 0xe3, 0x8d, + 0x48, 0x36, 0x0e, 0x11, 0xec, 0xe2, 0x0a, 0x4e, + 0xe4, 0x3c, 0xc0, 0xb5, 0xf8, 0xe7, 0xb9, 0x7a, + 0xc1, 0xf4, 0x3b, 0xa7, 0x8b, 0xaa, 0x89, 0xe6 + }, + .tag = { + 0xf3, 0x78, 0x87, 0xc3, 0xb0, 0x4a, 0xe7, 0x10, + 0x8c, 0x14, 0x67, 0x0b, 0x38, 0x9c, 0xc0, 0x2c + } + }, + { + .name = "order-padded", + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .nonce = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e + }, + .auth_len = 66, + .auth = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x01 + }, + .message_len = 66, + .message = (uint8_t[]) { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x01 + }, + .ciphertext = (uint8_t[]) { + 0x92, 0xf0, 0xd5, 0x7c, 0x31, 0x0f, 0x73, 0x38, + 0xbb, 0xc6, 0x11, 0xfb, 0xe7, 0x49, 0xd2, 0xcd, + 0xae, 0x29, 0x67, 0xeb, 0xcd, 0xca, 0xd1, 0x07, + 0xf0, 0x2d, 0x2a, 0x14, 0x8e, 0xec, 0x4d, 0xae, + 0x92, 0xe3, 0x96, 0x65, 0x96, 0x84, 0xe3, 0x8d, + 0x48, 0x36, 0x0e, 0x11, 0xec, 0xe2, 0x0a, 0x4e, + 0xe4, 0x3c, 0xc0, 0xb5, 0xf8, 0xe7, 0xb9, 0x7a, + 0xc1, 0xf4, 0x3b, 0xa7, 0x8b, 0xaa, 0x89, 0xe6, + 0x2d, 0x48 + }, + .tag = { + 0x12, 0x99, 0x0c, 0x33, 0x41, 0x59, 0x34, 0xa7, + 0xd9, 0xa6, 0xcc, 0xb2, 0x90, 0xfe, 0x6d, 0x3d + } + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + if (memcmp(ciphertext, v->ciphertext, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "ciphertext"); + diff++; + } + + if (memcmp(tag, v->tag, TAG_BYTES) != 0) + { + REPORT_DIFFERENCE(v->name, "tag"); + diff++; + } + } + + return diff; +} diff --git a/test/i-128/test-ae-roundtrip.c b/test/i-128/test-ae-roundtrip.c new file mode 100644 index 0000000..80ac737 --- /dev/null +++ b/test/i-128/test-ae-roundtrip.c @@ -0,0 +1,118 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0xdc, 0xd8, 0xcb, 0x6d, 0xf9, 0xda, 0xf2, 0xc9, + 0x7c, 0xc1, 0x6a, 0xff, 0x7e, 0x1d, 0x27, 0xa3 + }, + .nonce = { + 0xcd, 0x6f, 0x24, 0xe1, 0xf8, 0xcd, 0x64, 0xde, + 0x18, 0x2f, 0x92, 0xab, 0xdb, 0xfa, 0xff + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0x3f, 0x75, 0x05, 0x0a, 0xc1, 0xc6, 0xb5, 0xe0, + 0x57, 0x2e, 0x60, 0x9e, 0x32, 0xab, 0xbe, 0xd0 + }, + .nonce = { + 0xcd, 0x7d, 0xb0, 0xa0, 0x62, 0xdf, 0xda, 0x0a, + 0x23, 0x7a, 0x17, 0x32, 0x60, 0x42, 0xef + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0x13, 0x6a, 0x99, 0xfd, 0xbf, 0x88, 0xac, 0xf8, + 0x92, 0x7b, 0x27, 0xb1, 0x10, 0xa5, 0xe8, 0x73 + }, + .nonce = { + 0x59, 0x41, 0xa7, 0x53, 0x0f, 0xde, 0xf1, 0xb1, + 0xca, 0xd5, 0x80, 0xc4, 0x1c, 0x16, 0x2b + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} diff --git a/test/i-128/test-tbc-decrypt.c b/test/i-128/test-tbc-decrypt.c new file mode 100644 index 0000000..9bd6996 --- /dev/null +++ b/test/i-128/test-tbc-decrypt.c @@ -0,0 +1,84 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "cipher.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t tweak[TWEAK_BYTES]; + uint8_t ciphertext[BLOCK_BYTES]; + uint8_t message[BLOCK_BYTES]; +}; + +typedef struct vector vector; + + +/* [0]: LSB */ +const vector VECTORS[] = { + { + .name = "order", + .tweak = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 + }, + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .ciphertext = { + 0xf9, 0xe3, 0x95, 0x3c, 0x04, 0xf6, 0x2d, 0x9c, + 0x2d, 0x54, 0x58, 0xc0, 0x1d, 0xcc, 0x8a, 0x25 + }, + .message = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + } + }, + { + .name = "random", + .tweak = { + 0xa8, 0x43, 0xf3, 0x10, 0x81, 0x11, 0x1c, 0x84, + 0xdf, 0xf8, 0x2e, 0xfa, 0x90, 0x90, 0x26, 0x21, + 0x7d, 0x8d, 0x43, 0x12, 0x2a, 0xb3, 0xd2, 0x4d + }, + .key = { + 0xc1, 0x96, 0xc6, 0x0a, 0x02, 0x73, 0x91, 0x68, + 0x7f, 0xf4, 0x23, 0x4d, 0x3d, 0xd5, 0xf9, 0x9b + }, + .ciphertext = { + 0xf4, 0x4a, 0x06, 0x30, 0x7d, 0xd2, 0xb2, 0x5a, + 0xf9, 0xd2, 0xba, 0x6c, 0x41, 0xf4, 0x45, 0x7f + }, + .message = { + 0xbc, 0xd7, 0xf0, 0x29, 0x84, 0xb6, 0xc8, 0xf9, + 0x9c, 0x9d, 0x1d, 0xbd, 0x0d, 0x30, 0x94, 0x0b + } + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t message[BLOCK_BYTES]; + lilliput_tbc_decrypt(v->key, v->tweak, v->ciphertext, message); + + if (memcmp(message, v->message, sizeof(message)) != 0) + { + REPORT_DIFFERENCE(v->name, "decrypted message"); + diff++; + } + } + + return diff; +} diff --git a/test/i-128/test-tbc-encrypt.c b/test/i-128/test-tbc-encrypt.c new file mode 100644 index 0000000..60cc9cf --- /dev/null +++ b/test/i-128/test-tbc-encrypt.c @@ -0,0 +1,84 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "cipher.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t tweak[TWEAK_BYTES]; + uint8_t message[BLOCK_BYTES]; + uint8_t ciphertext[BLOCK_BYTES]; +}; + +typedef struct vector vector; + + +/* [0]: LSB */ +const vector VECTORS[] = { + { + .name = "order", + .tweak = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 + }, + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .message = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + }, + .ciphertext = { + 0xf9, 0xe3, 0x95, 0x3c, 0x04, 0xf6, 0x2d, 0x9c, + 0x2d, 0x54, 0x58, 0xc0, 0x1d, 0xcc, 0x8a, 0x25 + } + }, + { + .name = "random", + .tweak = { + 0xa8, 0x43, 0xf3, 0x10, 0x81, 0x11, 0x1c, 0x84, + 0xdf, 0xf8, 0x2e, 0xfa, 0x90, 0x90, 0x26, 0x21, + 0x7d, 0x8d, 0x43, 0x12, 0x2a, 0xb3, 0xd2, 0x4d + }, + .key = { + 0xc1, 0x96, 0xc6, 0x0a, 0x02, 0x73, 0x91, 0x68, + 0x7f, 0xf4, 0x23, 0x4d, 0x3d, 0xd5, 0xf9, 0x9b + }, + .message = { + 0xbc, 0xd7, 0xf0, 0x29, 0x84, 0xb6, 0xc8, 0xf9, + 0x9c, 0x9d, 0x1d, 0xbd, 0x0d, 0x30, 0x94, 0x0b + }, + .ciphertext = { + 0xf4, 0x4a, 0x06, 0x30, 0x7d, 0xd2, 0xb2, 0x5a, + 0xf9, 0xd2, 0xba, 0x6c, 0x41, 0xf4, 0x45, 0x7f + } + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[BLOCK_BYTES]; + lilliput_tbc_encrypt(v->key, v->tweak, v->message, ciphertext); + + if (memcmp(ciphertext, v->ciphertext, sizeof(ciphertext)) != 0) + { + REPORT_DIFFERENCE(v->name, "ciphertext"); + diff++; + } + } + + return diff; +} diff --git a/test/i-128/test-tweakey.c b/test/i-128/test-tweakey.c new file mode 100644 index 0000000..2b0bad5 --- /dev/null +++ b/test/i-128/test-tweakey.c @@ -0,0 +1,113 @@ +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include "tweakey.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t tweak[TWEAK_BYTES]; + uint8_t last_rtk[TWEAKEY_BYTES]; +}; + +typedef struct vector vector; + + +/* [0]: LSB */ +const vector VECTORS[] = { + { + .name = "full", + .tweak = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff + }, + .key = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff + }, + .last_rtk = { + 0x42, 0x5d, 0x5d, 0x5d, 0x5d, 0x5d, 0x5d, 0x5d + } + }, + { + .name = "null", + .tweak = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }, + .key = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }, + .last_rtk = { + 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + } + + }, + { + .name = "order", + .tweak = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 + }, + .key = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }, + .last_rtk = { + 0xa5, 0x92, 0x9b, 0xa7, 0x86, 0x8f, 0xb3, 0xae + } + }, + { + .name = "random", + .tweak = { + 0xa8, 0x43, 0xf3, 0x10, 0x81, 0x11, 0x1c, 0x84, + 0xdf, 0xf8, 0x2e, 0xfa, 0x90, 0x90, 0x26, 0x21, + 0x7d, 0x8d, 0x43, 0x12, 0x2a, 0xb3, 0xd2, 0x4d + }, + .key = { + 0xc1, 0x96, 0xc6, 0x0a, 0x02, 0x73, 0x91, 0x68, + 0x7f, 0xf4, 0x23, 0x4d, 0x3d, 0xd5, 0xf9, 0x9b + }, + .last_rtk = { + 0xed, 0xe3, 0x39, 0xac, 0x5e, 0xa6, 0xf9, 0xf1 + } + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t tk[TWEAKEY_BYTES]; + tweakey_state_init(tk, v->key, v->tweak); + + uint8_t rtk[ROUND_TWEAKEY_BYTES]; + tweakey_state_extract(tk, 0, rtk); + + for (uint8_t i=1; i<ROUNDS; i++) + { + tweakey_state_update(tk); + tweakey_state_extract(tk, i, rtk); + } + + if (memcmp(rtk, v->last_rtk, sizeof(rtk)) != 0) + { + REPORT_DIFFERENCE(v->name, "last RTK"); + diff++; + } + } + + return diff; +} diff --git a/test/i-192/Makefile b/test/i-192/Makefile new file mode 100644 index 0000000..c01fde4 --- /dev/null +++ b/test/i-192/Makefile @@ -0,0 +1,12 @@ +tests = test-ae-roundtrip + +traces = traces-ae-192-i traces-tbc-192-i + +include src/common.mk + +results/test-ae-roundtrip: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-192-i: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-192-i: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-roundtrip.o: src/lilliput-ae.h diff --git a/test/i-192/test-ae-roundtrip.c b/test/i-192/test-ae-roundtrip.c new file mode 100644 index 0000000..7298128 --- /dev/null +++ b/test/i-192/test-ae-roundtrip.c @@ -0,0 +1,121 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0x06, 0xf4, 0x60, 0x05, 0x12, 0x4b, 0xfd, 0xda, + 0x8f, 0xfc, 0x55, 0x1c, 0xaa, 0xef, 0x81, 0x60, + 0x08, 0xb3, 0xe0, 0xa6, 0xad, 0xcb, 0x68, 0x05 + }, + .nonce = { + 0xcd, 0x6f, 0x24, 0xe1, 0xf8, 0xcd, 0x64, 0xde, + 0x18, 0x2f, 0x92, 0xab, 0xdb, 0xfa, 0xff + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0xa0, 0x3d, 0x36, 0xef, 0xef, 0xdd, 0x72, 0xad, + 0x35, 0xa1, 0x2b, 0xe2, 0x3d, 0x1e, 0x43, 0x94, + 0x38, 0x0f, 0x00, 0x4d, 0x8d, 0x78, 0xbc, 0x02 + }, + .nonce = { + 0xcd, 0x7d, 0xb0, 0xa0, 0x62, 0xdf, 0xda, 0x0a, + 0x23, 0x7a, 0x17, 0x32, 0x60, 0x42, 0xef + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0x96, 0x6f, 0x23, 0x89, 0xb4, 0xa4, 0x00, 0x7c, + 0x9a, 0x9f, 0x34, 0x01, 0xb0, 0x73, 0x27, 0x56, + 0x09, 0xce, 0x17, 0xeb, 0xdc, 0xd6, 0xa7, 0x06 + }, + .nonce = { + 0x59, 0x41, 0xa7, 0x53, 0x0f, 0xde, 0xf1, 0xb1, + 0xca, 0xd5, 0x80, 0xc4, 0x1c, 0x16, 0x2b + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} diff --git a/test/i-256/Makefile b/test/i-256/Makefile new file mode 100644 index 0000000..738ec7b --- /dev/null +++ b/test/i-256/Makefile @@ -0,0 +1,12 @@ +tests = test-ae-roundtrip + +traces = traces-ae-256-i traces-tbc-256-i + +include src/common.mk + +results/test-ae-roundtrip: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-256-i: results/src/lilliput-ae-i.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-256-i: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-roundtrip.o: src/lilliput-ae.h diff --git a/test/i-256/test-ae-roundtrip.c b/test/i-256/test-ae-roundtrip.c new file mode 100644 index 0000000..cd2ea42 --- /dev/null +++ b/test/i-256/test-ae-roundtrip.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0x69, 0x5a, 0x10, 0x7a, 0x34, 0xb6, 0x80, 0xc1, + 0x54, 0x85, 0xca, 0xc0, 0xfb, 0xf6, 0x09, 0x4c, + 0x9d, 0xd7, 0x04, 0xfa, 0x1a, 0xb7, 0xb9, 0x22, + 0x52, 0xea, 0x36, 0xd3, 0x12, 0x51, 0x3f, 0x86 + }, + .nonce = { + 0x85, 0xc6, 0x6b, 0xf4, 0x82, 0x99, 0x3a, 0x14, + 0x87, 0x7e, 0x45, 0x48, 0xe4, 0x51, 0x6c + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0x9c, 0x59, 0x6a, 0xfb, 0x07, 0x67, 0xd7, 0x52, + 0xae, 0xfb, 0xde, 0x3f, 0x9b, 0x68, 0x69, 0x60, + 0x22, 0x48, 0x77, 0x95, 0x6f, 0xba, 0x5e, 0x17, + 0x25, 0x2a, 0xa0, 0x7f, 0x0e, 0xd8, 0xc3, 0x16 + }, + .nonce = { + 0x2f, 0xae, 0xfb, 0xa5, 0xd0, 0xc8, 0x2c, 0xc2, + 0xb5, 0x16, 0x2e, 0xcc, 0xf8, 0x4f, 0xc8 + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0xe3, 0x95, 0x7d, 0xc3, 0xc3, 0x29, 0x81, 0x0a, + 0x06, 0x20, 0x33, 0xf9, 0x05, 0x6e, 0xb0, 0x45, + 0x81, 0x33, 0x64, 0x7d, 0x9f, 0x31, 0x8f, 0x98, + 0x1c, 0x97, 0x06, 0x95, 0x6a, 0xe9, 0x93, 0x54 + }, + .nonce = { + 0x34, 0x87, 0x0f, 0xf9, 0x75, 0x49, 0x4c, 0x02, + 0x6c, 0xac, 0x50, 0xfc, 0xc8, 0xe9, 0xed + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} diff --git a/test/ii-128/Makefile b/test/ii-128/Makefile new file mode 100644 index 0000000..02496ce --- /dev/null +++ b/test/ii-128/Makefile @@ -0,0 +1,12 @@ +tests = test-ae-roundtrip + +traces = traces-ae-128-ii traces-tbc-128-ii + +include src/common.mk + +results/test-ae-roundtrip: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-128-ii: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-128-ii: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-roundtrip.o: src/lilliput-ae.h diff --git a/test/ii-128/test-ae-roundtrip.c b/test/ii-128/test-ae-roundtrip.c new file mode 100644 index 0000000..80ac737 --- /dev/null +++ b/test/ii-128/test-ae-roundtrip.c @@ -0,0 +1,118 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0xdc, 0xd8, 0xcb, 0x6d, 0xf9, 0xda, 0xf2, 0xc9, + 0x7c, 0xc1, 0x6a, 0xff, 0x7e, 0x1d, 0x27, 0xa3 + }, + .nonce = { + 0xcd, 0x6f, 0x24, 0xe1, 0xf8, 0xcd, 0x64, 0xde, + 0x18, 0x2f, 0x92, 0xab, 0xdb, 0xfa, 0xff + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0x3f, 0x75, 0x05, 0x0a, 0xc1, 0xc6, 0xb5, 0xe0, + 0x57, 0x2e, 0x60, 0x9e, 0x32, 0xab, 0xbe, 0xd0 + }, + .nonce = { + 0xcd, 0x7d, 0xb0, 0xa0, 0x62, 0xdf, 0xda, 0x0a, + 0x23, 0x7a, 0x17, 0x32, 0x60, 0x42, 0xef + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0x13, 0x6a, 0x99, 0xfd, 0xbf, 0x88, 0xac, 0xf8, + 0x92, 0x7b, 0x27, 0xb1, 0x10, 0xa5, 0xe8, 0x73 + }, + .nonce = { + 0x59, 0x41, 0xa7, 0x53, 0x0f, 0xde, 0xf1, 0xb1, + 0xca, 0xd5, 0x80, 0xc4, 0x1c, 0x16, 0x2b + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} diff --git a/test/ii-192/Makefile b/test/ii-192/Makefile new file mode 100644 index 0000000..a421206 --- /dev/null +++ b/test/ii-192/Makefile @@ -0,0 +1,12 @@ +tests = test-ae-roundtrip + +traces = traces-ae-192-ii traces-tbc-192-ii + +include src/common.mk + +results/test-ae-roundtrip: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-192-ii: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-192-ii: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-roundtrip.o: src/lilliput-ae.h diff --git a/test/ii-192/test-ae-roundtrip.c b/test/ii-192/test-ae-roundtrip.c new file mode 100644 index 0000000..7298128 --- /dev/null +++ b/test/ii-192/test-ae-roundtrip.c @@ -0,0 +1,121 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0x06, 0xf4, 0x60, 0x05, 0x12, 0x4b, 0xfd, 0xda, + 0x8f, 0xfc, 0x55, 0x1c, 0xaa, 0xef, 0x81, 0x60, + 0x08, 0xb3, 0xe0, 0xa6, 0xad, 0xcb, 0x68, 0x05 + }, + .nonce = { + 0xcd, 0x6f, 0x24, 0xe1, 0xf8, 0xcd, 0x64, 0xde, + 0x18, 0x2f, 0x92, 0xab, 0xdb, 0xfa, 0xff + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0xa0, 0x3d, 0x36, 0xef, 0xef, 0xdd, 0x72, 0xad, + 0x35, 0xa1, 0x2b, 0xe2, 0x3d, 0x1e, 0x43, 0x94, + 0x38, 0x0f, 0x00, 0x4d, 0x8d, 0x78, 0xbc, 0x02 + }, + .nonce = { + 0xcd, 0x7d, 0xb0, 0xa0, 0x62, 0xdf, 0xda, 0x0a, + 0x23, 0x7a, 0x17, 0x32, 0x60, 0x42, 0xef + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0x96, 0x6f, 0x23, 0x89, 0xb4, 0xa4, 0x00, 0x7c, + 0x9a, 0x9f, 0x34, 0x01, 0xb0, 0x73, 0x27, 0x56, + 0x09, 0xce, 0x17, 0xeb, 0xdc, 0xd6, 0xa7, 0x06 + }, + .nonce = { + 0x59, 0x41, 0xa7, 0x53, 0x0f, 0xde, 0xf1, 0xb1, + 0xca, 0xd5, 0x80, 0xc4, 0x1c, 0x16, 0x2b + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} diff --git a/test/ii-256/Makefile b/test/ii-256/Makefile new file mode 100644 index 0000000..906e1f0 --- /dev/null +++ b/test/ii-256/Makefile @@ -0,0 +1,12 @@ +tests = test-ae-roundtrip + +traces = traces-ae-256-ii traces-tbc-256-ii + +include src/common.mk + +results/test-ae-roundtrip: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results + +results/traces-ae-256-ii: results/src/lilliput-ae-ii.o results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src +results/traces-tbc-256-ii: results/src/cipher.o results/src/tweakey.o results/src/constants.o | results/src + +results/test-ae-roundtrip.o: src/lilliput-ae.h diff --git a/test/ii-256/test-ae-roundtrip.c b/test/ii-256/test-ae-roundtrip.c new file mode 100644 index 0000000..cd2ea42 --- /dev/null +++ b/test/ii-256/test-ae-roundtrip.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "lilliput-ae.h" + +#include "test-helpers.h" + + +struct vector +{ + char *name; + uint8_t key[KEY_BYTES]; + uint8_t nonce[NONCE_BYTES]; + size_t auth_len; + uint8_t *auth; + size_t message_len; + uint8_t *message; +}; + +typedef struct vector vector; + + +/* Keys and nonces generated with /dev/urandom. */ + +const vector VECTORS[] = { + { + .name = "short", + .key = { + 0x69, 0x5a, 0x10, 0x7a, 0x34, 0xb6, 0x80, 0xc1, + 0x54, 0x85, 0xca, 0xc0, 0xfb, 0xf6, 0x09, 0x4c, + 0x9d, 0xd7, 0x04, 0xfa, 0x1a, 0xb7, 0xb9, 0x22, + 0x52, 0xea, 0x36, 0xd3, 0x12, 0x51, 0x3f, 0x86 + }, + .nonce = { + 0x85, 0xc6, 0x6b, 0xf4, 0x82, 0x99, 0x3a, 0x14, + 0x87, 0x7e, 0x45, 0x48, 0xe4, 0x51, 0x6c + }, + .auth_len = 8, + .auth = (uint8_t*)"deadbeef", + .message_len = 4, + .message = (uint8_t[]){ + 0xde, 0xad, 0xbe, 0xef + } + }, + { + .name = "block-sized", + .key = { + 0x9c, 0x59, 0x6a, 0xfb, 0x07, 0x67, 0xd7, 0x52, + 0xae, 0xfb, 0xde, 0x3f, 0x9b, 0x68, 0x69, 0x60, + 0x22, 0x48, 0x77, 0x95, 0x6f, 0xba, 0x5e, 0x17, + 0x25, 0x2a, 0xa0, 0x7f, 0x0e, 0xd8, 0xc3, 0x16 + }, + .nonce = { + 0x2f, 0xae, 0xfb, 0xa5, 0xd0, 0xc8, 0x2c, 0xc2, + 0xb5, 0x16, 0x2e, 0xcc, 0xf8, 0x4f, 0xc8 + }, + .auth_len = 13, + .auth = (uint8_t*)"some metadata", + .message_len = 2*BLOCK_BYTES, + .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" + }, + { + .name = "arbitrarily long", + .key = { + 0xe3, 0x95, 0x7d, 0xc3, 0xc3, 0x29, 0x81, 0x0a, + 0x06, 0x20, 0x33, 0xf9, 0x05, 0x6e, 0xb0, 0x45, + 0x81, 0x33, 0x64, 0x7d, 0x9f, 0x31, 0x8f, 0x98, + 0x1c, 0x97, 0x06, 0x95, 0x6a, 0xe9, 0x93, 0x54 + }, + .nonce = { + 0x34, 0x87, 0x0f, 0xf9, 0x75, 0x49, 0x4c, 0x02, + 0x6c, 0xac, 0x50, 0xfc, 0xc8, 0xe9, 0xed + }, + .auth_len = 30, + .auth = (uint8_t*)"a bunch of associated metadata", + .message_len = 59, + .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" + } +}; + + +int main() +{ + int diff = 0; + + for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) + { + uint8_t ciphertext[v->message_len]; + uint8_t tag[TAG_BYTES]; + + lilliput_ae_encrypt( + v->message_len, v->message, + v->auth_len, v->auth, + v->key, v->nonce, + ciphertext, + tag + ); + + uint8_t deciphered[v->message_len]; + bool valid = lilliput_ae_decrypt( + v->message_len, ciphertext, + v->auth_len, v->auth, + v->key, v->nonce, tag, + deciphered + ); + + if (!valid) + { + REPORT_INVALID(v->name); + diff++; + continue; + } + + if (memcmp(deciphered, v->message, v->message_len) != 0) + { + REPORT_DIFFERENCE(v->name, "deciphered plaintext"); + diff++; + continue; + } + } + + return diff; +} |
