Skip to content

Commit e23490c

Browse files
Merge pull request #423 from rizlik/uefi_wolfcrypt
uefi: add wolfcrypt uefi example
2 parents 93f4e79 + 3fb44dc commit e23490c

7 files changed

Lines changed: 519 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ Please see the [utasker/README.md](utasker/README.md) for further usage and
370370
details.
371371

372372

373+
<br />
374+
375+
#### UEFI (wolfCrypt UEFI application Example)
376+
377+
This directory contains an example UEFI application that runs wolfcrypt test.
378+
379+
Please see the [uefi/README.md](uefi/README.md) for further usage and
380+
details.
381+
382+
373383
<br />
374384

375385
## Notes

uefi/Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
BUILD_DIR=./build
2+
CRYPTO_SRC=./wolfssl/wolfcrypt/src
3+
WOLFSSL_REPO=https://github.com/wolfSSL/wolfssl
4+
CFLAGS=-fpie -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -static-libgcc -nostdlib
5+
# enable user settings
6+
CFLAGS+= -DWOLFSSL_USER_SETTINGS
7+
CFLAGS+=-I./wolfssl/ -I. -I/usr/include/efi
8+
CFLAGS+=-ggdb
9+
CC=gcc
10+
LD=ld
11+
LDFLAGS_START=-static -pie --no-dynamic-linker -Bsymbolic -L/usr/lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/ -Lgnu-efi-dir/x86_64/gnuefi -T/usr/lib/elf_x86_64_efi.lds /usr/lib/crt0-efi-x86_64.o $(LDFLAGS)
12+
LDFLAGS_END=-lgnuefi -lefi -lgcc
13+
14+
_OBJS=sha256.o sha3.o misc.o coding.o hmac.o rsa.o random.o wolfmath.o \
15+
integer.o tfm.o asm.o cpuid.o memory.o logging.o wc_port.o asn.o hash.o \
16+
main.o test.o error.o string.o aes.o wc_encrypt.o
17+
18+
OBJS=$(addprefix $(BUILD_DIR)/,$(_OBJS))
19+
20+
$(shell mkdir -p $(BUILD_DIR))
21+
22+
all: wolfcrypt.efi
23+
24+
.PHONY: clone_repo
25+
26+
clone_repo:
27+
@if [ ! -d $(CRYPTO_SRC) ]; then \
28+
git clone $(WOLFSSL_REPO); \
29+
fi
30+
31+
create_build_dir:
32+
@if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
33+
34+
$(BUILD_DIR)/wolfcrypt.elf: clone_repo $(OBJS)
35+
$(LD) $(LDFLAGS_START) $(OBJS) -o $(BUILD_DIR)/wolfcrypt.elf $(LDFLAGS_END)
36+
37+
wolfcrypt.efi: $(BUILD_DIR)/wolfcrypt.elf
38+
objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $(BUILD_DIR)/wolfcrypt.elf wolfcrypt.efi
39+
40+
$(BUILD_DIR)/test.o: $(CRYPTO_SRC)/../test/test.c
41+
$(CC) $(CFLAGS) -c $? -o $@
42+
43+
$(BUILD_DIR)/main.o: main.c
44+
$(CC) $(CFLAGS) -c $? -o $@
45+
46+
$(BUILD_DIR)/string.o: string.c
47+
$(CC) $(CFLAGS) -c $? -o $@
48+
49+
$(BUILD_DIR)/%.o: $(CRYPTO_SRC)/%.c
50+
$(CC) $(CFLAGS) -c $? -o $@
51+
52+
clean:
53+
rm -rf $(BUILD_DIR) wolfcrypt.efi

uefi/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
wolfcrypt UEFI Example Applications
2+
===================================
3+
4+
Requires gnu-efi. Tested with qemu and OVFM UEFI implementation. It uses a
5+
custom implementation for string.h functions (`string.c`) based on wolfBoot
6+
`src/string.c`. You can customize the build by changing `user_settings.h` and
7+
adding the relevant `.o` file into the Makefile `_OBJS` variable.
8+
9+
# Compile
10+
11+
## Pre-requisites
12+
13+
```
14+
git make gcc gnu-efi
15+
```
16+
17+
## build
18+
19+
```
20+
make
21+
22+
```
23+
24+
# Test on qemu
25+
26+
## Pre-requisites
27+
```
28+
dosfstools qemu qemu-system-x86 ovmf
29+
```
30+
## Create efi disk
31+
32+
We need to create a FAT partition to store efi application.
33+
34+
```
35+
dd if=/dev/zero of=./efi.disk bs=256M count=1
36+
sudo mkfs.vfat ./efi.disk
37+
```
38+
39+
Move wolfcrypt.efi and startup.nsh into the fat32 partition
40+
```
41+
mkdir -p /tmp/efi
42+
sudo mount ./efi.disk /tmp/efi -oloop
43+
sudo cp wolfcrypt.efi /tmp/efi
44+
sudo cp startup.nsh /tmp/efi
45+
sudo umount /tmp/efi
46+
```
47+
48+
## Run qemu
49+
50+
```
51+
qemu-system-x86_64 -bios /path/to/OVMF.fd -display none -serial stdio -net none -m 256M -drive file=./efi.disk,index=0,media=disk,format=raw
52+
```
53+
54+
/path/to/OVMF.fd may be /usr/share/edk2-ovmf/x64 or /usr/share/qemu
55+
56+
## Example output
57+
58+
```
59+
UEFI Interactive Shell v2.2
60+
EDK II
61+
UEFI v2.70 (EDK II, 0x00010000)
62+
Mapping table
63+
FS0: Alias(s):F0a:;BLK0:
64+
PciRoot(0x0)/Pci(0x1,0x1)/Ata(0x0)
65+
BLK1: Alias(s):
66+
PciRoot(0x0)/Pci(0x1,0x1)/Ata(0x0)
67+
Press ESC in 1 seconds to skip startup.nsh or any other key to continue.
68+
Shell> fs0:
69+
FS0:\> wolfcrypt.efi
70+
status: 0x0
71+
Image base: 0xE15E000
72+
------------------------------------------------------------------------------
73+
wolfSSL version ⸵⸶6
74+
------------------------------------------------------------------------------
75+
error test passed!
76+
MEMORY test passed!
77+
base64 test passed!
78+
asn test passed!
79+
RANDOM test passed!
80+
SHA-256 test passed!
81+
SHA-3 test passed!
82+
SHAKE128 test passed!
83+
SHAKE256 test passed!
84+
Hash test passed!
85+
HMAC-SHA256 test passed!
86+
HMAC-SHA3 test passed!
87+
AES test passed!
88+
AES192 test passed!
89+
AES256 test passed!
90+
RSA test passed!
91+
logging test passed!
92+
mutex test passed!
93+
Test complete
94+
ret: 0
95+
FS0:\>
96+
```

uefi/main.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
#include <efi.h>
3+
#include <efilib.h>
4+
#include <wolfssl/wolfcrypt/settings.h>
5+
#include <wolfssl/wolfcrypt/logging.h>
6+
#include <wolfssl/wolfcrypt/test/test.h>
7+
8+
#define STR_SIZE 512
9+
10+
#ifndef WAIT_FOR_GDB
11+
#define WAIT_FOR_GDB 0
12+
#endif
13+
14+
#define uefi_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__)
15+
16+
void char8_to_char16(const char* str8, wchar_t* str16)
17+
{
18+
size_t i;
19+
size_t size_str8 = strlen(str8);
20+
for (i = 0; i < size_str8; ++i) {
21+
str16[i] = (wchar_t)str8[i];
22+
}
23+
str16[i] = '\0';
24+
}
25+
26+
void logging_cb(const int logLevel, const char *const logMessage)
27+
{
28+
wchar_t str16[STR_SIZE];
29+
char8_to_char16(logMessage, str16);
30+
uefi_printf("%s", str16);
31+
}
32+
33+
void *XMALLOC(size_t n, void* heap, int type)
34+
{
35+
return AllocateZeroPool(n);
36+
}
37+
38+
void *XREALLOC(void *p, size_t n, void* heap, int type)
39+
{
40+
FreePool(p);
41+
p = NULL;
42+
return AllocateZeroPool(n);
43+
}
44+
45+
void XFREE(void *p, void* heap, int type)
46+
{
47+
return FreePool(p);
48+
}
49+
50+
/* TODO: remove dependencies in random.c to open/read/close */
51+
int open (const char *__file, int __oflag)
52+
{
53+
uefi_printf("open\n");
54+
return -1;
55+
}
56+
57+
ssize_t read (int __fd, void *__buf, size_t __nbytes)
58+
{
59+
uefi_printf("read\n");
60+
return -1;
61+
}
62+
63+
int close(int __fd)
64+
{
65+
uefi_printf("close\n");
66+
return -1;
67+
}
68+
69+
EFI_STATUS
70+
EFIAPI
71+
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
72+
{
73+
EFI_LOADED_IMAGE *loaded_image = NULL;
74+
volatile int debug = 1;
75+
EFI_STATUS status;
76+
int ret;
77+
78+
InitializeLib(ImageHandle, SystemTable);
79+
wolfSSL_Debugging_ON();
80+
wolfSSL_SetLoggingCb(logging_cb);
81+
82+
status = uefi_call_wrapper(SystemTable->BootServices->HandleProtocol,
83+
3,
84+
ImageHandle,
85+
&LoadedImageProtocol,
86+
(void **)&loaded_image);
87+
Print(L"status: 0x%lx\n", status);
88+
Print(L"Image base: 0x%lx\n", loaded_image->ImageBase);
89+
90+
#if WAIT_FOR_GDB
91+
/* to debug from gdb:
92+
*
93+
* 1. run qemu with -s option. Take note of Image base value printed by the
94+
* app.
95+
* 2. run gdb, use command: symbol-file wolfcrypt.elf -o $image_base
96+
* with image based value from the print above.
97+
* 3. set variable debug = 0 to exit the loop and continue the debugging */
98+
while(debug) {};
99+
#else
100+
(void)debug;
101+
#endif
102+
103+
ret = wolfcrypt_test(NULL);
104+
Print(L"ret: %d\n", ret);
105+
106+
return EFI_SUCCESS;
107+
}

uefi/startup.nsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fs0:
2+
wolfcrypt.efi

0 commit comments

Comments
 (0)