|
| 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 | +static size_t request_plaintext(uint8_t* plaintext, size_t size) { |
| 82 | + char c; |
| 83 | + int number_read, number_written; |
| 84 | + |
| 85 | + printf("Enter plaintext to encrypt:\n"); |
| 86 | + |
| 87 | + // Clear all leading whitespace left in the buffer. |
| 88 | + do { |
| 89 | + libtocksync_console_read((uint8_t*)&c, 1, &number_read); |
| 90 | + } while (c == '\n' || c == '\r'); |
| 91 | + |
| 92 | + for (uint8_t i = 0; i < size; i++) { |
| 93 | + // Break on enter. |
| 94 | + if (c == '\n' || c == '\r') { |
| 95 | + libtocksync_console_write((uint8_t*)"\n", 1, &number_written); |
| 96 | + return i; |
| 97 | + } |
| 98 | + |
| 99 | + // Otherwise, echo the character. |
| 100 | + libtocksync_console_write((uint8_t*)&c, 1, &number_written); |
| 101 | + |
| 102 | + // Record the output. |
| 103 | + plaintext[i] = c; |
| 104 | + |
| 105 | + // Fetch a new character from input to add. |
| 106 | + libtocksync_console_read((uint8_t*)&c, 1, &number_read); |
| 107 | + } |
| 108 | + |
| 109 | + return size; |
| 110 | +} |
| 111 | + |
| 112 | +static void bytes_to_hex(char* hex_chars, uint8_t* bytes, size_t bytes_len) { |
| 113 | + char hex_byte[3]; |
| 114 | + |
| 115 | + for (uint8_t i = 0; i < bytes_len; i++) { |
| 116 | + // Convert the current byte into hex |
| 117 | + sprintf(hex_byte, "%02X", bytes[i]); |
| 118 | + |
| 119 | + // Load that serialized byte into the destination string. |
| 120 | + hex_chars[2 * i] = hex_byte[0]; |
| 121 | + hex_chars[2 * i + 1] = hex_byte[1]; |
| 122 | + } |
| 123 | + |
| 124 | + // Add a null-terminator. |
| 125 | + hex_chars[2 * bytes_len] = '\0'; |
| 126 | +} |
| 127 | + |
| 128 | +int main(void) { |
| 129 | + returncode_t ret; |
| 130 | + |
| 131 | + // Wait to receive the signal to start from the app selector. |
| 132 | + wait_for_start(); |
| 133 | + |
| 134 | + // Set up logging service. |
| 135 | + ret = setup_logging(); |
| 136 | + if (ret < 0) { |
| 137 | + printf("ERROR: cannot set up logging\n"); |
| 138 | + } |
| 139 | + |
| 140 | + // Prepare buffers for receiving plaintext |
| 141 | + uint8_t plaintext[512]; |
| 142 | + char plaintext_hex[1024]; |
| 143 | + |
| 144 | + while (1) { |
| 145 | + // Request a plaintext. |
| 146 | + ret = log_to_screen("Requesting plaintext..."); |
| 147 | + if (ret < 0) { |
| 148 | + printf("ERROR: cannot log to screen\n"); |
| 149 | + } |
| 150 | + size_t plaintext_len = request_plaintext(plaintext, sizeof(plaintext)); |
| 151 | + |
| 152 | + // Convert first 16 bytes of plaintext to hex. |
| 153 | + bytes_to_hex(plaintext_hex, plaintext, plaintext_len); |
| 154 | + |
| 155 | + // Display the resulting hex. |
| 156 | + ret = log_to_screen("Returning plaintext..."); |
| 157 | + if (ret < 0) { |
| 158 | + printf("ERROR: cannot log to screen\n"); |
| 159 | + } |
| 160 | + printf("Received plaintext: %s\n", plaintext_hex); |
| 161 | + } |
| 162 | + |
| 163 | + return 0; |
| 164 | +} |
0 commit comments