Enable compilation and execution on x86 as well
authorKonstantinos Margaritis <konstantinos.margaritis@vectorcamp.gr>
Mon, 20 Mar 2023 09:42:08 +0000 (09:42 +0000)
committerKonstantinos Margaritis <konstantinos.margaritis@vectorcamp.gr>
Mon, 20 Mar 2023 09:42:08 +0000 (09:42 +0000)
crypto/chacha20/Makefile
crypto/chacha20/src/xchacha20_wrapper.c

index 6fa39d114d764367d5cd1a3a50f582c52bcf4feb..0b18de133601d3d35068215694333df3403a631e 100644 (file)
@@ -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)
index a3c7ece183d92454806437116436423ce5f19103..39bda38e779bd67115d77099d7f344beb68ffb0b 100644 (file)
@@ -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);