From f5f0546acc3ba947c369dafda0a3f51ab71cff45 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 29 Dec 2022 16:48:01 +0000 Subject: [PATCH] add cross-compiled test of xchacha20 suitable for running under pypowersim --- crypto/chacha20/Makefile.cross | 39 ++++++++++++++++ crypto/chacha20/chacha20.gpr | 9 ++++ crypto/chacha20/chacha20.sh | 11 +++++ crypto/chacha20/common.spr | 1 + crypto/chacha20/memmap | 11 +++++ crypto/chacha20/src/svp64test.c | 80 +++++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+) create mode 100755 crypto/chacha20/Makefile.cross create mode 100644 crypto/chacha20/chacha20.gpr create mode 100755 crypto/chacha20/chacha20.sh create mode 100644 crypto/chacha20/common.spr create mode 100644 crypto/chacha20/memmap create mode 100644 crypto/chacha20/src/svp64test.c diff --git a/crypto/chacha20/Makefile.cross b/crypto/chacha20/Makefile.cross new file mode 100755 index 00000000..9ff32d29 --- /dev/null +++ b/crypto/chacha20/Makefile.cross @@ -0,0 +1,39 @@ +.PHONY: all # tests + +CROSS ?= powerpc64le-linux-gnu- +AS = $(CROSS)as +CC = $(CROSS)gcc +LD = $(CROSS)ld +OBJCOPY = $(CROSS)objcopy +CFLAGS = -O3 -Wall -Wextra + +AFLAGS ?= -mpwr9 + +SRCDIR = src + +SOURCES := $(SRCDIR)/xchacha20.c $(SRCDIR)/svp64test.c +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(SRCDIR)/%.o) + +OBJ = chacha20test.bin + +export DUMP = /tmp/out + +# commented for luke's convenience +#export SILENCELOG = 1 + +all: tests + +$(OBJECTS): $(SRCDIR)/%.o : $(SRCDIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ + +#pysvp64asm $< $<.sv +#$(AS) $(AFLAGS) -c $<.sv -le -o $<.o +chacha20test.bin: $(OBJECTS) + $(LD) $(OBJECTS) -EL -o $<.elf -T memmap + $(OBJCOPY) $<.elf -I elf64-little -O binary $@ + +tests: $(OBJ) + @echo chacha20 + ls -altr chacha20.sh + ./chacha20.sh $$i $$DUMP$$i || exit 1; + diff --git a/crypto/chacha20/chacha20.gpr b/crypto/chacha20/chacha20.gpr new file mode 100644 index 00000000..7b3a8c42 --- /dev/null +++ b/crypto/chacha20/chacha20.gpr @@ -0,0 +1,9 @@ +# void ff_mpadsp_apply_window_float(float *synth_buf, float *window, +# int *dither_state, float *samples, +# ptrdiff_t incr); +1: 0x8000 # stack pointer +3: 0x600000 # param 1: float *sunth_buf buf +4: 0x700000 # param 2: float *window win +5: 0x800000 # param 3: int *dither_state &unused +6: 0x900000 # param 3: float *samples out +7: 1 # param 5: ptr_diff_t incr 1 diff --git a/crypto/chacha20/chacha20.sh b/crypto/chacha20/chacha20.sh new file mode 100755 index 00000000..4e8419af --- /dev/null +++ b/crypto/chacha20/chacha20.sh @@ -0,0 +1,11 @@ +#!/bin/sh -xe + +#-l data/audio/mp3/mp3_0_data/buf${1}:0x600000 \ +#-l data/audio/mp3/mp3_0_data/win0:0x700000 \ + +pypowersim -g chacha20.gpr \ + -s common.spr \ + -p 0x20000000 \ + -d ${2}:0x900000:128 \ + -i chacha20test.bin +#cmp ${2} data/audio/mp3/mp3_0_data/samples${1} diff --git a/crypto/chacha20/common.spr b/crypto/chacha20/common.spr new file mode 100644 index 00000000..0a615d76 --- /dev/null +++ b/crypto/chacha20/common.spr @@ -0,0 +1 @@ +LR: 0xffffffff diff --git a/crypto/chacha20/memmap b/crypto/chacha20/memmap new file mode 100644 index 00000000..052ccb0c --- /dev/null +++ b/crypto/chacha20/memmap @@ -0,0 +1,11 @@ + +MEMORY +{ + ram : ORIGIN = 0x20000000, LENGTH = 512M +} + +SECTIONS +{ + .text : { *(.text*) } > ram + .bss : { *(.text*) } > ram +} diff --git a/crypto/chacha20/src/svp64test.c b/crypto/chacha20/src/svp64test.c new file mode 100644 index 00000000..88402b86 --- /dev/null +++ b/crypto/chacha20/src/svp64test.c @@ -0,0 +1,80 @@ +/************************************************************************* + * This is a simple program to calculate test vectors and compare them * + * to known good values for XChaCha20. + *************************************************************************/ +#include +#include +#include +#include +#include +#include "xchacha20.h" + +/* implementation of memset to stop complaining */ +void *memset(void *s, int c, size_t len) +{ + unsigned char* p=s; + while(len--) + { + *p++ = (unsigned char)c; + } + return s; +} + +/** Compare our output to the output of a known good XChaCha20 library. + * The test vectors used here are from examples given of the Crypto++ + * cryptographic library's XChaCha20 examples. These values can be + * found here: + * https://www.cryptopp.com/wiki/XChaCha20 + * @returns 0 on success, -1 on failure or error + * + */ +int check_cpp(void){ + XChaCha_ctx ctx; + uint8_t buffer[128]; + uint8_t counter[8] = {0x1}; + + /* Test values from Crypto++ documentation */ + uint8_t key[] = { + 0x5E, 0xC5, 0x8B, 0x6D, 0x51, 0x4F, 0xE0, 0xA5, + 0x6F, 0x1E, 0x0D, 0xEA, 0x7B, 0xDC, 0x09, 0x5A, + 0x10, 0xF5, 0xB6, 0x18, 0xBD, 0xB6, 0xF2, 0x26, + 0x2F, 0xCC, 0x59, 0x7B, 0xB2, 0x30, 0xB3, 0xEF + }; + + uint8_t iv[] = { + 0xA3, 0x45, 0xF5, 0xCF, 0x80, 0x23, 0x51, 0x7C, + 0xC0, 0xFC, 0xF0, 0x75, 0x74, 0x8C, 0x86, 0x5F, + 0x7D, 0xE8, 0xCA, 0x0C, 0x72, 0x36, 0xAB, 0xDA + }; + + uint8_t correct_ciphertext[] = { + 0xEE, 0xA7, 0xC2, 0x71, 0x19, 0x10, 0x65, 0x69, + 0x92, 0xE1, 0xCE, 0xD8, 0x16, 0xE2, 0x0E, 0x62, + 0x1B, 0x25, 0x17, 0x82, 0x36, 0x71, 0x6A, 0xE4, + 0x99, 0xF2, 0x97, 0x37, 0xA7, 0x2A, 0xFC, 0xF8, + 0x6C, 0x72 + }; + + // annoying: this is not word-aligned. + uint8_t plaintext[] = "My Plaintext!! My Dear plaintext!!!"; + uint32_t msglen = strlen((char *)plaintext); + + plaintext[msglen-1] = 0; + + xchacha_keysetup(&ctx, key, iv); + + /* Crypto++ initializes their counter to 1 instead of 0 */ + xchacha_set_counter(&ctx, counter); + xchacha_encrypt_bytes(&ctx, plaintext, buffer, msglen); + + /* Compare our ciphertext to the correct ciphertext */ + if(memcmp(buffer, correct_ciphertext, msglen) != 0){ + return(-1); + } + + return(0); +} + +int main(void){ + return check_cpp(); +} -- 2.30.2