From 129fc68c41bfbf2e37272d04762379b428daa781 Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Mon, 20 Mar 2023 09:42:08 +0000 Subject: [PATCH] Enable compilation and execution on x86 as well --- crypto/chacha20/Makefile | 27 +++++++++++++++++-------- crypto/chacha20/src/xchacha20_wrapper.c | 7 +++++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crypto/chacha20/Makefile b/crypto/chacha20/Makefile index 6fa39d11..0b18de13 100644 --- a/crypto/chacha20/Makefile +++ b/crypto/chacha20/Makefile @@ -1,13 +1,14 @@ # A simple Makefile, to build run: make all TARGET = test-chacha20 -CROSS ?= powerpc64le-linux-gnu- -AS = $(CROSS)as -CC = $(CROSS)gcc -LD = $(CROSS)ld +AS = powerpc64le-linux-gnu-as +CC = gcc +LD = ld +CROSSOBJCOPY = powerpc64le-linux-gnu-objcopy +CROSSLD = powerpc64le-linux-gnu-ld #compiler flags here -CFLAGS = -g3 -O -Wall -Wextra -mno-vsx -mno-altivec -I../../media/pypowersim_wrapper -I/usr/include/python3.7m +CFLAGS = -g3 -O -Wall -Wextra -I../../media/pypowersim_wrapper -I/usr/include/python3.7m # assembler flags here ASFLAGS= -mlibresoc -mregnames -Isrc @@ -16,20 +17,30 @@ ASFLAGS= -mlibresoc -mregnames -Isrc LDFLAGS = -Wall -pthread -lpython3.7m SRCDIR = src +BINDIR = bin CFILES := $(SRCDIR)/xchacha20.c $(SRCDIR)/test.c $(SRCDIR)/xchacha20_wrapper.c ASFILES := $(SRCDIR)/xchacha_hchacha20_svp64.s $(SRCDIR)/xchacha_encrypt_bytes_svp64.s INCLUDES := $(wildcard $(SRCDIR)/*.h)) -OBJECTS := $(CFILES:$(SRCDIR)/%.c=$(SRCDIR)/%.o) $(ASFILES:$(SRCDIR)/%.s=$(SRCDIR)/%.o) +SVP64OBJECTS := $(ASFILES:$(SRCDIR)/%.s=$(SRCDIR)/%.o) +OBJECTS := $(CFILES:$(SRCDIR)/%.c=$(SRCDIR)/%.o) +BINFILES := $(BINDIR)/xchacha_hchacha20_svp64.bin $(BINDIR)/xchacha_encrypt_bytes_svp64.bin + + +$(BINDIR)/%.elf: $(SRCDIR)/%.o + $(CROSSLD) -EL -o $@ -T memmap $^ + +$(BINDIR)/%.bin: $(BINDIR)/%.elf + $(CROSSOBJCOPY) -I elf64-little -O binary $< $@ .PHONY: all clean remove all: ${TARGET} -$(TARGET): $(OBJECTS) +$(TARGET): $(OBJECTS) $(SVP64OBJECTS) $(BINFILES) $(CC) -o $@ $(OBJECTS) $(LDFLAGS) clean: - $ rm -f $(OBJECTS) + $ rm -f $(OBJECTS) $(SVP64OBJECTS) remove: clean $ rm -f $(TARGET) diff --git a/crypto/chacha20/src/xchacha20_wrapper.c b/crypto/chacha20/src/xchacha20_wrapper.c index a3c7ece1..39bda38e 100644 --- a/crypto/chacha20/src/xchacha20_wrapper.c +++ b/crypto/chacha20/src/xchacha20_wrapper.c @@ -6,6 +6,9 @@ #include "xchacha20_wrapper.h" #include "xchacha20.h" +const char *xchacha_hchacha_svp64_filename = "./bin/xchacha_hchacha20_svp64.bin"; +const char *xchacha_encrypt_bytes_svp64_filename = "./bin/xchacha_encrypt_bytes_svp64.bin"; + void xchacha_hchacha20_svp64(uint8_t *out, const uint8_t *in, const uint8_t *k) { // These cannot be the same pointer as the original function, as it is really a separate CPU/RAM @@ -18,7 +21,7 @@ void xchacha_hchacha20_svp64(uint8_t *out, const uint8_t *in, const uint8_t *k) pypowersim_state_t *state = pypowersim_prepare(); // Change the relevant elements, mandatory: body - state->binary = PyBytes_FromStringAndSize((const char *)&xchacha_hchacha20_svp64_real, 10000); + state->binary = PyBytes_FromStringAndSize(xchacha_hchacha_svp64_filename, strlen(xchacha_hchacha_svp64_filename)); // Set GPR #3 to the output pointer PyObject *out_address = PyLong_FromUnsignedLongLong(outptr_svp64); PyList_SetItem(state->initial_regs, 3, out_address); @@ -100,7 +103,7 @@ void xchacha_encrypt_bytes_svp64(XChaCha_ctx *ctx, const uint8_t *m, uint8_t *c, pypowersim_state_t *state = pypowersim_prepare(); // Change the relevant elements - state->binary = PyBytes_FromStringAndSize((const char *)&xchacha_encrypt_bytes_svp64_real, 10000); + state->binary = PyBytes_FromStringAndSize(xchacha_encrypt_bytes_svp64_filename, strlen(xchacha_encrypt_bytes_svp64_filename)); // Set GPR #3 to the output pointer PyObject *ctxptr_address = PyLong_FromUnsignedLongLong(ctxptr_svp64); -- 2.30.2