blob: c4b6d9c9a234bbc1293fc334b5a3e7b753b42842 [file] [log] [blame]
Valerio Setti4f4ade92024-05-03 17:28:04 +02001/* 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 <unistd.h>
9#include <stdio.h>
10
11#include "psa/service.h"
12#include "psa/error.h"
13#include "psa/util.h"
14#include "psa_manifest/manifest.h"
15
16#define SERVER_PRINT(fmt, ...) \
17 PRINT("Server: " fmt, ##__VA_ARGS__)
18
19#define BUF_SIZE 25
20
21static int kill_on_disconnect = 0; /* Kill the server on client disconnection. */
22
23void parse_input_args(int argc, char *argv[])
24{
25 int opt;
26
27 while ((opt = getopt(argc, argv, "k")) != -1) {
28 switch (opt) {
29 case 'k':
30 kill_on_disconnect = 1;
31 break;
32 default:
33 fprintf(stderr, "Usage: %s [-k]\n", argv[0]);
34 exit(EXIT_FAILURE);
35 }
36 }
37}
38
39int psa_sha256_main(int argc, char *argv[])
40{
41 psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR;
42 psa_msg_t msg = { -1 };
43 char foo[BUF_SIZE] = { 0 };
44 const int magic_num = 66;
45 int client_disconnected = 0;
46
47 parse_input_args(argc, argv);
48 SERVER_PRINT("Starting");
49
50 while (!(kill_on_disconnect && client_disconnected)) {
51 psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
52
53 if (signals > 0) {
54 SERVER_PRINT("Signals: 0x%08x", signals);
55 }
56
57 if (signals & PSA_SHA256_SIGNAL) {
58 if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) {
59 SERVER_PRINT("My handle is %d", msg.handle);
60 SERVER_PRINT("My rhandle is %p", (int *) msg.rhandle);
61 switch (msg.type) {
62 case PSA_IPC_CONNECT:
63 SERVER_PRINT("Got a connection message");
64 psa_set_rhandle(msg.handle, (void *) &magic_num);
65 ret = PSA_SUCCESS;
66 break;
67 case PSA_IPC_DISCONNECT:
68 SERVER_PRINT("Got a disconnection message");
69 ret = PSA_SUCCESS;
70 client_disconnected = 1;
71 break;
72
73 default:
74 SERVER_PRINT("Got an IPC call of type %d", msg.type);
75 ret = 42;
76 size_t size = msg.in_size[0];
77
78 if ((size > 0) && (size <= sizeof(foo))) {
79 psa_read(msg.handle, 0, foo, 6);
80 foo[(BUF_SIZE-1)] = '\0';
81 SERVER_PRINT("Reading payload: %s", foo);
82 psa_read(msg.handle, 0, foo+6, 6);
83 foo[(BUF_SIZE-1)] = '\0';
84 SERVER_PRINT("Reading payload: %s", foo);
85 }
86
87 size = msg.out_size[0];
88 if ((size > 0)) {
89 SERVER_PRINT("Writing response");
90 psa_write(msg.handle, 0, "RESP", 4);
91 psa_write(msg.handle, 0, "ONSE", 4);
92 }
93
94 if (msg.client_id > 0) {
95 psa_notify(msg.client_id);
96 } else {
97 SERVER_PRINT("Client is non-secure, so won't notify");
98 }
99
100 }
101
102 psa_reply(msg.handle, ret);
103 } else {
104 SERVER_PRINT("Failed to retrieve message");
105 }
106 } else if (SIGSTP_SIG & signals) {
107 SERVER_PRINT("Recieved SIGSTP signal. Gonna EOI it.");
108 psa_eoi(SIGSTP_SIG);
109 } else if (SIGINT_SIG & signals) {
110 SERVER_PRINT("Handling interrupt!");
111 SERVER_PRINT("Gracefully quitting");
112 psa_panic();
113 } else {
114 SERVER_PRINT("No signal asserted");
115 }
116 }
117
118 return 0;
119}