Minos Galanakis | 2c824b4 | 2025-03-20 09:28:45 +0000 | [diff] [blame^] | 1 | /* psasim test server */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <psa/service.h> |
| 9 | #include "psa_manifest/manifest.h" |
| 10 | #include <unistd.h> |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | void printbits(uint32_t num) |
| 14 | { |
| 15 | for (int i = 0; i < 32; i++) { |
| 16 | if ((num >> (31-i) & 0x1)) { |
| 17 | printf("1"); |
| 18 | } else { |
| 19 | printf("0"); |
| 20 | } |
| 21 | } |
| 22 | printf("\n"); |
| 23 | } |
| 24 | |
| 25 | #define BUF_SIZE 25 |
| 26 | |
| 27 | int psa_sha256_main() |
| 28 | { |
| 29 | psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR; |
| 30 | psa_msg_t msg = { -1 }; |
| 31 | char foo[BUF_SIZE] = { 0 }; |
| 32 | const int magic_num = 66; |
| 33 | |
| 34 | puts("Starting"); |
| 35 | |
| 36 | while (1) { |
| 37 | puts("Calling psa_wait"); |
| 38 | psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK); |
| 39 | |
| 40 | if (signals > 0) { |
| 41 | printbits(signals); |
| 42 | } |
| 43 | |
| 44 | if (signals & PSA_SHA256_SIGNAL) { |
| 45 | puts("Oooh a signal!"); |
| 46 | |
| 47 | if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) { |
| 48 | printf("My handle is %d\n", msg.handle); |
| 49 | printf("My rhandle is %p\n", (int *) msg.rhandle); |
| 50 | switch (msg.type) { |
| 51 | case PSA_IPC_CONNECT: |
| 52 | puts("Got a connection message"); |
| 53 | psa_set_rhandle(msg.handle, (void *) &magic_num); |
| 54 | ret = PSA_SUCCESS; |
| 55 | break; |
| 56 | case PSA_IPC_DISCONNECT: |
| 57 | puts("Got a disconnection message"); |
| 58 | ret = PSA_SUCCESS; |
| 59 | break; |
| 60 | |
| 61 | default: |
| 62 | printf("Got an IPC call of type %d\n", msg.type); |
| 63 | ret = 42; |
| 64 | size_t size = msg.in_size[0]; |
| 65 | |
| 66 | if ((size > 0) && (size <= sizeof(foo))) { |
| 67 | psa_read(msg.handle, 0, foo, 6); |
| 68 | foo[(BUF_SIZE-1)] = '\0'; |
| 69 | printf("Reading payload: %s\n", foo); |
| 70 | psa_read(msg.handle, 0, foo+6, 6); |
| 71 | foo[(BUF_SIZE-1)] = '\0'; |
| 72 | printf("Reading payload: %s\n", foo); |
| 73 | } |
| 74 | |
| 75 | size = msg.out_size[0]; |
| 76 | if ((size > 0)) { |
| 77 | puts("Writing response"); |
| 78 | psa_write(msg.handle, 0, "RESP", 4); |
| 79 | psa_write(msg.handle, 0, "ONSE", 4); |
| 80 | } |
| 81 | |
| 82 | if (msg.client_id > 0) { |
| 83 | psa_notify(msg.client_id); |
| 84 | } else { |
| 85 | puts("Client is non-secure, so won't notify"); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | psa_reply(msg.handle, ret); |
| 91 | } else { |
| 92 | puts("Failed to retrieve message"); |
| 93 | } |
| 94 | } else if (SIGSTP_SIG & signals) { |
| 95 | puts("Recieved SIGSTP signal. Gonna EOI it."); |
| 96 | psa_eoi(SIGSTP_SIG); |
| 97 | } else if (SIGINT_SIG & signals) { |
| 98 | puts("Handling interrupt!\n"); |
| 99 | puts("Gracefully quitting"); |
| 100 | psa_panic(); |
| 101 | } else { |
| 102 | puts("No signal asserted"); |
| 103 | } |
| 104 | } |
| 105 | } |