Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit be8949a

Browse files
authored
Added test for signing via SGX enclave (#28)
* Added test for signing via SGX enclave * fix compile errors * address review comments, fix build * Fix compile error - also updated docker image
1 parent a52d8ad commit be8949a

File tree

6 files changed

+96
-5
lines changed

6 files changed

+96
-5
lines changed

ci/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo --- Build
1212
make install
1313

1414
ci/docker-run.sh solanalabs/sgxsdk ./src/sgx-ecc-ed25519/build.sh
15-
ci/docker-run.sh solanalabs/sgxsdk ./src/sgx/signing/build.sh
15+
ci/docker-run.sh solanalabs/sgxsdk ./src/sgx/build.sh
1616

1717
cd dist
1818
git rev-parse HEAD | tee solana-perf-HEAD.txt

ci/docker-sgx/Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
FROM ubuntu:18.04
22

33
ENV DEBIAN_FRONTEND=noninteractive
4-
54
RUN apt-get update && \
6-
apt-get install -y wget build-essential ocaml ocamlbuild automake autoconf
5+
apt-get install -y build-essential ocaml ocamlbuild automake autoconf libtool wget python libssl-dev libcurl4-openssl-dev protobuf-compiler libprotobuf-dev sudo kmod vim curl git-core libprotobuf-c0-dev libboost-thread-dev libboost-system-dev liblog4cpp5-dev libjsoncpp-dev alien uuid-dev libxml2-dev cmake pkg-config expect
6+
77

8-
RUN mkdir /root/sgx && \
8+
RUN mkdir /root/sgx && mkdir /etc/init/ && \
99
wget -O /root/sgx/sdk.bin https://download.01.org/intel-sgx/linux-2.3.1/ubuntu18.04/sgx_linux_x64_sdk_2.3.101.46683.bin && \
10+
wget -O /root/sgx/psw.deb https://download.01.org/intel-sgx/linux-2.3.1/ubuntu18.04/libsgx-enclave-common_2.3.101.46683-1_amd64.deb && \
1011
cd /root/sgx && \
12+
dpkg -i /root/sgx/psw.deb && \
1113
chmod +x /root/sgx/sdk.bin && \
1214
echo -e 'no\n/opt' | /root/sgx/sdk.bin && \
1315
echo 'source /opt/sgxsdk/environment' >> /root/.bashrc && \
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#!/bin/bash -e
22

33
pwd=$PWD
4-
cd "$(dirname "$0")"
54

65
echo --- Build
6+
cd "$(dirname "$0")/signing"
77
(
88
set -x
99
mkdir -p "$pwd"/temp
1010
openssl genrsa -out "$pwd"/temp/priv_key.pem -3 3072
1111
openssl rsa -in "$pwd"/temp/priv_key.pem -pubout -out "$pwd"/temp/pub_key.pem
1212
make LIBS_PATH="$pwd"/libs OUT="$pwd"/dist PRIV_KEY="$pwd"/temp/priv_key.pem PUB_KEY="$pwd"/temp/pub_key.pem
1313
)
14+
15+
echo --- Build Enclave Test
16+
cd "../test"
17+
(
18+
set -x
19+
make LIBS_PATH="$pwd"/libs OUT="$pwd"/dist
20+
)

src/sgx/signing/signing_public.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "sgx_eid.h"
34
#include "sgx_error.h"
45

56
#define ED25519_PUB_KEY_LEN 32

src/sgx/test/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SGX_SDK ?= /opt/sgxsdk
2+
LIBS_PATH ?= ../../../libs
3+
OUT ?= ../../../dist
4+
5+
C_Flags := -O2 -fpic -I. -I$(SGX_SDK)/include -I$(OUT) -I../../sgx-ecc-ed25519
6+
7+
C_Files := $(wildcard *.c)
8+
C_Objects := $(C_Files:%.c=%.o)
9+
10+
.PHONY: all run
11+
all: $(OUT)/signing_test
12+
run: all
13+
14+
%.o: %.c
15+
@echo "CC <= $<"
16+
$(CC) $(C_Flags) -c $< -o $@
17+
18+
$(OUT)/signing_test: $(C_Objects)
19+
@mkdir -p $(OUT)
20+
$(CC) $^ -o $@ -L$(OUT) -L$(LIBS_PATH) -lsigning -led25519.static
21+
22+
clean:
23+
@rm -rf $(C_Objects) $(OUT)/signing_test

src/sgx/test/signing_test.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdbool.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include "signing_public.h"
5+
6+
#include "ed25519.h"
7+
8+
void print_buffer(const uint8_t* buf, int len) {
9+
char str[BUFSIZ] = {'\0'};
10+
int offset = 0;
11+
for (int i = 0; i < len; i++) {
12+
offset += snprintf(&str[offset], BUFSIZ - offset, "0x%02x ", buf[i]);
13+
if (!((i + 1) % 8))
14+
offset += snprintf(&str[offset], BUFSIZ - offset, "\n");
15+
}
16+
offset += snprintf(&str[offset], BUFSIZ - offset, "\n");
17+
printf("%s", str);
18+
}
19+
20+
int main(int argc, char* argv[]) {
21+
if (argc < 2) {
22+
printf("Usage: %s <enclave file path>\n", argv[0]);
23+
return -1;
24+
}
25+
26+
ed25519_context_t ctxt;
27+
sgx_status_t status = init_ed25519(argv[1], &ctxt);
28+
if (SGX_SUCCESS != status) {
29+
printf("Failed in init_ed25519. Error %d\n", status);
30+
return -1;
31+
}
32+
33+
printf("Loaded the enclave. eid: %d\n", (uint32_t)ctxt.eid);
34+
35+
uint8_t* data =
36+
"This is a test string. We'll sign it using SGX enclave. Hope it works!!";
37+
uint8_t signature[64];
38+
memset(signature, 0, sizeof(signature));
39+
status =
40+
sign_ed25519(&ctxt, sizeof(data), data, sizeof(signature), signature);
41+
if (SGX_SUCCESS != status) {
42+
printf("Failed in sign_ed25519. Error %d\n", status);
43+
release_ed25519_context(&ctxt);
44+
return -1;
45+
}
46+
47+
printf("Signature:\n");
48+
print_buffer(signature, sizeof(signature));
49+
50+
if (ed25519_verify(signature, data, sizeof(data), ctxt.public_key) == 0) {
51+
printf("Failed in verifying the signature\n");
52+
} else {
53+
printf("Signature verified\n");
54+
}
55+
56+
release_ed25519_context(&ctxt);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)