Skip to content

Commit 00ad7e9

Browse files
pqcfoxalevy
authored andcommitted
tutorial: root_of_trust: add milestone one for encryption service
1 parent aa46156 commit 00ad7e9

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../../
5+
6+
PACKAGE_NAME = org.tockos.tutorials.attestation.encryption
7+
8+
# Which files to compile.
9+
C_SRCS := $(wildcard *.c)
10+
11+
# Include userland master makefile. Contains rules and flags for actually
12+
# building the application.
13+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Hardware Root of Trust (HWRoT) Demo Encryption Service Application
2+
------------------------------------------------------------------
3+
4+
This application implements a basic UART encryption service for a mock hardware
5+
root of trust (HWRoT) which inputs user-provided plaintext and encrypts it using
6+
a fixed key stored in a kernel driver, returning the ciphertext to the user.
7+
8+
This is part of a tutorial which improves the application in multiple steps. A
9+
writeup for the tutorial is available at https://book.tockos.org/.
10+
11+
Specific abilities of this version:
12+
13+
* Allows logging to screen over IPC
14+
* Does NOT allow prompting for plaintext over returning results in hex over UART
15+
* Does NOT allow for encryping user-provided plaintext using an encryption oracle driver
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "libtock/tock.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#include <libtock-sync/interface/console.h>
6+
#include <libtock/kernel/ipc.h>
7+
8+
#define LOG_WIDTH 32
9+
10+
bool started = false;
11+
bool log_done = false;
12+
size_t screen_service = -1;
13+
char log_buf[LOG_WIDTH] __attribute__((aligned(LOG_WIDTH)));
14+
15+
const char SCREEN_SERVICE_NAME[] = "org.tockos.tutorials.attestation.screen";
16+
17+
static void ipc_callback(__attribute__ ((unused)) int pid,
18+
__attribute__ ((unused)) int len,
19+
__attribute__ ((unused)) int arg2,
20+
__attribute__ ((unused)) void* ud) {
21+
started = true;
22+
}
23+
24+
static void log_done_callback(__attribute__ ((unused)) int pid,
25+
__attribute__ ((unused)) int len,
26+
__attribute__ ((unused)) int arg2,
27+
__attribute__ ((unused)) void* ud) {
28+
log_done = true;
29+
}
30+
31+
static void wait_for_start(void) {
32+
// Register an IPC callback and wait for it to be called by the
33+
// screen app based on the user's app selection.
34+
ipc_register_service_callback("org.tockos.tutorials.attestation.encryption", ipc_callback,
35+
NULL);
36+
yield_for(&started);
37+
}
38+
39+
static int setup_logging() {
40+
returncode_t ret;
41+
42+
// Find the PID of the screen logging service
43+
ret = ipc_discover(SCREEN_SERVICE_NAME, &screen_service);
44+
if (ret != RETURNCODE_SUCCESS) {
45+
printf("Screen logging service not found.\n");
46+
return ret;
47+
}
48+
49+
// Set up a callback and share so we can supply log messages
50+
// and know when they've been completely logged.
51+
ipc_register_client_callback(screen_service, log_done_callback, NULL);
52+
ipc_share(screen_service, log_buf, LOG_WIDTH);
53+
54+
return 0;
55+
}
56+
57+
static int log_to_screen(const char* message) {
58+
returncode_t ret;
59+
60+
// Copy up to the log buffer's size of the message, with room for a null byte.
61+
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
62+
memcpy(log_buf, message, len);
63+
64+
// Add the null byte.
65+
log_buf[len] = '\0';
66+
67+
// Start the logging process.
68+
ret = ipc_notify_service(screen_service);
69+
if (ret != RETURNCODE_SUCCESS) {
70+
printf("Failed to request a log to screen.\n");
71+
return ret;
72+
}
73+
74+
// Wait for the log to complete.
75+
yield_for(&log_done);
76+
log_done = false;
77+
78+
return 0;
79+
}
80+
81+
int main(void) {
82+
// Wait to receive the signal to start from the app selector.
83+
wait_for_start();
84+
85+
// Set up logging service.
86+
setup_logging();
87+
88+
// Test logging to screen over IPC.
89+
char message_buf[32];
90+
for (int i = 0; i < 10; i++) {
91+
sprintf(message_buf, "Test message #%i...", i);
92+
log_to_screen(message_buf);
93+
}
94+
95+
return 0;
96+
}

examples/tutorials/root_of_trust/encryption_service_starter/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Hardware Root of Trust (HWRoT) Demo Encryption Service Application
22
------------------------------------------------------------------
33

44
This application implements a basic UART encryption service for a mock hardware
5-
root of trust (HWRoT) which inputs user-provided
5+
root of trust (HWRoT) which inputs user-provided plaintext and encrypts it using
6+
a fixed key stored in a kernel driver, returning the ciphertext to the user.
67

78
This is part of a tutorial which improves the application in multiple steps. A
89
writeup for the tutorial is available at https://book.tockos.org/.

0 commit comments

Comments
 (0)